Skip to content

Commit 523a7dd

Browse files
feat(generate-config): add overwrite flag and change default env file (#1604)
1 parent bf93ff4 commit 523a7dd

File tree

5 files changed

+25
-7
lines changed

5 files changed

+25
-7
lines changed

docs/commands/rhoas_generate-config.md

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/cmd/generate/build-configs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func BuildConfiguration(svcConfig *servicecontext.ServiceConfig, opts *options)
115115
configurations.TokenURL = providerUrls.GetTokenUrl()
116116
configurations.Name = configInstanceName
117117

118-
if err = WriteConfig(opts.configType, opts.fileName, configurations); err != nil {
118+
if err = WriteConfig(opts, configurations); err != nil {
119119
return err
120120
}
121121

pkg/cmd/generate/configurations.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"io/ioutil"
77
"os"
88
"path/filepath"
9+
10+
"github.com/redhat-developer/app-services-cli/pkg/core/localize"
911
)
1012

1113
// configuration types for generate-config command
@@ -27,28 +29,35 @@ var (
2729

2830
// WriteConfig saves the configurations to a file
2931
// in the specified output format
30-
func WriteConfig(configType string, filePath string, config *configValues) error {
32+
func WriteConfig(opts *options, config *configValues) error {
3133

3234
var fileBody bytes.Buffer
33-
fileTemplate := getFileFormat(configType)
35+
fileTemplate := getFileFormat(opts.configType)
3436
err := fileTemplate.Execute(&fileBody, config)
3537
if err != nil {
3638
return err
3739
}
3840

3941
fileData := []byte(fileBody.String())
40-
if filePath == "" {
41-
filePath = getDefaultPath(configType)
42+
if opts.fileName == "" {
43+
opts.fileName = getDefaultPath(opts.configType)
44+
}
45+
46+
// If the file already exists, and the --overwrite flag is not set then return an error
47+
// indicating that the user should explicitly request overwriting of the file
48+
_, err = os.Stat(opts.fileName)
49+
if err == nil && !opts.overwrite {
50+
return opts.localizer.MustLocalizeError("generate.error.configFileAlreadyExists", localize.NewEntry("FilePath", opts.fileName))
4251
}
4352

44-
return ioutil.WriteFile(filePath, fileData, 0o600)
53+
return ioutil.WriteFile(opts.fileName, fileData, 0o600)
4554
}
4655

4756
// getDefaultPath returns the default absolute path for the configuration file
4857
func getDefaultPath(configType string) (filePath string) {
4958
switch configType {
5059
case envFormat:
51-
filePath = "rhoas.env"
60+
filePath = ".env"
5261
case propertiesFormat:
5362
filePath = "rhoas.properties"
5463
case jsonFormat:

pkg/cmd/generate/generate-config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type options struct {
2727
name string
2828
fileName string
2929
configType string
30+
overwrite bool
3031
}
3132

3233
// NewGenerateCommand creates configuration files for service context
@@ -61,6 +62,7 @@ func NewGenerateCommand(f *factory.Factory) *cobra.Command {
6162
flags := contextcmdutil.NewFlagSet(cmd, f)
6263
flags.AddContextName(&opts.name)
6364
flags.StringVar(&opts.configType, "type", "", opts.localizer.MustLocalize("generate.flag.type"))
65+
cmd.Flags().BoolVar(&opts.overwrite, "overwrite", false, opts.localizer.MustLocalize("generate.flag.overwrite.description"))
6466
cmd.Flags().StringVar(&opts.fileName, "output-file", "", opts.localizer.MustLocalize("generate.common.flag.fileLocation.description"))
6567
_ = cmd.MarkFlagRequired("type")
6668

pkg/core/localize/locales/en/cmd/generate_config.en.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,16 @@ $ rhoas generate-config --name qaprod --type secret
2828
[generate.flag.type]
2929
one='Type of configuration file to be generated'
3030

31+
[generate.flag.overwrite.description]
32+
one = 'Forcibly overwrite a configuration file if it already exists'
33+
3134
[generate.common.flag.fileLocation.description]
3235
description = 'Description for --output-file flag'
3336
one = 'Sets a custom file location to save the configurations'
3437

38+
[generate.error.configFileAlreadyExists]
39+
description = 'Error message for when a configuration file alredy exists at a location'
40+
one = 'file {{.FilePath}} already exists. Use --overwrite to overwrite the file, or the --output-file flag to choose a different location'
3541

3642
[generate.log.info.noSevices]
3743
one='No services available to generate configurations'

0 commit comments

Comments
 (0)