Skip to content

Commit 7ce8ebb

Browse files
authored
fix: return an error when a filename is used with the yaml flag (#179)
1 parent 8480d12 commit 7ce8ebb

8 files changed

+127
-46
lines changed

cli/cmd/collector_create.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package cmd
33
import (
44
"fmt"
55
"io/ioutil"
6+
"strings"
67

8+
"github.com/pkg/errors"
79
"github.com/spf13/cobra"
810
)
911

@@ -16,25 +18,29 @@ func (r *runners) InitCollectorCreate(parent *cobra.Command) {
1618
cmd.Hidden = true // Not supported in KOTS
1719
parent.AddCommand(cmd)
1820

19-
cmd.Flags().StringVar(&r.args.createCollectorYaml, "yaml", "", "The YAML config for this collector. Use '-' to read from stdin. Cannot be used with the `yaml-file` flag.")
20-
cmd.Flags().StringVar(&r.args.createCollectorYamlFile, "yaml-file", "", "The file name with YAML config for this collector. Cannot be used with the `yaml` flag.")
21+
cmd.Flags().StringVar(&r.args.createCollectorYaml, "yaml", "", "The YAML config for this collector. Use '-' to read from stdin. Cannot be used with the --yaml-file flag.")
22+
cmd.Flags().StringVar(&r.args.createCollectorYamlFile, "yaml-file", "", "The file name with YAML config for this collector. Cannot be used with the --yaml flag.")
2123
cmd.Flags().StringVar(&r.args.createCollectorName, "name", "", "The name for this collector")
2224

2325
cmd.RunE = r.collectorCreate
2426
}
2527

2628
func (r *runners) collectorCreate(cmd *cobra.Command, args []string) error {
27-
2829
if r.args.createCollectorName == "" {
29-
return fmt.Errorf("collector name is required")
30+
return errors.New("collector name is required")
3031
}
3132

3233
if r.args.createCollectorYaml == "" && r.args.createCollectorYamlFile == "" {
33-
return fmt.Errorf("yaml is required")
34+
return errors.New("one of --yaml or --yaml-file is required")
3435
}
3536

3637
if r.args.createCollectorYaml != "" && r.args.createCollectorYamlFile != "" {
37-
return fmt.Errorf("only one of yaml or yaml-file may be specified")
38+
return errors.New("only one of yaml or yaml-file may be specified")
39+
}
40+
41+
if (strings.HasSuffix(r.args.createCollectorYaml, ".yaml") || strings.HasSuffix(r.args.createCollectorYaml, ".yml")) &&
42+
len(strings.Split(r.args.createCollectorYaml, " ")) == 1 {
43+
return errors.New("use the --yaml-file flag when passing a yaml filename")
3844
}
3945

4046
if r.args.createCollectorYaml == "-" {
@@ -62,5 +68,4 @@ func (r *runners) collectorCreate(cmd *cobra.Command, args []string) error {
6268
r.w.Flush()
6369

6470
return nil
65-
6671
}

cli/cmd/collector_update.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package cmd
22

33
import (
4-
"errors"
54
"fmt"
65
"io/ioutil"
6+
"strings"
77

8+
"github.com/pkg/errors"
89
"github.com/spf13/cobra"
910
)
1011

@@ -17,26 +18,30 @@ func (r *runners) InitCollectorUpdate(parent *cobra.Command) {
1718
cmd.Hidden = true // Not supported in KOTS
1819
parent.AddCommand(cmd)
1920

20-
cmd.Flags().StringVar(&r.args.updateCollectorYaml, "yaml", "", "The new YAML config for this collector. Use '-' to read from stdin. Cannot be used with the `yaml-file` flag.")
21-
cmd.Flags().StringVar(&r.args.updateCollectorYamlFile, "yaml-file", "", "The file name with YAML config for this collector. Cannot be used with the `yaml` flag.")
21+
cmd.Flags().StringVar(&r.args.updateCollectorYaml, "yaml", "", "The new YAML config for this collector. Use '-' to read from stdin. Cannot be used with the --yaml-file` flag.")
22+
cmd.Flags().StringVar(&r.args.updateCollectorYamlFile, "yaml-file", "", "The file name with YAML config for this collector. Cannot be used with the --yaml flag.")
2223
cmd.Flags().StringVar(&r.args.updateCollectorName, "name", "", "The name for this collector")
2324

2425
cmd.RunE = r.collectorUpdate
2526
}
2627

2728
func (r *runners) collectorUpdate(cmd *cobra.Command, args []string) error {
28-
2929
if len(args) < 1 {
3030
return errors.New("collector spec ID is required")
3131
}
3232
specID := args[0]
3333

3434
if r.args.updateCollectorName == "" && r.args.updateCollectorYaml == "" && r.args.updateCollectorYamlFile == "" {
35-
return fmt.Errorf("name or yaml is required")
35+
return errors.New("one of --name, --yaml or --yaml-file is required")
3636
}
3737

3838
if r.args.updateCollectorYaml != "" && r.args.updateCollectorYamlFile != "" {
39-
return fmt.Errorf("only yaml or yaml-file has to be specified")
39+
return errors.New("only one of --yaml or --yaml-file may be specified")
40+
}
41+
42+
if (strings.HasSuffix(r.args.updateCollectorYaml, ".yaml") || strings.HasSuffix(r.args.updateCollectorYaml, ".yml")) &&
43+
len(strings.Split(r.args.updateCollectorYaml, " ")) == 1 {
44+
return errors.New("use the --yaml-file flag when passing a yaml filename")
4045
}
4146

4247
if r.args.updateCollectorYaml == "-" {
@@ -58,14 +63,14 @@ func (r *runners) collectorUpdate(cmd *cobra.Command, args []string) error {
5863
if r.args.updateCollectorYaml != "" {
5964
_, err := r.api.UpdateCollector(r.appID, specID, r.args.updateCollectorYaml)
6065
if err != nil {
61-
return fmt.Errorf("Failure setting updates for collector: %w", err)
66+
return errors.Wrap(err, "failure setting updates for collector")
6267
}
6368
}
6469

6570
if r.args.updateCollectorName != "" {
6671
_, err := r.api.UpdateCollectorName(r.appID, specID, r.args.updateCollectorName)
6772
if err != nil {
68-
return fmt.Errorf("Failure setting new yaml config for collector: %w", err)
73+
return errors.Wrap(err, "failure setting new yaml config for collector")
6974
}
7075
}
7176

cli/cmd/installer_create.go

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,27 @@ package cmd
22

33
import (
44
"fmt"
5+
"io/ioutil"
6+
"strings"
7+
"time"
8+
59
"github.com/pkg/errors"
610
"github.com/replicatedhq/replicated/cli/print"
711
"github.com/spf13/cobra"
8-
"io/ioutil"
9-
"time"
1012
)
1113

1214
func (r *runners) InitInstallerCreate(parent *cobra.Command) {
1315
cmd := &cobra.Command{
14-
Use: "create",
15-
Short: "Create a new installer spec",
16-
Long: `Create a new installer spec by providing YAML configuration for a https://kurl.sh cluster.`,
16+
Use: "create",
17+
Short: "Create a new installer spec",
18+
Long: `Create a new installer spec by providing YAML configuration for a https://kurl.sh cluster.`,
1719
SilenceUsage: true,
1820
}
1921

2022
parent.AddCommand(cmd)
2123

22-
cmd.Flags().StringVar(&r.args.createInstallerYaml, "yaml", "", "The YAML config for this installer. Use '-' to read from stdin. Cannot be used with the `yaml-file` flag.")
23-
cmd.Flags().StringVar(&r.args.createInstallerYamlFile, "yaml-file", "", "The file name with YAML config for this installer. Cannot be used with the `yaml` flag.")
24+
cmd.Flags().StringVar(&r.args.createInstallerYaml, "yaml", "", "The YAML config for this installer. Use '-' to read from stdin. Cannot be used with the --yaml-file flag.")
25+
cmd.Flags().StringVar(&r.args.createInstallerYamlFile, "yaml-file", "", "The file name with YAML config for this installer. Cannot be used with the --yaml flag.")
2426
cmd.Flags().StringVar(&r.args.createInstallerPromote, "promote", "", "Channel name or id to promote this installer to")
2527
cmd.Flags().BoolVar(&r.args.createInstallerPromoteEnsureChannel, "ensure-channel", false, "When used with --promote <channel>, will create the channel if it doesn't exist")
2628
cmd.Flags().BoolVar(&r.args.createInstallerAutoDefaults, "auto", false, "generate default values for use in CI")
@@ -30,7 +32,6 @@ func (r *runners) InitInstallerCreate(parent *cobra.Command) {
3032
}
3133

3234
func (r *runners) setKOTSDefaultInstallerParams() error {
33-
3435
if r.args.createInstallerYamlFile == "" {
3536
r.args.createInstallerYamlFile = "./kurl-installer.yaml"
3637
}
@@ -90,18 +91,23 @@ Prepared to create release with defaults:
9091

9192
if r.args.createInstallerYaml == "" &&
9293
r.args.createInstallerYamlFile == "" {
93-
return errors.New("one of --yaml, --yaml-file is required")
94+
return errors.New("one of --yaml or --yaml-file is required")
95+
}
96+
97+
if r.args.createInstallerYaml != "" && r.args.createInstallerYamlFile != "" {
98+
return errors.New("only one of --yaml or --yaml-file may be specified")
99+
}
100+
101+
if (strings.HasSuffix(r.args.createInstallerYaml, ".yaml") || strings.HasSuffix(r.args.createInstallerYaml, ".yml")) &&
102+
len(strings.Split(r.args.createInstallerYaml, " ")) == 1 {
103+
return errors.New("use the --yaml-file flag when passing a yaml filename")
94104
}
95105

96106
// can't ensure a channel if you didn't pass one
97107
if r.args.createInstallerPromoteEnsureChannel && r.args.createInstallerPromote == "" {
98108
return errors.New("cannot use the flag --ensure-channel without also using --promote <channel> ")
99109
}
100110

101-
if r.args.createInstallerYaml != "" && r.args.createInstallerYamlFile != "" {
102-
return errors.New("only one of --yaml or --yaml-file may be specified")
103-
}
104-
105111
if r.args.createInstallerYaml == "-" {
106112
bytes, err := ioutil.ReadAll(r.stdin)
107113
if err != nil {

cli/cmd/release_create.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ func (r *runners) InitReleaseCreate(parent *cobra.Command) error {
4141

4242
parent.AddCommand(cmd)
4343

44-
cmd.Flags().StringVar(&r.args.createReleaseYaml, "yaml", "", "The YAML config for this release. Use '-' to read from stdin. Cannot be used with the `yaml-file` flag.")
45-
cmd.Flags().StringVar(&r.args.createReleaseYamlFile, "yaml-file", "", "The file name with YAML config for this release. Cannot be used with the `yaml` flag.")
46-
cmd.Flags().StringVar(&r.args.createReleaseYamlDir, "yaml-dir", "", "The directory containing multiple yamls for a Kots release. Cannot be used with the `yaml` flag.")
44+
cmd.Flags().StringVar(&r.args.createReleaseYaml, "yaml", "", "The YAML config for this release. Use '-' to read from stdin. Cannot be used with the --yaml-file flag.")
45+
cmd.Flags().StringVar(&r.args.createReleaseYamlFile, "yaml-file", "", "The file name with YAML config for this release. Cannot be used with the --yaml flag.")
46+
cmd.Flags().StringVar(&r.args.createReleaseYamlDir, "yaml-dir", "", "The directory containing multiple yamls for a Kots release. Cannot be used with the --yaml flag.")
4747
cmd.Flags().StringVar(&r.args.createReleasePromote, "promote", "", "Channel name or id to promote this release to")
4848
cmd.Flags().StringVar(&r.args.createReleasePromoteNotes, "release-notes", "", "When used with --promote <channel>, sets the **markdown** release notes")
4949
cmd.Flags().StringVar(&r.args.createReleasePromoteVersion, "version", "", "When used with --promote <channel>, sets the version label for the release in this channel")
@@ -102,7 +102,6 @@ func (r *runners) gitSHABranch() (sha string, branch string, dirty bool, err err
102102
}
103103

104104
func (r *runners) setKOTSDefaultReleaseParams() error {
105-
106105
if r.args.createReleaseYamlDir == "" {
107106
r.args.createReleaseYamlDir = "./manifests"
108107
}
@@ -152,12 +151,11 @@ func (r *runners) setKOTSDefaultReleaseParams() error {
152151

153152
r.args.createReleasePromoteEnsureChannel = true
154153
r.args.createReleaseLint = true
155-
154+
156155
return nil
157156
}
158157

159158
func (r *runners) releaseCreate(cmd *cobra.Command, args []string) error {
160-
161159
log := print.NewLogger(r.w)
162160

163161
if r.appType == "kots" && r.args.createReleaseAutoDefaults {
@@ -286,13 +284,18 @@ Prepared to create release with defaults:
286284

287285
func (r *runners) validateReleaseCreateParams() error {
288286
if r.args.createReleaseYaml == "" && r.args.createReleaseYamlFile == "" && r.appType != "kots" {
289-
return errors.New("one of --yaml, --yaml-file must be provided")
287+
return errors.New("one of --yaml or --yaml-file must be provided")
290288
}
291289

292290
if r.args.createReleaseYaml != "" && r.args.createReleaseYamlFile != "" {
293291
return errors.New("only one of --yaml or --yaml-file may be specified")
294292
}
295293

294+
if (strings.HasSuffix(r.args.createReleaseYaml, ".yaml") || strings.HasSuffix(r.args.createReleaseYaml, ".yml")) &&
295+
len(strings.Split(r.args.createReleaseYaml, " ")) == 1 {
296+
return errors.New("use the --yaml-file flag when passing a yaml filename")
297+
}
298+
296299
if r.args.createReleaseYamlDir == "" && r.appType == "kots" {
297300
return errors.New("--yaml-dir flag must be provided for KOTS applications")
298301
}
@@ -323,7 +326,6 @@ func (r *runners) validateReleaseCreateParams() error {
323326
}
324327

325328
func (r *runners) getOrCreateChannelForPromotion(channelName string, createIfAbsent bool) (string, error) {
326-
327329
description := "" // todo: do we want a flag for the desired channel description
328330

329331
channel, err := r.api.GetOrCreateChannelByName(
@@ -384,7 +386,6 @@ func encodeKotsFile(prefix, path string, info os.FileInfo, err error) (*kotsSing
384386
}
385387

386388
func readYAMLDir(yamlDir string) (string, error) {
387-
388389
var allKotsReleaseSpecs []kotsSingleSpec
389390
err := filepath.Walk(yamlDir, func(path string, info os.FileInfo, err error) error {
390391
spec, err := encodeKotsFile(yamlDir, path, info, err)
@@ -408,7 +409,6 @@ func readYAMLDir(yamlDir string) (string, error) {
408409
}
409410

410411
func promptForConfirm() (string, error) {
411-
412412
prompt := promptui.Prompt{
413413
Label: "Create with these properties? (default Yes) [Y/n]",
414414
Templates: templates,

cli/cmd/release_update.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"io/ioutil"
66
"strconv"
7+
"strings"
78

89
"github.com/pkg/errors"
910
"github.com/spf13/cobra"
@@ -18,20 +19,25 @@ func (r *runners) InitReleaseUpdate(parent *cobra.Command) {
1819

1920
parent.AddCommand(cmd)
2021

21-
cmd.Flags().StringVar(&r.args.updateReleaseYaml, "yaml", "", "The new YAML config for this release. Use '-' to read from stdin. Cannot be used with the `yaml-file` flag.")
22-
cmd.Flags().StringVar(&r.args.updateReleaseYamlFile, "yaml-file", "", "The file name with YAML config for this release. Cannot be used with the `yaml` flag.")
23-
cmd.Flags().StringVar(&r.args.updateReleaseYamlDir, "yaml-dir", "", "The directory containing multiple yamls for a Kots release. Cannot be used with the `yaml` flag.")
22+
cmd.Flags().StringVar(&r.args.updateReleaseYaml, "yaml", "", "The new YAML config for this release. Use '-' to read from stdin. Cannot be used with the --yaml-file flag.")
23+
cmd.Flags().StringVar(&r.args.updateReleaseYamlFile, "yaml-file", "", "The file name with YAML config for this release. Cannot be used with the --yaml flag.")
24+
cmd.Flags().StringVar(&r.args.updateReleaseYamlDir, "yaml-dir", "", "The directory containing multiple yamls for a Kots release. Cannot be used with the --yaml flag.")
2425

2526
cmd.RunE = r.releaseUpdate
2627
}
2728

2829
func (r *runners) releaseUpdate(cmd *cobra.Command, args []string) error {
2930
if r.args.updateReleaseYaml == "" && r.args.updateReleaseYamlFile == "" && r.args.updateReleaseYamlDir == "" {
30-
return fmt.Errorf("yaml is required")
31+
return errors.New("one of --yaml or --yaml-file is required")
3132
}
3233

3334
if r.args.updateReleaseYaml != "" && r.args.updateReleaseYamlFile != "" {
34-
return fmt.Errorf("only yaml or yaml-file has to be specified")
35+
return errors.New("only one of --yaml or --yaml-file may be specified")
36+
}
37+
38+
if (strings.HasSuffix(r.args.updateReleaseYaml, ".yaml") || strings.HasSuffix(r.args.updateReleaseYaml, ".yml")) &&
39+
len(strings.Split(r.args.updateReleaseYaml, " ")) == 1 {
40+
return errors.New("use the --yaml-file flag when passing a yaml filename")
3541
}
3642

3743
if r.args.updateReleaseYaml == "-" {
@@ -55,7 +61,7 @@ func (r *runners) releaseUpdate(cmd *cobra.Command, args []string) error {
5561
}
5662
seq, err := strconv.ParseInt(args[0], 10, 64)
5763
if err != nil {
58-
return fmt.Errorf("invalid release sequence: %s", args[0])
64+
return errors.Errorf("invalid release sequence: %s", args[0])
5965
}
6066

6167
if r.args.updateReleaseYamlDir != "" {
@@ -65,7 +71,7 @@ func (r *runners) releaseUpdate(cmd *cobra.Command, args []string) error {
6571
}
6672
}
6773
if err := r.api.UpdateRelease(r.appID, r.appType, seq, r.args.updateReleaseYaml); err != nil {
68-
return fmt.Errorf("Failure setting new yaml config for release: %w", err)
74+
return errors.Wrap(err, "failure setting new yaml config for release")
6975
}
7076

7177
// ignore the error since operation was successful

cli/test/kots_installer_create_test.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package test
22

33
import (
44
"bytes"
5-
"github.com/replicatedhq/replicated/pkg/kotsclient"
65
"io/ioutil"
76
"os"
87
"path/filepath"
98

9+
"github.com/replicatedhq/replicated/pkg/kotsclient"
10+
1011
. "github.com/onsi/ginkgo"
1112
"github.com/replicatedhq/replicated/cli/cmd"
1213
"github.com/replicatedhq/replicated/pkg/platformclient"
@@ -51,6 +52,8 @@ var _ = Describe("kots installer create", func() {
5152
kind: Installer
5253
metadata:
5354
name: 'myapp'
55+
help_text: |
56+
Please check this file exists in root directory: config.yaml
5457
spec:
5558
kubernetes:
5659
version: latest
@@ -86,4 +89,22 @@ spec:
8689
req.Contains(stdout.String(), `successfully set to installer 2`)
8790
})
8891
})
92+
93+
Context("error case using --yaml flag with yaml filename", func() {
94+
It("should return an error telling user to use --yaml-file flag", func() {
95+
var stdout bytes.Buffer
96+
var stderr bytes.Buffer
97+
98+
rootCmd := cmd.GetRootCmd()
99+
rootCmd.SetArgs([]string{"installer", "create", "--yaml", "installer.yaml", "--app", app.Slug, "--promote", "Unstable"})
100+
101+
err = cmd.Execute(rootCmd, nil, &stdout, &stderr)
102+
assert.NotNil(t, err)
103+
104+
req.Empty(stderr.String(), "Expected no stderr output")
105+
req.NotEmpty(stdout.String(), "Expected stdout output")
106+
107+
req.Contains(stdout.String(), `use the --yaml-file flag when passing a yaml filename`)
108+
})
109+
})
89110
})

cli/test/release_create_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,22 @@ var _ = Describe("release create", func() {
8989
assert.Contains(t, stdout.String(), "SEQUENCE: 1")
9090
})
9191
})
92+
93+
Context("error case using --yaml flag with yaml filename", func() {
94+
It("should return an error telling user to use --yaml-file flag", func() {
95+
var stdout bytes.Buffer
96+
var stderr bytes.Buffer
97+
98+
rootCmd := cmd.GetRootCmd()
99+
rootCmd.SetArgs([]string{"release", "create", "--yaml", "installer.yaml", "--app", app.Slug})
100+
101+
err := cmd.Execute(rootCmd, nil, &stdout, &stderr)
102+
assert.NotNil(t, err)
103+
104+
assert.Empty(t, stderr.String(), "Expected no stderr output")
105+
assert.NotEmpty(t, stdout.String(), "Expected stdout output")
106+
107+
assert.Contains(t, stdout.String(), `use the --yaml-file flag when passing a yaml filename`)
108+
})
109+
})
92110
})

0 commit comments

Comments
 (0)