GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — cli/cmd/config.go at main · PastureStack/cli

Expand file tree

/

config.go

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
228 lines (195 loc) · 4.36 KB
/

config.go

Copy path

File metadata and controls

228 lines (195 loc) · 4.36 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package cmd
import (
"bufio"
"encoding/json"
"fmt"
"io"
"net/url"
"os"
"path"
"strings"
"github.com/PastureStack/cli/internal/rancherclient/v2"
cli "github.com/PastureStack/cli/internal/legacycli"
"github.com/sirupsen/logrus"
)
type Config struct {
AccessKey string `json:"accessKey"`
SecretKey string `json:"secretKey"`
URL string `json:"url"`
Environment string `json:"environment"`
Path string `json:"path,omitempty"`
}
func baseURL(fullURL string) (string, error) {
idx := strings.LastIndex(fullURL, "/v2-beta")
if idx == -1 {
u, err := url.Parse(fullURL)
if err != nil {
return "", err
}
newURL := url.URL{
Scheme: u.Scheme,
Host: u.Host,
}
return newURL.String(), nil
}
return fullURL[:idx], nil
}
func (c Config) EnvironmentURL() (string, error) {
projectID := c.Environment
if projectID == "" || !strings.HasPrefix(projectID, "1a") {
rancherClient, err := client.NewRancherClient(&client.ClientOpts{
Url: c.URL,
AccessKey: c.AccessKey,
SecretKey: c.SecretKey,
})
if err != nil {
return "", err
}
project, err := GetEnvironment(c.Environment, rancherClient)
if err != nil {
return "", err
}
projectID = project.Id
}
url, err := baseURL(c.URL)
if err != nil {
return "", err
}
url = url + "/v2-beta/projects/" + projectID + "/schemas"
return url, nil
}
func (c Config) Write() error {
err := os.MkdirAll(path.Dir(c.Path), 0700)
if err != nil {
return err
}
logrus.Infof(T("config.saved"), c.Path)
p := c.Path
c.Path = ""
output, err := os.OpenFile(p, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
defer output.Close()
return json.NewEncoder(output).Encode(c)
}
func LoadConfig(path string) (Config, error) {
config := Config{
Path: path,
}
content, err := os.ReadFile(path)
if os.IsNotExist(err) {
return config, nil
} else if err != nil {
return config, err
}
err = json.Unmarshal(content, &config)
config.Path = path
return config, err
}
func ConfigCommand() cli.Command {
return cli.Command{
Name: "config",
Usage: "Setup client configuration",
Action: configSetup,
ArgsUsage: "None",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "print,p",
Usage: "Print the current configuration",
},
},
}
}
func getConfig(reader *bufio.Reader, text, def string) (string, error) {
for {
fmt.Printf("%s [%s]: ", text, def)
input, err := reader.ReadString('\n')
if err != nil {
return "", err
}
input = strings.TrimSpace(input)
if input != "" {
return input, nil
}
if input == "" && def != "" {
return def, nil
}
}
}
func getSecretConfig(reader *bufio.Reader, writer io.Writer, text, def string) (string, error) {
for {
if def == "" {
fmt.Fprintf(writer, "%s: ", text)
} else {
fmt.Fprintf(writer, "%s [configured]: ", text)
}
input, err := reader.ReadString('\n')
if err != nil {
return "", err
}
input = strings.TrimSpace(input)
if input != "" {
return input, nil
}
if def != "" {
return def, nil
}
}
}
func configSetup(ctx *cli.Context) error {
config, err := lookupConfig(ctx)
if err != nil && err != errNoURL {
return err
}
if ctx.Bool("print") {
safeConfig := config
if safeConfig.SecretKey != "" {
safeConfig.SecretKey = "[redacted]"
}
return json.NewEncoder(os.Stdout).Encode(safeConfig)
}
reader := bufio.NewReader(os.Stdin)
config.URL, err = getConfig(reader, "URL", config.URL)
if err != nil {
return err
}
config.AccessKey, err = getConfig(reader, "Access Key", config.AccessKey)
if err != nil {
return err
}
config.SecretKey, err = getSecretConfig(reader, os.Stdout, "Secret Key", config.SecretKey)
if err != nil {
return err
}
c, err := client.NewRancherClient(&client.ClientOpts{
Url: config.URL,
AccessKey: config.AccessKey,
SecretKey: config.SecretKey,
})
if err != nil {
return err
}
if schema, ok := c.GetSchemas().CheckSchema("schema"); ok {
// Normalize URL
config.URL = schema.Links["collection"]
} else {
return fmt.Errorf("Failed to find schema URL")
}
c, err = client.NewRancherClient(&client.ClientOpts{
Url: config.URL,
AccessKey: config.AccessKey,
SecretKey: config.SecretKey,
})
if err != nil {
return err
}
project, err := GetEnvironment("", c)
if err != errNoEnv {
if err != nil {
return err
}
config.Environment = project.Id
}
return config.Write()
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.