From 4aac24c444a9ec2466f8e92810f3a0c84295914f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Kalke?= <56382792+MichalKalke@users.noreply.github.com> Date: Mon, 3 Jun 2024 14:19:56 +0200 Subject: [PATCH] Add new catalog flag (#2126) --- go.mod | 1 + go.sum | 2 + internal/cmd/kyma.go | 2 + internal/cmd/modules/modules.go | 91 +++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+) create mode 100644 internal/cmd/modules/modules.go diff --git a/go.mod b/go.mod index 0c1afb552..2b795b955 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.22.2 require ( github.com/gboddin/go-www-authenticate-parser v0.0.0-20230926203616-ec0b649bb077 github.com/google/go-containerregistry v0.19.1 + github.com/savaki/jq v0.0.0-20161209013833-0e6baecebbf8 github.com/spf13/cobra v1.8.0 github.com/stretchr/testify v1.9.0 k8s.io/api v0.30.1 diff --git a/go.sum b/go.sum index 83932d479..1947269bf 100644 --- a/go.sum +++ b/go.sum @@ -129,6 +129,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/savaki/jq v0.0.0-20161209013833-0e6baecebbf8 h1:ajJQhvqPSQFJJ4aV5mDAMx8F7iFi6Dxfo6y62wymLNs= +github.com/savaki/jq v0.0.0-20161209013833-0e6baecebbf8/go.mod h1:Nw/CCOXNyF5JDd6UpYxBwG5WWZ2FOJ/d5QnXL4KQ6vY= github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= diff --git a/internal/cmd/kyma.go b/internal/cmd/kyma.go index 04e2c9943..9444e4672 100644 --- a/internal/cmd/kyma.go +++ b/internal/cmd/kyma.go @@ -2,6 +2,7 @@ package cmd import ( "context" + "github.com/kyma-project/cli.v3/internal/cmd/modules" "github.com/kyma-project/cli.v3/internal/cmd/access" @@ -39,6 +40,7 @@ func NewKymaCMD() *cobra.Command { cmd.AddCommand(referenceinstance.NewReferenceInstanceCMD(config)) cmd.AddCommand(access.NewAccessCMD(config)) cmd.AddCommand(oidc.NewOIDCCMD(config)) + cmd.AddCommand(modules.NewModulesCMD()) return cmd } diff --git a/internal/cmd/modules/modules.go b/internal/cmd/modules/modules.go new file mode 100644 index 000000000..f1b6bc181 --- /dev/null +++ b/internal/cmd/modules/modules.go @@ -0,0 +1,91 @@ +package modules + +import ( + "encoding/json" + "fmt" + "io" + "net/http" + + "github.com/kyma-project/cli.v3/internal/clierror" + "github.com/spf13/cobra" +) + +type modulesConfig struct { + catalog bool + managed bool + installed bool +} + +func NewModulesCMD() *cobra.Command { + + config := modulesConfig{} + + cmd := &cobra.Command{ + Use: "modules", + Short: "List modules.", + Long: `List either installed, managed or available Kyma modules.`, + Run: func(_ *cobra.Command, _ []string) { + clierror.Check(runModules(&config)) + }, + } + + cmd.Flags().BoolVar(&config.catalog, "catalog", false, "List of al available Kyma modules.") + cmd.Flags().BoolVar(&config.managed, "managed", false, "List of all Kyma modules managed by central control-plane.") + cmd.Flags().BoolVar(&config.installed, "installed", false, "List of all currently installed Kyma modules.") + + cmd.MarkFlagsOneRequired("catalog", "managed", "installed") + cmd.MarkFlagsMutuallyExclusive("catalog", "managed") + cmd.MarkFlagsMutuallyExclusive("catalog", "installed") + cmd.MarkFlagsMutuallyExclusive("managed", "installed") + + return cmd +} + +func runModules(config *modulesConfig) clierror.Error { + var err error + if config.catalog { + modules, err := listAllModules() + if err != nil { + return clierror.WrapE(err, clierror.New("failed to list all Kyma modules")) + } + fmt.Println("Available modules:\n") + for _, rec := range modules { + fmt.Println(rec) + } + return nil + } + + if config.managed || config.installed { + clierror.Wrap(err, clierror.New("not implemented yet, please use the catalog flag")) + } + //TODO: installed and managed to implement + + return clierror.Wrap(err, clierror.New("failed to get modules", "please use one of: catalog, managed or installed flags")) +} + +func listAllModules() ([]string, clierror.Error) { + resp, err := http.Get("https://raw.githubusercontent.com/kyma-project/community-modules/main/model.json") + if err != nil { + return nil, clierror.Wrap(err, clierror.New("while getting modules list")) + } + defer resp.Body.Close() + + var template []struct { + Name string `json:"name"` + } + + bodyText, err := io.ReadAll(resp.Body) + if err != nil { + return nil, clierror.Wrap(err, clierror.New("while reading http response")) + } + err = json.Unmarshal(bodyText, &template) + if err != nil { + return nil, clierror.Wrap(err, clierror.New("while unmarshalling")) + } + + var out []string + for _, rec := range template { + out = append(out, rec.Name) + } + return out, nil +}