Skip to content

Commit 59a465f

Browse files
Merge pull request #327 from arc9tek/ak/220/build-strategies
feat(cmd): add buildstrategy & clusterbuildstrategy list/delete
2 parents 5207b75 + 8beb532 commit 59a465f

File tree

15 files changed

+586
-46
lines changed

15 files changed

+586
-46
lines changed

docs/shp.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,7 @@ shp [command] [resource] [flags]
1919

2020
* [shp build](shp_build.md) - Manage Builds
2121
* [shp buildrun](shp_buildrun.md) - Manage BuildRuns
22+
* [shp buildstrategy](shp_buildstrategy.md) - Manage namespaced BuildStrategies
23+
* [shp clusterbuildstrategy](shp_clusterbuildstrategy.md) - Manage cluster-scoped BuildStrategies
2224
* [shp version](shp_version.md) - version
2325

docs/shp_buildstrategy.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## shp buildstrategy
2+
3+
Manage namespaced BuildStrategies
4+
5+
```
6+
shp buildstrategy [flags]
7+
```
8+
9+
### Options
10+
11+
```
12+
-h, --help help for buildstrategy
13+
```
14+
15+
### Options inherited from parent commands
16+
17+
```
18+
--kubeconfig string Path to the kubeconfig file to use for CLI requests.
19+
-n, --namespace string If present, the namespace scope for this CLI request
20+
--request-timeout string The length of time to wait before giving up on a single server request. Non-zero values should contain a corresponding time unit (e.g. 1s, 2m, 3h). A value of zero means don't timeout requests. (default "0")
21+
```
22+
23+
### SEE ALSO
24+
25+
* [shp](shp.md) - Command-line client for Shipwright's Build API.
26+
* [shp buildstrategy delete](shp_buildstrategy_delete.md) - Delete a BuildStrategy in the current namespace
27+
* [shp buildstrategy list](shp_buildstrategy_list.md) - List BuildStrategies in the current namespace
28+

docs/shp_buildstrategy_delete.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## shp buildstrategy delete
2+
3+
Delete a BuildStrategy in the current namespace
4+
5+
```
6+
shp buildstrategy delete <name> [flags]
7+
```
8+
9+
### Options
10+
11+
```
12+
-h, --help help for delete
13+
```
14+
15+
### Options inherited from parent commands
16+
17+
```
18+
--kubeconfig string Path to the kubeconfig file to use for CLI requests.
19+
-n, --namespace string If present, the namespace scope for this CLI request
20+
--request-timeout string The length of time to wait before giving up on a single server request. Non-zero values should contain a corresponding time unit (e.g. 1s, 2m, 3h). A value of zero means don't timeout requests. (default "0")
21+
```
22+
23+
### SEE ALSO
24+
25+
* [shp buildstrategy](shp_buildstrategy.md) - Manage namespaced BuildStrategies
26+

docs/shp_buildstrategy_list.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## shp buildstrategy list
2+
3+
List BuildStrategies in the current namespace
4+
5+
```
6+
shp buildstrategy list [flags]
7+
```
8+
9+
### Options
10+
11+
```
12+
-h, --help help for list
13+
--no-header Do not print the table header
14+
```
15+
16+
### Options inherited from parent commands
17+
18+
```
19+
--kubeconfig string Path to the kubeconfig file to use for CLI requests.
20+
-n, --namespace string If present, the namespace scope for this CLI request
21+
--request-timeout string The length of time to wait before giving up on a single server request. Non-zero values should contain a corresponding time unit (e.g. 1s, 2m, 3h). A value of zero means don't timeout requests. (default "0")
22+
```
23+
24+
### SEE ALSO
25+
26+
* [shp buildstrategy](shp_buildstrategy.md) - Manage namespaced BuildStrategies
27+

docs/shp_clusterbuildstrategy.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## shp clusterbuildstrategy
2+
3+
Manage cluster-scoped BuildStrategies
4+
5+
```
6+
shp clusterbuildstrategy [flags]
7+
```
8+
9+
### Options
10+
11+
```
12+
-h, --help help for clusterbuildstrategy
13+
```
14+
15+
### Options inherited from parent commands
16+
17+
```
18+
--kubeconfig string Path to the kubeconfig file to use for CLI requests.
19+
-n, --namespace string If present, the namespace scope for this CLI request
20+
--request-timeout string The length of time to wait before giving up on a single server request. Non-zero values should contain a corresponding time unit (e.g. 1s, 2m, 3h). A value of zero means don't timeout requests. (default "0")
21+
```
22+
23+
### SEE ALSO
24+
25+
* [shp](shp.md) - Command-line client for Shipwright's Build API.
26+
* [shp clusterbuildstrategy delete](shp_clusterbuildstrategy_delete.md) - Delete a ClusterBuildStrategy
27+
* [shp clusterbuildstrategy list](shp_clusterbuildstrategy_list.md) - List ClusterBuildStrategies
28+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## shp clusterbuildstrategy delete
2+
3+
Delete a ClusterBuildStrategy
4+
5+
```
6+
shp clusterbuildstrategy delete <name> [flags]
7+
```
8+
9+
### Options
10+
11+
```
12+
-h, --help help for delete
13+
```
14+
15+
### Options inherited from parent commands
16+
17+
```
18+
--kubeconfig string Path to the kubeconfig file to use for CLI requests.
19+
-n, --namespace string If present, the namespace scope for this CLI request
20+
--request-timeout string The length of time to wait before giving up on a single server request. Non-zero values should contain a corresponding time unit (e.g. 1s, 2m, 3h). A value of zero means don't timeout requests. (default "0")
21+
```
22+
23+
### SEE ALSO
24+
25+
* [shp clusterbuildstrategy](shp_clusterbuildstrategy.md) - Manage cluster-scoped BuildStrategies
26+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## shp clusterbuildstrategy list
2+
3+
List ClusterBuildStrategies
4+
5+
```
6+
shp clusterbuildstrategy list [flags]
7+
```
8+
9+
### Options
10+
11+
```
12+
-h, --help help for list
13+
--no-header Omit table header
14+
```
15+
16+
### Options inherited from parent commands
17+
18+
```
19+
--kubeconfig string Path to the kubeconfig file to use for CLI requests.
20+
-n, --namespace string If present, the namespace scope for this CLI request
21+
--request-timeout string The length of time to wait before giving up on a single server request. Non-zero values should contain a corresponding time unit (e.g. 1s, 2m, 3h). A value of zero means don't timeout requests. (default "0")
22+
```
23+
24+
### SEE ALSO
25+
26+
* [shp clusterbuildstrategy](shp_clusterbuildstrategy.md) - Manage cluster-scoped BuildStrategies
27+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package buildstrategy
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
"k8s.io/cli-runtime/pkg/genericclioptions"
6+
7+
"github.com/shipwright-io/cli/pkg/shp/cmd/runner"
8+
"github.com/shipwright-io/cli/pkg/shp/params"
9+
)
10+
11+
// Command represents "shp buildstrategy".
12+
func Command(p *params.Params, io *genericclioptions.IOStreams) *cobra.Command {
13+
cmd := &cobra.Command{
14+
Use: "buildstrategy",
15+
Aliases: []string{"bs"},
16+
Short: "Manage namespaced BuildStrategies",
17+
Annotations: map[string]string{
18+
"commandType": "main",
19+
},
20+
}
21+
22+
cmd.AddCommand(
23+
runner.NewRunner(p, io, listCmd()).Cmd(),
24+
runner.NewRunner(p, io, deleteCmd()).Cmd(),
25+
)
26+
27+
return cmd
28+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package buildstrategy
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8+
"k8s.io/cli-runtime/pkg/genericclioptions"
9+
10+
"github.com/shipwright-io/cli/pkg/shp/cmd/runner"
11+
"github.com/shipwright-io/cli/pkg/shp/params"
12+
)
13+
14+
// DeleteCommand contains data input from user for delete sub-command
15+
type DeleteCommand struct {
16+
cmd *cobra.Command
17+
name string
18+
}
19+
20+
func deleteCmd() runner.SubCommand {
21+
return &DeleteCommand{
22+
cmd: &cobra.Command{
23+
Use: "delete <name>",
24+
Short: "Delete a BuildStrategy in the current namespace",
25+
Args: cobra.ExactArgs(1),
26+
},
27+
}
28+
}
29+
30+
// Cmd returns cobra command object
31+
func (c *DeleteCommand) Cmd() *cobra.Command { return c.cmd }
32+
33+
// Complete fills in data provided by user
34+
func (c *DeleteCommand) Complete(_ *params.Params, _ *genericclioptions.IOStreams, args []string) error {
35+
c.name = args[0]
36+
return nil
37+
}
38+
39+
// Validate validates data input by user
40+
func (c *DeleteCommand) Validate() error { return nil }
41+
42+
// Run executes delete sub-command logic
43+
func (c *DeleteCommand) Run(p *params.Params, io *genericclioptions.IOStreams) error {
44+
cs, err := p.ShipwrightClientSet()
45+
if err != nil {
46+
return err
47+
}
48+
if err := cs.ShipwrightV1beta1().
49+
BuildStrategies(p.Namespace()).
50+
Delete(c.cmd.Context(), c.name, metav1.DeleteOptions{}); err != nil {
51+
return err
52+
}
53+
fmt.Fprintf(io.Out, "BuildStrategy deleted '%s'\n", c.name)
54+
return nil
55+
}

pkg/shp/cmd/buildstrategy/list.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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

Comments
 (0)