Skip to content

Commit e541286

Browse files
Print components (#276)
* wip * wip * wip * wip * CR-9588 * wip * wip * bump * codegen * codegen
1 parent fe35268 commit e541286

38 files changed

+588
-287
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.248
1+
VERSION=v0.0.249
22

33
OUT_DIR=dist
44
YEAR?=$(shell date +"%Y")

cmd/commands/component.go

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ package commands
1717
import (
1818
"context"
1919
"fmt"
20+
"io"
2021
"os"
2122

2223
"github.com/codefresh-io/cli-v2/pkg/log"
2324
"github.com/codefresh-io/cli-v2/pkg/util"
25+
"github.com/codefresh-io/go-sdk/pkg/codefresh/model"
2426
"github.com/juju/ansiterm"
2527

2628
"github.com/spf13/cobra"
@@ -47,8 +49,9 @@ func NewComponentCommand() *cobra.Command {
4749

4850
func NewComponentListCommand() *cobra.Command {
4951
cmd := &cobra.Command{
50-
Use: "list [runtime_name]",
52+
Use: "list RUNTIME_NAME",
5153
Short: "List all the components under a specific runtime",
54+
Args: cobra.MaximumNArgs(1),
5255
Example: util.Doc(`
5356
<BIN> component list runtime_name
5457
`),
@@ -79,30 +82,51 @@ func RunComponentList(ctx context.Context, runtimeName string) error {
7982
}
8083

8184
tb := ansiterm.NewTabWriter(os.Stdout, 0, 0, 4, ' ', 0)
82-
_, err = fmt.Fprintln(tb, "NAME\tHEALTH STATUS\tSYNC STATUS\tVERSION")
85+
86+
if err := printComponents(tb, components); err != nil {
87+
return err
88+
}
89+
90+
return tb.Flush()
91+
}
92+
93+
func printComponents(w io.Writer, components []model.Component) error {
94+
_, err := fmt.Fprintln(w, "NAME\tHEALTH STATUS\tSYNC STATUS\tVERSION")
8395
if err != nil {
8496
return err
8597
}
8698

8799
for _, c := range components {
88-
name := c.Metadata.Name
89-
healthStatus := "N/A"
90-
syncStatus := c.Self.Status.SyncStatus.String()
91-
version := c.Version
100+
if err := printComponent(w, c); err != nil {
101+
return err
102+
}
103+
}
104+
105+
return nil
106+
}
107+
108+
func printComponent(w io.Writer, c model.Component) error {
109+
name := c.Metadata.Name
110+
healthStatus := "N/A"
111+
syncStatus := "N/A"
112+
version := c.Version
113+
114+
if c.Self != nil {
115+
if c.Self.Status != nil {
116+
syncStatus = c.Self.Status.SyncStatus.String()
117+
}
92118

93119
if c.Self.Status.HealthStatus != nil {
94120
healthStatus = c.Self.Status.HealthStatus.String()
95121
}
96-
_, err = fmt.Fprintf(tb, "%s\t%s\t%s\t%s\n",
97-
name,
98-
healthStatus,
99-
syncStatus,
100-
version,
101-
)
102-
if err != nil {
103-
return err
104-
}
105122
}
106123

107-
return tb.Flush()
124+
_, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\n",
125+
name,
126+
healthStatus,
127+
syncStatus,
128+
version,
129+
)
130+
131+
return err
108132
}

cmd/commands/config.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ func NewConfigCreateContextCommand() *cobra.Command {
6666
)
6767

6868
cmd := &cobra.Command{
69-
Use: "create-context",
69+
Use: "create-context NAME",
7070
Short: "Create a new Codefresh authentication context",
71+
Args: cobra.MaximumNArgs(1),
7172
Example: util.Doc(`
7273
# Create a new context named 'test':
7374
@@ -99,6 +100,7 @@ func NewConfigGetContextsCommand() *cobra.Command {
99100
cmd := &cobra.Command{
100101
Use: "get-contexts",
101102
Aliases: []string{"view"},
103+
Args: cobra.NoArgs,
102104
Short: "Lists all Codefresh authentication contexts",
103105
Example: util.Doc(`
104106
# List all authentication contexts:
@@ -120,6 +122,7 @@ func NewConfigCurrentContextCommand() *cobra.Command {
120122
cmd := &cobra.Command{
121123
Use: "current-context",
122124
Short: "Shows the currently selected Codefresh authentication context",
125+
Args: cobra.NoArgs,
123126
Example: util.Doc(`
124127
# Shows the current context:
125128
@@ -146,14 +149,14 @@ func NewConfigSetRuntimeCommand() *cobra.Command {
146149
cmd := &cobra.Command{
147150
Use: "set-runtime RUNTIME",
148151
Short: "Sets the default runtime name to use for the current authentication context",
152+
Args: cobra.MaximumNArgs(1),
149153
Example: util.Doc(`
150154
# Sets the default runtime to 'runtime-2':
151155
152156
<BIN> config set-runtime runtime-2`),
153157
RunE: func(cmd *cobra.Command, args []string) error {
154158
if len(args) < 1 {
155159
return fmt.Errorf("must provide runtime name to use")
156-
157160
}
158161

159162
return RunConfigSetRuntime(cmd.Context(), args[0])
@@ -189,6 +192,7 @@ func NewConfigGetRuntimeCommand() *cobra.Command {
189192
cmd := &cobra.Command{
190193
Use: "get-runtime",
191194
Short: "Gets the default runtime for the current authentication context",
195+
Args: cobra.NoArgs,
192196
Example: util.Doc(`
193197
# Prints the default runtime:
194198
@@ -216,6 +220,7 @@ func NewConfigUseContextCommand() *cobra.Command {
216220
cmd := &cobra.Command{
217221
Use: "use-context CONTEXT",
218222
Short: "Switch the current authentication context",
223+
Args: cobra.MaximumNArgs(1),
219224
Example: util.Doc(`
220225
# Switch to another authentication context:
221226
@@ -243,6 +248,7 @@ func NewConfigDeleteContextCommand() *cobra.Command {
243248
cmd := &cobra.Command{
244249
Use: "delete-context CONTEXT",
245250
Short: "Delete the specified authentication context",
251+
Args: cobra.MaximumNArgs(1),
246252
Example: util.Doc(`
247253
# Deleting an authentication context name 'test':
248254

cmd/commands/git-source.go

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,9 @@ func NewGitSourceCreateCommand() *cobra.Command {
129129
)
130130

131131
cmd := &cobra.Command{
132-
Use: "create runtime_name git-source_name",
133-
Short: "add a new git-source to an existing runtime",
132+
Use: "create RUNTIME_NAME GITSOURCE_NAME",
133+
Short: "Adds a new git-source to an existing runtime",
134+
Args: cobra.MaximumNArgs(2),
134135
Example: util.Doc(`
135136
<BIN> git-source create runtime_name git-source-name --git-src-repo https://github.com/owner/repo-name/my-workflow
136137
`),
@@ -228,7 +229,7 @@ func RunGitSourceCreate(ctx context.Context, opts *GitSourceCreateOptions) error
228229
if err := appDef.CreateApp(ctx, nil, opts.InsCloneOpts, opts.RuntimeName, store.Get().CFGitSourceType, opts.Include, ""); err != nil {
229230
return fmt.Errorf("failed to create git-source application. Err: %w", err)
230231
}
231-
232+
232233
log.G(ctx).Infof("Successfully created git-source: '%s'", opts.GsName)
233234

234235
return nil
@@ -264,7 +265,7 @@ func createDemoResources(ctx context.Context, opts *GitSourceCreateOptions, gsRe
264265
commitMsg := fmt.Sprintf("Created demo pipelines in %s Directory", opts.GsCloneOpts.Path())
265266

266267
log.G(ctx).Info("Pushing demo pipelines to the new git-source repo")
267-
268+
268269
if err := apu.PushWithMessage(ctx, gsRepo, commitMsg); err != nil {
269270
return fmt.Errorf("failed to push demo pipelines to git-source repo: %w", err)
270271
}
@@ -280,7 +281,7 @@ func createPlaceholderIfNeeded(ctx context.Context, opts *GitSourceCreateOptions
280281
}
281282

282283
if len(fi) == 0 {
283-
if err = billyUtils.WriteFile(gsFs, "DUMMY", []byte{}, 0666); err != nil {
284+
if err = billyUtils.WriteFile(gsFs, "DUMMY", []byte{}, 0666); err != nil {
284285
return fmt.Errorf("failed to write the git-source placeholder file. Err: %w", err)
285286
}
286287

@@ -434,18 +435,16 @@ func createCronExampleTrigger() (*sensorsv1alpha1.Trigger, error) {
434435

435436
func NewGitSourceListCommand() *cobra.Command {
436437
cmd := &cobra.Command{
437-
Use: "list runtime_name",
438+
Use: "list RUNTIME_NAME",
438439
Short: "List all Codefresh git-sources of a given runtime",
440+
Args: cobra.MaximumNArgs(1),
439441
Example: util.Doc(`<BIN> git-source list my-runtime`),
440-
PreRun: func(cmd *cobra.Command, args []string) {
442+
RunE: func(cmd *cobra.Command, args []string) error {
441443
if len(args) < 1 {
442-
log.G(cmd.Context()).Fatal("must enter runtime name")
444+
return fmt.Errorf("must enter runtime name")
443445
}
444-
},
445-
RunE: func(cmd *cobra.Command, args []string) error {
446-
ctx := cmd.Context()
447446

448-
return RunGitSourceList(ctx, args[0])
447+
return RunGitSourceList(cmd.Context(), args[0])
449448
},
450449
}
451450
return cmd
@@ -514,21 +513,21 @@ func NewGitSourceDeleteCommand() *cobra.Command {
514513
)
515514

516515
cmd := &cobra.Command{
517-
Use: "delete runtime_name git-source_name",
516+
Use: "delete RUNTIME_NAME GITSOURCE_NAME",
518517
Short: "delete a git-source from a runtime",
518+
Args: cobra.MaximumNArgs(2),
519519
Example: util.Doc(`
520520
<BIN> git-source delete runtime_name git-source_name
521521
`),
522522
PreRunE: func(cmd *cobra.Command, args []string) error {
523-
ctx := cmd.Context()
524523
store.Get().Silent = true
525524

526525
if len(args) < 1 {
527-
log.G(ctx).Fatal("must enter runtime name")
526+
return fmt.Errorf("must enter runtime name")
528527
}
529528

530529
if len(args) < 2 {
531-
log.G(ctx).Fatal("must enter git-source name")
530+
return fmt.Errorf("must enter git-source name")
532531
}
533532

534533
err := ensureRepo(cmd, args[0], insCloneOpts, true)
@@ -580,25 +579,25 @@ func NewGitSourceEditCommand() *cobra.Command {
580579
)
581580

582581
cmd := &cobra.Command{
583-
Use: "edit runtime_name git-source_name",
582+
Use: "edit RUNTIME_NAME GITSOURCE_NAME",
584583
Short: "edit a git-source of a runtime",
584+
Args: cobra.MaximumNArgs(2),
585585
Example: util.Doc(`
586-
<BIN> git-source edit runtime_name git-source_name --git-src-repo https://github.com/owner/repo-name/my-workflow
586+
<BIN> git-source edit runtime_name git-source_name --git-src-repo https://github.com/owner/repo-name.git/path/to/dir
587587
`),
588588
PreRunE: func(cmd *cobra.Command, args []string) error {
589-
ctx := cmd.Context()
590589
store.Get().Silent = true
591590

592591
if len(args) < 1 {
593-
log.G(ctx).Fatal("must enter a runtime name")
592+
return fmt.Errorf("must enter a runtime name")
594593
}
595594

596595
if len(args) < 2 {
597-
log.G(ctx).Fatal("must enter a git-source name")
596+
return fmt.Errorf("must enter a git-source name")
598597
}
599598

600599
if gsCloneOpts.Repo == "" {
601-
log.G(ctx).Fatal("must enter a valid value to --git-src-repo. Example: https://github.com/owner/repo-name/path/to/workflow")
600+
return fmt.Errorf("must enter a valid value to --git-src-repo. Example: https://github.com/owner/repo-name.git/path/to/dir")
602601
}
603602

604603
err := ensureRepo(cmd, args[0], insCloneOpts, true)

cmd/commands/integrations.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,14 @@ func NewIntegrationCommand() *cobra.Command {
6666

6767
cmd.AddCommand(NewGitIntegrationCommand(&client))
6868

69-
cmd.Hidden = true // hide this command for now
70-
7169
return cmd
7270
}
7371

7472
func NewGitIntegrationCommand(client *sdk.AppProxyAPI) *cobra.Command {
7573
cmd := &cobra.Command{
7674
Use: "git",
7775
Short: "Manage your git integrations",
76+
Args: cobra.NoArgs,
7877
Run: func(cmd *cobra.Command, args []string) {
7978
cmd.HelpFunc()(cmd, args)
8079
exit(1)
@@ -102,6 +101,7 @@ func NewGitIntegrationListCommand(client *sdk.AppProxyAPI) *cobra.Command {
102101
cmd := &cobra.Command{
103102
Use: "list",
104103
Short: "List your git integrations",
104+
Args: cobra.NoArgs,
105105
RunE: func(cmd *cobra.Command, args []string) error {
106106
if err := verifyOutputFormat(format, allowedFormats...); err != nil {
107107
return err
@@ -159,6 +159,7 @@ func NewGitIntegrationGetCommand(client *sdk.AppProxyAPI) *cobra.Command {
159159
cmd := &cobra.Command{
160160
Use: "get [NAME]",
161161
Short: "Retrieve a git integration",
162+
Args: cobra.MaximumNArgs(1),
162163
RunE: func(cmd *cobra.Command, args []string) error {
163164
if len(args) > 0 {
164165
integration = &args[0]
@@ -196,6 +197,7 @@ func NewGitIntegrationAddCommand(client *sdk.AppProxyAPI) *cobra.Command {
196197
cmd := &cobra.Command{
197198
Use: "add [NAME]",
198199
Short: "Add a new git integration",
200+
Args: cobra.MaximumNArgs(1),
199201
RunE: func(cmd *cobra.Command, args []string) error {
200202
var err error
201203

@@ -246,6 +248,7 @@ func NewGitIntegrationEditCommand(client *sdk.AppProxyAPI) *cobra.Command {
246248
cmd := &cobra.Command{
247249
Use: "edit [NAME]",
248250
Short: "Edit a git integration",
251+
Args: cobra.MaximumNArgs(1),
249252
RunE: func(cmd *cobra.Command, args []string) error {
250253
if len(args) > 0 {
251254
opts.Name = &args[0]
@@ -282,6 +285,7 @@ func NewGitIntegrationRemoveCommand(client *sdk.AppProxyAPI) *cobra.Command {
282285
cmd := &cobra.Command{
283286
Use: "remove NAME",
284287
Short: "Remove a git integration",
288+
Args: cobra.MaximumNArgs(1),
285289
RunE: func(cmd *cobra.Command, args []string) error {
286290
if len(args) < 1 {
287291
return fmt.Errorf("missing integration name")
@@ -312,6 +316,7 @@ func NewGitIntegrationRegisterCommand(client *sdk.AppProxyAPI) *cobra.Command {
312316
cmd := &cobra.Command{
313317
Use: "register [NAME]",
314318
Short: "Register to a git integrations",
319+
Args: cobra.MaximumNArgs(1),
315320
RunE: func(cmd *cobra.Command, args []string) error {
316321
if len(args) > 0 {
317322
opts.Name = &args[0]
@@ -349,6 +354,7 @@ func NewGitIntegrationDeregisterCommand(client *sdk.AppProxyAPI) *cobra.Command
349354
cmd := &cobra.Command{
350355
Use: "deregister [NAME]",
351356
Short: "Deregister user from a git integrations",
357+
Args: cobra.MaximumNArgs(1),
352358
RunE: func(cmd *cobra.Command, args []string) error {
353359
if len(args) > 0 {
354360
integration = &args[0]

cmd/commands/pipeline.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func NewPipelineGetCommand() *cobra.Command {
5555
cmd := &cobra.Command{
5656
Use: "get --runtime <runtime> --namespace <namespace> --name <name>",
5757
Short: "Get a pipeline under a specific runtime and namespace",
58+
Args: cobra.NoArgs,
5859
Example: util.Doc(`
5960
<BIN> pipeline --runtime runtime_name --namespace namespace --name pipeline_name
6061
@@ -88,6 +89,7 @@ func NewPipelineListCommand() *cobra.Command {
8889
cmd := &cobra.Command{
8990
Use: "list",
9091
Short: "List all the pipelines",
92+
Args: cobra.NoArgs,
9193
Example: util.Doc(`
9294
<BIN> pipelines list
9395

0 commit comments

Comments
 (0)