Skip to content

Commit

Permalink
fix: fixed interface casting issues
Browse files Browse the repository at this point in the history
Signed-off-by: Bruno Bressi <[email protected]>
  • Loading branch information
puffitos committed Dec 18, 2023
1 parent 4114bbe commit a64099f
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 16 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ Additionally check out the sparrow [configuration](#configuration) variants.
## Usage
Use `sparrow run` to execute the instance using the binary. A `name` (a valid DNS name) is required to be passed, else
Use `sparrow run` to execute the instance using the binary. A `sparrowName` (a valid DNS name) is required to be passed, else
the sparrow will not start:

```sh
sparrow run --name sparrow.telekom.de
sparrow run --sparrowName sparrow.telekom.de
```

### Container Image
Expand Down
9 changes: 4 additions & 5 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const (
func NewCmdRun() *cobra.Command {
flagMapping := config.RunFlagsNameMapping{
ApiAddress: "apiAddress",
Name: "name",
SparrowName: "sparrowName",
LoaderType: "loaderType",
LoaderInterval: "loaderInterval",
LoaderHttpUrl: "loaderHttpUrl",
Expand All @@ -60,7 +60,7 @@ func NewCmdRun() *cobra.Command {
}

cmd.PersistentFlags().String(flagMapping.ApiAddress, ":8080", "api: The address the server is listening on")
cmd.PersistentFlags().String(flagMapping.Name, "sparrow", "The DNS name of the sparrow")
cmd.PersistentFlags().String(flagMapping.SparrowName, "sparrow", "The DNS name of the sparrow")
cmd.PersistentFlags().StringP(flagMapping.LoaderType, "l", "http",
"defines the loader type that will load the checks configuration during the runtime. The fallback is the fileLoader")
cmd.PersistentFlags().Int(flagMapping.LoaderInterval, defaultLoaderInterval, "defines the interval the loader reloads the configuration in seconds")
Expand All @@ -73,7 +73,7 @@ func NewCmdRun() *cobra.Command {
cmd.PersistentFlags().String(flagMapping.TargetManagerConfig, "tmconfig.yaml", "target manager: The path to the file to read the target manager config from")

_ = viper.BindPFlag(flagMapping.ApiAddress, cmd.PersistentFlags().Lookup(flagMapping.ApiAddress))
_ = viper.BindPFlag(flagMapping.Name, cmd.PersistentFlags().Lookup(flagMapping.Name))
_ = viper.BindPFlag(flagMapping.SparrowName, cmd.PersistentFlags().Lookup(flagMapping.SparrowName))
_ = 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))
Expand All @@ -97,7 +97,7 @@ func run(fm *config.RunFlagsNameMapping) func(cmd *cobra.Command, args []string)
cfg.SetTargetManagerConfig(config.NewTargetManagerConfig(viper.GetString(fm.TargetManagerConfig)))

cfg.SetApiAddress(viper.GetString(fm.ApiAddress))
cfg.SetName(viper.GetString(fm.Name))
cfg.SetName(viper.GetString(fm.SparrowName))

cfg.SetLoaderType(viper.GetString(fm.LoaderType))
cfg.SetLoaderInterval(viper.GetInt(fm.LoaderInterval))
Expand All @@ -107,7 +107,6 @@ func run(fm *config.RunFlagsNameMapping) func(cmd *cobra.Command, args []string)
cfg.SetLoaderHttpRetryCount(viper.GetInt(fm.LoaderHttpRetryCount))
cfg.SetLoaderHttpRetryDelay(viper.GetInt(fm.LoaderHttpRetryDelay))
cfg.SetLoaderFilePath(viper.GetString(fm.LoaderFilePath))
cfg.SetName(viper.GetString(fm.Name))

if err := cfg.Validate(ctx, fm); err != nil {
log.Error("Error while validating the config", "error", err)
Expand Down
2 changes: 1 addition & 1 deletion docs/sparrow_run.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ sparrow run [flags]
--loaderHttpUrl string http loader: The url where to get the remote configuration
--loaderInterval int defines the interval the loader reloads the configuration in seconds (default 300)
-l, --loaderType string defines the loader type that will load the checks configuration during the runtime. The fallback is the fileLoader (default "http")
--name string The DNS name of the sparrow (default "sparrow")
--sparrowName string The DNS name of the sparrow (default "sparrow")
--tmconfig string target manager: The path to the file to read the target manager config from (default "tmconfig.yaml")
```

Expand Down
4 changes: 2 additions & 2 deletions pkg/config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
package config

type RunFlagsNameMapping struct {
ApiAddress string
Name string
ApiAddress string
SparrowName string

LoaderType string
LoaderInterval string
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (c *Config) Validate(ctx context.Context, fm *RunFlagsNameMapping) error {

if !isDNSName(c.Name) {
ok = false
log.Error("The name of the sparrow must be DNS compliant", fm.Name, c.Name)
log.Error("The name of the sparrow must be DNS compliant", fm.SparrowName, c.Name)
}

switch c.Loader.Type { //nolint:gocritic
Expand Down
9 changes: 7 additions & 2 deletions pkg/sparrow/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,15 @@ func (s *Sparrow) updateCheckTargets(cfg any) any {
return checkCfg
}

actual, ok := checkCfg["targets"].([]string)
actuali, ok := checkCfg["targets"].([]any)
if !ok {
return checkCfg
}

var actual []string
for _, v := range actuali {
actual = append(actual, v.(string))
}
var urls []string
gt := s.targets.GetTargets()

Expand All @@ -177,7 +182,7 @@ func (s *Sparrow) updateCheckTargets(cfg any) any {
urls = append(urls, t.Url)
}

checkCfg["targets"] = append(checkCfg["targets"].([]string), urls...)
checkCfg["targets"] = append(actual, urls...)
return checkCfg
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/sparrow/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func TestSparrow_updateCheckTargets(t *testing.T) {
{
name: "config with targets",
config: map[string]any{
"targets": []string{"https://gitlab.com"},
"targets": []any{"https://gitlab.com"},
},
globalTargets: gt,
expected: map[string]any{
Expand All @@ -279,7 +279,7 @@ func TestSparrow_updateCheckTargets(t *testing.T) {
{
name: "config has a target already present in global targets - no duplicates",
config: map[string]any{
"targets": []string{"https://localhost.de"},
"targets": []any{"https://localhost.de"},
},
globalTargets: gt,
expected: map[string]any{
Expand All @@ -289,7 +289,7 @@ func TestSparrow_updateCheckTargets(t *testing.T) {
{
name: "global targets contains self - do not add to config",
config: map[string]any{
"targets": []string{"https://localhost.de"},
"targets": []any{"https://localhost.de"},
},
globalTargets: append(gt, checks.GlobalTarget{
Url: "https://wonderhost.usa",
Expand Down

0 comments on commit a64099f

Please sign in to comment.