-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
122 lines (112 loc) · 3.48 KB
/
main.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"fmt"
"github.com/manifoldco/promptui"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"os/user"
)
type OpenstackContextConfig struct {
CurrentContext string `yaml:"current_context"`
Contexts []OpenstackContext `yaml:"contexts"`
}
type OpenstackContext struct {
Name string `yaml:"name"`
OsProjectDomainName string `yaml:"os_project_domain_name"`
OsUserDomainName string `yaml:"os_user_domain_name"`
OsProjectId string `yaml:"os_project_id"`
OsUsername string `yaml:"os_username"`
OsPassword string `yaml:"os_password"`
OsAuthUrl string `yaml:"os_auth_url"`
OsRegionName string `yaml:"os_region_name"`
}
func main() {
// retrieve and parse configuration file
path := getConfigPath()
f, err := ioutil.ReadFile(path)
if err != nil {
log.Error(fmt.Sprintf("Could not open %s", path))
os.Exit(1)
}
config := OpenstackContextConfig{}
err = yaml.Unmarshal(f, &config)
if err != nil {
log.Error("Invalid configuration file syntax")
}
if len(os.Args) > 1 {
switch os.Args[1] {
case "current-context":
fmt.Printf(config.CurrentContext)
os.Exit(0)
case "activate":
for _, context := range config.Contexts {
if context.Name == os.Args[2] {
ActivateContext(&config, context)
}
}
fmt.Printf("Could not find context \"%s\".\n", os.Args[2])
os.Exit(1)
default:
fmt.Println("Unexpected argument")
os.Exit(1)
}
}
// otherwise prompt for context selection
SelectContext(config)
}
func SelectContext(openstackConfig OpenstackContextConfig) {
contextsNames := make([]string, len(openstackConfig.Contexts))
contextsMap := make(map[string]OpenstackContext)
for i, context := range openstackConfig.Contexts {
contextsNames[i] = context.Name
contextsMap[context.Name] = context
}
prompt := promptui.Select{
Label: "Select Openstack context",
Items: contextsNames,
HideSelected: true,
Stdout: os.Stderr,
}
_, selectedContext, err := prompt.Run()
if err != nil {
fmt.Printf("Unable to start prompt %v\n", err)
return
}
ActivateContext(&openstackConfig, contextsMap[selectedContext])
}
func ActivateContext(openstackConfig *OpenstackContextConfig, context OpenstackContext) {
_ = os.Setenv("OS_AUTH_URL", context.OsAuthUrl)
_ = os.Setenv("OS_PROJECT_ID", context.OsProjectId)
_ = os.Setenv("OS_PROJECT_DOMAIN_NAME", context.OsProjectDomainName)
_ = os.Setenv("OS_USER_DOMAIN_NAME", context.OsUserDomainName)
_ = os.Setenv("OS_USERNAME", context.OsUsername)
_ = os.Setenv("OS_PASSWORD", context.OsPassword)
_ = os.Setenv("OS_REGION_NAME", context.OsRegionName)
_ = os.Setenv("OS_IDENTITY_API_VERSION", "3")
path := getConfigPath()
openstackConfig.CurrentContext = context.Name
data, _ := yaml.Marshal(openstackConfig)
err := ioutil.WriteFile(path, data, 0600)
if err != nil {
log.Error("Could not write to %s", path)
}
fmt.Printf(`export OS_AUTH_URL="%s";export OS_PROJECT_ID="%s";export OS_PROJECT_DOMAIN_NAME="%s";export OS_USER_DOMAIN_NAME="%s";export OS_USERNAME="%s";export OS_PASSWORD="%s";export OS_REGION_NAME="%s"`,
context.OsAuthUrl,
context.OsProjectId,
context.OsProjectDomainName,
context.OsUserDomainName,
context.OsUsername,
context.OsPassword,
context.OsRegionName)
os.Exit(0)
}
func getConfigPath() string {
u, err := user.Current()
if err != nil {
log.Error("Could not retrieve current user information")
os.Exit(1)
}
return fmt.Sprintf("%s/.openstack/config", u.HomeDir)
}