From 355e09645594aa86a1f810ad5f9d3f40347505c9 Mon Sep 17 00:00:00 2001 From: gmeghnag Date: Thu, 24 Nov 2022 17:18:04 +0100 Subject: [PATCH] [RFE] close #62 --- cmd/config/config.go | 2 ++ cmd/machineconfig/diff.go | 23 +++++++++++++++-------- cmd/machineconfig/machineconfig.go | 1 - root/root.go | 5 +++-- types/types.go | 1 + vars/vars.go | 2 +- 6 files changed, 22 insertions(+), 12 deletions(-) diff --git a/cmd/config/config.go b/cmd/config/config.go index be0305e8..32fef57f 100644 --- a/cmd/config/config.go +++ b/cmd/config/config.go @@ -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 \"") } func SetConfig() { @@ -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) diff --git a/cmd/machineconfig/diff.go b/cmd/machineconfig/diff.go index 73e108bd..92b3737b 100644 --- a/cmd/machineconfig/diff.go +++ b/cmd/machineconfig/diff.go @@ -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{ diff --git a/cmd/machineconfig/machineconfig.go b/cmd/machineconfig/machineconfig.go index 3da85e27..43c3e3f8 100644 --- a/cmd/machineconfig/machineconfig.go +++ b/cmd/machineconfig/machineconfig.go @@ -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() diff --git a/root/root.go b/root/root.go index 6c4654bb..1617bdc8 100644 --- a/root/root.go +++ b/root/root.go @@ -80,7 +80,7 @@ func init() { machineconfig.MachineConfig, upgrade.Upgrade, ) - loadBooleanConfigs() + loadOmcConfigs() } // initConfig reads in config file and ENV variables if set. @@ -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 } diff --git a/types/types.go b/types/types.go index b3224eba..00a3f46b 100644 --- a/types/types.go +++ b/types/types.go @@ -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 { diff --git a/vars/vars.go b/vars/vars.go index 999d1faa..98f42f30 100644 --- a/vars/vars.go +++ b/vars/vars.go @@ -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