This repository has been archived by the owner on Feb 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
344 lines (288 loc) · 7.75 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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package main
import (
"context"
"encoding/base64"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strings"
"github.com/gen2brain/beeep"
"github.com/ansd/lastpass-go"
"github.com/fatih/color"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
type OperatingSystem string
var (
Windows = OperatingSystem("windows")
Linux = OperatingSystem("linux")
Mac = OperatingSystem("macOs")
Darwin = OperatingSystem("darwin")
)
func (o *OperatingSystem) String() string {
if *o == Windows {
return "windows"
}
if *o == Linux || *o == Mac || *o == Darwin {
return "linux"
}
return "Undefined"
}
type Targets struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
KeyName string `yaml:"keyName"`
Resources *struct {
Npm *struct {
Sources []string `yaml:"sources,omitempty"`
Projects []string `yaml:"projects,omitempty"`
RegistryName string `yaml:"registryName"`
} `yaml:"npm"`
NuGet *struct {
Sources []string `yaml:"sources,omitempty"`
Projects []string `yaml:"projects,omitempty"`
} `yaml:"nuget"`
} `yaml:"resources"`
}
func (t *Targets) Validate() error {
if t.Resources == nil {
return errors.New("resources can not be empty")
}
if len(t.Resources.Npm.Sources) == 0 {
return errors.New("npm source can not be empty")
}
if len(t.Resources.NuGet.Sources) == 0 {
return errors.New("nuGet source can not be empty")
}
if t.KeyName == "" {
return errors.New("keyName can not be empty")
}
return nil
}
func main() {
ctx := context.Background()
file, err := os.Open("./targets.yaml")
if os.IsNotExist(err) {
log.Fatalf("targets.yaml file does not exist, please provide a file")
}
defer file.Close()
var targets Targets
buf, err := ioutil.ReadAll(file)
if err != nil {
log.Fatalf("targets.yaml could not read")
}
err = yaml.Unmarshal(buf, &targets)
if err != nil {
log.Fatalf("can not parse targets.yaml file: %v", err.Error())
}
err = targets.Validate()
if err != nil {
log.Fatalf(color.RedString("targets.yaml file validation was failed: %v", err))
}
var (
username = flag.String("username", targets.Username, "")
password = flag.String("password", targets.Password, "")
updateLocal = flag.Bool("update-local", true, "('false' if not present)")
jsonOnly = flag.Bool("json", false, "Returns only json response. ('false' if not present)")
stdout = os.Stdout
)
flag.Parse()
if *username == "" || *password == "" {
_, _ = fmt.Fprintf(stdout, "Please provide username and password to login LastPass\n")
return
}
client, _ := lastpass.NewClient(context.Background(), *username, *password)
acs, err := client.Accounts(context.Background())
if err != nil {
log.Fatal("Can not access to LastPass. Are you sure about the credentials you provide?")
}
var credentials *struct {
Username string `json:"username"`
Token string `json:"token"`
}
for _, ac := range acs {
if ac.Name == targets.KeyName {
_ = json.Unmarshal([]byte(ac.Notes), &credentials)
jToken, err := json.MarshalIndent(&credentials, "", " ")
if err != nil {
log.Fatalf("There was a problem with decoding json response, please check Artifactory Token is shared with you in a JSON format")
}
if *jsonOnly {
_, _ = fmt.Fprintf(stdout, string(jToken))
return
}
}
}
if *updateLocal {
log.Printf("I will let you develop by changing local .npmrc and .nuget.config files!")
err := updateNuGet(targets, ctx, credentials)
if err != nil {
log.Println(color.YellowString("NuGet update was failed, because: %v", err))
}
err = updateNpm(targets, *username, credentials)
if err != nil {
log.Println(color.YellowString("Npm update was failed, because: %v", err))
}
err = copyToProjects(targets)
if err != nil {
log.Println(color.YellowString("Copying config files are failed, because: %v", err))
}
_ = beeep.Notify("Let Me Pass!", "Last changes are applied to local environment", "")
}
}
func updateNuGet(targets Targets, ctx context.Context, tr *struct {
Username string `json:"username"`
Token string `json:"token"`
}) error {
for i, source := range targets.Resources.NuGet.Sources {
cmd := exec.CommandContext(ctx,
"nuget",
"sources",
"Add",
"-Name", fmt.Sprintf("Nuget_%v", i),
"-Source", source,
"-username", tr.Username,
"-password", tr.Token,
)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
name := fmt.Sprintf("Nuget_%v", i)
err := cmd.Run()
if err != nil {
log.Println(color.YellowString("nuget sources add got an error: %v", err))
log.Println(color.GreenString("nuget resources trying to update..."))
cmd = exec.CommandContext(ctx,
"nuget",
"sources",
"Update",
"-Name", name,
"-Source", source,
"-username", tr.Username,
"-password", tr.Token,
)
err = cmd.Run()
if err != nil {
log.Println(color.YellowString("update nuget sources got an error: %v", err))
} else {
log.Println(color.GreenString("update succeeded!"))
}
}
cmd = exec.CommandContext(ctx,
"nuget",
"setapikey",
fmt.Sprintf("%v:%v", tr.Username, tr.Token),
"-Source", name,
)
err = cmd.Run()
if err != nil {
return err
}
log.Printf(color.GreenString("NuGet Source: %v has been added successfully", source))
}
return nil
}
func updateNpm(
targets Targets,
username string,
tr *struct {
Username string `json:"username"`
Token string `json:"token"`
}) error {
npmTemplate := `{registryName}:registry=%v
//{registry}:_password=%v
//{registry}:username=%v
//{registry}:email=%v
//{registry}:always-auth=true
`
npm := ""
for _, source := range targets.Resources.Npm.Sources {
registry := cleanProtocol(source)
data := []byte(tr.Token)
base64Token := base64.StdEncoding.EncodeToString(data)
npm += strings.ReplaceAll(
strings.ReplaceAll(
fmt.Sprintf(npmTemplate, source, base64Token, tr.Username, username),
"{registry}",
registry), "{registryName}",
targets.Resources.Npm.RegistryName)
log.Printf(color.GreenString("Npm Source: %v has been added successfully", source))
}
npmRcFilePath := path.Join(GetHomeDir(), ".npmrc")
file, err := os.OpenFile(npmRcFilePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0660)
if err != nil {
return err
}
defer file.Close()
_, err = file.WriteString(npm)
err = file.Sync()
if err != nil {
return err
}
log.Println(color.GreenString("NpmRc updated successfully"))
return nil
}
func copyToProjects(targets Targets) error {
for _, project := range targets.Resources.Npm.Projects {
err := CopyFile(path.Join(GetHomeDir(), ".npmrc"), path.Join(project, ".npmrc"))
if err != nil {
return err
}
}
for _, project := range targets.Resources.NuGet.Projects {
err := CopyFile(GetNugetConfig(), path.Join(project, "NuGet.Config"))
if err != nil {
return err
}
}
return nil
}
func cleanProtocol(source string) string {
registry := strings.Replace(source, "https://", "", 1)
registry = strings.Replace(registry, "http://", "", 1)
return registry
}
func GetHomeDir() string {
if runtime.GOOS == "windows" {
return os.Getenv("APPDATA")
}
return os.Getenv("HOME")
}
func GetNugetConfig() string {
home := GetHomeDir()
isWindows := runtime.GOOS == "windows"
if isWindows {
return path.Join(home, "/nuget/NuGet.Config")
}
return path.Join(home, "/.config/NuGet/NuGet.Config")
}
func CopyFile(src, dst string) error {
dir := filepath.Dir(dst)
err := os.MkdirAll(dir, os.ModePerm)
if err != nil {
return err
}
sourceFile, err := os.Open(src)
if err != nil {
return err
}
defer sourceFile.Close()
newFile, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE, os.ModePerm)
if err != nil {
return err
}
defer newFile.Close()
_, err = io.Copy(newFile, sourceFile)
if err != nil {
return err
}
return nil
}