Skip to content

Commit

Permalink
Fix gocritic findings main entry
Browse files Browse the repository at this point in the history
Refactor code to address `gocritic` findings.
  • Loading branch information
HeavyWombat committed Jun 5, 2024
1 parent 6848a20 commit 5ec067a
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,27 +75,32 @@ func ResetSettings() {
jsonCmdSettings = jsonCmdOptions{}
}

// rearrange will rearrange the OS args to match `dyff between --flags from to`
// to mitigate an issue in `kubectl`, which puts the `from` and `to` at the
// second and third position in the command arguments.
func rearrange() []string {
var paths, args []string
for _, entry := range os.Args {
if info, err := os.Stat(entry); err == nil && info.IsDir() {
paths = append(paths, entry)

} else {
args = append(args, entry)
}
}

return append(args, paths...)
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() error {
// In case `KUBECTL_EXTERNAL_DIFF` is set with `dyff`, it is very likely
// that `kubectl` intends to use `dyff` for its `diff` command. Therefore,
// enable Kubernetes specific entity detection and fix the order issue.
if strings.Contains(os.Getenv("KUBECTL_EXTERNAL_DIFF"), name) {
// Rearrange the arguments to match `dyff between --flags from to` to
// mitigate an issue in `kubectl`, which puts the `from` and `to` at
// the second and third position in the command arguments.
var paths, args []string
for _, entry := range os.Args {
if info, err := os.Stat(entry); err == nil && info.IsDir() {
paths = append(paths, entry)

} else {
args = append(args, entry)
}
}

os.Args = append(args, paths...)
// Make sure the OS args are in a supported order
os.Args = rearrange()

// Enable Kubernetes specific entity detection implicitly
reportOptions.kubernetesEntityDetection = true
Expand All @@ -110,8 +115,7 @@ func Execute() error {
if err := rootCmd.Execute(); err != nil {
// Special case ExitCode, which means that we will exit immediately
// with the given exit code
switch err.(type) {
case ExitCode:
if err, ok := err.(ExitCode); ok {
return err
}

Expand Down

0 comments on commit 5ec067a

Please sign in to comment.