-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from caas-team/feat/cli-config
Feat/cli config
- Loading branch information
Showing
10 changed files
with
713 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
// NewCmdRoot creates a new root command | ||
func NewCmdRoot(version string) *cobra.Command { | ||
var cfgFile string | ||
|
||
rootCmd := &cobra.Command{ | ||
Use: "sparrow", | ||
Short: "Sparrow, the infrastructure monitoring agent", | ||
Long: "Sparrow is an infrastructure monitoring agent that is able to perform different checks.\n" + | ||
"The check results are exposed via an API.", | ||
Version: version, | ||
} | ||
|
||
cobra.OnInitialize(func() { | ||
initConfig(cfgFile) | ||
}) | ||
|
||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.sparrow.yaml)") | ||
|
||
return rootCmd | ||
} | ||
|
||
// Execute adds all child commands to the root command | ||
// and executes the cmd tree | ||
func Execute(version string) { | ||
cmd := NewCmdRoot(version) | ||
cmd.AddCommand(NewCmdRun()) | ||
|
||
if err := cmd.Execute(); err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func initConfig(cfgFile string) { | ||
if cfgFile != "" { | ||
// Use config file from the flag | ||
viper.SetConfigFile(cfgFile) | ||
} else { | ||
// Find home directory | ||
home, err := os.UserHomeDir() | ||
cobra.CheckErr(err) | ||
|
||
// Search config in home directory with name ".sparrow" (without extension) | ||
viper.AddConfigPath(home) | ||
viper.SetConfigType("yaml") | ||
viper.SetConfigName(".sparrow") | ||
} | ||
|
||
viper.AutomaticEnv() | ||
|
||
if err := viper.ReadInConfig(); err == nil { | ||
fmt.Println("Using config file:", viper.ConfigFileUsed()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"log" | ||
|
||
"github.com/caas-team/sparrow/pkg/config" | ||
"github.com/caas-team/sparrow/pkg/sparrow" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
type RunFlagsNameMapping struct { | ||
loaderType string | ||
loaderInterval string | ||
loaderHttpUrl string | ||
loaderHttpToken string | ||
} | ||
|
||
// NewCmdRun creates a new run command | ||
func NewCmdRun() *cobra.Command { | ||
flagMapping := RunFlagsNameMapping{ | ||
loaderType: "loaderType", | ||
loaderInterval: "loaderInterval", | ||
loaderHttpUrl: "loaderHttpUrl", | ||
loaderHttpToken: "loaderHttpToken", | ||
} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "run", | ||
Short: "Run sparrow", | ||
Long: `Sparrow will be started with the provided configuration`, | ||
Run: run(&flagMapping), | ||
} | ||
|
||
cmd.PersistentFlags().StringP(flagMapping.loaderType, "l", "http", "defines the loader type that will load the checks configuration during the runtime") | ||
cmd.PersistentFlags().Int(flagMapping.loaderInterval, 300, "defines the interval the loader reloads the configuration in seconds") | ||
cmd.PersistentFlags().String(flagMapping.loaderHttpUrl, "", "http loader: The url where to get the remote configuration") | ||
cmd.PersistentFlags().String(flagMapping.loaderHttpToken, "", "http loader: Bearer token to authenticate the http endpoint") | ||
|
||
viper.BindPFlag(flagMapping.loaderType, cmd.PersistentFlags().Lookup(flagMapping.loaderType)) | ||
viper.BindPFlag(flagMapping.loaderInterval, cmd.PersistentFlags().Lookup(flagMapping.loaderInterval)) | ||
viper.BindPFlag(flagMapping.loaderHttpUrl, cmd.PersistentFlags().Lookup(flagMapping.loaderHttpUrl)) | ||
viper.BindPFlag(flagMapping.loaderHttpToken, cmd.PersistentFlags().Lookup(flagMapping.loaderHttpToken)) | ||
|
||
return cmd | ||
} | ||
|
||
// run is the entry point to start the sparrow | ||
func run(fm *RunFlagsNameMapping) func(cmd *cobra.Command, args []string) { | ||
return func(cmd *cobra.Command, args []string) { | ||
cfg := config.NewConfig() | ||
|
||
cfg.SetLoaderType(viper.GetString(fm.loaderType)) | ||
cfg.SetLoaderInterval(viper.GetInt(fm.loaderInterval)) | ||
cfg.SetLoaderHttpUrl(viper.GetString(fm.loaderHttpUrl)) | ||
cfg.SetLoaderHttpToken(viper.GetString(fm.loaderHttpToken)) | ||
|
||
if err := cfg.Validate(); err != nil { | ||
log.Panic(err) | ||
} | ||
|
||
sparrow := sparrow.New(cfg) | ||
|
||
log.Println("running sparrow") | ||
if err := sparrow.Run(context.Background()); err != nil { | ||
panic(err) | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.