Skip to content

Commit

Permalink
fix: fsoc-106: additional config bypass needed (#18)
Browse files Browse the repository at this point in the history
Stop requiring config profile for certain commands: version, help, gendocs and complete
  • Loading branch information
pnickolov authored Feb 2, 2023
1 parent f634a2e commit 2b2ef9b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
2 changes: 2 additions & 0 deletions cmd/gendocs/gendocs.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"

"github.com/cisco-open/fsoc/cmd/config"
"github.com/cisco-open/fsoc/output"
)

Expand All @@ -46,6 +47,7 @@ The directory should either be empty or not exist.`,
Args: cobra.ExactArgs(1),
Run: genDocs,
TraverseChildren: true,
Annotations: map[string]string{config.AnnotationForConfigBypass: ""},
}

func NewSubCmd() *cobra.Command {
Expand Down
17 changes: 15 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,11 @@ func preExecHook(cmd *cobra.Command, args []string) {
config.SetSelectedProfile(profile)
}
}
_, bypassConfig := cmd.Annotations[config.AnnotationForConfigBypass]
if !bypassConfig {

// enforce presence of a configured profile (unless bypassed for commands that
// must work or can work without it)
bypass := bypassConfig(cmd) || cmd.Name() == "help" || isCompletionCommand(cmd)
if !bypass {
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
profile := config.GetCurrentProfileName()
Expand All @@ -161,3 +164,13 @@ func preExecHook(cmd *cobra.Command, args []string) {
}
}
}

func bypassConfig(cmd *cobra.Command) bool {
_, bypassConfig := cmd.Annotations[config.AnnotationForConfigBypass]
return bypassConfig
}

func isCompletionCommand(cmd *cobra.Command) bool {
p := cmd.Parent()
return (p != nil && p.Name() == "completion")
}
13 changes: 8 additions & 5 deletions cmd/version/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,24 @@ import (

"github.com/apex/log"
"github.com/spf13/cobra"

"github.com/cisco-open/fsoc/cmd/config"
)

var updateCmd = &cobra.Command{
Use: "update",
Short: "Update fsoc",
Long: `Update fsoc if a new version is available.`,
Run: update,
Use: "update",
Short: "Update fsoc",
Long: `Update fsoc if a new version is available.`,
Run: update,
Annotations: map[string]string{config.AnnotationForConfigBypass: ""},
}

func init() {
versionCmd.AddCommand(updateCmd)
}

func update(cmd *cobra.Command, args []string) {
log.Fatalf("Update command is not yet implemented. Please check version manually and download update if available.")
log.Fatalf("Update command is not implemented yet. Please check version manually and download an update if available.")
}

// func update(core *Core, pkg dependency.Installable) {
Expand Down
2 changes: 2 additions & 0 deletions cmd/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/spf13/cobra"

"github.com/cisco-open/fsoc/cmd/config"
"github.com/cisco-open/fsoc/output"
)

Expand All @@ -29,6 +30,7 @@ var versionCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
displayVersion(cmd)
},
Annotations: map[string]string{config.AnnotationForConfigBypass: ""},
}

func init() {
Expand Down

0 comments on commit 2b2ef9b

Please sign in to comment.