|
| 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/codefresh-io/go-sdk/pkg/codefresh/model" |
| 25 | + "github.com/juju/ansiterm" |
| 26 | + |
| 27 | + "github.com/spf13/cobra" |
| 28 | +) |
| 29 | + |
| 30 | +func NewPipelineCommand() *cobra.Command { |
| 31 | + cmd := &cobra.Command{ |
| 32 | + Use: "pipeline", |
| 33 | + Short: "Manage pipelines of Codefresh runtimes", |
| 34 | + PersistentPreRunE: cfConfig.RequireAuthentication, |
| 35 | + Run: func(cmd *cobra.Command, args []string) { |
| 36 | + cmd.HelpFunc()(cmd, args) |
| 37 | + exit(1) |
| 38 | + }, |
| 39 | + } |
| 40 | + |
| 41 | + cmd.AddCommand(NewPipelineGetCommand()) |
| 42 | + cmd.AddCommand(NewPipelineListCommand()) |
| 43 | + |
| 44 | + return cmd |
| 45 | +} |
| 46 | + |
| 47 | +func NewPipelineGetCommand() *cobra.Command { |
| 48 | + var ( |
| 49 | + name string |
| 50 | + namespace string |
| 51 | + runtime string |
| 52 | + ) |
| 53 | + |
| 54 | + cmd := &cobra.Command{ |
| 55 | + Use: "get --runtime <runtime> --namespace <namespace> --name <name>", |
| 56 | + Short: "Get a pipeline under a specific runtime and namespace", |
| 57 | + Example: util.Doc(` |
| 58 | + <BIN> pipeline --runtime runtime_name --namespace namespace --name pipeline_name |
| 59 | +
|
| 60 | + <BIN> pipeline -r runtime_name -N namespace -n pipeline_name |
| 61 | + `), |
| 62 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 63 | + ctx := cmd.Context() |
| 64 | + |
| 65 | + return RunPipelineGet(ctx, name, namespace, runtime) |
| 66 | + }, |
| 67 | + } |
| 68 | + |
| 69 | + cmd.Flags().StringVarP(&name, "name", "n", "", "Name of target pipeline") |
| 70 | + util.Die(cmd.MarkFlagRequired("name")) |
| 71 | + cmd.Flags().StringVarP(&namespace, "namespace", "N", "", "Namespace of target pipeline") |
| 72 | + util.Die(cmd.MarkFlagRequired("namespace")) |
| 73 | + cmd.Flags().StringVarP(&runtime, "runtime", "r", "", "Runtime name of target pipeline") |
| 74 | + util.Die(cmd.MarkFlagRequired("runtime")) |
| 75 | + |
| 76 | + return cmd |
| 77 | +} |
| 78 | + |
| 79 | +func NewPipelineListCommand() *cobra.Command { |
| 80 | + var ( |
| 81 | + name string |
| 82 | + namespace string |
| 83 | + runtime string |
| 84 | + project string |
| 85 | + ) |
| 86 | + |
| 87 | + cmd := &cobra.Command{ |
| 88 | + Use: "list", |
| 89 | + Short: "List all the pipelines", |
| 90 | + Example: util.Doc(` |
| 91 | + <BIN> pipelines list |
| 92 | +
|
| 93 | + <BIN> pipelines list --runtime <runtime> |
| 94 | +
|
| 95 | + <BIN> pipelines list -r <runtime> |
| 96 | + `), |
| 97 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 98 | + ctx := cmd.Context() |
| 99 | + |
| 100 | + filterArgs := model.PipelinesFilterArgs{ |
| 101 | + Name: &name, |
| 102 | + Namespace: &namespace, |
| 103 | + Runtime: &runtime, |
| 104 | + Project: &project, |
| 105 | + } |
| 106 | + return RunPipelineList(ctx, filterArgs) |
| 107 | + }, |
| 108 | + } |
| 109 | + |
| 110 | + cmd.Flags().StringVarP(&name, "name", "n", "", "Filter by pipeline name") |
| 111 | + cmd.Flags().StringVarP(&namespace, "namespace", "N", "", "Filter by pipeline namespace") |
| 112 | + cmd.Flags().StringVarP(&runtime, "runtime", "r", "", "Filter by pipeline runtime") |
| 113 | + cmd.Flags().StringVarP(&project, "project", "p", "", "Filter by pipeline project") |
| 114 | + |
| 115 | + return cmd |
| 116 | +} |
| 117 | + |
| 118 | +func RunPipelineGet(ctx context.Context, name, namespace, runtime string) error { |
| 119 | + pipeline, err := cfConfig.NewClient().V2().Pipeline().Get(ctx, name, namespace, runtime) |
| 120 | + if err != nil { |
| 121 | + return err |
| 122 | + } |
| 123 | + |
| 124 | + if pipeline == nil { |
| 125 | + fields := log.Fields{ |
| 126 | + "name": name, |
| 127 | + "namespace": namespace, |
| 128 | + "runtime": runtime, |
| 129 | + } |
| 130 | + log.G(ctx).WithFields(fields).Warn("pipeline was not found") |
| 131 | + return nil |
| 132 | + } |
| 133 | + |
| 134 | + tb := ansiterm.NewTabWriter(os.Stdout, 0, 0, 4, ' ', 0) |
| 135 | + _, err = fmt.Fprintln(tb, "NAME\tNAMESPACE\tRUNTIME\tHEALTH STATUS\tSYNC STATUS") |
| 136 | + if err != nil { |
| 137 | + return err |
| 138 | + } |
| 139 | + |
| 140 | + healthStatus := "N/A" |
| 141 | + if pipeline.Self.HealthStatus != nil { |
| 142 | + healthStatus = pipeline.Self.HealthStatus.String() |
| 143 | + } |
| 144 | + _, err = fmt.Fprintf(tb, "%s\t%s\t%s\t%s\t%s\n", |
| 145 | + pipeline.Metadata.Name, |
| 146 | + *pipeline.Metadata.Namespace, |
| 147 | + pipeline.Metadata.Runtime, |
| 148 | + healthStatus, |
| 149 | + pipeline.Self.SyncStatus, |
| 150 | + ) |
| 151 | + if err != nil { |
| 152 | + return err |
| 153 | + } |
| 154 | + |
| 155 | + return tb.Flush() |
| 156 | +} |
| 157 | + |
| 158 | +func RunPipelineList(ctx context.Context, filterArgs model.PipelinesFilterArgs) error { |
| 159 | + pipelines, err := cfConfig.NewClient().V2().Pipeline().List(ctx, filterArgs) |
| 160 | + if err != nil { |
| 161 | + return err |
| 162 | + } |
| 163 | + |
| 164 | + if len(pipelines) == 0 { |
| 165 | + log.G(ctx).Warn("no pipelines were found") |
| 166 | + return nil |
| 167 | + } |
| 168 | + |
| 169 | + tb := ansiterm.NewTabWriter(os.Stdout, 0, 0, 4, ' ', 0) |
| 170 | + _, err = fmt.Fprintln(tb, "NAME\tNAMESPACE\tRUNTIME\tHEALTH STATUS\tSYNC STATUS") |
| 171 | + if err != nil { |
| 172 | + return err |
| 173 | + } |
| 174 | + |
| 175 | + for _, p := range pipelines { |
| 176 | + healthStatus := "N/A" |
| 177 | + if p.Self.HealthStatus != nil { |
| 178 | + healthStatus = p.Self.HealthStatus.String() |
| 179 | + } |
| 180 | + _, err = fmt.Fprintf(tb, "%s\t%s\t%s\t%s\t%s\n", |
| 181 | + p.Metadata.Name, |
| 182 | + *p.Metadata.Namespace, |
| 183 | + p.Metadata.Runtime, |
| 184 | + healthStatus, |
| 185 | + p.Self.SyncStatus, |
| 186 | + ) |
| 187 | + if err != nil { |
| 188 | + return err |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + return tb.Flush() |
| 193 | +} |
0 commit comments