Skip to content

Commit

Permalink
[RFE] close #62
Browse files Browse the repository at this point in the history
  • Loading branch information
gmeghnag committed Nov 24, 2022
1 parent 22b5541 commit 355e096
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 12 deletions.
2 changes: 2 additions & 0 deletions cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var ConfigCmd = &cobra.Command{

func init() {
ConfigCmd.PersistentFlags().BoolVarP(&vars.UseLocalCRDs, "use-local-crds", "", false, "If set to true, omc will search for valid CRDs also in ~/.omc/customresourcedefinitions")
ConfigCmd.PersistentFlags().StringVarP(&vars.DiffCmd, "diff-command", "", "", "Set the binary tool to use to execute \"omc mc diff <machineConfig1> <machineConfig2>\"")
}

func SetConfig() {
Expand All @@ -45,6 +46,7 @@ func SetConfig() {
omcConfigJson := types.Config{}
_ = json.Unmarshal([]byte(file), &omcConfigJson)
omcConfigJson.UseLocalCRDs = vars.UseLocalCRDs
omcConfigJson.DiffCmd = vars.DiffCmd
file, _ = json.MarshalIndent(omcConfigJson, "", " ")
_ = ioutil.WriteFile(home+"/.omc/omc.json", file, 0644)

Expand Down
23 changes: 15 additions & 8 deletions cmd/machineconfig/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,35 @@ package machineconfig

import (
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"strconv"

"github.com/gmeghnag/omc/vars"
"github.com/google/go-cmp/cmp"
"github.com/spf13/cobra"
)

func checkMachineConfigDiff(first string, second string) {
firstMachineConfigPath := vars.MustGatherRootPath + "/cluster-scoped-resources/machineconfiguration.openshift.io/machineconfigs/" + first + ".yaml"
secondMachineConfigPath := vars.MustGatherRootPath + "/cluster-scoped-resources/machineconfiguration.openshift.io/machineconfigs/" + second + ".yaml"
firstMachineConfigContent, err := ioutil.ReadFile(firstMachineConfigPath)
if vars.DiffCmd == "" {
vars.DiffCmd = "vimdiff"
}
_, err := exec.LookPath(vars.DiffCmd)
if err != nil {
fmt.Println(err)
log.Fatal(err)
}
secondMachineConfigContent, err := ioutil.ReadFile(secondMachineConfigPath)
cmd := exec.Command(vars.DiffCmd, firstMachineConfigPath, secondMachineConfigPath)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

err = cmd.Run()
if err != nil {
fmt.Println(err)
log.Fatal(err)
}
x := cmp.Diff(firstMachineConfigContent, secondMachineConfigContent)
fmt.Println(x)

}

var Diff = &cobra.Command{
Expand Down
1 change: 0 additions & 1 deletion cmd/machineconfig/machineconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

var MachineConfig = &cobra.Command{
Use: "machine-config",
Hidden: true,
Aliases: []string{"mc"},
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
Expand Down
5 changes: 3 additions & 2 deletions root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func init() {
machineconfig.MachineConfig,
upgrade.Upgrade,
)
loadBooleanConfigs()
loadOmcConfigs()
}

// initConfig reads in config file and ENV variables if set.
Expand Down Expand Up @@ -159,10 +159,11 @@ func initConfig() {
}
}

func loadBooleanConfigs() {
func loadOmcConfigs() {
home, _ := os.UserHomeDir()
file, _ := ioutil.ReadFile(home + "/.omc/omc.json")
omcConfigJson := types.Config{}
_ = json.Unmarshal([]byte(file), &omcConfigJson)
vars.UseLocalCRDs = omcConfigJson.UseLocalCRDs
vars.DiffCmd = omcConfigJson.DiffCmd
}
1 change: 1 addition & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Config struct {
Id string `json:"id,omitempty"`
Contexts []Context `json:"contexts,omitempty"`
UseLocalCRDs bool `json:"use_local_crds,omitempty"`
DiffCmd string `json:"diff_command,omitempty"`
}

type DescribeClient struct {
Expand Down
2 changes: 1 addition & 1 deletion vars/vars.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package vars

var CfgFile, Namespace, MustGatherRootPath, OutputStringVar, LabelSelectorStringVar, Id, Container, OMCVersionHash, OMCVersionTag string
var CfgFile, Namespace, MustGatherRootPath, OutputStringVar, LabelSelectorStringVar, Id, Container, OMCVersionHash, OMCVersionTag, DiffCmd string
var AllNamespaceBoolVar, ShowLabelsBoolVar, Previous, AllContainers, UseLocalCRDs bool

0 comments on commit 355e096

Please sign in to comment.