Skip to content

Commit

Permalink
fix: create folder for the initial config (#806)
Browse files Browse the repository at this point in the history
  • Loading branch information
wtrocki authored Jul 7, 2021
1 parent 5de7a50 commit e1f5747
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
34 changes: 27 additions & 7 deletions cmd/rhoas/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/redhat-developer/app-services-cli/pkg/api/kas"
"github.com/redhat-developer/app-services-cli/pkg/doc"
"github.com/redhat-developer/app-services-cli/pkg/dump"
"github.com/redhat-developer/app-services-cli/pkg/localize"
"github.com/redhat-developer/app-services-cli/pkg/localize/goi18n"

"github.com/redhat-developer/app-services-cli/pkg/cmdutil"
Expand Down Expand Up @@ -40,7 +41,11 @@ func main() {
os.Exit(1)
}

initConfig(cmdFactory)
err = initConfig(cmdFactory)
if err != nil {
logger.Errorf(localizer.MustLocalize("main.config.error", localize.NewEntry("Error", err)))
os.Exit(1)
}

rootCmd := root.NewRootCommand(cmdFactory, buildVersion)

Expand Down Expand Up @@ -94,20 +99,35 @@ func generateDocumentation(rootCommand *cobra.Command) {
}
}

func initConfig(f *factory.Factory) {
func initConfig(f *factory.Factory) error {
if !config.HasCustomLocation() {
rhoasCfgDir, err := config.DefaultDir()
if err != nil {
return err
}

// create rhoas config directory
if _, err = os.Stat(rhoasCfgDir); os.IsNotExist(err) {
err = os.MkdirAll(rhoasCfgDir, 0o700)
if err != nil {
return err
}
}
}

cfgFile, err := f.Config.Load()

if cfgFile != nil {
return
return err
}

if !os.IsNotExist(err) {
fmt.Fprintln(f.IOStreams.ErrOut, err)
os.Exit(1)
return err
}

cfgFile = &config.Config{}
if err := f.Config.Save(cfgFile); err != nil {
fmt.Fprintln(f.IOStreams.ErrOut, err)
os.Exit(1)
return err
}
return nil
}
10 changes: 9 additions & 1 deletion internal/config/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type File struct{}

const errorFormat = "%v: %w"

const EnvName = "RHOASCONFIG"

// Load loads the configuration from the configuration file. If the configuration file doesn't exist
// it will return an empty configuration object.
func (c *File) Load() (*Config, error) {
Expand Down Expand Up @@ -93,7 +95,7 @@ func (c *File) Remove() error {

// Location gets the path to the config file
func (c *File) Location() (path string, err error) {
if rhoasConfig := os.Getenv("RHOASCONFIG"); rhoasConfig != "" {
if rhoasConfig := os.Getenv(EnvName); rhoasConfig != "" {
path = rhoasConfig
} else {
rhoasCfgDir, err := DefaultDir()
Expand All @@ -108,6 +110,12 @@ func (c *File) Location() (path string, err error) {
return path, nil
}

// Checks if config has custom location
func HasCustomLocation() bool {
rhoasConfig := os.Getenv(EnvName)
return rhoasConfig != ""
}

// DefaultDir returns the default parent directory of the config file
func DefaultDir() (string, error) {
userCfgDir, err := os.UserConfigDir()
Expand Down
2 changes: 2 additions & 0 deletions pkg/localize/locales/en/cmd/main.en.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[main.config.error]
one = 'Error when initializing configuration: "{{.Error}}"'

0 comments on commit e1f5747

Please sign in to comment.