Skip to content

Commit d68dfb7

Browse files
committed
Refactor impl dealing with cvdr configuration on main.go
1 parent 44ceafd commit d68dfb7

1 file changed

Lines changed: 50 additions & 42 deletions

File tree

cmd/cvdr/main.go

Lines changed: 50 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ const (
3030
envVarSystemConfigPath = "CVDR_SYSTEM_CONFIG_PATH"
3131
// User config values overrieds system config values.
3232
envVarUserConfigPath = "CVDR_USER_CONFIG_PATH"
33+
// It may ask for importing acloud config if cvdr user config is empty.
34+
acloudConfigPath = "~/.config/acloud/acloud.config"
3335
)
3436

3537
func loadInitialConfig() (*cli.Config, error) {
@@ -39,60 +41,66 @@ func loadInitialConfig() (*cli.Config, error) {
3941
sysConfigSrc = cli.ExpandPath(path)
4042
}
4143
if path, ok := os.LookupEnv(envVarUserConfigPath); ok {
42-
path = cli.ExpandPath(path)
43-
_, statErr := os.Stat(path)
44-
if errors.Is(statErr, os.ErrNotExist) {
45-
imported, err := importAcloudConfig(path)
46-
if err != nil {
47-
return nil, err
48-
}
49-
if !imported {
50-
// Create empty user configuration file.
51-
dir := filepath.Dir(path)
52-
if err := os.MkdirAll(dir, 0750); err != nil {
53-
return nil, fmt.Errorf("failed creating user config directory: %w", err)
54-
}
55-
f, err := os.Create(path)
56-
if err != nil {
57-
return nil, fmt.Errorf("failed creating user config file: %w", err)
58-
}
59-
f.Close()
60-
}
61-
statErr = nil
44+
userConfigSrc = cli.ExpandPath(path)
45+
if err := createUserConfigIfNeeded(userConfigSrc); err != nil {
46+
return nil, err
6247
}
63-
if statErr != nil {
64-
return nil, fmt.Errorf("invalid user config file path: %w", statErr)
65-
}
66-
userConfigSrc = path
6748
}
6849
if err := cli.LoadConfig(sysConfigSrc, userConfigSrc, config); err != nil {
6950
return nil, err
7051
}
7152
return config, nil
7253
}
7354

74-
func importAcloudConfig(dst string) (bool, error) {
55+
func createUserConfigIfNeeded(dst string) error {
56+
if _, err := os.Stat(dst); err != nil && !errors.Is(err, os.ErrNotExist) {
57+
return fmt.Errorf("invalid user config file path: %w", err)
58+
} else if err == nil {
59+
return nil
60+
}
61+
imported, err := importAcloudConfig(dst)
62+
if err != nil {
63+
return fmt.Errorf("failed creating user config file with acloud config:%w", err)
64+
}
65+
if imported {
66+
return nil
67+
}
68+
// Create empty user configuration file.
69+
if err := os.MkdirAll(filepath.Dir(dst), 0750); err != nil {
70+
return fmt.Errorf("failed creating user config directory: %w", err)
71+
}
72+
f, err := os.Create(dst)
73+
if err != nil {
74+
return fmt.Errorf("failed creating user config file: %w", err)
75+
}
76+
f.Close()
77+
return nil
78+
}
79+
80+
func importAcloudConfig(ucPath string) (bool, error) {
7581
// Create a new user configuration file importing existing acloud configuration.
76-
acPath := cli.ExpandPath("~/.config/acloud/acloud.config")
77-
if _, err := os.Stat(acPath); err == nil {
78-
yes := true
79-
// Prompt only on terminal.
80-
if term.IsTerminal(int(os.Stdout.Fd())) {
81-
const p = "No user configuration found, would you like to generate it by importing " +
82-
"your acloud configuration?"
83-
yes, err = cli.PromptYesOrNo(os.Stdout, os.Stdin, p)
84-
if err != nil {
85-
return false, err
86-
}
82+
acPath := cli.ExpandPath(acloudConfigPath)
83+
if _, err := os.Stat(acPath); errors.Is(err, os.ErrNotExist) {
84+
return false, nil
85+
} else if err != nil {
86+
return false, fmt.Errorf("failed to stat acloud config file: %w", err)
87+
}
88+
// It doesn't import acloud config when user says no via prompt.
89+
if term.IsTerminal(int(os.Stdout.Fd())) {
90+
const p = "No user configuration found, would you like to generate it by importing " +
91+
"your acloud configuration?"
92+
yes, err := cli.PromptYesOrNo(os.Stdout, os.Stdin, p)
93+
if err != nil {
94+
return false, err
8795
}
88-
if yes {
89-
if err := cli.ImportAcloudConfig(acPath, dst); err != nil {
90-
return false, fmt.Errorf("failed importing acloud config file: %w", err)
91-
}
92-
return true, nil
96+
if !yes {
97+
return false, nil
9398
}
9499
}
95-
return false, nil
100+
if err := cli.ImportAcloudConfig(acPath, ucPath); err != nil {
101+
return false, fmt.Errorf("failed importing acloud config file: %w", err)
102+
}
103+
return true, nil
96104
}
97105

98106
type cmdRunner struct{}

0 commit comments

Comments
 (0)