This repository was archived by the owner on Dec 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathconfig.go
184 lines (166 loc) · 4.26 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"regexp"
"github.com/google/uuid"
"github.com/mitchellh/go-homedir"
)
// IoSDKConfig is the global configuration type
type IoSDKConfig struct {
// WhiskApiHost is the openwhisk api host
WhiskAPIHostLocal string `json:"whisk-apihost-local"`
// WhiskApiHostDocker is the api host within docker
WhiskAPIHostDocker string `json:"whisk-apihost-docker"`
// WhiskAPIKey is the openwhisk api key
WhiskAPIKey string `json:"whisk-apikey"`
// WhiskNamespace is the openwhisk namespace
WhiskNamespace string `json:"whisk-namespace"`
// IoAPIKey is the io api key
IoAPIKey string `json:"io-apikey"`
// IoMessages is the io api key
IoMessages string `json:"io-messages"`
// AppDir is the application directory
AppDir string `json:"app-dir"`
}
// ConfigMap returns a map of the configuration
func ConfigMap() map[string]string {
return map[string]string{
"whisk-apihost-local": Config.WhiskAPIHostLocal,
"whisk-apihost-docker": Config.WhiskAPIHostDocker,
"whisk-apikey": Config.WhiskAPIKey,
"whisk-namespace": Config.WhiskNamespace,
"io-apikey": Config.IoAPIKey,
"io-messages": Config.IoMessages,
"app-dir": Config.AppDir,
}
}
// Config is the global configuration
var Config *IoSDKConfig
// ConfigLoad loads the configuration
func ConfigLoad() error {
configFile, err := homedir.Expand(ConfigFile)
if err != nil {
return err
}
_, err = os.Stat(configFile)
if err != nil {
return err
}
buf, err := ioutil.ReadFile(configFile)
if err != nil {
return err
}
json.Unmarshal(buf, &Config)
return nil
}
// ConfigSave save configuration file
func ConfigSave() error {
if Config == nil {
return errors.New("empty configuration")
}
configFile, err := homedir.Expand(ConfigFile)
if err != nil {
return err
}
// saving
json, err := json.MarshalIndent(Config, "", " ")
err = ioutil.WriteFile(configFile, json, 0644)
if err != nil {
return err
}
fmt.Println("Wrote", configFile)
if *initWskPropsFlag {
return WskPropsSave()
}
return nil
}
// WskPropsSave save wskprops
func WskPropsSave() error {
wskFile, err := homedir.Expand("~/.wskprops")
if err != nil {
return err
}
data := fmt.Sprintf("APIHOST=%s\nAUTH=%s\n", Config.WhiskAPIHostLocal, Config.WhiskAPIKey)
err = ioutil.WriteFile(wskFile, []byte(data), 0644)
if err != nil {
return err
}
fmt.Println("Wrote", wskFile)
return nil
}
// configureDefaults sets defaults in configuration
func configureDefaults() {
if Config.IoMessages == "" {
Config.IoMessages = IoAPIHost + "/messages"
}
if Config.WhiskAPIHostLocal == "" {
Config.WhiskAPIHostLocal = "http://localhost:3280"
}
if Config.WhiskAPIHostDocker == "" {
Config.WhiskAPIHostDocker = "http://openwhisk:3280"
}
if Config.WhiskNamespace == "" {
Config.WhiskNamespace = "guest"
}
// generate random key if not there, using the default api key if required
if Config.WhiskAPIKey == "" {
key := *initWhiskKeyFlag
if key == "" {
Config.WhiskAPIKey = fmt.Sprintf("%s:%s", uuid.New(), RandomString(64))
} else {
Config.WhiskAPIKey = key
}
}
}
func configureAsk() error {
// ask or override api key
key := *initIOKeyFlag
if key == "" {
key = Input("IO Api Key", Config.IoAPIKey)
}
if key == "" {
return errors.New("You need to provide an api key")
}
Config.IoAPIKey = key
return nil
}
// Configure asking values or setting defaults
func Configure(dir string) error {
err := ConfigLoad()
if err != nil {
// initialized
Config = &IoSDKConfig{}
}
// ignore errors
Config.AppDir = dir
configureDefaults()
if err := configureAsk(); err != nil {
return err
}
return ConfigSave()
}
func configureIde(info string) {
uid := ""
var search = regexp.MustCompile(`Operating System: Boot2Docker`)
if search.FindString(info) != "" {
uid = "-u root"
}
cmd := fmt.Sprintf("docker exec %s iosdk-theia wsk property set --apihost %s --auth %s", uid, Config.WhiskAPIHostDocker, Config.WhiskAPIKey)
err := Run(cmd)
if err != nil {
fmt.Println(err)
}
}
// PropagateConfig propagate configurations to started services
func PropagateConfig(info string) {
fmt.Println("Configuring Whisk")
WhiskUpdatePackageParameters("iosdk", ConfigMap())
if !*skipIde {
fmt.Println("Configuring IDE")
configureIde(info)
}
}