Skip to content

Commit

Permalink
Added support for archive flow command kosli-dev/server#1200
Browse files Browse the repository at this point in the history
  • Loading branch information
ToreMerkely committed Jan 16, 2024
1 parent 49dd55c commit c9b6c66
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 0 deletions.
23 changes: 23 additions & 0 deletions cmd/kosli/archive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"io"

"github.com/spf13/cobra"
)

const archiveDesc = `All Kosli archive commands.`

func newArchiveCmd(out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "archive",
Short: archiveDesc,
Long: archiveDesc,
}

// Add subcommands
cmd.AddCommand(
newArchiveFlowCmd(out),
)
return cmd
}
63 changes: 63 additions & 0 deletions cmd/kosli/archiveFlow.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 archiveFlowShortDesc = `Archive a Kosli flow.`

const archiveFlowLongDesc = archiveFlowShortDesc + `
The flow will no longer be visible in list of flows, data is still stored in database.
`

const archiveFlowExample = `
# archive a Kosli flow:
kosli archive flow yourFlowName \
--api-token yourAPIToken \
--org yourOrgName
`

type ArchiveFlowPayload struct {
}

func newArchiveFlowCmd(out io.Writer) *cobra.Command {
payload := new(ArchiveFlowPayload)
cmd := &cobra.Command{
Use: "flow FLOW-NAME",
Short: archiveFlowShortDesc,
Long: archiveFlowLongDesc,
Example: archiveFlowExample,
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())
}

Check warning on line 40 in cmd/kosli/archiveFlow.go

View check run for this annotation

Codecov / codecov/patch

cmd/kosli/archiveFlow.go#L39-L40

Added lines #L39 - L40 were not covered by tests

return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
url := fmt.Sprintf("%s/api/v2/flows/%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("flow %s was archived", args[0])
}
return err
},
}
addDryRunFlag(cmd)
return cmd
}
65 changes: 65 additions & 0 deletions cmd/kosli/archiveFlow_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 ArchiveFlowCommandTestSuite struct {
suite.Suite
defaultKosliArguments string
flowName string
}

func (suite *ArchiveFlowCommandTestSuite) SetupTest() {
suite.flowName = "archive-flow"

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)
CreateFlow(suite.flowName, suite.T())
}

func (suite *ArchiveFlowCommandTestSuite) TestArchiveFlowCmd() {
tests := []cmdTestCase{
{
name: "can archive flow",
cmd: fmt.Sprintf(`archive flow %s %s`, suite.flowName, suite.defaultKosliArguments),
golden: "flow archive-flow was archived\n",
},
{
wantError: true,
name: "archiving non-existing flow fails",
cmd: fmt.Sprintf(`archive flow non-existing %s`, suite.defaultKosliArguments),
golden: "Error: Flow named 'non-existing' does not exist for organization 'docs-cmd-test-user'\n",
},
{
wantError: true,
name: "archive flow fails when 2 args are provided",
cmd: fmt.Sprintf(`archive flow %s arg2 %s`, suite.flowName, suite.defaultKosliArguments),
golden: "Error: accepts 1 arg(s), received 2\n",
},
{
wantError: true,
name: "archive flow fails when no args are provided",
cmd: fmt.Sprintf(`archive flow %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 TestArchiveFlowCommandTestSuite(t *testing.T) {
suite.Run(t, new(ArchiveFlowCommandTestSuite))
}
1 change: 1 addition & 0 deletions cmd/kosli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ func newRootCmd(out io.Writer, args []string) (*cobra.Command, error) {
newAllowCmd(out),
newListCmd(out),
newRenameCmd(out),
newArchiveCmd(out),
newSnapshotCmd(out),
newRequestCmd(out),
newLogCmd(out),
Expand Down

0 comments on commit c9b6c66

Please sign in to comment.