Skip to content

Commit 0282400

Browse files
committed
Create the config file if doesn't exist
Fixes #2
1 parent fece308 commit 0282400

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
[submodule "vendor/github.com/xanzy/go-gitlab"]
22
path = vendor/github.com/xanzy/go-gitlab
33
url = https://github.com/xanzy/go-gitlab
4+
[submodule "vendor/github.com/mitchellh/go-homedir"]
5+
path = vendor/github.com/mitchellh/go-homedir
6+
url = https://github.com/mitchellh/go-homedir

cmd/root.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var (
1616
repo, repourl, token string
1717
user, password string
1818
verbose bool
19+
configName = ".gitlab-cli"
1920
)
2021

2122
// RootCmd represents the base command when called without any subcommands
@@ -67,9 +68,9 @@ func initConfig() {
6768
viper.SetConfigFile(cfgFile)
6869
}
6970

70-
viper.SetConfigName(".gitlab-cli") // name of config file (without extension)
71-
viper.AddConfigPath("$HOME") // adding home directory as first search path
72-
viper.AutomaticEnv() // read in environment variables that match
71+
viper.SetConfigName(configName) // name of config file (without extension)
72+
viper.AddConfigPath("$HOME") // adding home directory as first search path
73+
viper.AutomaticEnv() // read in environment variables that match
7374
viper.ConfigFileUsed()
7475

7576
// If a config file is found, read it in.

cmd/viper.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
package cmd
22

33
import (
4-
"os"
5-
64
"fmt"
7-
5+
"os"
6+
"path/filepath"
87
"strings"
98

9+
"github.com/mitchellh/go-homedir"
1010
"github.com/spf13/viper"
1111
"gopkg.in/yaml.v2"
1212
)
1313

1414
func SaveViperConfig() error {
15-
f, err := os.Create(viper.ConfigFileUsed())
15+
filename := viper.ConfigFileUsed()
16+
if filename == "" {
17+
hdir, err := homedir.Dir()
18+
if err != nil {
19+
return err
20+
}
21+
filename = filepath.Join(hdir, configName+".yml")
22+
}
23+
f, err := os.Create(filename)
1624
if err != nil {
1725
return err
1826
}
Submodule go-homedir added at b8bc1bf

0 commit comments

Comments
 (0)