Skip to content

Commit

Permalink
Create config directory if it doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
cblecker committed Jun 13, 2023
1 parent d933c9e commit ba26a7b
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions cmd/ocm-backplane/config/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package config
import (
"fmt"
"os"
"path"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/term"
"gopkg.in/AlecAivazis/survey.v1"

"github.com/openshift/backplane-cli/pkg/cli/config"
)
Expand Down Expand Up @@ -43,6 +46,29 @@ func setConfig(cmd *cobra.Command, args []string) error {
bpConfig.SessionDirectory = viper.GetString("session-dir")
}

// Create config directory if it doesn't exist
if dir, err := os.Stat(path.Dir(configPath)); os.IsNotExist(err) || !dir.IsDir() {
if term.IsTerminal(int(os.Stdout.Fd())) {
confirm := false
prompt := &survey.Confirm{
Message: fmt.Sprintf("Config directory \"%s\" does not exist. Create it?", path.Dir(configPath)),
Default: true,
}
if err := survey.AskOne(prompt, &confirm, nil); err != nil {
return err
}
if !confirm {
fmt.Println("Aborted")
return nil
}
if err := os.MkdirAll(path.Dir(configPath), 0750); err != nil {
return err
}
} else {
return fmt.Errorf("config directory does not exist: %s", path.Dir(configPath))
}
}

switch args[0] {
case URLConfigVar:
bpConfig.URL = args[1]
Expand Down

0 comments on commit ba26a7b

Please sign in to comment.