Skip to content

Commit

Permalink
Refactor command flags and their usage in rootCmd
Browse files Browse the repository at this point in the history
  • Loading branch information
buty4649 committed Mar 20, 2024
1 parent 18c72b7 commit 8b63d40
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
7 changes: 4 additions & 3 deletions cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ import (

// applyCmd represents the apply command
var applyCmd = &cobra.Command{
Use: "apply",
Short: "Apply netns networks configuration to running system",
Long: "Apply netns networks configuration to running system",
Use: "apply",
Short: "Apply netns networks configuration to running system",
Long: "Apply netns networks configuration to running system",
RunE: func(cmd *cobra.Command, args []string) error {
for netns, values := range cfg.Netns {
if ip.NetnsExists(netns) {
slog.Warn("netns is already exists", "name", netns)
} else {
slog.Info("create netns", "name", netns)
err := ip.AddNetns(netns)
if err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions cmd/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ import (

// destroyCmd represents the destroy command
var destroyCmd = &cobra.Command{
Use: "destroy",
Short: "Destroy netns networks configuration from running system",
Long: "Destroy netns networks configuration from running system",
Use: "destroy",
Short: "Destroy netns networks configuration from running system",
Long: "Destroy netns networks configuration from running system",
RunE: func(cmd *cobra.Command, args []string) error {
for n := range cfg.Netns {
if ip.NetnsExists(n) {
Expand Down
36 changes: 25 additions & 11 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ import (
"gitlab.com/greyxor/slogor"
)

var cfgFilePath string
var ipCmdPath string
var debug bool
type Flags struct {
ConfigPath string
IpCmdPath string
Debug, Quiet bool
}

var flags Flags

var cfg *config.Config
var ip *iproute2.Iproute2
Expand All @@ -47,23 +51,30 @@ var rootCmd = &cobra.Command{
Long: "Easily automate Linux netns networks and configurations via YAML",
SilenceUsage: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
logLevel := slog.LevelInfo
if debug {
err := cmd.ValidateFlagGroups()
if err != nil {
return err
}

var logLevel slog.Level
if flags.Debug {
logLevel = slog.LevelDebug
}
if flags.Quiet {
logLevel = slog.LevelError
}
slog.SetDefault(slog.New(slogor.NewHandler(os.Stdout, slogor.Options{
TimeFormat: "",
Level: logLevel,
ShowSource: false,
})))

var err error
cfg, err = config.LoadConfig(cfgFilePath)
cfg, err = config.LoadConfig(flags.ConfigPath)
if err != nil {
return err
}

ip = iproute2.New(ipCmdPath)
ip = iproute2.New(flags.IpCmdPath)
return nil
},
}
Expand All @@ -82,7 +93,10 @@ func Execute() {
}

func init() {
rootCmd.PersistentFlags().StringVar(&cfgFilePath, "config", "./netnsplan.yaml", "config file")
rootCmd.PersistentFlags().StringVar(&ipCmdPath, "cmd", "/bin/ip", "ip command path")
rootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "debug mode")
rootCmd.PersistentFlags().StringVar(&flags.ConfigPath, "config", "./netnsplan.yaml", "config file")
rootCmd.PersistentFlags().StringVar(&flags.IpCmdPath, "cmd", "/bin/ip", "ip command path")

rootCmd.PersistentFlags().BoolVar(&flags.Debug, "debug", false, "debug mode")
rootCmd.PersistentFlags().BoolVarP(&flags.Quiet, "quiet", "q", false, "debug mode")
rootCmd.MarkFlagsMutuallyExclusive("debug", "quiet")
}

0 comments on commit 8b63d40

Please sign in to comment.