This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
package.go
126 lines (107 loc) · 2.56 KB
/
package.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
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"path"
"strings"
)
// Package is a struct to hold a repository name and the desired branch
// Config holds an array of Packages
type Package struct {
Name string
Branch string
GitUrl string
}
func (self *Package) IsGitHub() bool {
return strings.HasPrefix(self.Name, "github.com/")
}
func (self *Package) setGitUrl() {
self.GitUrl = strings.Replace(self.Name, "github.com", "github.com:", -1)
}
func (self *Package) setMirroredGitUrl(mirror string) {
self.GitUrl = strings.Replace(self.GitUrl, "github.com", mirror, -1)
}
func (self *Package) branchIsPath() bool {
if self.Branch == "" {
return false
}
if self.Branch[0] == '/' || self.Branch[0] == '.' || self.Branch == "self" {
return true
}
return false
}
func (self *Package) sourceExist() bool {
dir := getGoPath() + "/src/" + self.Name
if _, err := os.Stat(dir); os.IsNotExist(err) {
return false
}
return true
}
func (self *Package) getSource() {
var err error
if self.IsGitHub() {
err = git.clone(self)
} else {
var out []byte
if out, err = exec.Command("go", "get", "-d", self.Name).CombinedOutput(); err != nil {
err = errors.New(string(out))
}
}
if err != nil {
delErr := os.RemoveAll(path.Dir(getGoPath() + "/src/" + self.Name))
if delErr != nil {
fmt.Printf("An error occured while getting the source, but I am unable to tidy up\n")
fmt.Printf("Please remove %s manually\n\n", path.Dir(getGoPath()+"/src/"+self.Name))
}
stderrAndExit(err)
}
}
func (self *Package) updateSource() {
var err error
if self.IsGitHub() {
err = git.pull(self)
} else {
var out []byte
if out, err = exec.Command("go", "get", "-d", "-u", self.Name).CombinedOutput(); err != nil {
err = errors.New(string(out))
}
}
if err != nil {
stderrAndExit(err)
}
}
func (self *Package) setHead() {
err := git.checkout(self, "")
if err != nil {
stderrAndExit(err)
}
}
func (self *Package) createSymlink() {
name, err := os.Getwd()
if err != nil {
stderrAndExit(err)
}
name += "/" + self.Branch
err = os.RemoveAll(getGoPath() + "/src/" + self.Name)
if err != nil {
stderrAndExit(err)
}
os.MkdirAll(path.Dir(getGoPath()+"/src/"+self.Name), 0777)
err = os.Symlink(name, getGoPath()+"/src/"+self.Name)
if err != nil {
stderrAndExit(err)
}
}
type Packages []*Package
func (self Packages) Len() int {
return len(self)
}
func (self Packages) Swap(i, j int) {
self[i], self[j] = self[j], self[i]
}
type ByName struct{ Packages }
func (self *ByName) Less(i, j int) bool {
return self.Packages[i].Name < self.Packages[j].Name
}