Skip to content

Commit

Permalink
Add new catalog flag (#2126)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalKalke authored Jun 3, 2024
1 parent 4e2198a commit 4aac24c
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 2 additions & 0 deletions internal/cmd/kyma.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
}
91 changes: 91 additions & 0 deletions internal/cmd/modules/modules.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 4aac24c

Please sign in to comment.