Skip to content

Commit d7c0ad7

Browse files
Cr 5420 (#81)
* added workflow get,list * added pipeline get,list * upgraded go-sdk
1 parent 627cf08 commit d7c0ad7

15 files changed

+589
-7
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION=v0.0.82
1+
VERSION=v0.0.83
22
OUT_DIR=dist
33
YEAR?=$(shell date +"%Y")
44

cmd/commands/pipeline.go

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
// Copyright 2021 The Codefresh Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package commands
16+
17+
import (
18+
"context"
19+
"fmt"
20+
"os"
21+
22+
"github.com/codefresh-io/cli-v2/pkg/log"
23+
"github.com/codefresh-io/cli-v2/pkg/util"
24+
"github.com/codefresh-io/go-sdk/pkg/codefresh/model"
25+
"github.com/juju/ansiterm"
26+
27+
"github.com/spf13/cobra"
28+
)
29+
30+
func NewPipelineCommand() *cobra.Command {
31+
cmd := &cobra.Command{
32+
Use: "pipeline",
33+
Short: "Manage pipelines of Codefresh runtimes",
34+
PersistentPreRunE: cfConfig.RequireAuthentication,
35+
Run: func(cmd *cobra.Command, args []string) {
36+
cmd.HelpFunc()(cmd, args)
37+
exit(1)
38+
},
39+
}
40+
41+
cmd.AddCommand(NewPipelineGetCommand())
42+
cmd.AddCommand(NewPipelineListCommand())
43+
44+
return cmd
45+
}
46+
47+
func NewPipelineGetCommand() *cobra.Command {
48+
var (
49+
name string
50+
namespace string
51+
runtime string
52+
)
53+
54+
cmd := &cobra.Command{
55+
Use: "get --runtime <runtime> --namespace <namespace> --name <name>",
56+
Short: "Get a pipeline under a specific runtime and namespace",
57+
Example: util.Doc(`
58+
<BIN> pipeline --runtime runtime_name --namespace namespace --name pipeline_name
59+
60+
<BIN> pipeline -r runtime_name -N namespace -n pipeline_name
61+
`),
62+
RunE: func(cmd *cobra.Command, args []string) error {
63+
ctx := cmd.Context()
64+
65+
return RunPipelineGet(ctx, name, namespace, runtime)
66+
},
67+
}
68+
69+
cmd.Flags().StringVarP(&name, "name", "n", "", "Name of target pipeline")
70+
util.Die(cmd.MarkFlagRequired("name"))
71+
cmd.Flags().StringVarP(&namespace, "namespace", "N", "", "Namespace of target pipeline")
72+
util.Die(cmd.MarkFlagRequired("namespace"))
73+
cmd.Flags().StringVarP(&runtime, "runtime", "r", "", "Runtime name of target pipeline")
74+
util.Die(cmd.MarkFlagRequired("runtime"))
75+
76+
return cmd
77+
}
78+
79+
func NewPipelineListCommand() *cobra.Command {
80+
var (
81+
name string
82+
namespace string
83+
runtime string
84+
project string
85+
)
86+
87+
cmd := &cobra.Command{
88+
Use: "list",
89+
Short: "List all the pipelines",
90+
Example: util.Doc(`
91+
<BIN> pipelines list
92+
93+
<BIN> pipelines list --runtime <runtime>
94+
95+
<BIN> pipelines list -r <runtime>
96+
`),
97+
RunE: func(cmd *cobra.Command, args []string) error {
98+
ctx := cmd.Context()
99+
100+
filterArgs := model.PipelinesFilterArgs{
101+
Name: &name,
102+
Namespace: &namespace,
103+
Runtime: &runtime,
104+
Project: &project,
105+
}
106+
return RunPipelineList(ctx, filterArgs)
107+
},
108+
}
109+
110+
cmd.Flags().StringVarP(&name, "name", "n", "", "Filter by pipeline name")
111+
cmd.Flags().StringVarP(&namespace, "namespace", "N", "", "Filter by pipeline namespace")
112+
cmd.Flags().StringVarP(&runtime, "runtime", "r", "", "Filter by pipeline runtime")
113+
cmd.Flags().StringVarP(&project, "project", "p", "", "Filter by pipeline project")
114+
115+
return cmd
116+
}
117+
118+
func RunPipelineGet(ctx context.Context, name, namespace, runtime string) error {
119+
pipeline, err := cfConfig.NewClient().V2().Pipeline().Get(ctx, name, namespace, runtime)
120+
if err != nil {
121+
return err
122+
}
123+
124+
if pipeline == nil {
125+
fields := log.Fields{
126+
"name": name,
127+
"namespace": namespace,
128+
"runtime": runtime,
129+
}
130+
log.G(ctx).WithFields(fields).Warn("pipeline was not found")
131+
return nil
132+
}
133+
134+
tb := ansiterm.NewTabWriter(os.Stdout, 0, 0, 4, ' ', 0)
135+
_, err = fmt.Fprintln(tb, "NAME\tNAMESPACE\tRUNTIME\tHEALTH STATUS\tSYNC STATUS")
136+
if err != nil {
137+
return err
138+
}
139+
140+
healthStatus := "N/A"
141+
if pipeline.Self.HealthStatus != nil {
142+
healthStatus = pipeline.Self.HealthStatus.String()
143+
}
144+
_, err = fmt.Fprintf(tb, "%s\t%s\t%s\t%s\t%s\n",
145+
pipeline.Metadata.Name,
146+
*pipeline.Metadata.Namespace,
147+
pipeline.Metadata.Runtime,
148+
healthStatus,
149+
pipeline.Self.SyncStatus,
150+
)
151+
if err != nil {
152+
return err
153+
}
154+
155+
return tb.Flush()
156+
}
157+
158+
func RunPipelineList(ctx context.Context, filterArgs model.PipelinesFilterArgs) error {
159+
pipelines, err := cfConfig.NewClient().V2().Pipeline().List(ctx, filterArgs)
160+
if err != nil {
161+
return err
162+
}
163+
164+
if len(pipelines) == 0 {
165+
log.G(ctx).Warn("no pipelines were found")
166+
return nil
167+
}
168+
169+
tb := ansiterm.NewTabWriter(os.Stdout, 0, 0, 4, ' ', 0)
170+
_, err = fmt.Fprintln(tb, "NAME\tNAMESPACE\tRUNTIME\tHEALTH STATUS\tSYNC STATUS")
171+
if err != nil {
172+
return err
173+
}
174+
175+
for _, p := range pipelines {
176+
healthStatus := "N/A"
177+
if p.Self.HealthStatus != nil {
178+
healthStatus = p.Self.HealthStatus.String()
179+
}
180+
_, err = fmt.Fprintf(tb, "%s\t%s\t%s\t%s\t%s\n",
181+
p.Metadata.Name,
182+
*p.Metadata.Namespace,
183+
p.Metadata.Runtime,
184+
healthStatus,
185+
p.Self.SyncStatus,
186+
)
187+
if err != nil {
188+
return err
189+
}
190+
}
191+
192+
return tb.Flush()
193+
}

cmd/commands/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ variables in advanced to simplify the use of those commands.
5353
cmd.AddCommand(NewRuntimeCommand())
5454
cmd.AddCommand(NewGitSourceCommand())
5555
cmd.AddCommand(NewComponentCommand())
56+
cmd.AddCommand(NewWorkflowCommand())
57+
cmd.AddCommand(NewPipelineCommand())
5658

5759
cobra.OnInitialize(func() { postInitCommands(cmd.Commands()) })
5860

cmd/commands/workflow.go

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
// Copyright 2021 The Codefresh Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package commands
16+
17+
import (
18+
"context"
19+
"fmt"
20+
"os"
21+
22+
"github.com/codefresh-io/cli-v2/pkg/log"
23+
"github.com/codefresh-io/cli-v2/pkg/util"
24+
"github.com/codefresh-io/go-sdk/pkg/codefresh/model"
25+
"github.com/juju/ansiterm"
26+
27+
"github.com/spf13/cobra"
28+
)
29+
30+
func NewWorkflowCommand() *cobra.Command {
31+
cmd := &cobra.Command{
32+
Use: "workflow",
33+
Short: "Manage workflows of Codefresh runtimes",
34+
PersistentPreRunE: cfConfig.RequireAuthentication,
35+
Run: func(cmd *cobra.Command, args []string) {
36+
cmd.HelpFunc()(cmd, args)
37+
exit(1)
38+
},
39+
}
40+
41+
cmd.AddCommand(NewWorkflowGetCommand())
42+
cmd.AddCommand(NewWorkflowListCommand())
43+
44+
return cmd
45+
}
46+
47+
func NewWorkflowGetCommand() *cobra.Command {
48+
cmd := &cobra.Command{
49+
Use: "get [uid]",
50+
Short: "Get a workflow under a specific uid",
51+
Example: util.Doc(`
52+
<BIN> workflow get 0732b138-b74c-4a5e-b065-e23e6da0803d
53+
`),
54+
PreRun: func(cmd *cobra.Command, args []string) {
55+
if len(args) < 1 {
56+
log.G(cmd.Context()).Fatal("must enter uid")
57+
}
58+
},
59+
RunE: func(cmd *cobra.Command, args []string) error {
60+
ctx := cmd.Context()
61+
62+
return RunWorkflowGet(ctx, args[0])
63+
},
64+
}
65+
66+
return cmd
67+
}
68+
69+
func NewWorkflowListCommand() *cobra.Command {
70+
var (
71+
namespace string
72+
runtime string
73+
project string
74+
)
75+
76+
cmd := &cobra.Command{
77+
Use: "list",
78+
Short: "List all the workflows",
79+
Example: util.Doc(`
80+
<BIN> workflows list
81+
82+
<BIN> workflows list --runtime <runtime>
83+
84+
<BIN> workflows list -r <runtime>
85+
`),
86+
RunE: func(cmd *cobra.Command, args []string) error {
87+
ctx := cmd.Context()
88+
89+
filterArgs := model.WorkflowsFilterArgs{
90+
Namespace: &namespace,
91+
Runtime: &runtime,
92+
Project: &project,
93+
}
94+
return RunWorkflowList(ctx, filterArgs)
95+
},
96+
}
97+
98+
cmd.Flags().StringVarP(&namespace, "namespace", "N", "", "Filter by workflow namespace")
99+
cmd.Flags().StringVarP(&runtime, "runtime", "r", "", "Filter by workflow runtime")
100+
cmd.Flags().StringVarP(&project, "project", "p", "", "Filter by workflow project")
101+
102+
return cmd
103+
}
104+
105+
func RunWorkflowGet(ctx context.Context, uid string) error {
106+
workflow, err := cfConfig.NewClient().V2().Workflow().Get(ctx, uid)
107+
if err != nil {
108+
return err
109+
}
110+
111+
if workflow == nil {
112+
log.G(ctx).WithField("uid", uid).Warn("workflow was not found")
113+
return nil
114+
}
115+
116+
tb := ansiterm.NewTabWriter(os.Stdout, 0, 0, 4, ' ', 0)
117+
_, err = fmt.Fprintln(tb, "NAME\tNAMESPACE\tRUNTIME\tPHASE\tUID")
118+
if err != nil {
119+
return err
120+
}
121+
122+
_, err = fmt.Fprintf(tb, "%s\t%s\t%s\t%s\t%s\n",
123+
workflow.Metadata.Name,
124+
*workflow.Metadata.Namespace,
125+
workflow.Metadata.Runtime,
126+
workflow.Status.Phase,
127+
*workflow.Metadata.UID,
128+
)
129+
if err != nil {
130+
return err
131+
}
132+
133+
return tb.Flush()
134+
}
135+
136+
func RunWorkflowList(ctx context.Context, filterArgs model.WorkflowsFilterArgs) error {
137+
workflows, err := cfConfig.NewClient().V2().Workflow().List(ctx, filterArgs)
138+
if err != nil {
139+
return err
140+
}
141+
142+
if len(workflows) == 0 {
143+
log.G(ctx).Warn("no workflows were found")
144+
return nil
145+
}
146+
147+
tb := ansiterm.NewTabWriter(os.Stdout, 0, 0, 4, ' ', 0)
148+
_, err = fmt.Fprintln(tb, "NAME\tNAMESPACE\tRUNTIME\tPHASE\tUID")
149+
if err != nil {
150+
return err
151+
}
152+
153+
for _, w := range workflows {
154+
_, err = fmt.Fprintf(tb, "%s\t%s\t%s\t%s\t%s\n",
155+
w.Metadata.Name,
156+
*w.Metadata.Namespace,
157+
w.Metadata.Runtime,
158+
w.Status.Phase,
159+
*w.Metadata.UID,
160+
)
161+
if err != nil {
162+
return err
163+
}
164+
}
165+
166+
return tb.Flush()
167+
}

docs/commands/cli-v2.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ cli-v2 [flags]
3434
* [cli-v2 component](cli-v2_component.md) - Manage components of Codefresh runtimes
3535
* [cli-v2 config](cli-v2_config.md) - Manage Codefresh authentication contexts
3636
* [cli-v2 git-source](cli-v2_git-source.md) - Manage git-sources of Codefresh runtimes
37+
* [cli-v2 pipeline](cli-v2_pipeline.md) - Manage pipelines of Codefresh runtimes
3738
* [cli-v2 runtime](cli-v2_runtime.md) - Manage Codefresh runtimes
3839
* [cli-v2 version](cli-v2_version.md) - Show cli version
40+
* [cli-v2 workflow](cli-v2_workflow.md) - Manage workflows of Codefresh runtimes
3941

0 commit comments

Comments
 (0)