Skip to content

Commit

Permalink
Cr 4369 - component list (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
ATGardner authored Aug 17, 2021
1 parent 80b888f commit 9de17e1
Show file tree
Hide file tree
Showing 13 changed files with 268 additions and 81 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION=v0.0.67
VERSION=v0.0.68
OUT_DIR=dist
YEAR?=$(shell date +"%Y")

Expand Down
107 changes: 107 additions & 0 deletions cmd/commands/component.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright 2021 The Codefresh Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package commands

import (
"context"
"fmt"
"os"

"github.com/codefresh-io/cli-v2/pkg/log"
"github.com/codefresh-io/cli-v2/pkg/util"
"github.com/juju/ansiterm"

"github.com/spf13/cobra"
)

type ()

func NewComponentCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "component",
Short: "Manage components of Codefresh runtimes",
PersistentPreRunE: cfConfig.RequireAuthentication,
Run: func(cmd *cobra.Command, args []string) {
cmd.HelpFunc()(cmd, args)
exit(1)
},
}

cmd.AddCommand(NewComponentListCommand())

return cmd
}

func NewComponentListCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "list [runtime_name]",
Short: "List all the components under a specific runtime",
Example: util.Doc(`
<BIN> component list runtime_name
`),
PreRun: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
log.G(cmd.Context()).Fatal("must enter runtime name")
}
},
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

return RunComponentList(ctx, args[0])
},
}

return cmd
}

func RunComponentList(ctx context.Context, runtimeName string) error {
components, err := cfConfig.NewClient().V2().Component().List(ctx, runtimeName)
if err != nil {
return err
}

if len(components) == 0 {
log.G(ctx).WithField("runtime", runtimeName).Warn("no components were found in runtime")
return nil
}

tb := ansiterm.NewTabWriter(os.Stdout, 0, 0, 4, ' ', 0)
_, err = fmt.Fprintln(tb, "NAME\tHEALTH STATUS\tSYNC STATUS\tVERSION")
if err != nil {
return err
}

for _, c := range components {
name := c.Metadata.Name
healthStatus := "N/A"
syncStatus := c.Self.Status.SyncStatus.String()
version := c.Version

if c.Self.Status.HealthStatus != nil {
healthStatus = c.Self.Status.HealthStatus.String()
}
_, err = fmt.Fprintf(tb, "%s\t%s\t%s\t%s\n",
name,
healthStatus,
syncStatus,
version,
)
if err != nil {
return err
}
}

return tb.Flush()
}
139 changes: 74 additions & 65 deletions cmd/commands/git-source.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func NewGitSourceCreateCommand() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

return RunCreateGitSource(ctx, &GitSourceCreateOptions{
return RunGitSourceCreate(ctx, &GitSourceCreateOptions{
insCloneOpts: insCloneOpts,
gsCloneOpts: gsCloneOpts,
gsName: args[1],
Expand All @@ -144,25 +144,73 @@ func NewGitSourceCreateCommand() *cobra.Command {
return cmd
}

func RunGitSourceCreate(ctx context.Context, opts *GitSourceCreateOptions) error {
gsRepo, gsFs, err := opts.gsCloneOpts.GetRepo(ctx)
if err != nil {
return err
}

fi, err := gsFs.ReadDir(".")
if err != nil {
return fmt.Errorf("failed to read files in git-source repo. Err: %w", err)
}

if len(fi) == 0 {
if err = createDemoWorkflowTemplate(gsFs, opts.gsName, opts.runtimeName); err != nil {
return fmt.Errorf("failed to create demo workflowTemplate: %w", err)
}

_, err = gsRepo.Persist(ctx, &git.PushOptions{
CommitMsg: fmt.Sprintf("Created demo workflow template in %s Directory", opts.gsCloneOpts.Path()),
})

if err != nil {
return fmt.Errorf("failed to push changes. Err: %w", err)
}
}

appDef := &runtime.AppDef{
Name: opts.gsName,
Type: application.AppTypeDirectory,
URL: opts.gsCloneOpts.Repo,
}
if err := appDef.CreateApp(ctx, nil, opts.insCloneOpts, opts.runtimeName, store.Get().CFGitSourceType, nil); err != nil {
return fmt.Errorf("failed to create git-source application. Err: %w", err)
}

log.G(ctx).Infof("Successfully created the git-source: '%s'", opts.gsName)

return nil
}

func NewGitSourceListCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "list runtime_name",
Short: "List all Codefresh git-sources of a given runtime",
Example: util.Doc(`<BIN> git-source list my-runtime`),
RunE: func(_ *cobra.Command, args []string) error {
return RunGitSourceList(args[0])
PreRun: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
log.G(cmd.Context()).Fatal("must enter runtime name")
}
},
RunE: func(cmd *cobra.Command, args []string) error {
return RunGitSourceList(cmd.Context(), args[0])
},
}
return cmd
}

func RunGitSourceList(runtimeName string) error {
gitSources, err := cfConfig.NewClient().GitSource().List(runtimeName)

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

if len(gitSources) == 0 {
log.G(ctx).WithField("runtime", runtimeName).Info("no git-sources were found in runtime")
return nil
}

tb := ansiterm.NewTabWriter(os.Stdout, 0, 0, 4, ' ', 0)
_, err = fmt.Fprintln(tb, "NAME\tREPOURL\tPATH\tHEALTH-STATUS\tSYNC-STATUS")
if err != nil {
Expand Down Expand Up @@ -223,7 +271,7 @@ func NewGitSourceDeleteCommand() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

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

func RunGitSourceDelete(ctx context.Context, opts *GitSourceDeleteOptions) error {
err := apcmd.RunAppDelete(ctx, &apcmd.AppDeleteOptions{
CloneOpts: opts.InsCloneOpts,
ProjectName: opts.RuntimeName,
AppName: opts.GsName,
Global: false,
})

if err != nil {
return fmt.Errorf("failed to delete the git-source %s. Err: %w", opts.GsName, err)
}

log.G(ctx).Debug("Successfully deleted the git-source: %s", opts.GsName)

return nil
}

func NewGitSourceEditCommand() *cobra.Command {
var (
insCloneOpts *git.CloneOptions
Expand Down Expand Up @@ -272,7 +337,7 @@ func NewGitSourceEditCommand() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

return RunEditGitSource(ctx, &GitSourceEditOptions{
return RunGitSourceEdit(ctx, &GitSourceEditOptions{
RuntimeName: args[0],
GsName: args[1],
InsCloneOpts: insCloneOpts,
Expand All @@ -296,7 +361,7 @@ func NewGitSourceEditCommand() *cobra.Command {
return cmd
}

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

func RunCreateGitSource(ctx context.Context, opts *GitSourceCreateOptions) error {
gsRepo, gsFs, err := opts.gsCloneOpts.GetRepo(ctx)
if err != nil {
return err
}

fi, err := gsFs.ReadDir(".")
if err != nil {
return fmt.Errorf("failed to read files in git-source repo. Err: %w", err)
}

if len(fi) == 0 {
if err = createDemoWorkflowTemplate(gsFs, opts.gsName, opts.runtimeName); err != nil {
return fmt.Errorf("failed to create demo workflowTemplate: %w", err)
}

_, err = gsRepo.Persist(ctx, &git.PushOptions{
CommitMsg: fmt.Sprintf("Created demo workflow template in %s Directory", opts.gsCloneOpts.Path()),
})

if err != nil {
return fmt.Errorf("failed to push changes. Err: %w", err)
}
}

appDef := &runtime.AppDef{
Name: opts.gsName,
Type: application.AppTypeDirectory,
URL: opts.gsCloneOpts.Repo,
}
if err := appDef.CreateApp(ctx, nil, opts.insCloneOpts, opts.runtimeName, store.Get().CFGitSourceType, nil); err != nil {
return fmt.Errorf("failed to create git-source application. Err: %w", err)
}

log.G(ctx).Infof("Successfully created the git-source: '%s'", opts.gsName)

return nil
}

func RunDeleteGitSource(ctx context.Context, opts *GitSourceDeleteOptions) error {
err := apcmd.RunAppDelete(ctx, &apcmd.AppDeleteOptions{
CloneOpts: opts.InsCloneOpts,
ProjectName: opts.RuntimeName,
AppName: opts.GsName,
Global: false,
})

if err != nil {
return fmt.Errorf("failed to delete the git-source %s. Err: %w", opts.GsName, err)
}

log.G(ctx).Debug("Successfully deleted the git-source: %s", opts.GsName)

return nil
}

func createDemoWorkflowTemplate(gsFs fs.FS, gsName, runtimeName string) error {
wfTemplate := &wfv1alpha1.WorkflowTemplate{
TypeMeta: metav1.TypeMeta{
Expand Down
1 change: 1 addition & 0 deletions cmd/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ variables in advanced to simplify the use of those commands.
cmd.AddCommand(NewConfigCommand())
cmd.AddCommand(NewRuntimeCommand())
cmd.AddCommand(NewGitSourceCommand())
cmd.AddCommand(NewComponentCommand())

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

Expand Down
20 changes: 13 additions & 7 deletions cmd/commands/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ func RunRuntimeInstall(ctx context.Context, opts *RuntimeInstallOptions) error {
if rt.Spec.Version != nil { // in dev mode
runtimeVersion = rt.Spec.Version.String()
}
runtimeCreationResponse, err := cfConfig.NewClient().ArgoRuntime().Create(opts.RuntimeName, server, runtimeVersion)

runtimeCreationResponse, err := cfConfig.NewClient().V2().Runtime().Create(ctx, opts.RuntimeName, server, runtimeVersion)
if err != nil {
return fmt.Errorf("failed to create a new runtime: %w", err)
}
Expand Down Expand Up @@ -248,7 +249,7 @@ func RunRuntimeInstall(ctx context.Context, opts *RuntimeInstallOptions) error {
gsPath := opts.gsCloneOpts.FS.Join(apstore.Default.AppsDir, store.Get().GitSourceName, opts.RuntimeName)
fullGsPath := opts.gsCloneOpts.FS.Join(opts.gsCloneOpts.FS.Root(), gsPath)[1:]

if err = RunCreateGitSource(ctx, &GitSourceCreateOptions{
if err = RunGitSourceCreate(ctx, &GitSourceCreateOptions{
insCloneOpts: opts.insCloneOpts,
gsCloneOpts: opts.gsCloneOpts,
gsName: store.Get().GitSourceName,
Expand All @@ -267,19 +268,24 @@ func NewRuntimeListCommand() *cobra.Command {
Use: "list [runtime_name]",
Short: "List all Codefresh runtimes",
Example: util.Doc(`<BIN> runtime list`),
RunE: func(_ *cobra.Command, _ []string) error {
return RunRuntimeList()
RunE: func(cmd *cobra.Command, _ []string) error {
return RunRuntimeList(cmd.Context())
},
}
return cmd
}

func RunRuntimeList() error {
runtimes, err := cfConfig.NewClient().ArgoRuntime().List()
func RunRuntimeList(ctx context.Context) error {
runtimes, err := cfConfig.NewClient().V2().Runtime().List(ctx)
if err != nil {
return err
}

if len(runtimes) == 0 {
log.G(ctx).Info("no runtimes were found")
return nil
}

tb := ansiterm.NewTabWriter(os.Stdout, 0, 0, 4, ' ', 0)
_, err = fmt.Fprintln(tb, "NAME\tNAMESPACE\tCLUSTER\tSTATUS\tVERSION")
if err != nil {
Expand All @@ -293,7 +299,7 @@ func RunRuntimeList() error {
name := "N/A"
version := "N/A"

if rt.Self != nil && rt.Self.HealthMessage != nil {
if rt.Self.HealthMessage != nil {
status = *rt.Self.HealthMessage
}

Expand Down
1 change: 1 addition & 0 deletions docs/commands/cli-v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ cli-v2 [flags]

### SEE ALSO

* [cli-v2 component](cli-v2_component.md) - Manage components of Codefresh runtimes
* [cli-v2 config](cli-v2_config.md) - Manage Codefresh authentication contexts
* [cli-v2 git-source](cli-v2_git-source.md) - Manage git-sources of Codefresh runtimes
* [cli-v2 runtime](cli-v2_runtime.md) - Manage Codefresh runtimes
Expand Down
Loading

0 comments on commit 9de17e1

Please sign in to comment.