-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added workflow get,list * added pipeline get,list * upgraded go-sdk
- Loading branch information
1 parent
627cf08
commit d7c0ad7
Showing
15 changed files
with
589 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.