Skip to content

Commit

Permalink
Cr 5420 (#81)
Browse files Browse the repository at this point in the history
* added workflow get,list
* added pipeline get,list
* upgraded go-sdk
  • Loading branch information
daniel-codefresh authored Aug 29, 2021
1 parent 627cf08 commit d7c0ad7
Show file tree
Hide file tree
Showing 15 changed files with 589 additions and 7 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.82
VERSION=v0.0.83
OUT_DIR=dist
YEAR?=$(shell date +"%Y")

Expand Down
193 changes: 193 additions & 0 deletions cmd/commands/pipeline.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
// 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/codefresh-io/go-sdk/pkg/codefresh/model"
"github.com/juju/ansiterm"

"github.com/spf13/cobra"
)

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

cmd.AddCommand(NewPipelineGetCommand())
cmd.AddCommand(NewPipelineListCommand())

return cmd
}

func NewPipelineGetCommand() *cobra.Command {
var (
name string
namespace string
runtime string
)

cmd := &cobra.Command{
Use: "get --runtime <runtime> --namespace <namespace> --name <name>",
Short: "Get a pipeline under a specific runtime and namespace",
Example: util.Doc(`
<BIN> pipeline --runtime runtime_name --namespace namespace --name pipeline_name
<BIN> pipeline -r runtime_name -N namespace -n pipeline_name
`),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

return RunPipelineGet(ctx, name, namespace, runtime)
},
}

cmd.Flags().StringVarP(&name, "name", "n", "", "Name of target pipeline")
util.Die(cmd.MarkFlagRequired("name"))
cmd.Flags().StringVarP(&namespace, "namespace", "N", "", "Namespace of target pipeline")
util.Die(cmd.MarkFlagRequired("namespace"))
cmd.Flags().StringVarP(&runtime, "runtime", "r", "", "Runtime name of target pipeline")
util.Die(cmd.MarkFlagRequired("runtime"))

return cmd
}

func NewPipelineListCommand() *cobra.Command {
var (
name string
namespace string
runtime string
project string
)

cmd := &cobra.Command{
Use: "list",
Short: "List all the pipelines",
Example: util.Doc(`
<BIN> pipelines list
<BIN> pipelines list --runtime <runtime>
<BIN> pipelines list -r <runtime>
`),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

filterArgs := model.PipelinesFilterArgs{
Name: &name,
Namespace: &namespace,
Runtime: &runtime,
Project: &project,
}
return RunPipelineList(ctx, filterArgs)
},
}

cmd.Flags().StringVarP(&name, "name", "n", "", "Filter by pipeline name")
cmd.Flags().StringVarP(&namespace, "namespace", "N", "", "Filter by pipeline namespace")
cmd.Flags().StringVarP(&runtime, "runtime", "r", "", "Filter by pipeline runtime")
cmd.Flags().StringVarP(&project, "project", "p", "", "Filter by pipeline project")

return cmd
}

func RunPipelineGet(ctx context.Context, name, namespace, runtime string) error {
pipeline, err := cfConfig.NewClient().V2().Pipeline().Get(ctx, name, namespace, runtime)
if err != nil {
return err
}

if pipeline == nil {
fields := log.Fields{
"name": name,
"namespace": namespace,
"runtime": runtime,
}
log.G(ctx).WithFields(fields).Warn("pipeline was not found")
return nil
}

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

healthStatus := "N/A"
if pipeline.Self.HealthStatus != nil {
healthStatus = pipeline.Self.HealthStatus.String()
}
_, err = fmt.Fprintf(tb, "%s\t%s\t%s\t%s\t%s\n",
pipeline.Metadata.Name,
*pipeline.Metadata.Namespace,
pipeline.Metadata.Runtime,
healthStatus,
pipeline.Self.SyncStatus,
)
if err != nil {
return err
}

return tb.Flush()
}

func RunPipelineList(ctx context.Context, filterArgs model.PipelinesFilterArgs) error {
pipelines, err := cfConfig.NewClient().V2().Pipeline().List(ctx, filterArgs)
if err != nil {
return err
}

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

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

for _, p := range pipelines {
healthStatus := "N/A"
if p.Self.HealthStatus != nil {
healthStatus = p.Self.HealthStatus.String()
}
_, err = fmt.Fprintf(tb, "%s\t%s\t%s\t%s\t%s\n",
p.Metadata.Name,
*p.Metadata.Namespace,
p.Metadata.Runtime,
healthStatus,
p.Self.SyncStatus,
)
if err != nil {
return err
}
}

return tb.Flush()
}
2 changes: 2 additions & 0 deletions cmd/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ variables in advanced to simplify the use of those commands.
cmd.AddCommand(NewRuntimeCommand())
cmd.AddCommand(NewGitSourceCommand())
cmd.AddCommand(NewComponentCommand())
cmd.AddCommand(NewWorkflowCommand())
cmd.AddCommand(NewPipelineCommand())

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

Expand Down
167 changes: 167 additions & 0 deletions cmd/commands/workflow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
// 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/codefresh-io/go-sdk/pkg/codefresh/model"
"github.com/juju/ansiterm"

"github.com/spf13/cobra"
)

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

cmd.AddCommand(NewWorkflowGetCommand())
cmd.AddCommand(NewWorkflowListCommand())

return cmd
}

func NewWorkflowGetCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "get [uid]",
Short: "Get a workflow under a specific uid",
Example: util.Doc(`
<BIN> workflow get 0732b138-b74c-4a5e-b065-e23e6da0803d
`),
PreRun: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
log.G(cmd.Context()).Fatal("must enter uid")
}
},
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

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

return cmd
}

func NewWorkflowListCommand() *cobra.Command {
var (
namespace string
runtime string
project string
)

cmd := &cobra.Command{
Use: "list",
Short: "List all the workflows",
Example: util.Doc(`
<BIN> workflows list
<BIN> workflows list --runtime <runtime>
<BIN> workflows list -r <runtime>
`),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

filterArgs := model.WorkflowsFilterArgs{
Namespace: &namespace,
Runtime: &runtime,
Project: &project,
}
return RunWorkflowList(ctx, filterArgs)
},
}

cmd.Flags().StringVarP(&namespace, "namespace", "N", "", "Filter by workflow namespace")
cmd.Flags().StringVarP(&runtime, "runtime", "r", "", "Filter by workflow runtime")
cmd.Flags().StringVarP(&project, "project", "p", "", "Filter by workflow project")

return cmd
}

func RunWorkflowGet(ctx context.Context, uid string) error {
workflow, err := cfConfig.NewClient().V2().Workflow().Get(ctx, uid)
if err != nil {
return err
}

if workflow == nil {
log.G(ctx).WithField("uid", uid).Warn("workflow was not found")
return nil
}

tb := ansiterm.NewTabWriter(os.Stdout, 0, 0, 4, ' ', 0)
_, err = fmt.Fprintln(tb, "NAME\tNAMESPACE\tRUNTIME\tPHASE\tUID")
if err != nil {
return err
}

_, err = fmt.Fprintf(tb, "%s\t%s\t%s\t%s\t%s\n",
workflow.Metadata.Name,
*workflow.Metadata.Namespace,
workflow.Metadata.Runtime,
workflow.Status.Phase,
*workflow.Metadata.UID,
)
if err != nil {
return err
}

return tb.Flush()
}

func RunWorkflowList(ctx context.Context, filterArgs model.WorkflowsFilterArgs) error {
workflows, err := cfConfig.NewClient().V2().Workflow().List(ctx, filterArgs)
if err != nil {
return err
}

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

tb := ansiterm.NewTabWriter(os.Stdout, 0, 0, 4, ' ', 0)
_, err = fmt.Fprintln(tb, "NAME\tNAMESPACE\tRUNTIME\tPHASE\tUID")
if err != nil {
return err
}

for _, w := range workflows {
_, err = fmt.Fprintf(tb, "%s\t%s\t%s\t%s\t%s\n",
w.Metadata.Name,
*w.Metadata.Namespace,
w.Metadata.Runtime,
w.Status.Phase,
*w.Metadata.UID,
)
if err != nil {
return err
}
}

return tb.Flush()
}
2 changes: 2 additions & 0 deletions docs/commands/cli-v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ cli-v2 [flags]
* [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 pipeline](cli-v2_pipeline.md) - Manage pipelines of Codefresh runtimes
* [cli-v2 runtime](cli-v2_runtime.md) - Manage Codefresh runtimes
* [cli-v2 version](cli-v2_version.md) - Show cli version
* [cli-v2 workflow](cli-v2_workflow.md) - Manage workflows of Codefresh runtimes

Loading

0 comments on commit d7c0ad7

Please sign in to comment.