Skip to content

Commit

Permalink
Fix NPE on unmarshalling input parameters - stock yaml library is bug…
Browse files Browse the repository at this point in the history
…gy? (#2)
  • Loading branch information
dee-kryvenko authored Mar 21, 2024
1 parent 9bf2fd0 commit 1ebb68f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
18 changes: 16 additions & 2 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,28 @@ func initConfig() {

handlerOptions := &slog.HandlerOptions{
Level: level,
AddSource: level <= slog.LevelDebug,
AddSource: level <= -100,
}
var handler slog.Handler
switch format {
case "json":
handler = slog.NewJSONHandler(os.Stderr, handlerOptions)
case "text":
handler = slog.NewTextHandler(os.Stderr, handlerOptions)
suppress := func(
next func([]string, slog.Attr) slog.Attr,
) func([]string, slog.Attr) slog.Attr {
return func(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.TimeKey {
return slog.Attr{}
}
if next == nil {
return a
}
return next(groups, a)
}
}
handlerOptions.ReplaceAttr = suppress(handlerOptions.ReplaceAttr)
handler = slog.NewTextHandler(os.Stdout, handlerOptions)
default:
log.Panicf("unknown log format: %s", format)
}
Expand Down
7 changes: 6 additions & 1 deletion cmd/secrets/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/plumber-cd/argocd-cmp-replicator/k8s"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
"sigs.k8s.io/yaml"
)

func init() {
Expand Down Expand Up @@ -47,12 +47,17 @@ var Cmd = &cobra.Command{
slog.Error("Both ARGOCD_APP_PARAMETERS and --alternative-label-selector were set")
return fmt.Errorf("Both ARGOCD_APP_PARAMETERS and --alternative-label-selector were set")
}
slog.Debug("ARGOCD_APP_PARAMETERS", "value", v)
params := argocdv1alpha1.ApplicationSourcePluginParameters{}
if err := yaml.Unmarshal([]byte(v), &params); err != nil {
return err
}
for _, param := range params {
if param.Name == "alternative-label-selector" {
if param.String_ == nil {
slog.Error("alternative-label-selector is not a string")
return fmt.Errorf("alternative-label-selector is not a string")
}
alternativeLabelSelector = *param.String_
break
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ require (
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.8.4
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.26.11
k8s.io/apimachinery v0.26.11
k8s.io/cli-runtime v0.26.11
k8s.io/client-go v0.26.11
sigs.k8s.io/yaml v1.3.0
)

// https://argo-cd.readthedocs.io/en/stable/user-guide/import/
Expand Down Expand Up @@ -179,6 +179,7 @@ require (
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiextensions-apiserver v0.26.10 // indirect
k8s.io/apiserver v0.26.11 // indirect
Expand All @@ -195,5 +196,4 @@ require (
sigs.k8s.io/kustomize/api v0.13.5-0.20230601165947-6ce0bf390ce3 // indirect
sigs.k8s.io/kustomize/kyaml v0.14.3-0.20230601165947-6ce0bf390ce3 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

0 comments on commit 1ebb68f

Please sign in to comment.