Skip to content

Commit eecb76d

Browse files
Refactor cli to add tests (#26)
1 parent c7b9a0a commit eecb76d

26 files changed

+1720
-523
lines changed

.golangci.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ linters:
1515
- unparam
1616
- wastedassign
1717
linters-settings:
18+
goimports:
19+
local-prefixes: github.com/argoproj-labs/argo-cloudops
20+
1821
errcheck:
1922
exclude: .errcheck_excludes.txt
2023
revive:

CHANGELOG.md

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
# Changelog
22

33
## Unreleased
4-
* Add more linting.
5-
* Fix issues reported by linter.
4+
5+
## v0.2.1 (2021-06-16)
6+
### Add
7+
* CLI tests.
8+
* More linting.
9+
10+
### Changed
11+
* Break up CLI commands into separate files.
12+
13+
### Fixed
14+
* Issues reported by linter.
615

716
## v0.2.0 (2021-06-15)
817
### Changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ build_service: clean_service
66
CGO_ENABLED=0 GOARCH=amd64 go build -trimpath $(GO_LDFLAGS) $(BUILDARGS) -o build/service ./service/
77

88
build_cli: clean_cli
9-
CGO_ENABLED=0 GOARCH=amd64 go build -trimpath $(GO_LDFLAGS) $(BUILDARGS) -o build/argo-cloudops ./cli/...
9+
CGO_ENABLED=0 GOARCH=amd64 go build -trimpath $(GO_LDFLAGS) $(BUILDARGS) -o build/argo-cloudops ./cli/
1010

1111
lint:
1212
@#Install the linter from here:

cli/cmd/diff.go

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// +build !test
2+
3+
package cmd
4+
5+
import (
6+
"context"
7+
"fmt"
8+
9+
"github.com/argoproj-labs/argo-cloudops/cli/internal/api"
10+
11+
"github.com/spf13/cobra"
12+
)
13+
14+
// diffCmd represents the diff command
15+
var diffCmd = &cobra.Command{
16+
Use: "diff",
17+
Short: "Diff a project target using a manifest in git",
18+
Long: "Diff a project target using a manifest in git",
19+
Run: func(cmd *cobra.Command, args []string) {
20+
token, err := argoCloudOpsUserToken()
21+
if err != nil {
22+
cobra.CheckErr(err)
23+
}
24+
25+
apiCl := api.NewClient(argoCloudOpsServiceAddr(), token)
26+
27+
resp, err := apiCl.Diff(context.Background(), projectName, targetName, gitSHA, gitPath)
28+
if err != nil {
29+
cobra.CheckErr(err)
30+
}
31+
32+
// Our current contract is to output only the name.
33+
fmt.Print(resp.WorkflowName)
34+
},
35+
}
36+
37+
func init() {
38+
rootCmd.AddCommand(diffCmd)
39+
40+
// TODO these should be '-' separated.
41+
diffCmd.Flags().StringVarP(&gitPath, "path", "p", "", "Path to manifest within git repository")
42+
diffCmd.Flags().StringVarP(&gitSHA, "sha", "s", "", "Commit sha to use when creating workflow through git")
43+
diffCmd.Flags().StringVarP(&projectName, "project_name", "n", "", "Name of project")
44+
// TODO inconsistent
45+
diffCmd.Flags().StringVarP(&targetName, "target", "t", "", "Name of target")
46+
47+
diffCmd.MarkFlagRequired("path")
48+
diffCmd.MarkFlagRequired("sha")
49+
diffCmd.MarkFlagRequired("project_name")
50+
diffCmd.MarkFlagRequired("target_name")
51+
}

cli/cmd/get.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
8+
"github.com/argoproj-labs/argo-cloudops/cli/internal/api"
9+
10+
"github.com/spf13/cobra"
11+
)
12+
13+
// getCmd represents the get command.
14+
var getCmd = &cobra.Command{
15+
Use: "get [workflow name]",
16+
Short: "Gets status of workflow",
17+
Long: "Gets status of workflow",
18+
Args: cobra.ExactArgs(1),
19+
Run: func(cmd *cobra.Command, args []string) {
20+
name := args[0]
21+
apiCl := api.NewClient(argoCloudOpsServiceAddr(), "")
22+
23+
status, err := apiCl.GetWorkflowStatus(context.Background(), name)
24+
if err != nil {
25+
cobra.CheckErr(err)
26+
}
27+
28+
// Our current "contract" is to output json.
29+
output, err := json.Marshal(status)
30+
if err != nil {
31+
cobra.CheckErr(fmt.Errorf("unable to generate output, error: %w", err))
32+
}
33+
34+
fmt.Println(string(output))
35+
},
36+
}
37+
38+
func init() {
39+
rootCmd.AddCommand(getCmd)
40+
}

cli/cmd/list.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// +build !test
2+
3+
package cmd
4+
5+
import (
6+
"context"
7+
"fmt"
8+
9+
"github.com/argoproj-labs/argo-cloudops/cli/internal/api"
10+
11+
"github.com/spf13/cobra"
12+
)
13+
14+
// listCmd represents the list command
15+
var listCmd = &cobra.Command{
16+
Use: "list",
17+
Short: "List workflow executions for a given project and target",
18+
Long: "List workflow executions for a given project and target",
19+
Run: func(cmd *cobra.Command, args []string) {
20+
apiCl := api.NewClient(argoCloudOpsServiceAddr(), "")
21+
22+
resp, err := apiCl.GetWorkflows(context.Background(), projectName, targetName)
23+
if err != nil {
24+
cobra.CheckErr(err)
25+
}
26+
27+
for _, w := range resp {
28+
fmt.Printf("%s\n", w)
29+
}
30+
},
31+
}
32+
33+
func init() {
34+
rootCmd.AddCommand(listCmd)
35+
36+
// TODO this is our current contract. These should really be `-` separated.
37+
listCmd.Flags().StringVarP(&projectName, "project_name", "n", "", "Name of project")
38+
listCmd.Flags().StringVarP(&targetName, "target_name", "t", "", "Name of target")
39+
40+
listCmd.MarkFlagRequired("project_name")
41+
listCmd.MarkFlagRequired("target_name")
42+
}

cli/cmd/logs.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"strings"
8+
9+
"github.com/argoproj-labs/argo-cloudops/cli/internal/api"
10+
11+
"github.com/spf13/cobra"
12+
)
13+
14+
// logsCmd represents the logs command
15+
var logsCmd = &cobra.Command{
16+
Use: "logs [workflow name]",
17+
Short: "Gets logs from a workflow",
18+
Long: "Gets logs from a workflow",
19+
Args: cobra.ExactArgs(1),
20+
Run: func(cmd *cobra.Command, args []string) {
21+
workflowName := args[0]
22+
23+
apiCl := api.NewClient(argoCloudOpsServiceAddr(), "")
24+
25+
if streamLogs {
26+
// This is a _very_ simple approach to streaming.
27+
cobra.CheckErr(apiCl.StreamLogs(context.Background(), os.Stdout, workflowName))
28+
}
29+
30+
resp, err := apiCl.GetLogs(context.Background(), workflowName)
31+
if err != nil {
32+
cobra.CheckErr(err)
33+
}
34+
35+
fmt.Println(strings.Join(resp.Logs, "\n"))
36+
},
37+
}
38+
39+
func init() {
40+
rootCmd.AddCommand(logsCmd)
41+
42+
logsCmd.Flags().BoolVarP(&streamLogs, "follow", "f", false, "Follow workflow logs and stream to standard out until workflow is complete")
43+
}

cli/cmd/root.go

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/spf13/cobra"
8+
)
9+
10+
// rootCmd represents the base command when called without any subcommands
11+
var rootCmd = &cobra.Command{
12+
Use: "argo-cloudops",
13+
Short: "Argo CloudOps Command Line Interface",
14+
Long: "Argo CloudOps Command Line Interface",
15+
}
16+
17+
var (
18+
// Flags
19+
argumentsCSV string
20+
environmentVariablesCSV string
21+
framework string
22+
gitPath string
23+
gitSHA string
24+
parametersCSV string
25+
projectName string
26+
streamLogs bool
27+
targetName string
28+
workflowTemplateName string
29+
workflowType string
30+
31+
// This is set here so we can access it in this package.
32+
version string
33+
)
34+
35+
// Execute adds all child commands to the root command and sets flags
36+
// appropriately. This is called by main.main(). It only needs to happen once
37+
// to the rootCmd.
38+
func Execute(versionInfo string) {
39+
version = versionInfo
40+
cobra.CheckErr(rootCmd.Execute())
41+
}
42+
43+
// For root level flags
44+
func init() {
45+
}
46+
47+
// TODO refactor
48+
func argoCloudOpsServiceAddr() string {
49+
addr := os.Getenv("ARGO_CLOUDOPS_SERVICE_ADDR")
50+
if addr == "" {
51+
addr = "https://localhost:8443"
52+
}
53+
return addr
54+
}
55+
56+
// TODO refactor
57+
func argoCloudOpsUserToken() (string, error) {
58+
key := "ARGO_CLOUDOPS_USER_TOKEN"
59+
result := os.Getenv(key)
60+
if len(result) == 0 {
61+
return "", fmt.Errorf("%s not found", key)
62+
}
63+
return result, nil
64+
}

cli/cmd/sync.go

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// +build !test
2+
3+
package cmd
4+
5+
import (
6+
"context"
7+
"fmt"
8+
9+
"github.com/argoproj-labs/argo-cloudops/cli/internal/api"
10+
11+
"github.com/spf13/cobra"
12+
)
13+
14+
// syncCmd represents the sync command
15+
var syncCmd = &cobra.Command{
16+
Use: "sync",
17+
Short: "Syncs a project target using a manifest in git",
18+
Long: "Syncs a project target using a manifest in git",
19+
Run: func(cmd *cobra.Command, args []string) {
20+
token, err := argoCloudOpsUserToken()
21+
if err != nil {
22+
cobra.CheckErr(err)
23+
}
24+
25+
apiCl := api.NewClient(argoCloudOpsServiceAddr(), token)
26+
27+
resp, err := apiCl.Sync(context.Background(), projectName, targetName, gitSHA, gitPath)
28+
if err != nil {
29+
cobra.CheckErr(err)
30+
}
31+
32+
// Our current contract is to output only the name.
33+
fmt.Print(resp.WorkflowName)
34+
},
35+
}
36+
37+
func init() {
38+
rootCmd.AddCommand(syncCmd)
39+
40+
// TODO these should be '-' separated.
41+
syncCmd.Flags().StringVarP(&gitPath, "path", "p", "", "Path to manifest within git repository")
42+
syncCmd.Flags().StringVarP(&gitSHA, "sha", "s", "", "Commit sha to use when creating workflow through git")
43+
syncCmd.Flags().StringVarP(&projectName, "project_name", "n", "", "Name of project")
44+
// TODO inconsistent
45+
syncCmd.Flags().StringVarP(&targetName, "target", "t", "", "Name of target")
46+
47+
syncCmd.MarkFlagRequired("path")
48+
syncCmd.MarkFlagRequired("sha")
49+
syncCmd.MarkFlagRequired("project_name")
50+
syncCmd.MarkFlagRequired("target_name")
51+
}

cli/cmd/version.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
// versionCmd represents the version command.
10+
var versionCmd = &cobra.Command{
11+
Use: "version",
12+
Short: "Reports the version",
13+
Long: "Reports the version",
14+
Run: func(cmd *cobra.Command, args []string) {
15+
fmt.Println(version)
16+
},
17+
}
18+
19+
func init() {
20+
rootCmd.AddCommand(versionCmd)
21+
}

0 commit comments

Comments
 (0)