Skip to content

Commit

Permalink
Merge pull request #6 from caas-team/feat/cli-config
Browse files Browse the repository at this point in the history
Feat/cli config
  • Loading branch information
y-eight authored Nov 7, 2023
2 parents 20c1485 + 18d700f commit 4aaf296
Show file tree
Hide file tree
Showing 10 changed files with 713 additions and 29 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
# Dependency directories (remove the comment below to include it)
# vendor/

# Environment
*.env

# Go workspace file
go.work
gen
Expand Down
7 changes: 6 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/run"
"program": "${workspaceFolder}/main.go",
"args": [
"run",
"--config",
"config.yaml"
]
}
]
}
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ tbd

tbd

Priority of configuration (high to low):

1. CLI flags
2. Environment variables
3. Defined configuration file
4. Default configuration file

## Development

tbd
Expand Down
64 changes: 64 additions & 0 deletions cmd/root.go
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())
}
}
70 changes: 70 additions & 0 deletions cmd/run.go
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)
}
}
}
19 changes: 0 additions & 19 deletions cmd/run/main.go

This file was deleted.

33 changes: 28 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,44 @@ module github.com/caas-team/sparrow

go 1.20

require github.com/getkin/kin-openapi v0.120.0
require (
github.com/getkin/kin-openapi v0.120.0
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.8.4
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.10.0 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
)

require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/invopop/yaml v0.2.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/perimeterx/marshmallow v1.1.5 // indirect
github.com/stretchr/testify v1.8.4
gopkg.in/yaml.v3 v3.0.1
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.17.0
)
Loading

0 comments on commit 4aaf296

Please sign in to comment.