|
| 1 | +package buildstrategy |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "text/tabwriter" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/spf13/cobra" |
| 10 | + k8serrors "k8s.io/apimachinery/pkg/api/errors" |
| 11 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 12 | + "k8s.io/apimachinery/pkg/util/duration" |
| 13 | + "k8s.io/cli-runtime/pkg/genericclioptions" |
| 14 | + |
| 15 | + "github.com/shipwright-io/cli/pkg/shp/cmd/runner" |
| 16 | + "github.com/shipwright-io/cli/pkg/shp/params" |
| 17 | +) |
| 18 | + |
| 19 | +// ListCommand contains data input from user for list sub-command |
| 20 | +type ListCommand struct { |
| 21 | + cmd *cobra.Command |
| 22 | + noHeader bool |
| 23 | +} |
| 24 | + |
| 25 | +func listCmd() runner.SubCommand { |
| 26 | + c := &ListCommand{ |
| 27 | + cmd: &cobra.Command{ |
| 28 | + Use: "list [flags]", |
| 29 | + Short: "List BuildStrategies in the current namespace", |
| 30 | + }, |
| 31 | + } |
| 32 | + c.cmd.Flags().BoolVar(&c.noHeader, "no-header", false, "Do not print the table header") |
| 33 | + return c |
| 34 | +} |
| 35 | + |
| 36 | +// Cmd returns cobra command object |
| 37 | +func (c *ListCommand) Cmd() *cobra.Command { |
| 38 | + return c.cmd |
| 39 | +} |
| 40 | + |
| 41 | +// Complete fills in data provided by user |
| 42 | +func (c *ListCommand) Complete(_ *params.Params, _ *genericclioptions.IOStreams, _ []string) error { |
| 43 | + return nil |
| 44 | +} |
| 45 | + |
| 46 | +// Validate validates data input by user |
| 47 | +func (c *ListCommand) Validate() error { |
| 48 | + return nil |
| 49 | +} |
| 50 | + |
| 51 | +// Run executes list sub-command logic |
| 52 | +func (c *ListCommand) Run(p *params.Params, io *genericclioptions.IOStreams) error { |
| 53 | + w := tabwriter.NewWriter(os.Stdout, 0, 8, 2, '\t', 0) |
| 54 | + if !c.noHeader { |
| 55 | + fmt.Fprintln(w, "NAME\tAGE") |
| 56 | + } |
| 57 | + |
| 58 | + cs, err := p.ShipwrightClientSet() |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + k8s, err := p.ClientSet() |
| 63 | + if err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + |
| 67 | + ns := p.Namespace() |
| 68 | + if _, err = k8s.CoreV1().Namespaces().Get(c.cmd.Context(), ns, metav1.GetOptions{}); err != nil { |
| 69 | + if k8serrors.IsNotFound(err) { |
| 70 | + fmt.Fprintf(io.Out, "Namespace '%s' not found.\n", ns) |
| 71 | + return nil |
| 72 | + } |
| 73 | + return err |
| 74 | + } |
| 75 | + |
| 76 | + list, err := cs.ShipwrightV1beta1().BuildStrategies(ns).List(c.cmd.Context(), metav1.ListOptions{}) |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + if len(list.Items) == 0 { |
| 81 | + fmt.Fprintf(io.Out, "No BuildStrategies found in namespace '%s'.\n", ns) |
| 82 | + return nil |
| 83 | + } |
| 84 | + |
| 85 | + now := time.Now() |
| 86 | + for _, bs := range list.Items { |
| 87 | + age := duration.ShortHumanDuration(now.Sub(bs.CreationTimestamp.Time)) |
| 88 | + fmt.Fprintf(w, "%s\t%s\n", bs.Name, age) |
| 89 | + } |
| 90 | + return w.Flush() |
| 91 | +} |
0 commit comments