forked from drone-plugins/drone-hg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
154 lines (132 loc) · 2.64 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
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"
"github.com/drone/drone-plugin-go/plugin"
)
var hgrcFile = `
[auth]
drone.prefix = %s
drone.username = %s
drone.password = %s
drone.schemes = http https
`
// Params stores the git clone parameters used to
// configure and customzie the git clone behavior.
type Params struct {
}
var (
buildCommit string
)
func main() {
fmt.Printf("Drone Mercurial Plugin built from %s\n", buildCommit)
v := new(Params)
r := new(plugin.Repo)
b := new(plugin.Build)
w := new(plugin.Workspace)
plugin.Param("repo", r)
plugin.Param("build", b)
plugin.Param("workspace", w)
plugin.Param("vargs", &v)
plugin.MustParse()
err := run(r, b, w, v)
if err != nil {
os.Exit(1)
}
}
// run clones the repository and build revision
// into the build workspace.
func run(r *plugin.Repo, b *plugin.Build, w *plugin.Workspace, v *Params) error {
err := os.MkdirAll(w.Path, 0777)
if err != nil {
fmt.Printf("Error creating directory %s. %s\n", w.Path, err)
return err
}
// generate the .hgrc file
if err := writeHgrc(w); err != nil {
fmt.Fprintln(os.Stderr, err)
return err
}
var cmds []*exec.Cmd
if isDirEmpty(filepath.Join(w.Path, ".hg")) {
cmds = append(cmds, initHg())
}
cmds = append(cmds, pull(r, b))
cmds = append(cmds, update(b))
for _, cmd := range cmds {
cmd.Dir = w.Path
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
trace(cmd)
err := cmd.Run()
if err != nil {
return err
}
}
return nil
}
func initHg() *exec.Cmd {
return exec.Command(
"hg",
"init",
".",
)
}
func pull(r *plugin.Repo, b *plugin.Build) *exec.Cmd {
return exec.Command(
"hg",
"pull",
"-r",
b.Commit,
r.Clone,
)
}
func update(b *plugin.Build) *exec.Cmd {
return exec.Command(
"hg",
"update",
b.Commit,
)
}
// Trace writes each command to standard error (preceded by a ‘$ ’) before it
// is executed. Used for debugging your build.
func trace(cmd *exec.Cmd) {
fmt.Println("$", strings.Join(cmd.Args, " "))
}
// Writes the hgrc file.
func writeHgrc(in *plugin.Workspace) error {
if in.Netrc == nil || len(in.Netrc.Machine) == 0 {
return nil
}
out := fmt.Sprintf(
hgrcFile,
in.Netrc.Machine, // TODO this may require adding http(s) prefix
in.Netrc.Login,
in.Netrc.Password,
)
home := "/root"
u, err := user.Current()
if err == nil {
home = u.HomeDir
}
path := filepath.Join(home, ".hgrc")
return ioutil.WriteFile(path, []byte(out), 0600)
}
func isDirEmpty(name string) bool {
f, err := os.Open(name)
if err != nil {
return true
}
defer f.Close()
_, err = f.Readdir(1)
if err == io.EOF {
return true
}
return false
}