forked from rainycape/gondola
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packages.go
94 lines (86 loc) · 2.24 KB
/
packages.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
package docs
import (
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"gnd.la/log"
)
var (
// Groups includes the packages to list in the package index.
// See the Group struct for further information.
Groups []*Group
ticker *time.Ticker
)
// Group represents a group of packages to be displayed under the same
// title. Note that all subpackages of any included package will also
// be listed. Packages must be referred by their import path (e.g.
// example.com/pkg).
type Group struct {
Title string
Packages []string
}
// StartUpdatingPackages starts regularly updating the packages listed
// in Groups at the given interval. Note that for updating packages, the
// system should have installed the client for the SCM systems used by
// them (e.g. git, hg, etc...). A working Go installation on the host
// is also required, since go get will be used to download them.
func StartUpdatingPackages(interval time.Duration) {
StopUpdatingPackages()
go updatePackages()
ticker = time.NewTicker(interval)
go func() {
for _ = range ticker.C {
updatePackages()
}
}()
}
// StopUpdatingPackages stops updating packages. Note that an in-flight update
// won't be stopped, but no more updates will be scheduled.
func StopUpdatingPackages() {
if ticker != nil {
ticker.Stop()
ticker = nil
}
}
func updatePackages() {
for _, gr := range Groups {
for _, pkg := range gr.Packages {
if err := updatePackage(pkg); err != nil {
log.Errorf("error updating %s: %s", pkg, err)
}
}
}
}
func updatePackage(pkg string) error {
if strings.HasSuffix(pkg, "/") {
pkg += "..."
}
goBin := "go"
env := make(map[string]string)
for _, v := range os.Environ() {
if eq := strings.Index(v, "="); eq >= 0 {
env[v[:eq]] = v[eq+1:]
}
}
if goRoot := DefaultContext.GOROOT; goRoot != "" {
goBin = filepath.Join(goRoot, "bin", "go")
env["GOROOT"] = goRoot
}
if goPath := DefaultContext.GOPATH; goPath != "" {
env["GOPATH"] = goPath
}
cmd := exec.Command(goBin, "get", "-u", "-v", pkg)
cmdEnv := make([]string, 0, len(env))
for k, v := range env {
cmdEnv = append(cmdEnv, k+"="+v)
}
cmd.Env = cmdEnv
log.Debugf("Updating package %s", pkg)
if log.Level() == log.LDebug {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}
return cmd.Run()
}