-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathci.go
More file actions
132 lines (118 loc) · 2.72 KB
/
ci.go
File metadata and controls
132 lines (118 loc) · 2.72 KB
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
package ci
import (
"bytes"
"fmt"
"os"
"os/exec"
"time"
"github.com/gophergala2016/hugoku/store"
)
// Step is a task to perform during the deployment
type Step struct {
Command string
Args []string
Stdout string
Stderr string
}
// ExecuteCommand ...
func (s *Step) ExecuteCommand() error {
cmd := exec.Command(s.Command, s.Args...)
stdout, err := cmd.StdoutPipe()
buf := new(bytes.Buffer)
if err != nil {
return err
}
stderr, err := cmd.StderrPipe()
bufErr := new(bytes.Buffer)
if err != nil {
return err
}
if err := cmd.Start(); err != nil {
return err
}
_, err = buf.ReadFrom(stdout)
if err != nil {
return err
}
_, err = bufErr.ReadFrom(stderr)
if err != nil {
return err
}
_, err = bufErr.ReadFrom(stderr)
if err != nil {
return err
}
if err := cmd.Wait(); err != nil {
return err
}
s.Stdout = buf.String()
s.Stderr = bufErr.String()
return err
}
func initCommandsNewSite(username string, name string, path string) []Step {
return []Step{
Step{
Command: "hugo",
Args: []string{"new", "site", path},
Stdout: "",
Stderr: "",
},
Step{
Command: "git",
Args: []string{"clone", "https://github.com/hbpasti/heather-hugo.git", path + "/themes/heather-hugo"},
Stdout: "",
Stderr: "",
},
Step{
Command: "hugo",
Args: []string{"-s", path, "--theme=heather-hugo"},
Stdout: "",
Stderr: "",
},
}
}
func initCommandsExistingSite(username string, name string, path string) []Step {
return []Step{}
/*
commands = append(commands, Step{
Command: "git",
Args: []string{"pull", "origin", "master"},
Stdout: "",
Stderr: "",
})
commands = append(commands, Step{
Command: "hugo",
Stdout: "",
Stderr: "",
})
*/
}
// Build compiles a project
func Build(username string, name string, path string) (store.BuildInfo, error) {
var commands []Step
var buildInfo = store.BuildInfo{BuildPath: path, BuildTime: time.Now()}
if _, err := os.Stat(path); os.IsNotExist(err) {
commands = initCommandsNewSite(username, name, path)
} else {
commands = initCommandsExistingSite(username, name, path)
}
for i := range commands {
err := commands[i].ExecuteCommand()
buildInfo.BuildLog += commands[i].Stdout
buildInfo.BuildErrorLog += commands[i].Stderr
if err != nil {
buildInfo.BuildDuration = time.Since(buildInfo.BuildTime)
buildInfo.BuildStatus = "fail"
return buildInfo, err
}
}
buildInfo.BuildDuration = time.Since(buildInfo.BuildTime)
buildInfo.BuildStatus = "ok"
return buildInfo, nil
}
// Deploy deploys a project
func Deploy(username string, name string) (store.BuildInfo, error) {
path := fmt.Sprintf("./repos/%s/%s", username, name)
buildInfo, err := Build(username, name, path)
return buildInfo, err
}