From 79056b0178e89e033a22d8da3e5c1418496b7396 Mon Sep 17 00:00:00 2001 From: Marcin Maciaszczyk Date: Wed, 4 Dec 2024 14:02:18 +0100 Subject: [PATCH] remove push and repos commands --- cmd/command/plural/plural.go | 5 - cmd/command/push/push.go | 186 --------------------------- cmd/command/repo/repos.go | 222 --------------------------------- cmd/command/repo/repos_test.go | 102 --------------- 4 files changed, 515 deletions(-) delete mode 100644 cmd/command/push/push.go delete mode 100644 cmd/command/repo/repos.go delete mode 100644 cmd/command/repo/repos_test.go diff --git a/cmd/command/plural/plural.go b/cmd/command/plural/plural.go index cf0b6d84a..8fab304d8 100644 --- a/cmd/command/plural/plural.go +++ b/cmd/command/plural/plural.go @@ -19,8 +19,6 @@ import ( "github.com/pluralsh/plural-cli/cmd/command/pr" "github.com/pluralsh/plural-cli/cmd/command/profile" "github.com/pluralsh/plural-cli/cmd/command/proxy" - "github.com/pluralsh/plural-cli/cmd/command/push" - "github.com/pluralsh/plural-cli/cmd/command/repo" "github.com/pluralsh/plural-cli/cmd/command/up" "github.com/pluralsh/plural-cli/cmd/command/upgrade" "github.com/pluralsh/plural-cli/cmd/command/version" @@ -230,9 +228,6 @@ func CreateNewApp(plural *Plural) *cli.App { profile.Command(), pr.Command(plural.Plural), proxy.Command(plural.Plural), - push.Command(plural.Plural), - repo.Command(plural.Plural), - repo.APICommand(plural.Plural), log.Command(plural.Plural), info.Command(plural.Plural), cmdinit.Command(plural.Plural), diff --git a/cmd/command/push/push.go b/cmd/command/push/push.go deleted file mode 100644 index 7533a880b..000000000 --- a/cmd/command/push/push.go +++ /dev/null @@ -1,186 +0,0 @@ -package push - -import ( - "fmt" - "os" - "path/filepath" - - "github.com/pluralsh/plural-cli/pkg/client" - - "github.com/pluralsh/plural-cli/pkg/common" - - "github.com/pluralsh/plural-cli/pkg/api" - "github.com/pluralsh/plural-cli/pkg/config" - "github.com/pluralsh/plural-cli/pkg/helm" - scftmpl "github.com/pluralsh/plural-cli/pkg/scaffold/template" - "github.com/pluralsh/plural-cli/pkg/utils" - "github.com/pluralsh/plural-cli/pkg/utils/pathing" - "github.com/urfave/cli" - "sigs.k8s.io/yaml" -) - -type Plural struct { - client.Plural -} - -func Command(clients client.Plural) cli.Command { - p := Plural{ - Plural: clients, - } - return cli.Command{ - Name: "push", - Usage: "utilities for pushing tf or helm packages", - Subcommands: p.pushCommands(), - Category: "Publishing", - } -} - -func (p *Plural) pushCommands() []cli.Command { - return []cli.Command{ - { - Name: "terraform", - Usage: "pushes a terraform module", - ArgsUsage: "{path-to-module} {repo}", - Action: common.LatestVersion(p.handleTerraformUpload), - }, - { - Name: "helm", - Usage: "pushes a helm chart", - ArgsUsage: "{path-to-chart} {repo}", - Action: common.LatestVersion(handleHelmUpload), - }, - { - Name: "recipe", - Usage: "pushes a recipe", - ArgsUsage: "{path-to-recipe} {repo}", - Action: common.LatestVersion(p.handleRecipeUpload), - }, - { - Name: "artifact", - Usage: "creates an artifact for the repo", - ArgsUsage: "{path-to-def} {repo}", - Action: common.LatestVersion(p.handleArtifact), - Flags: []cli.Flag{ - cli.StringFlag{ - Name: "platform", - Value: "mac", - Usage: "name of the OS this binary is built for", - }, - cli.StringFlag{ - Name: "arch", - Value: "amd64", - Usage: "machine architecture the binary is compatible with", - }, - }, - }, - { - Name: "crd", - Usage: "registers a new crd for a chart", - ArgsUsage: "{path-to-def} {repo} {chart}", - Action: common.LatestVersion(p.createCrd), - }, - } -} - -func (p *Plural) handleTerraformUpload(c *cli.Context) error { - p.InitPluralClient() - _, err := p.UploadTerraform(c.Args().Get(0), c.Args().Get(1)) - return api.GetErrorResponse(err, "UploadTerraform") -} - -func handleHelmUpload(c *cli.Context) error { - conf := config.Read() - pth, repo := c.Args().Get(0), c.Args().Get(1) - - f, err := buildValuesFromTemplate(pth) - if err != nil { - return err - } - defer func(name string) { - _ = os.Remove(name) - - }(f.Name()) - - utils.Highlight("linting helm: ") - values, err := getValues(f.Name()) - if err != nil { - return err - } - if err := helm.Lint(pth, "default", values); err != nil { - return err - } - - cmUrl := fmt.Sprintf("%s/cm/%s", conf.BaseUrl(), repo) - return helm.Push(pth, cmUrl) -} - -func buildValuesFromTemplate(pth string) (f *os.File, err error) { - templatePath := pathing.SanitizeFilepath(filepath.Join(pth, "values.yaml.tpl")) - _, err = utils.ReadFile(templatePath) - if os.IsNotExist(err) { - templatePath = pathing.SanitizeFilepath(filepath.Join(pth, "values.yaml.lua")) - _, err := utils.ReadFile(templatePath) - if err != nil { - return nil, err - } - - } - - return scftmpl.TmpValuesFile(templatePath) -} - -func (p *Plural) handleRecipeUpload(c *cli.Context) error { - p.InitPluralClient() - fullPath, _ := filepath.Abs(c.Args().Get(0)) - contents, err := os.ReadFile(fullPath) - if err != nil { - return err - } - - recipeInput, err := api.ConstructRecipe(contents) - if err != nil { - return err - } - - _, err = p.CreateRecipe(c.Args().Get(1), recipeInput) - return api.GetErrorResponse(err, "CreateRecipe") -} - -func (p *Plural) handleArtifact(c *cli.Context) error { - p.InitPluralClient() - fullPath, _ := filepath.Abs(c.Args().Get(0)) - contents, err := os.ReadFile(fullPath) - if err != nil { - return err - } - - input, err := api.ConstructArtifactAttributes(contents) - if err != nil { - return err - } - input.Platform = c.String("platform") - input.Arch = c.String("arch") - _, err = p.CreateArtifact(c.Args().Get(1), input) - return api.GetErrorResponse(err, "CreateArtifact") -} - -func (p *Plural) createCrd(c *cli.Context) error { - p.InitPluralClient() - fullPath, _ := filepath.Abs(c.Args().Get(0)) - repo := c.Args().Get(1) - chart := c.Args().Get(2) - err := p.CreateCrd(repo, chart, fullPath) - return api.GetErrorResponse(err, "CreateCrd") -} - -func getValues(path string) (map[string]interface{}, error) { - values := make(map[string]interface{}) - valsContent, err := os.ReadFile(path) - if err != nil { - return nil, err - } - if err := yaml.Unmarshal(valsContent, &values); err != nil { - return nil, err - } - return values, nil -} diff --git a/cmd/command/repo/repos.go b/cmd/command/repo/repos.go deleted file mode 100644 index b34b52b77..000000000 --- a/cmd/command/repo/repos.go +++ /dev/null @@ -1,222 +0,0 @@ -package repo - -import ( - "fmt" - "strings" - - "github.com/pluralsh/plural-cli/pkg/client" - - "github.com/pluralsh/plural-cli/pkg/common" - - "github.com/urfave/cli" - - "github.com/pluralsh/plural-cli/pkg/api" - "github.com/pluralsh/plural-cli/pkg/bundle" - "github.com/pluralsh/plural-cli/pkg/config" - "github.com/pluralsh/plural-cli/pkg/format" - "github.com/pluralsh/plural-cli/pkg/manifest" - "github.com/pluralsh/plural-cli/pkg/utils" -) - -type Plural struct { - client.Plural -} - -func Command(clients client.Plural) cli.Command { - p := Plural{ - Plural: clients, - } - return cli.Command{ - Name: "repos", - Usage: "view and manage plural repositories", - Subcommands: p.reposCommands(), - Category: "API", - } -} - -func APICommand(clients client.Plural) cli.Command { - p := Plural{ - Plural: clients, - } - return cli.Command{ - Name: "apps", - Usage: "view and manage plural repositories", - Subcommands: p.reposCommands(), - Category: "API", - } -} - -func (p *Plural) reposCommands() []cli.Command { - return []cli.Command{ - { - Name: "unlock", - Usage: "unlocks installations in a repo that have breaking changes", - ArgsUsage: "{app}", - Action: common.LatestVersion(common.RequireArgs(p.handleUnlockRepo, []string{"{app}"})), - }, - { - Name: "release", - Usage: "tags the installations in the current cluster with the given release channels", - ArgsUsage: "{app}", - Flags: []cli.Flag{ - cli.StringSliceFlag{ - Name: "tag", - Usage: "tag name for a given release channel, eg stable, warm, dev, prod", - }, - }, - Action: common.LatestVersion(common.RequireArgs(p.handleRelease, []string{"{app}"})), - }, - { - Name: "reinstall", - Usage: "reinstalls all bundles from a previous installation", - Flags: []cli.Flag{ - cli.BoolFlag{ - Name: "refresh", - Usage: "re-enter the configuration for all bundles", - }, - }, - Action: p.handleReinstall, - }, - { - Name: "reset", - Usage: "eliminates your current plural installation set, to change cloud provider or eject from plural", - Action: common.LatestVersion(p.handleResetInstallations), - }, - { - Name: "synced", - Usage: "marks installations in this repo as being synced", - Action: p.handleMarkSynced, - }, - { - Name: "uninstall", - Usage: "uninstall an app from the plural api", - ArgsUsage: "{app}", - Action: common.LatestVersion(common.RequireArgs(p.handleUninstall, []string{"{app}"})), - }, - { - Name: "list", - Usage: "list available repositories to install", - Flags: []cli.Flag{ - cli.StringFlag{ - Name: "query", - Usage: "string to search by", - }, - cli.StringFlag{ - Name: "format", - Usage: "format to print the repositories out, eg csv or default is table", - }, - }, - Action: common.LatestVersion(p.handleListRepositories), - }, - } -} - -func (p *Plural) handleRelease(c *cli.Context) error { - p.InitPluralClient() - app := c.Args().First() - tags := c.StringSlice("tag") - err := p.Release(c.Args().First(), c.StringSlice("tag")) - if err != nil { - return api.GetErrorResponse(err, "Release") - } - - utils.Success("Published release for %s to channels [%s]\n", app, strings.Join(tags, ", ")) - return nil -} - -func (p *Plural) handleUnlockRepo(c *cli.Context) error { - p.InitPluralClient() - err := p.UnlockRepository(c.Args().First()) - return api.GetErrorResponse(err, "UnlockRepository") -} - -func (p *Plural) handleUninstall(c *cli.Context) error { - p.InitPluralClient() - inst, err := p.GetInstallation(c.Args().First()) - if err != nil { - return api.GetErrorResponse(err, "GetInstallation") - } - - if inst == nil { - return fmt.Errorf("%s already uninstalled", c.Args().First()) - } - err = p.DeleteInstallation(inst.Id) - return api.GetErrorResponse(err, "DeleteInstallation") -} - -func (p *Plural) handleListRepositories(c *cli.Context) error { - p.InitPluralClient() - repos, err := p.ListRepositories(c.String("query")) - if err != nil { - return api.GetErrorResponse(err, "ListRepositories") - } - - addIcon := c.String("format") == "csv" - - formatter := format.New(format.FormatType(c.String("format"))) - header := []string{"Repo", "Description", "Publisher", "Bundles"} - if addIcon { - header = append(header, "Icon") - } - - formatter.Header(header) - for _, repo := range repos { - recipeNames := utils.Map(repo.Recipes, func(recipe *api.Recipe) string { - return recipe.Name - }) - - line := []string{repo.Name, repo.Description, repo.Publisher.Name, strings.Join(recipeNames, ", ")} - if addIcon { - line = append(line, repo.Icon) - } - if err := formatter.Write(line); err != nil { - return err - } - } - - if err := formatter.Flush(); err != nil { - return err - } - return nil -} - -func (p *Plural) handleReinstall(c *cli.Context) error { - p.InitPluralClient() - ctx, err := manifest.FetchContext() - if err != nil { - return err - } - - for _, b := range ctx.Bundles { - if err := bundle.Install(p.Client, b.Repository, b.Name, c.Bool("refresh")); err != nil { - return err - } - - fmt.Println("Moving to the next bundle....") - } - - return nil -} - -func (p *Plural) handleMarkSynced(c *cli.Context) error { - p.InitPluralClient() - return p.MarkSynced(c.Args().Get(0)) -} - -func (p *Plural) handleResetInstallations(c *cli.Context) error { - p.InitPluralClient() - conf := config.Read() - if !common.Confirm(fmt.Sprintf("Are you sure you want to reset installations for %s? This will also wipe all oidc providers and any other associated state in the plural api", conf.Email), "PLURAL_REPOS_RESET_CONFIRM") { - return nil - } - - count, err := p.ResetInstallations() - if err != nil { - return api.GetErrorResponse(err, "ResetInstallations") - } - - fmt.Printf("Deleted %d installations in app.plural.sh\n", count) - fmt.Println("(you can recreate these at any time and any running infrastructure is not affected, plural will simply no longer deliver upgrades)") - utils.Note("Now run `plural bundle install ` to install a new app \n") - return nil -} diff --git a/cmd/command/repo/repos_test.go b/cmd/command/repo/repos_test.go deleted file mode 100644 index c2dfa965e..000000000 --- a/cmd/command/repo/repos_test.go +++ /dev/null @@ -1,102 +0,0 @@ -package repo_test - -import ( - "os" - "testing" - - clientcmd "github.com/pluralsh/plural-cli/pkg/client" - - "github.com/pluralsh/plural-cli/pkg/common" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/mock" - - "github.com/pluralsh/plural-cli/cmd/command/plural" - "github.com/pluralsh/plural-cli/pkg/api" - "github.com/pluralsh/plural-cli/pkg/test/mocks" -) - -func TestListRepositories(t *testing.T) { - tests := []struct { - name string - args []string - repos []*api.Repository - expectedResponse string - }{ - { - name: `test "repos list"`, - args: []string{plural.ApplicationName, "repos", "list"}, - repos: []*api.Repository{ - { - Id: "123", - Name: "test", - Description: "test application", - Publisher: &api.Publisher{ - Id: "456", - Name: "test", - }, - Recipes: []*api.Recipe{ - { - Id: "789", - Name: "r1", - }, - { - Id: "101", - Name: "r2", - }, - }, - }, - }, - expectedResponse: `+------+------------------+-----------+---------+ -| REPO | DESCRIPTION | PUBLISHER | BUNDLES | -+------+------------------+-----------+---------+ -| test | test application | test | r1, r2 | -+------+------------------+-----------+---------+ -`, - }, - } - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - client := mocks.NewClient(t) - client.On("ListRepositories", mock.AnythingOfType("string")).Return(test.repos, nil) - app := plural.CreateNewApp(&plural.Plural{Plural: clientcmd.Plural{ - Client: client, - }}) - app.HelpName = plural.ApplicationName - os.Args = test.args - res, err := common.CaptureStdout(app, os.Args) - assert.NoError(t, err) - assert.Equal(t, test.expectedResponse, res) - }) - } -} - -// func TestResetRepositories(t *testing.T) { -// tests := []struct { -// name string -// args []string -// count int -// expectedResponse string -// }{ -// { -// name: `test "repos reset"`, -// args: []string{plural.ApplicationName, "repos", "reset"}, -// count: 5, -// expectedResponse: `Deleted 5 installations in app.plural.sh -// (you can recreate these at any time and any running infrastructure is not affected, plural will simply no longer deliver upgrades) -// `, -// }, -// } -// for _, test := range tests { -// t.Run(test.name, func(t *testing.T) { -// client := mocks.NewClient(t) -// client.On("ResetInstallations").Return(test.count, nil) -// app := plural.CreateNewApp(&plural.Plural{Client: client}) -// app.HelpName = plural.ApplicationName -// os.Args = test.args -// res, err := captureStdout(app, os.Args) -// assert.NoError(t, err) -// assert.Equal(t, test.expectedResponse, res) -// }) -// } -// }