-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathget_config.go
79 lines (68 loc) · 1.66 KB
/
get_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
package main
import (
"fmt"
"os"
"strings"
"github.com/BurntSushi/toml"
"github.com/OpenNebula/one/src/oca/go/src/goca"
)
type Config struct {
OpenNebulaUsername string
OpenNebulaToken string
OpenNebulaAPI string
SshKey string
ProjectsPath string
ConfigPath string
}
var DefaultConfigPath = "/.config/OpenNebulaUp/opennebulaup.config"
func GetOpenNebulaConfig(conf *Config) goca.OneConfig {
config := goca.NewConfig(conf.OpenNebulaUsername, conf.OpenNebulaToken, conf.OpenNebulaAPI)
return config
}
func GetConfig(ConfigFilePath string) (*Config, error) {
var conf Config
if _, err := os.Stat(ConfigFilePath); os.IsNotExist(err) {
fmt.Printf("%s does not exists\n", ConfigFilePath)
return &conf, err
}
if _, err := toml.DecodeFile(ConfigFilePath, &conf); err != nil {
fmt.Println(err)
return &conf, err
}
if !strings.HasSuffix(conf.ProjectsPath, "/") {
conf.ProjectsPath = conf.ProjectsPath + "/"
}
return &conf, nil
}
func GetPublicKey(env Env) string {
return env.config.SshKey
}
func GetUserHomeDir() string {
return os.Getenv("HOME")
}
func GetAltProjectsPath(args []string) string {
path := ""
for num, arg := range args {
if arg == "--ProjectsPath" {
if num == len(args)-1 {
fmt.Println("Please sepcify --ProjectsPath")
} else {
path = args[num+1]
}
}
}
return path
}
func GetConfigLocation(args []string) string {
path := GetUserHomeDir() + DefaultConfigPath
for num, arg := range args {
if arg == "--config" {
if num == len(args)-1 {
fmt.Println("Please specify a path after --config. Using default config path")
} else {
path = args[num+1]
}
}
}
return path
}