Skip to content

Commit 9de17e1

Browse files
authored
Cr 4369 - component list (#65)
1 parent 80b888f commit 9de17e1

File tree

13 files changed

+268
-81
lines changed

13 files changed

+268
-81
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.67
1+
VERSION=v0.0.68
22
OUT_DIR=dist
33
YEAR?=$(shell date +"%Y")
44

cmd/commands/component.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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/juju/ansiterm"
25+
26+
"github.com/spf13/cobra"
27+
)
28+
29+
type ()
30+
31+
func NewComponentCommand() *cobra.Command {
32+
cmd := &cobra.Command{
33+
Use: "component",
34+
Short: "Manage components of Codefresh runtimes",
35+
PersistentPreRunE: cfConfig.RequireAuthentication,
36+
Run: func(cmd *cobra.Command, args []string) {
37+
cmd.HelpFunc()(cmd, args)
38+
exit(1)
39+
},
40+
}
41+
42+
cmd.AddCommand(NewComponentListCommand())
43+
44+
return cmd
45+
}
46+
47+
func NewComponentListCommand() *cobra.Command {
48+
cmd := &cobra.Command{
49+
Use: "list [runtime_name]",
50+
Short: "List all the components under a specific runtime",
51+
Example: util.Doc(`
52+
<BIN> component list runtime_name
53+
`),
54+
PreRun: func(cmd *cobra.Command, args []string) {
55+
if len(args) < 1 {
56+
log.G(cmd.Context()).Fatal("must enter runtime name")
57+
}
58+
},
59+
RunE: func(cmd *cobra.Command, args []string) error {
60+
ctx := cmd.Context()
61+
62+
return RunComponentList(ctx, args[0])
63+
},
64+
}
65+
66+
return cmd
67+
}
68+
69+
func RunComponentList(ctx context.Context, runtimeName string) error {
70+
components, err := cfConfig.NewClient().V2().Component().List(ctx, runtimeName)
71+
if err != nil {
72+
return err
73+
}
74+
75+
if len(components) == 0 {
76+
log.G(ctx).WithField("runtime", runtimeName).Warn("no components were found in runtime")
77+
return nil
78+
}
79+
80+
tb := ansiterm.NewTabWriter(os.Stdout, 0, 0, 4, ' ', 0)
81+
_, err = fmt.Fprintln(tb, "NAME\tHEALTH STATUS\tSYNC STATUS\tVERSION")
82+
if err != nil {
83+
return err
84+
}
85+
86+
for _, c := range components {
87+
name := c.Metadata.Name
88+
healthStatus := "N/A"
89+
syncStatus := c.Self.Status.SyncStatus.String()
90+
version := c.Version
91+
92+
if c.Self.Status.HealthStatus != nil {
93+
healthStatus = c.Self.Status.HealthStatus.String()
94+
}
95+
_, err = fmt.Fprintf(tb, "%s\t%s\t%s\t%s\n",
96+
name,
97+
healthStatus,
98+
syncStatus,
99+
version,
100+
)
101+
if err != nil {
102+
return err
103+
}
104+
}
105+
106+
return tb.Flush()
107+
}

cmd/commands/git-source.go

Lines changed: 74 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func NewGitSourceCreateCommand() *cobra.Command {
120120
RunE: func(cmd *cobra.Command, args []string) error {
121121
ctx := cmd.Context()
122122

123-
return RunCreateGitSource(ctx, &GitSourceCreateOptions{
123+
return RunGitSourceCreate(ctx, &GitSourceCreateOptions{
124124
insCloneOpts: insCloneOpts,
125125
gsCloneOpts: gsCloneOpts,
126126
gsName: args[1],
@@ -144,25 +144,73 @@ func NewGitSourceCreateCommand() *cobra.Command {
144144
return cmd
145145
}
146146

147+
func RunGitSourceCreate(ctx context.Context, opts *GitSourceCreateOptions) error {
148+
gsRepo, gsFs, err := opts.gsCloneOpts.GetRepo(ctx)
149+
if err != nil {
150+
return err
151+
}
152+
153+
fi, err := gsFs.ReadDir(".")
154+
if err != nil {
155+
return fmt.Errorf("failed to read files in git-source repo. Err: %w", err)
156+
}
157+
158+
if len(fi) == 0 {
159+
if err = createDemoWorkflowTemplate(gsFs, opts.gsName, opts.runtimeName); err != nil {
160+
return fmt.Errorf("failed to create demo workflowTemplate: %w", err)
161+
}
162+
163+
_, err = gsRepo.Persist(ctx, &git.PushOptions{
164+
CommitMsg: fmt.Sprintf("Created demo workflow template in %s Directory", opts.gsCloneOpts.Path()),
165+
})
166+
167+
if err != nil {
168+
return fmt.Errorf("failed to push changes. Err: %w", err)
169+
}
170+
}
171+
172+
appDef := &runtime.AppDef{
173+
Name: opts.gsName,
174+
Type: application.AppTypeDirectory,
175+
URL: opts.gsCloneOpts.Repo,
176+
}
177+
if err := appDef.CreateApp(ctx, nil, opts.insCloneOpts, opts.runtimeName, store.Get().CFGitSourceType, nil); err != nil {
178+
return fmt.Errorf("failed to create git-source application. Err: %w", err)
179+
}
180+
181+
log.G(ctx).Infof("Successfully created the git-source: '%s'", opts.gsName)
182+
183+
return nil
184+
}
185+
147186
func NewGitSourceListCommand() *cobra.Command {
148187
cmd := &cobra.Command{
149188
Use: "list runtime_name",
150189
Short: "List all Codefresh git-sources of a given runtime",
151190
Example: util.Doc(`<BIN> git-source list my-runtime`),
152-
RunE: func(_ *cobra.Command, args []string) error {
153-
return RunGitSourceList(args[0])
191+
PreRun: func(cmd *cobra.Command, args []string) {
192+
if len(args) < 1 {
193+
log.G(cmd.Context()).Fatal("must enter runtime name")
194+
}
195+
},
196+
RunE: func(cmd *cobra.Command, args []string) error {
197+
return RunGitSourceList(cmd.Context(), args[0])
154198
},
155199
}
156200
return cmd
157201
}
158202

159-
func RunGitSourceList(runtimeName string) error {
160-
gitSources, err := cfConfig.NewClient().GitSource().List(runtimeName)
161-
203+
func RunGitSourceList(ctx context.Context, runtimeName string) error {
204+
gitSources, err := cfConfig.NewClient().V2().GitSource().List(ctx, runtimeName)
162205
if err != nil {
163206
return fmt.Errorf("failed to get git-sources list. Err: %w", err)
164207
}
165208

209+
if len(gitSources) == 0 {
210+
log.G(ctx).WithField("runtime", runtimeName).Info("no git-sources were found in runtime")
211+
return nil
212+
}
213+
166214
tb := ansiterm.NewTabWriter(os.Stdout, 0, 0, 4, ' ', 0)
167215
_, err = fmt.Fprintln(tb, "NAME\tREPOURL\tPATH\tHEALTH-STATUS\tSYNC-STATUS")
168216
if err != nil {
@@ -223,7 +271,7 @@ func NewGitSourceDeleteCommand() *cobra.Command {
223271
RunE: func(cmd *cobra.Command, args []string) error {
224272
ctx := cmd.Context()
225273

226-
return RunDeleteGitSource(ctx, &GitSourceDeleteOptions{
274+
return RunGitSourceDelete(ctx, &GitSourceDeleteOptions{
227275
RuntimeName: args[0],
228276
GsName: args[1],
229277
Timeout: aputil.MustParseDuration(cmd.Flag("request-timeout").Value.String()),
@@ -239,6 +287,23 @@ func NewGitSourceDeleteCommand() *cobra.Command {
239287
return cmd
240288
}
241289

290+
func RunGitSourceDelete(ctx context.Context, opts *GitSourceDeleteOptions) error {
291+
err := apcmd.RunAppDelete(ctx, &apcmd.AppDeleteOptions{
292+
CloneOpts: opts.InsCloneOpts,
293+
ProjectName: opts.RuntimeName,
294+
AppName: opts.GsName,
295+
Global: false,
296+
})
297+
298+
if err != nil {
299+
return fmt.Errorf("failed to delete the git-source %s. Err: %w", opts.GsName, err)
300+
}
301+
302+
log.G(ctx).Debug("Successfully deleted the git-source: %s", opts.GsName)
303+
304+
return nil
305+
}
306+
242307
func NewGitSourceEditCommand() *cobra.Command {
243308
var (
244309
insCloneOpts *git.CloneOptions
@@ -272,7 +337,7 @@ func NewGitSourceEditCommand() *cobra.Command {
272337
RunE: func(cmd *cobra.Command, args []string) error {
273338
ctx := cmd.Context()
274339

275-
return RunEditGitSource(ctx, &GitSourceEditOptions{
340+
return RunGitSourceEdit(ctx, &GitSourceEditOptions{
276341
RuntimeName: args[0],
277342
GsName: args[1],
278343
InsCloneOpts: insCloneOpts,
@@ -296,7 +361,7 @@ func NewGitSourceEditCommand() *cobra.Command {
296361
return cmd
297362
}
298363

299-
func RunEditGitSource(ctx context.Context, opts *GitSourceEditOptions) error {
364+
func RunGitSourceEdit(ctx context.Context, opts *GitSourceEditOptions) error {
300365
repo, fs, err := opts.InsCloneOpts.GetRepo(ctx)
301366
if err != nil {
302367
return fmt.Errorf("failed to clone the installation repo, attemptint to edit git-source %s. Err: %w", opts.GsName, err)
@@ -328,62 +393,6 @@ func RunEditGitSource(ctx context.Context, opts *GitSourceEditOptions) error {
328393
return nil
329394
}
330395

331-
func RunCreateGitSource(ctx context.Context, opts *GitSourceCreateOptions) error {
332-
gsRepo, gsFs, err := opts.gsCloneOpts.GetRepo(ctx)
333-
if err != nil {
334-
return err
335-
}
336-
337-
fi, err := gsFs.ReadDir(".")
338-
if err != nil {
339-
return fmt.Errorf("failed to read files in git-source repo. Err: %w", err)
340-
}
341-
342-
if len(fi) == 0 {
343-
if err = createDemoWorkflowTemplate(gsFs, opts.gsName, opts.runtimeName); err != nil {
344-
return fmt.Errorf("failed to create demo workflowTemplate: %w", err)
345-
}
346-
347-
_, err = gsRepo.Persist(ctx, &git.PushOptions{
348-
CommitMsg: fmt.Sprintf("Created demo workflow template in %s Directory", opts.gsCloneOpts.Path()),
349-
})
350-
351-
if err != nil {
352-
return fmt.Errorf("failed to push changes. Err: %w", err)
353-
}
354-
}
355-
356-
appDef := &runtime.AppDef{
357-
Name: opts.gsName,
358-
Type: application.AppTypeDirectory,
359-
URL: opts.gsCloneOpts.Repo,
360-
}
361-
if err := appDef.CreateApp(ctx, nil, opts.insCloneOpts, opts.runtimeName, store.Get().CFGitSourceType, nil); err != nil {
362-
return fmt.Errorf("failed to create git-source application. Err: %w", err)
363-
}
364-
365-
log.G(ctx).Infof("Successfully created the git-source: '%s'", opts.gsName)
366-
367-
return nil
368-
}
369-
370-
func RunDeleteGitSource(ctx context.Context, opts *GitSourceDeleteOptions) error {
371-
err := apcmd.RunAppDelete(ctx, &apcmd.AppDeleteOptions{
372-
CloneOpts: opts.InsCloneOpts,
373-
ProjectName: opts.RuntimeName,
374-
AppName: opts.GsName,
375-
Global: false,
376-
})
377-
378-
if err != nil {
379-
return fmt.Errorf("failed to delete the git-source %s. Err: %w", opts.GsName, err)
380-
}
381-
382-
log.G(ctx).Debug("Successfully deleted the git-source: %s", opts.GsName)
383-
384-
return nil
385-
}
386-
387396
func createDemoWorkflowTemplate(gsFs fs.FS, gsName, runtimeName string) error {
388397
wfTemplate := &wfv1alpha1.WorkflowTemplate{
389398
TypeMeta: metav1.TypeMeta{

cmd/commands/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ variables in advanced to simplify the use of those commands.
5252
cmd.AddCommand(NewConfigCommand())
5353
cmd.AddCommand(NewRuntimeCommand())
5454
cmd.AddCommand(NewGitSourceCommand())
55+
cmd.AddCommand(NewComponentCommand())
5556

5657
cobra.OnInitialize(func() { postInitCommands(cmd.Commands()) })
5758

cmd/commands/runtime.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ func RunRuntimeInstall(ctx context.Context, opts *RuntimeInstallOptions) error {
194194
if rt.Spec.Version != nil { // in dev mode
195195
runtimeVersion = rt.Spec.Version.String()
196196
}
197-
runtimeCreationResponse, err := cfConfig.NewClient().ArgoRuntime().Create(opts.RuntimeName, server, runtimeVersion)
197+
198+
runtimeCreationResponse, err := cfConfig.NewClient().V2().Runtime().Create(ctx, opts.RuntimeName, server, runtimeVersion)
198199
if err != nil {
199200
return fmt.Errorf("failed to create a new runtime: %w", err)
200201
}
@@ -248,7 +249,7 @@ func RunRuntimeInstall(ctx context.Context, opts *RuntimeInstallOptions) error {
248249
gsPath := opts.gsCloneOpts.FS.Join(apstore.Default.AppsDir, store.Get().GitSourceName, opts.RuntimeName)
249250
fullGsPath := opts.gsCloneOpts.FS.Join(opts.gsCloneOpts.FS.Root(), gsPath)[1:]
250251

251-
if err = RunCreateGitSource(ctx, &GitSourceCreateOptions{
252+
if err = RunGitSourceCreate(ctx, &GitSourceCreateOptions{
252253
insCloneOpts: opts.insCloneOpts,
253254
gsCloneOpts: opts.gsCloneOpts,
254255
gsName: store.Get().GitSourceName,
@@ -267,19 +268,24 @@ func NewRuntimeListCommand() *cobra.Command {
267268
Use: "list [runtime_name]",
268269
Short: "List all Codefresh runtimes",
269270
Example: util.Doc(`<BIN> runtime list`),
270-
RunE: func(_ *cobra.Command, _ []string) error {
271-
return RunRuntimeList()
271+
RunE: func(cmd *cobra.Command, _ []string) error {
272+
return RunRuntimeList(cmd.Context())
272273
},
273274
}
274275
return cmd
275276
}
276277

277-
func RunRuntimeList() error {
278-
runtimes, err := cfConfig.NewClient().ArgoRuntime().List()
278+
func RunRuntimeList(ctx context.Context) error {
279+
runtimes, err := cfConfig.NewClient().V2().Runtime().List(ctx)
279280
if err != nil {
280281
return err
281282
}
282283

284+
if len(runtimes) == 0 {
285+
log.G(ctx).Info("no runtimes were found")
286+
return nil
287+
}
288+
283289
tb := ansiterm.NewTabWriter(os.Stdout, 0, 0, 4, ' ', 0)
284290
_, err = fmt.Fprintln(tb, "NAME\tNAMESPACE\tCLUSTER\tSTATUS\tVERSION")
285291
if err != nil {
@@ -293,7 +299,7 @@ func RunRuntimeList() error {
293299
name := "N/A"
294300
version := "N/A"
295301

296-
if rt.Self != nil && rt.Self.HealthMessage != nil {
302+
if rt.Self.HealthMessage != nil {
297303
status = *rt.Self.HealthMessage
298304
}
299305

docs/commands/cli-v2.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ cli-v2 [flags]
3131

3232
### SEE ALSO
3333

34+
* [cli-v2 component](cli-v2_component.md) - Manage components of Codefresh runtimes
3435
* [cli-v2 config](cli-v2_config.md) - Manage Codefresh authentication contexts
3536
* [cli-v2 git-source](cli-v2_git-source.md) - Manage git-sources of Codefresh runtimes
3637
* [cli-v2 runtime](cli-v2_runtime.md) - Manage Codefresh runtimes

0 commit comments

Comments
 (0)