diff --git a/cmd/command/api/api.go b/cmd/command/api/api.go index 004c6d11c..f0946c416 100644 --- a/cmd/command/api/api.go +++ b/cmd/command/api/api.go @@ -4,8 +4,6 @@ import ( "github.com/pluralsh/plural-cli/pkg/api" "github.com/pluralsh/plural-cli/pkg/client" "github.com/pluralsh/plural-cli/pkg/common" - "github.com/pluralsh/plural-cli/pkg/utils" - "github.com/pluralsh/polly/algorithms" "github.com/urfave/cli" ) @@ -27,55 +25,6 @@ func Command(clients client.Plural) cli.Command { func (p *Plural) apiCommands() []cli.Command { return []cli.Command{ - { - Name: "list", - Usage: "lists plural resources", - Subcommands: []cli.Command{ - { - Name: "installations", - Usage: "lists your installations", - Action: common.LatestVersion(p.handleInstallations), - }, - { - Name: "charts", - Usage: "lists charts for a repository", - ArgsUsage: "{repository-id}", - Action: common.LatestVersion(common.RequireArgs(p.handleCharts, []string{"{repository-id}"})), - }, - { - Name: "terraform", - Usage: "lists terraform modules for a repository", - ArgsUsage: "{repository-id}", - Action: common.LatestVersion(common.RequireArgs(p.handleTerraforma, []string{"{repository-id}"})), - }, - { - Name: "versions", - Usage: "lists versions of a chart", - ArgsUsage: "{chart-id}", - Action: common.LatestVersion(common.RequireArgs(p.handleVersions, []string{"{chart-id}"})), - }, - { - Name: "chartinstallations", - Aliases: []string{"ci"}, - Usage: "lists chart installations for a repository", - ArgsUsage: "{repository-id}", - Action: common.LatestVersion(common.RequireArgs(p.handleChartInstallations, []string{"{repository-id}"})), - }, - { - Name: "terraforminstallations", - Aliases: []string{"ti"}, - Usage: "lists terraform installations for a repository", - ArgsUsage: "{repository-id}", - Action: common.LatestVersion(common.RequireArgs(p.handleTerraformInstallations, []string{"{repository-id}"})), - }, - { - Name: "artifacts", - Usage: "Lists artifacts for a repository", - ArgsUsage: "{repository-id}", - Action: common.LatestVersion(common.RequireArgs(p.handleArtifacts, []string{"{repository-id}"})), - }, - }, - }, { Name: "create", Usage: "creates plural resources", @@ -91,116 +40,6 @@ func (p *Plural) apiCommands() []cli.Command { } } -func (p *Plural) handleInstallations(c *cli.Context) error { - p.InitPluralClient() - installations, err := p.GetInstallations() - if err != nil { - return api.GetErrorResponse(err, "GetInstallations") - } - - installations = algorithms.Filter(installations, func(v *api.Installation) bool { - return v.Repository != nil - }) - - headers := []string{"Repository", "Repository Id", "Publisher"} - return utils.PrintTable(installations, headers, func(inst *api.Installation) ([]string, error) { - repo := inst.Repository - publisherName := "" - if repo.Publisher != nil { - publisherName = repo.Publisher.Name - } - return []string{repo.Name, repo.Id, publisherName}, nil - }) -} - -func (p *Plural) handleCharts(c *cli.Context) error { - p.InitPluralClient() - charts, err := p.GetCharts(c.Args().First()) - if err != nil { - return api.GetErrorResponse(err, "GetCharts") - } - - headers := []string{"Id", "Name", "Description", "Latest Version"} - return utils.PrintTable(charts, headers, func(c *api.Chart) ([]string, error) { - return []string{c.Id, c.Name, c.Description, c.LatestVersion}, nil - }) -} - -func (p *Plural) handleTerraforma(c *cli.Context) error { - p.InitPluralClient() - tfs, err := p.GetTerraform(c.Args().First()) - if err != nil { - return api.GetErrorResponse(err, "GetTerraforma") - } - - headers := []string{"Id", "Name", "Description"} - return utils.PrintTable(tfs, headers, func(tf *api.Terraform) ([]string, error) { - return []string{tf.Id, tf.Name, tf.Description}, nil - }) -} - -func (p *Plural) handleVersions(c *cli.Context) error { - p.InitPluralClient() - versions, err := p.GetVersions(c.Args().First()) - if err != nil { - return api.GetErrorResponse(err, "GetVersions") - } - - headers := []string{"Id", "Version"} - return utils.PrintTable(versions, headers, func(v *api.Version) ([]string, error) { - return []string{v.Id, v.Version}, nil - }) -} - -func (p *Plural) handleChartInstallations(c *cli.Context) error { - p.InitPluralClient() - chartInstallations, err := p.GetChartInstallations(c.Args().First()) - if err != nil { - return api.GetErrorResponse(err, "GetChartInstallations") - } - - cis := algorithms.Filter(chartInstallations, func(ci *api.ChartInstallation) bool { - return ci.Chart != nil && ci.Version != nil - }) - - row := func(ci *api.ChartInstallation) ([]string, error) { - return []string{ci.Id, ci.Chart.Id, ci.Chart.Name, ci.Version.Version}, nil - } - headers := []string{"Id", "Chart Id", "Chart Name", "Version"} - return utils.PrintTable(cis, headers, row) -} - -func (p *Plural) handleTerraformInstallations(c *cli.Context) error { - p.InitPluralClient() - terraformInstallations, err := p.GetTerraformInstallations(c.Args().First()) - if err != nil { - return api.GetErrorResponse(err, "GetTerraformInstallations") - } - - tis := algorithms.Filter(terraformInstallations, func(ti *api.TerraformInstallation) bool { - return ti != nil - }) - - headers := []string{"Id", "Terraform Id", "Name"} - return utils.PrintTable(tis, headers, func(ti *api.TerraformInstallation) ([]string, error) { - tf := ti.Terraform - return []string{ti.Id, tf.Id, tf.Name}, nil - }) -} - -func (p *Plural) handleArtifacts(c *cli.Context) error { - p.InitPluralClient() - artifacts, err := p.ListArtifacts(c.Args().First()) - if err != nil { - return api.GetErrorResponse(err, "ListArtifacts") - } - - headers := []string{"Id", "Name", "Platform", "Blob", "Sha"} - return utils.PrintTable(artifacts, headers, func(art api.Artifact) ([]string, error) { - return []string{art.Id, art.Name, art.Platform, art.Blob, art.Sha}, nil - }) -} - func (p *Plural) handleCreateDomain(c *cli.Context) error { p.InitPluralClient() err := p.CreateDomain(c.Args().First()) diff --git a/cmd/command/api/api_test.go b/cmd/command/api/api_test.go deleted file mode 100644 index e940e3d6a..000000000 --- a/cmd/command/api/api_test.go +++ /dev/null @@ -1,436 +0,0 @@ -package api_test - -import ( - "os" - "testing" - - "github.com/pluralsh/plural-cli/cmd/command/plural" - "github.com/pluralsh/plural-cli/pkg/api" - pluralclient "github.com/pluralsh/plural-cli/pkg/client" - "github.com/pluralsh/plural-cli/pkg/common" - "github.com/pluralsh/plural-cli/pkg/test/mocks" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/mock" -) - -func TestListArtifacts(t *testing.T) { - tests := []struct { - name string - args []string - artifacts []api.Artifact - expectedResponse string - expectedError string - }{ - { - name: `test "api list artifacts" with single response`, - args: []string{plural.ApplicationName, "api", "list", "artifacts", "test"}, - artifacts: []api.Artifact{{ - Id: "abc", - Name: "test", - Blob: "test", - Sha: "xyz", - Platform: "aws", - }}, - expectedResponse: `+-----+------+----------+------+-----+ -| ID | NAME | PLATFORM | BLOB | SHA | -+-----+------+----------+------+-----+ -| abc | test | aws | test | xyz | -+-----+------+----------+------+-----+ -`, - }, - { - name: `test "api list artifacts" without {repository-id} parameter`, - args: []string{plural.ApplicationName, "api", "list", "artifacts"}, - expectedError: "Not enough arguments provided: needs {repository-id}. Try running --help to see usage.", - artifacts: []api.Artifact{}, - }, - } - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - client := mocks.NewClient(t) - if test.expectedError == "" { - client.On("ListArtifacts", mock.AnythingOfType("string")).Return(test.artifacts, nil) - } - app := plural.CreateNewApp(&plural.Plural{Plural: pluralclient.Plural{ - Client: client, - }}) - app.HelpName = plural.ApplicationName - os.Args = test.args - res, err := common.CaptureStdout(app, os.Args) - if test.expectedError != "" { - assert.Equal(t, err.Error(), test.expectedError) - } else { - assert.NoError(t, err) - assert.Equal(t, test.expectedResponse, res) - } - - }) - } -} - -func TestGetInstallations(t *testing.T) { - tests := []struct { - name string - args []string - installations []*api.Installation - expectedResponse string - }{ - { - name: `test "api list installations"`, - args: []string{plural.ApplicationName, "api", "list", "installations"}, - installations: []*api.Installation{ - {Id: "123", Repository: &api.Repository{Id: "abc", Name: "test-1", Publisher: &api.Publisher{Name: "Plural"}}}, - {Id: "456", Repository: &api.Repository{Id: "def", Name: "test-2", Publisher: &api.Publisher{Name: "Plural"}}}, - }, - expectedResponse: `+------------+---------------+-----------+ -| REPOSITORY | REPOSITORY ID | PUBLISHER | -+------------+---------------+-----------+ -| test-1 | abc | Plural | -| test-2 | def | Plural | -+------------+---------------+-----------+ -`, - }, - { - name: `test "api list installations" when Repository is nil`, - args: []string{plural.ApplicationName, "api", "list", "installations"}, - installations: []*api.Installation{{Id: "abc"}}, - expectedResponse: `+------------+---------------+-----------+ -| REPOSITORY | REPOSITORY ID | PUBLISHER | -+------------+---------------+-----------+ -+------------+---------------+-----------+ -`, - }, - { - name: `test "api list installations" when Publisher is nil`, - args: []string{plural.ApplicationName, "api", "list", "installations"}, - installations: []*api.Installation{{Id: "abc", Repository: &api.Repository{Id: "abc", Name: "test"}}}, - expectedResponse: `+------------+---------------+-----------+ -| REPOSITORY | REPOSITORY ID | PUBLISHER | -+------------+---------------+-----------+ -| test | abc | | -+------------+---------------+-----------+ -`, - }, - } - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - client := mocks.NewClient(t) - client.On("GetInstallations").Return(test.installations, nil) - app := plural.CreateNewApp(&plural.Plural{Plural: pluralclient.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 TestGetCharts(t *testing.T) { - tests := []struct { - name string - args []string - charts []*api.Chart - expectedResponse string - expectedError string - }{ - { - name: `test "api list charts" with single response`, - args: []string{plural.ApplicationName, "api", "list", "charts", "test"}, - charts: []*api.Chart{{ - Id: "123", - Name: "test", - Description: "test chart", - LatestVersion: "0.1.0", - }}, - expectedResponse: `+-----+------+-------------+----------------+ -| ID | NAME | DESCRIPTION | LATEST VERSION | -+-----+------+-------------+----------------+ -| 123 | test | test chart | 0.1.0 | -+-----+------+-------------+----------------+ -`, - }, - { - name: `test "api list charts" without {repository-id} parameter`, - args: []string{plural.ApplicationName, "api", "list", "charts"}, - charts: []*api.Chart{}, - expectedError: "Not enough arguments provided: needs {repository-id}. Try running --help to see usage.", - }, - } - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - client := mocks.NewClient(t) - if test.expectedError == "" { - client.On("GetCharts", mock.AnythingOfType("string")).Return(test.charts, nil) - } - app := plural.CreateNewApp(&plural.Plural{Plural: pluralclient.Plural{ - Client: client, - }}) - app.HelpName = plural.ApplicationName - os.Args = test.args - res, err := common.CaptureStdout(app, os.Args) - if test.expectedError != "" { - assert.Equal(t, err.Error(), test.expectedError) - } else { - assert.NoError(t, err) - assert.Equal(t, test.expectedResponse, res) - } - }) - } -} - -func TestGetTerraform(t *testing.T) { - tests := []struct { - name string - args []string - terraform []*api.Terraform - expectedResponse string - expectedError string - }{ - { - name: `test "api list terraform"`, - args: []string{plural.ApplicationName, "api", "list", "terraform", "test"}, - terraform: []*api.Terraform{ - { - Id: "123", - Name: "test-1", - Description: "test terraform", - }, - { - Id: "456", - Name: "test-2", - Description: "test terraform", - }, - }, - expectedResponse: `+-----+--------+----------------+ -| ID | NAME | DESCRIPTION | -+-----+--------+----------------+ -| 123 | test-1 | test terraform | -| 456 | test-2 | test terraform | -+-----+--------+----------------+ -`, - }, - { - name: `test "api list terraform" without {repository-id} parameter`, - args: []string{plural.ApplicationName, "api", "list", "terraform"}, - terraform: []*api.Terraform{}, - expectedError: "Not enough arguments provided: needs {repository-id}. Try running --help to see usage.", - }, - } - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - client := mocks.NewClient(t) - if test.expectedError == "" { - client.On("GetTerraform", mock.AnythingOfType("string")).Return(test.terraform, nil) - } - - app := plural.CreateNewApp(&plural.Plural{Plural: pluralclient.Plural{ - Client: client, - }}) - app.HelpName = plural.ApplicationName - os.Args = test.args - res, err := common.CaptureStdout(app, os.Args) - if test.expectedError != "" { - assert.Equal(t, err.Error(), test.expectedError) - } else { - assert.NoError(t, err) - assert.Equal(t, test.expectedResponse, res) - } - }) - } -} - -func TestGetVersons(t *testing.T) { - tests := []struct { - name string - args []string - versions []*api.Version - expectedResponse string - expectedError string - }{ - { - name: `test "api list versions"`, - args: []string{plural.ApplicationName, "api", "list", "versions", "abc"}, - versions: []*api.Version{ - { - Id: "abc", - Version: "1", - }, - { - Id: "abc", - Version: "2", - }, - }, - expectedResponse: `+-----+---------+ -| ID | VERSION | -+-----+---------+ -| abc | 1 | -| abc | 2 | -+-----+---------+ -`, - }, - { - name: `test "api list versions" without {chart-id} parameter`, - args: []string{plural.ApplicationName, "api", "list", "versions"}, - versions: []*api.Version{}, - expectedError: "Not enough arguments provided: needs {chart-id}. Try running --help to see usage.", - }, - } - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - client := mocks.NewClient(t) - if test.expectedError == "" { - client.On("GetVersions", mock.AnythingOfType("string")).Return(test.versions, nil) - } - app := plural.CreateNewApp(&plural.Plural{Plural: pluralclient.Plural{ - Client: client, - }}) - app.HelpName = plural.ApplicationName - os.Args = test.args - res, err := common.CaptureStdout(app, os.Args) - if test.expectedError != "" { - assert.Equal(t, err.Error(), test.expectedError) - } else { - assert.NoError(t, err) - assert.Equal(t, test.expectedResponse, res) - } - }) - } -} - -func TestGetChartInstallations(t *testing.T) { - tests := []struct { - name string - args []string - chartInstallations []*api.ChartInstallation - expectedResponse string - expectedError string - }{ - { - name: `test "api list chartinstallations"`, - args: []string{plural.ApplicationName, "api", "list", "chartinstallations", "abc"}, - chartInstallations: []*api.ChartInstallation{ - { - Id: "abc", - Chart: &api.Chart{ - Id: "abc", - Name: "test-1", - }, - Version: &api.Version{ - Version: "1", - }, - }, - { - Id: "abc", - Chart: &api.Chart{ - Id: "abc", - Name: "test-2", - }, - Version: &api.Version{ - Version: "2", - }, - }, - }, - expectedResponse: `+-----+----------+------------+---------+ -| ID | CHART ID | CHART NAME | VERSION | -+-----+----------+------------+---------+ -| abc | abc | test-1 | 1 | -| abc | abc | test-2 | 2 | -+-----+----------+------------+---------+ -`, - }, - { - name: `test "api list chartinstallations" without {repository-id} parameter`, - args: []string{plural.ApplicationName, "api", "list", "chartinstallations"}, - chartInstallations: []*api.ChartInstallation{}, - expectedError: "Not enough arguments provided: needs {repository-id}. Try running --help to see usage.", - }, - } - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - client := mocks.NewClient(t) - if test.expectedError == "" { - client.On("GetChartInstallations", mock.AnythingOfType("string")).Return(test.chartInstallations, nil) - } - app := plural.CreateNewApp(&plural.Plural{Plural: pluralclient.Plural{ - Client: client, - }}) - app.HelpName = plural.ApplicationName - os.Args = test.args - res, err := common.CaptureStdout(app, os.Args) - if test.expectedError != "" { - assert.Equal(t, err.Error(), test.expectedError) - } else { - assert.NoError(t, err) - assert.Equal(t, test.expectedResponse, res) - } - }) - } -} - -func TestGetTerraformInstallations(t *testing.T) { - tests := []struct { - name string - args []string - terraformInstallations []*api.TerraformInstallation - expectedResponse string - expectedError string - }{ - { - name: `test "api list terraforminstallations"`, - args: []string{plural.ApplicationName, "api", "list", "terraforminstallations", "abc"}, - terraformInstallations: []*api.TerraformInstallation{ - { - Id: "abc", - Terraform: &api.Terraform{ - Id: "cde", - Name: "tf-1", - }, - }, - { - Id: "abc", - Terraform: &api.Terraform{ - Id: "fgh", - Name: "tf-2", - }, - }, - }, - expectedResponse: `+-----+--------------+------+ -| ID | TERRAFORM ID | NAME | -+-----+--------------+------+ -| abc | cde | tf-1 | -| abc | fgh | tf-2 | -+-----+--------------+------+ -`, - }, - { - name: `test "api list terraforminstallations" without {repository-id} parameter`, - args: []string{plural.ApplicationName, "api", "list", "terraforminstallations"}, - terraformInstallations: []*api.TerraformInstallation{}, - expectedError: "Not enough arguments provided: needs {repository-id}. Try running --help to see usage.", - }, - } - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - client := mocks.NewClient(t) - if test.expectedError == "" { - client.On("GetTerraformInstallations", mock.AnythingOfType("string")).Return(test.terraformInstallations, nil) - } - app := plural.CreateNewApp(&plural.Plural{Plural: pluralclient.Plural{ - Client: client, - }}) - app.HelpName = plural.ApplicationName - os.Args = test.args - res, err := common.CaptureStdout(app, os.Args) - if test.expectedError != "" { - assert.Equal(t, err.Error(), test.expectedError) - } else { - assert.NoError(t, err) - assert.Equal(t, test.expectedResponse, res) - } - }) - } -} diff --git a/cmd/command/plural/plural.go b/cmd/command/plural/plural.go index ca6f63a0e..434fc7cc8 100644 --- a/cmd/command/plural/plural.go +++ b/cmd/command/plural/plural.go @@ -11,11 +11,9 @@ import ( cryptocmd "github.com/pluralsh/plural-cli/cmd/command/crypto" "github.com/pluralsh/plural-cli/cmd/command/down" cmdinit "github.com/pluralsh/plural-cli/cmd/command/init" - "github.com/pluralsh/plural-cli/cmd/command/log" "github.com/pluralsh/plural-cli/cmd/command/ops" "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/up" "github.com/pluralsh/plural-cli/cmd/command/version" "github.com/pluralsh/plural-cli/cmd/command/vpn"