Skip to content

Commit

Permalink
tfrobot add init function to every subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
Eslam-Nawara committed Nov 5, 2024
1 parent 4ccbc61 commit 62660c5
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 34 deletions.
1 change: 1 addition & 0 deletions grid-client/deployer/tf_plugin_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ func parsePluginOpts(opts ...PluginOpt) (pluginCfg, error) {
relayURLs: []string{},
rmbTimeout: 60, // default rmbTimeout is 60
showLogs: false,
noColorLogs: false,
rmbInMemCache: true,
}

Expand Down
13 changes: 7 additions & 6 deletions tfrobot/cmd/cancel.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"
"path/filepath"

"github.com/rs/zerolog"
"github.com/spf13/cobra"
"github.com/threefoldtech/tfgrid-sdk-go/tfrobot/internal/parser"
"github.com/threefoldtech/tfgrid-sdk-go/tfrobot/pkg/deployer"
Expand All @@ -26,11 +25,6 @@ var cancelCmd = &cobra.Command{
return fmt.Errorf("invalid log debug mode input '%v' with error: %w", debug, err)
}

zerolog.SetGlobalLevel(zerolog.InfoLevel)
if debug {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}

configPath, err := cmd.Flags().GetString("config")
if err != nil {
return fmt.Errorf("error in configuration file: %w", err)
Expand Down Expand Up @@ -70,3 +64,10 @@ var cancelCmd = &cobra.Command{
return nil
},
}

func init() {
rootCmd.AddCommand(cancelCmd)

cancelCmd.Flags().BoolP("debug", "d", false, "allow debug logs")
cancelCmd.Flags().StringP("config", "c", "", "path to config file")
}
14 changes: 8 additions & 6 deletions tfrobot/cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os"
"path/filepath"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/threefoldtech/tfgrid-sdk-go/tfrobot/internal/parser"
Expand Down Expand Up @@ -36,11 +35,6 @@ var deployCmd = &cobra.Command{
return fmt.Errorf("invalid log debug mode input '%v' with error: %w", debug, err)
}

zerolog.SetGlobalLevel(zerolog.InfoLevel)
if debug {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}

outputPath, err := cmd.Flags().GetString("output")
if err != nil {
return fmt.Errorf("error in output file: %w", err)
Expand Down Expand Up @@ -103,3 +97,11 @@ var deployCmd = &cobra.Command{
return nil
},
}

func init() {
rootCmd.AddCommand(deployCmd)

deployCmd.Flags().BoolP("debug", "d", false, "allow debug logs")
deployCmd.Flags().StringP("config", "c", "", "path to config file")
deployCmd.Flags().StringP("output", "o", "output.yaml", "path to output file")
}
13 changes: 8 additions & 5 deletions tfrobot/cmd/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os"
"path/filepath"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/threefoldtech/tfgrid-sdk-go/tfrobot/internal/parser"
Expand All @@ -29,10 +28,6 @@ var loadCmd = &cobra.Command{
if err != nil {
return fmt.Errorf("invalid log debug mode input '%v' with error: %w", debug, err)
}
zerolog.SetGlobalLevel(zerolog.InfoLevel)
if debug {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}

outputPath, err := cmd.Flags().GetString("output")
if err != nil {
Expand Down Expand Up @@ -90,3 +85,11 @@ var loadCmd = &cobra.Command{
return nil
},
}

func init() {
rootCmd.AddCommand(loadCmd)

loadCmd.Flags().BoolP("debug", "d", false, "allow debug logs")
loadCmd.Flags().StringP("config", "c", "", "path to config file")
loadCmd.Flags().StringP("output", "o", "output.yaml", "path to output file")
}
19 changes: 2 additions & 17 deletions tfrobot/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,13 @@ var rootCmd = &cobra.Command{
// 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() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})

rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(deployCmd)
rootCmd.AddCommand(cancelCmd)
rootCmd.AddCommand(loadCmd)

err := rootCmd.Execute()
if err != nil {
log.Fatal().Err(err).Send()
}
}

func init() {
deployCmd.Flags().BoolP("debug", "d", false, "allow debug logs")
deployCmd.Flags().StringP("config", "c", "", "path to config file")
deployCmd.Flags().StringP("output", "o", "output.yaml", "path to output file")

loadCmd.Flags().BoolP("debug", "d", false, "allow debug logs")
loadCmd.Flags().StringP("config", "c", "", "path to config file")
loadCmd.Flags().StringP("output", "o", "output.yaml", "path to output file")

cancelCmd.Flags().BoolP("debug", "d", false, "allow debug logs")
cancelCmd.Flags().StringP("config", "c", "", "path to config file")
zerolog.SetGlobalLevel(zerolog.InfoLevel)
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
}
4 changes: 4 additions & 0 deletions tfrobot/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ var versionCmd = &cobra.Command{
return nil
},
}

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

0 comments on commit 62660c5

Please sign in to comment.