From b0a0dae9cf9aa4b2a14961b64638d68882bd4a80 Mon Sep 17 00:00:00 2001 From: Julian Lawrence Date: Tue, 25 Jul 2023 22:07:32 -0700 Subject: [PATCH] rename and lint check --- README.md | 2 +- pkg/cmd/config.go | 6 ++--- pkg/cmd/k8url.go | 4 ++-- pkg/parser/parser.go | 23 +++++-------------- pkg/parser/parser_test.go | 47 --------------------------------------- 5 files changed, 11 insertions(+), 71 deletions(-) delete mode 100644 pkg/parser/parser_test.go diff --git a/README.md b/README.md index d8ea270..aafb342 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ pod: - | Other Cool Dashboards: - "https://example.com/dashboards/xyz1?name={.metadata.name}&namespace={.metadata.namespace}" -extraCommands: +commands: meta: kinds: ["*"] urls: diff --git a/pkg/cmd/config.go b/pkg/cmd/config.go index c799f13..f6e04aa 100644 --- a/pkg/cmd/config.go +++ b/pkg/cmd/config.go @@ -1,13 +1,13 @@ package cmd type Config struct { - KindAndTemplates KindAndTemplates `mapstructure:",remain"` - ExtraCommands map[string]ExtraCommand `yaml:"extraCommands"` + KindAndTemplates KindAndTemplates `mapstructure:",remain"` + Commands map[string]Command `yaml:"commands"` } type KindAndTemplates map[string]Tmpl -type ExtraCommand struct { +type Command struct { Tmpl `mapstructure:",squash"` Kinds []string `yaml:"kinds"` } diff --git a/pkg/cmd/k8url.go b/pkg/cmd/k8url.go index d8d1e85..7e4efe5 100644 --- a/pkg/cmd/k8url.go +++ b/pkg/cmd/k8url.go @@ -72,7 +72,7 @@ func initConfig() { cobra.CheckErr(fmt.Errorf("failed to unmsrshal config: %s", err.Error())) } - for k, v := range config.ExtraCommands { + for k, v := range config.Commands { k, v := k, v dynamicCmd := &cobra.Command{ Use: k, @@ -106,7 +106,7 @@ func output(obj runtime.Object, templates []string, urlTemplates []string) { // TODO cleanup - template will be a single string we need to split somehow... allUrls := strings.Split(url, "https://") for _, u := range allUrls { - browser.OpenURL(fmt.Sprintf("https://%s", u)) + browser.OpenURL(fmt.Sprintf("https://%s", u)) // nolint: errcheck } } } diff --git a/pkg/parser/parser.go b/pkg/parser/parser.go index 1e455e8..730dc3e 100644 --- a/pkg/parser/parser.go +++ b/pkg/parser/parser.go @@ -2,14 +2,15 @@ package parser import ( "bytes" + "fmt" "io" + "os" "strings" "golang.org/x/exp/slices" "k8s.io/apimachinery/pkg/runtime" "k8s.io/cli-runtime/pkg/printers" "k8s.io/client-go/kubernetes/scheme" - "k8s.io/client-go/util/jsonpath" ) func Decode(reader io.Reader) (runtime.Object, string, error) { @@ -44,25 +45,11 @@ func RenderTemplates(object runtime.Object, templates []string) ([]string, error return nil, err } buf := new(bytes.Buffer) - p.PrintObj(object, buf) + if err := p.PrintObj(object, buf); err != nil { + fmt.Fprintf(os.Stderr, "error printing object: %v", err.Error()) + } resp = append(resp, buf.String()) } return resp, nil } - -func Parse(template string, input interface{}) (string, error) { - j := jsonpath.New("").AllowMissingKeys(true) - - if err := j.Parse(template); err != nil { - return "", err - } - - buf := new(bytes.Buffer) - err := j.Execute(buf, input) - if err != nil { - return "", err - } - - return buf.String(), nil -} diff --git a/pkg/parser/parser_test.go b/pkg/parser/parser_test.go deleted file mode 100644 index 174a192..0000000 --- a/pkg/parser/parser_test.go +++ /dev/null @@ -1,47 +0,0 @@ -package parser - -import ( - "testing" - - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func TestParse(t *testing.T) { - type args struct { - template string - input interface{} - } - tests := []struct { - name string - args args - want string - wantErr bool - }{ - { - name: "what", - args: args{ - template: "{.metadata.name}", - input: corev1.Pod{ - ObjectMeta: metav1.ObjectMeta{ - Name: "what", - }, - }, - }, - want: "what", - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := Parse(tt.args.template, tt.args.input) - if (err != nil) != tt.wantErr { - t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr) - return - } - if got != tt.want { - t.Errorf("Parse() = %v, want %v", got, tt.want) - } - }) - } -}