-
Notifications
You must be signed in to change notification settings - Fork 41
/
googet.go
553 lines (500 loc) · 13.5 KB
/
googet.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*
Copyright 2016 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// The googet binary is the client for the GoGet packaging system, it performs the listing,
// getting, installing and removing functions on client machines.
package main
import (
"context"
"errors"
"flag"
"fmt"
"io/ioutil"
"net/url"
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/go-yaml/yaml"
"github.com/google/googet/v2/client"
"github.com/google/googet/v2/goolib"
"github.com/google/googet/v2/priority"
"github.com/google/googet/v2/system"
"github.com/google/logger"
"github.com/google/subcommands"
"github.com/olekukonko/tablewriter"
)
const (
stateFile = "googet.state"
confFile = "googet.conf"
logFile = "googet.log"
cacheDir = "cache"
repoDir = "repos"
envVar = "GooGetRoot"
logSize = 10 * 1024 * 1024
)
var (
rootDir string
noConfirm bool
verbose bool
systemLog bool
showVer bool
version string
cacheLife = 3 * time.Minute
archs []string
proxyServer string
allowUnsafeURL bool
lockFile string
)
type packageMap map[string]string
// installedPackages returns a packagemap of all installed packages based on the
// googet state file given.
func installedPackages(state client.GooGetState) packageMap {
pm := make(packageMap)
for _, p := range state {
pm[p.PackageSpec.Name+"."+p.PackageSpec.Arch] = p.PackageSpec.Version
}
return pm
}
type repoFile struct {
fileName string
repoEntries []repoEntry
}
type repoEntry struct {
Name string
URL string
UseOAuth bool
Priority priority.Value `yaml:",omitempty"`
}
// UnmarshalYAML provides custom unmarshalling for repoEntry objects.
func (r *repoEntry) UnmarshalYAML(unmarshal func(any) error) error {
var u map[string]string
if err := unmarshal(&u); err != nil {
return err
}
for k, v := range u {
switch key := strings.ToLower(k); key {
case "name":
r.Name = v
case "url":
r.URL = v
case "useoauth":
r.UseOAuth = strings.ToLower(v) == "true"
case "priority":
var err error
r.Priority, err = priority.FromString(v)
if err != nil {
return fmt.Errorf("invalid priority: %v", v)
}
}
}
if r.URL == "" {
return fmt.Errorf("repo entry missing url: %+v", u)
}
return nil
}
func writeRepoFile(rf repoFile) error {
d, err := yaml.Marshal(rf.repoEntries)
if err != nil {
return err
}
return ioutil.WriteFile(rf.fileName, d, 0664)
}
func unmarshalRepoFile(p string) (repoFile, error) {
b, err := ioutil.ReadFile(p)
if err != nil {
return repoFile{}, err
}
// Don't try to unmarshal files with no YAML content
var yml bool
lns := strings.Split(string(b), "\n")
for _, ln := range lns {
ln = strings.TrimSpace(ln)
if !strings.HasPrefix(ln, "#") && ln != "" {
yml = true
break
}
}
if !yml {
return repoFile{}, nil
}
// Both repoFile and []repoFile are valid for backwards compatibility.
var re repoEntry
if err := yaml.Unmarshal(b, &re); err == nil && re.URL != "" {
return repoFile{fileName: p, repoEntries: []repoEntry{re}}, nil
}
var res []repoEntry
if err := yaml.Unmarshal(b, &res); err != nil {
return repoFile{}, err
}
return repoFile{fileName: p, repoEntries: res}, nil
}
type conf struct {
Archs []string
CacheLife string
ProxyServer string
AllowUnsafeURL bool
}
func unmarshalConfFile(p string) (*conf, error) {
b, err := ioutil.ReadFile(p)
if err != nil {
return nil, err
}
var cf conf
return &cf, yaml.Unmarshal(b, &cf)
}
// validateRepoURL uses the global allowUnsafeURL to determine if u should be checked for https or
// GCS status.
func validateRepoURL(u string) bool {
if allowUnsafeURL {
return true
}
gcs, _, _ := goolib.SplitGCSUrl(u)
parsed, err := url.Parse(u)
if err != nil {
logger.Errorf("Failed to parse URL '%s', skipping repo", u)
return false
}
if parsed.Scheme != "https" && !gcs {
logger.Errorf("%s will not be used as a repository, only https and Google Cloud Storage endpoints will be used unless 'allowunsafeurl' is set to 'true' in googet.conf", u)
return false
}
return true
}
// repoList returns a deduped set of all repos listed in the repo config files contained in dir.
// The repos are mapped to priority values. If a repo config does not specify a priority, the repo
// is assigned the default priority value. If the same repo appears multiple times with different
// priority values, it is mapped to the highest seen priority value.
func repoList(dir string) (map[string]priority.Value, error) {
rfs, err := repos(dir)
if err != nil {
return nil, err
}
result := make(map[string]priority.Value)
for _, rf := range rfs {
for _, re := range rf.repoEntries {
u := re.URL
if u == "" || !validateRepoURL(u) {
continue
}
if re.UseOAuth {
u = "oauth-" + u
}
p := re.Priority
if p <= 0 {
p = priority.Default
}
if q, ok := result[u]; !ok || p > q {
result[u] = p
}
}
}
return result, nil
}
func repos(dir string) ([]repoFile, error) {
fl, err := filepath.Glob(filepath.Join(dir, "*.repo"))
if err != nil {
return nil, err
}
var rfs []repoFile
for _, f := range fl {
rf, err := unmarshalRepoFile(f)
if err != nil {
logger.Error(err)
continue
}
if rf.fileName != "" {
rfs = append(rfs, rf)
}
}
return rfs, nil
}
func writeState(s *client.GooGetState, sf string) error {
b, err := s.Marshal()
if err != nil {
return err
}
// Write state to a temporary file first
tmp, err := ioutil.TempFile(rootDir, "googet.*.state")
if err != nil {
return err
}
newStateFile := tmp.Name()
if _, err = tmp.Write(b); err != nil {
return err
}
if err := tmp.Close(); err != nil {
return err
}
if err := os.Chmod(newStateFile, 0664); err != nil {
return err
}
// Back up the old state file so we can recover it if need be
backupStateFile := sf + ".bak"
if err = os.Rename(sf, backupStateFile); err != nil {
logger.Infof("Unable to back up state file %s to %s. Err: %v", sf, backupStateFile, err)
}
// Move the new temp file to the live path
return os.Rename(newStateFile, sf)
}
func readState(sf string) (*client.GooGetState, error) {
state, err := readStateFromPath(sf)
if err != nil {
sfNotExist := os.IsNotExist(err)
state, err = readStateFromPath(sf + ".bak")
if sfNotExist && os.IsNotExist(err) {
logger.Info("No state file found, assuming no packages installed.")
return &client.GooGetState{}, nil
}
}
return state, err
}
func readStateFromPath(sf string) (*client.GooGetState, error) {
b, err := ioutil.ReadFile(sf)
if err != nil {
return nil, err
}
return client.UnmarshalState(b)
}
func buildSources(s string) (map[string]priority.Value, error) {
if s == "" {
return repoList(filepath.Join(rootDir, repoDir))
}
m := make(map[string]priority.Value)
for _, src := range strings.Split(s, ",") {
m[src] = priority.Default
}
return m, nil
}
func confirmation(msg string) bool {
var c string
fmt.Print(msg + " (y/N): ")
fmt.Scanln(&c)
c = strings.ToLower(c)
return c == "y" || c == "yes"
}
func info(ps *goolib.PkgSpec, r string) {
fmt.Println()
pkgInfo := []struct {
name, value string
}{
{"Name", ps.Name},
{"Arch", ps.Arch},
{"Version", ps.Version},
{"Repo", path.Base(r)},
{"Authors", ps.Authors},
{"Owners", ps.Owners},
{"Source", ps.Source},
{"Description", ps.Description},
{"Dependencies", ""},
{"ReleaseNotes", ""},
}
var w int
for _, pi := range pkgInfo {
if len(pi.name) > w {
w = len(pi.name)
}
}
wf := fmt.Sprintf("%%-%vs: %%s\n", w+1)
for _, pi := range pkgInfo {
if pi.name == "Dependencies" {
var deps []string
for p, v := range ps.PkgDependencies {
deps = append(deps, p+" "+v)
}
if len(deps) == 0 {
fmt.Printf(wf, pi.name, "None")
} else {
fmt.Printf(wf, pi.name, deps[0])
for _, l := range deps[1:] {
fmt.Printf(wf, "", l)
}
}
} else if pi.name == "ReleaseNotes" && ps.ReleaseNotes != nil {
sl, _ := tablewriter.WrapString(ps.ReleaseNotes[0], 76-w)
fmt.Printf(wf, pi.name, sl[0])
for _, l := range sl[1:] {
fmt.Printf(wf, "", l)
}
for _, l := range ps.ReleaseNotes[1:] {
sl, _ := tablewriter.WrapString(l, 76-w)
fmt.Printf(wf, "", sl[0])
for _, l := range sl[1:] {
fmt.Printf(wf, "", l)
}
}
} else {
cl := strings.Split(strings.TrimSpace(pi.value), "\n")
sl, _ := tablewriter.WrapString(cl[0], 76-w)
fmt.Printf(wf, pi.name, sl[0])
for _, l := range sl[1:] {
fmt.Printf(wf, "", l)
}
for _, l := range cl[1:] {
sl, _ := tablewriter.WrapString(l, 76-w)
for _, l := range sl {
fmt.Printf(wf, "", l)
}
}
}
}
}
func rotateLog(logPath string, ls int64) error {
fi, err := os.Stat(logPath)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
if fi.Size() < ls {
return nil
}
oldLog := logPath + ".old"
if err := os.Rename(logPath, oldLog); err != nil {
return fmt.Errorf("error moving log file: %v", err)
}
return nil
}
func readConf(cf string) {
gc, err := unmarshalConfFile(cf)
if err != nil {
if os.IsNotExist(err) {
gc = &conf{}
} else {
logger.Errorf("Error unmarshalling conf file: %v", err)
}
}
if gc.Archs != nil {
archs = gc.Archs
} else {
archs, err = system.InstallableArchs()
if err != nil {
logger.Fatal(err)
}
}
if gc.CacheLife != "" {
cacheLife, err = time.ParseDuration(gc.CacheLife)
if err != nil {
logger.Error(err)
}
}
if gc.ProxyServer != "" {
proxyServer = gc.ProxyServer
}
allowUnsafeURL = gc.AllowUnsafeURL
}
var deferredFuncs []func()
func runDeferredFuncs() {
for _, f := range deferredFuncs {
f()
}
}
func obtainLock(lockFile string) error {
err := os.MkdirAll(filepath.Dir(lockFile), 0755)
if err != nil && !os.IsExist(err) {
return err
}
f, err := os.OpenFile(lockFile, os.O_RDWR|os.O_CREATE, 0600)
if err != nil && !os.IsExist(err) {
return err
}
c := make(chan error)
go func() {
c <- lock(f)
}()
ticker := time.NewTicker(5 * time.Second)
// 90% of all GooGet runs happen in < 60s, we wait 70s.
for i := 1; i < 15; i++ {
select {
case err := <-c:
if err != nil {
return err
}
return nil
case <-ticker.C:
fmt.Fprintln(os.Stdout, "GooGet lock already held, waiting...")
}
}
return errors.New("timed out waiting for lock")
}
func main() {
ggFlags := flag.NewFlagSet(filepath.Base(os.Args[0]), flag.ContinueOnError)
ggFlags.StringVar(&rootDir, "root", os.Getenv(envVar), "googet root directory")
ggFlags.BoolVar(&noConfirm, "noconfirm", false, "skip confirmation")
ggFlags.BoolVar(&verbose, "verbose", false, "print info level logs to stdout")
ggFlags.BoolVar(&systemLog, "system_log", true, "log to Linux Syslog or Windows Event Log")
ggFlags.BoolVar(&showVer, "version", false, "display GooGet version and exit")
if err := ggFlags.Parse(os.Args[1:]); err != nil && err != flag.ErrHelp {
logger.Fatal(err)
}
if showVer {
fmt.Println("GooGet version:", version)
os.Exit(0)
}
cmdr := subcommands.NewCommander(ggFlags, "googet")
cmdr.Register(cmdr.FlagsCommand(), "")
cmdr.Register(cmdr.CommandsCommand(), "")
cmdr.Register(cmdr.HelpCommand(), "")
cmdr.Register(&installCmd{}, "package management")
cmdr.Register(&downloadCmd{}, "package management")
cmdr.Register(&removeCmd{}, "package management")
cmdr.Register(&updateCmd{}, "package management")
cmdr.Register(&verifyCmd{}, "package management")
cmdr.Register(&installedCmd{}, "package query")
cmdr.Register(&latestCmd{}, "package query")
cmdr.Register(&availableCmd{}, "package query")
cmdr.Register(&listReposCmd{}, "repository management")
cmdr.Register(&addRepoCmd{}, "repository management")
cmdr.Register(&rmRepoCmd{}, "repository management")
cmdr.Register(&cleanCmd{}, "")
cmdr.ImportantFlag("verbose")
cmdr.ImportantFlag("noconfirm")
nonLockingCommands := []string{"help", "commands", "flags"}
if ggFlags.NArg() == 0 || goolib.ContainsString(ggFlags.Args()[0], nonLockingCommands) {
os.Exit(int(cmdr.Execute(context.Background())))
}
if rootDir == "" {
logger.Fatalf("The environment variable %q not defined and no '-root' flag passed.", envVar)
}
if err := os.MkdirAll(rootDir, 0774); err != nil {
logger.Fatalln("Error setting up root directory:", err)
}
lockFile = filepath.Join(rootDir, "googet.lock")
if err := obtainLock(lockFile); err != nil {
logger.Fatalf("Cannot obtain GooGet lock, you may need to run with admin rights, error: %v", err)
}
readConf(filepath.Join(rootDir, confFile))
logPath := filepath.Join(rootDir, logFile)
if err := rotateLog(logPath, logSize); err != nil {
logger.Error(err)
}
lf, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0660)
if err != nil {
runDeferredFuncs()
logger.Fatalln("Failed to open log file:", err)
}
deferredFuncs = append(deferredFuncs, func() { lf.Close() })
logger.Init("GooGet", verbose, systemLog, lf)
if err := os.MkdirAll(filepath.Join(rootDir, cacheDir), 0774); err != nil {
runDeferredFuncs()
logger.Fatalf("Error setting up cache directory: %v", err)
}
if err := os.MkdirAll(filepath.Join(rootDir, repoDir), 0774); err != nil {
runDeferredFuncs()
logger.Fatalf("Error setting up repo directory: %v", err)
}
es := cmdr.Execute(context.Background())
runDeferredFuncs()
os.Exit(int(es))
}