Skip to content

Commit

Permalink
Change buildrun create command to have random name
Browse files Browse the repository at this point in the history
There could be the use case that a user will run multiple buildruns
while due to frequent changes during development cycles. It can be
cumbersome to come up with a unused name for the buildrun every time.

Change code to accept either one or no buildrun name. In case none is
provided, come up with a random name for the buildrun.
  • Loading branch information
HeavyWombat committed Jul 14, 2021
1 parent 69933ac commit fd53f8f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
19 changes: 18 additions & 1 deletion pkg/shp/cmd/buildrun/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package buildrun

import (
"fmt"
"strings"

buildv1alpha1 "github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
"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"
"github.com/shipwright-io/cli/pkg/shp/resource"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/util/rand"
"k8s.io/cli-runtime/pkg/genericclioptions"
)

Expand All @@ -35,11 +37,26 @@ func (c *CreateCommand) Cmd() *cobra.Command {
// Complete checks if the arguments is informing the BuildRun name.
func (c *CreateCommand) Complete(params *params.Params, args []string) error {
switch len(args) {
case 0:
var randomName = func(name, suffix string) string {
return fmt.Sprintf("%s%s-%s", name, suffix, rand.String(5))
}

switch {
case strings.HasSuffix(c.buildRunSpec.BuildRef.Name, "build"):
c.name = randomName(c.buildRunSpec.BuildRef.Name, "run")

default:
c.name = randomName(c.buildRunSpec.BuildRef.Name, "-buildrun")
}

case 1:
c.name = args[0]

default:
return fmt.Errorf("wrong amount of arguments, expected only one")
return fmt.Errorf("wrong amount of arguments, expected only one (specific name) or none (random name)")
}

return nil
}

Expand Down
1 change: 1 addition & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ k8s.io/apimachinery/pkg/util/json
k8s.io/apimachinery/pkg/util/mergepatch
k8s.io/apimachinery/pkg/util/naming
k8s.io/apimachinery/pkg/util/net
k8s.io/apimachinery/pkg/util/rand
k8s.io/apimachinery/pkg/util/runtime
k8s.io/apimachinery/pkg/util/sets
k8s.io/apimachinery/pkg/util/strategicpatch
Expand Down

0 comments on commit fd53f8f

Please sign in to comment.