/
config.go
84 lines (67 loc) · 1.88 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package config
import (
"fmt"
"log"
"os"
"path/filepath"
"github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
)
// Config handles all overall configuration for the CLI
type Config struct {
Profile Profile
ProfilesFile string
}
// GetConfigFolder retrieves the folder where the profiles file is stored
// It searches for the xdg environment path first and will secondarily
// place it in the home directory
func (c *Config) GetConfigFolder(xdgPath string) string {
configPath := xdgPath
if configPath == "" {
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
configPath = filepath.Join(home, ".config")
}
return filepath.Join(configPath, "razorpayx")
}
// InitConfig reads in profiles file and ENV variables if set.
func (c *Config) InitConfig() {
if c.ProfilesFile != "" {
viper.SetConfigFile(c.ProfilesFile)
} else {
configFolder := c.GetConfigFolder(os.Getenv("XDG_CONFIG_HOME"))
configFile := filepath.Join(configFolder, "config.toml")
c.ProfilesFile = configFile
viper.SetConfigType("toml")
viper.SetConfigFile(configFile)
viper.SetConfigPermissions(os.FileMode(0600))
// Try to change permissions manually, because we used to create files
// with default permissions (0644)
err := os.Chmod(configFile, os.FileMode(0600))
if err != nil && !os.IsNotExist(err) {
log.Fatalf("%s", err)
}
}
// If a profiles file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
}
if c.Profile.DeviceName == "" {
deviceName, err := os.Hostname()
if err != nil {
deviceName = "unknown"
}
c.Profile.DeviceName = deviceName
}
}
func (c *Config) CreateProfile() error {
c.Profile.LiveModeAPIKey = "test"
c.Profile.LiveModeAPISecret = "test"
c.Profile.TestModeAPIKey = "test"
c.Profile.TestModeAPISecret = "test"
c.Profile.DisplayName = "test"
profileErr := c.Profile.CreateProfile()
return profileErr
}