-
Notifications
You must be signed in to change notification settings - Fork 26
/
create.go
98 lines (81 loc) · 2.75 KB
/
create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package buildrun
import (
"fmt"
buildv1alpha1 "github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
"github.com/shipwright-io/cli/pkg/shp/cmd/runner"
"github.com/shipwright-io/cli/pkg/shp/flags"
"github.com/shipwright-io/cli/pkg/shp/params"
)
// CreateCommand reprents the build's create subcommand.
type CreateCommand struct {
cmd *cobra.Command // cobra command instance
name string // buildrun name
buildRunSpec *buildv1alpha1.BuildRunSpec // stores command-line flags
}
const buildRunCreateLongDesc = `
Creates a new BuildRun instance using the given name, and requires --buildref-name to
find the Build object. Example:
$ shp buildrun create my-app-build --buildref-name="..."
`
// Cmd returns cobra.Command object of the create sub-command.
func (c *CreateCommand) Cmd() *cobra.Command {
return c.cmd
}
// Complete checks if the arguments is informing the BuildRun name.
func (c *CreateCommand) Complete(_ *params.Params, _ *genericclioptions.IOStreams, args []string) error {
switch len(args) {
case 1:
c.name = args[0]
default:
return fmt.Errorf("wrong amount of arguments, expected only one")
}
return nil
}
// Validate makes sure a name is informed.
func (c *CreateCommand) Validate() error {
if c.name == "" {
return fmt.Errorf("name is not informed")
}
return nil
}
// Run executes the creation of BuildRun object.
func (c *CreateCommand) Run(params *params.Params, ioStreams *genericclioptions.IOStreams) error {
br := &buildv1alpha1.BuildRun{
ObjectMeta: metav1.ObjectMeta{
Name: c.name,
},
Spec: *c.buildRunSpec,
}
flags.SanitizeBuildRunSpec(&br.Spec)
clientset, err := params.ShipwrightClientSet()
if err != nil {
return err
}
if _, err = clientset.ShipwrightV1alpha1().BuildRuns(params.Namespace()).Create(c.cmd.Context(), br, metav1.CreateOptions{}); err != nil {
return err
}
fmt.Fprintf(ioStreams.Out, "BuildRun created %q for Build %q\n", c.name, br.Spec.BuildRef.Name)
return nil
}
// createCmd instantiate a new CreateCommand, by wiring it as a cobra.Command and registering the
// flags and marking flags required.
func createCmd() runner.SubCommand {
cmd := &cobra.Command{
Use: "create <name> [flags]",
Short: "Creates a BuildRun instance.",
Long: buildRunCreateLongDesc,
}
// instantiating command-line flags, using an actual BuildRunSpec object to receive the flags
// issued on command-line, also marking flags as required
buildRunSpecFlags := flags.BuildRunSpecFromFlags(cmd.Flags())
if err := cmd.MarkFlagRequired(flags.BuildrefNameFlag); err != nil {
panic(err)
}
return &CreateCommand{
cmd: cmd,
buildRunSpec: buildRunSpecFlags,
}
}