Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add archive environment command kosli-dev/server#324 #92

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/kosli/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func newArchiveCmd(out io.Writer) *cobra.Command {
// Add subcommands
cmd.AddCommand(
newArchiveFlowCmd(out),
newArchiveEnvironmentCmd(out),
)
return cmd
}
63 changes: 63 additions & 0 deletions cmd/kosli/archiveEnvironment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package main

import (
"fmt"
"io"
"net/http"

"github.com/kosli-dev/cli/internal/requests"
"github.com/spf13/cobra"
)

const archiveEnvironmentShortDesc = `Archive a Kosli environment.`

const archiveEnvironmentLongDesc = archiveEnvironmentShortDesc + `
The environment will no longer be visible in list of environments, data is still stored in database.
`

const archiveEnvironmentExample = `
# archive a Kosli environment:
kosli archive environment yourEnvironmentName \
--api-token yourAPIToken \
--org yourOrgName
`

type ArchiveEnvironmentPayload struct {
}

func newArchiveEnvironmentCmd(out io.Writer) *cobra.Command {
payload := new(ArchiveEnvironmentPayload)
cmd := &cobra.Command{
Use: "environment ENVIRONMENT-NAME",
Short: archiveEnvironmentShortDesc,
Long: archiveEnvironmentLongDesc,
Example: archiveEnvironmentExample,
Args: cobra.ExactArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
err := RequireGlobalFlags(global, []string{"Org", "ApiToken"})
if err != nil {
return ErrorBeforePrintingUsage(cmd, err.Error())
}

return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
url := fmt.Sprintf("%s/api/v2/environments/%s/%s/archive", global.Host, global.Org, args[0])

reqParams := &requests.RequestParams{
Method: http.MethodPut,
URL: url,
Payload: payload,
DryRun: global.DryRun,
Password: global.ApiToken,
}
_, err := kosliClient.Do(reqParams)
if err == nil && !global.DryRun {
logger.Info("environment %s was archived", args[0])
}
return err
},
}
addDryRunFlag(cmd)
return cmd
}
65 changes: 65 additions & 0 deletions cmd/kosli/archiveEnvironment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package main

import (
"fmt"
"testing"

"github.com/stretchr/testify/suite"
)

// Define the suite, and absorb the built-in basic suite
// functionality from testify - including a T() method which
// returns the current testing context
type ArchiveEnvironmentCommandTestSuite struct {
suite.Suite
defaultKosliArguments string
environmentName string
}

func (suite *ArchiveEnvironmentCommandTestSuite) SetupTest() {
suite.environmentName = "archive-environment"

global = &GlobalOpts{
ApiToken: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6ImNkNzg4OTg5In0.e8i_lA_QrEhFncb05Xw6E_tkCHU9QfcY4OLTVUCHffY",
Org: "docs-cmd-test-user",
Host: "http://localhost:8001",
}
suite.defaultKosliArguments = fmt.Sprintf(" --host %s --org %s --api-token %s", global.Host, global.Org, global.ApiToken)
CreateEnv(global.Org, suite.environmentName, "server", suite.T())
}

func (suite *ArchiveEnvironmentCommandTestSuite) TestArchiveEnvironmentCmd() {
tests := []cmdTestCase{
{
name: "can archive environment",
cmd: fmt.Sprintf(`archive environment %s %s`, suite.environmentName, suite.defaultKosliArguments),
golden: "environment archive-environment was archived\n",
},
{
wantError: true,
name: "archiving non-existing environment fails",
cmd: fmt.Sprintf(`archive environment non-existing %s`, suite.defaultKosliArguments),
golden: "Error: Environment named 'non-existing' does not exist for organization 'docs-cmd-test-user'. \n",
},
{
wantError: true,
name: "archive environment fails when 2 args are provided",
cmd: fmt.Sprintf(`archive environment %s arg2 %s`, suite.environmentName, suite.defaultKosliArguments),
golden: "Error: accepts 1 arg(s), received 2\n",
},
{
wantError: true,
name: "archive environment fails when no args are provided",
cmd: fmt.Sprintf(`archive environment %s`, suite.defaultKosliArguments),
golden: "Error: accepts 1 arg(s), received 0\n",
},
}

runTestCmd(suite.T(), tests)
}

// In order for 'go test' to run this suite, we need to create
// a normal test function and pass our suite to suite.Run
func TestArchiveEnvironmentCommandTestSuite(t *testing.T) {
suite.Run(t, new(ArchiveEnvironmentCommandTestSuite))
}
Loading