-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for archive flow command kosli-dev/server#1200
- Loading branch information
1 parent
49dd55c
commit c9b6c66
Showing
4 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters