/
continuum.go
105 lines (95 loc) · 2.38 KB
/
continuum.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
/*
* Main file for Continuum.
*/
package main
import (
"fmt"
"os"
"os/user"
"path"
"time"
)
// Help page
const Help = `Usage: goontinuum [configuration.yml]
Where configuration.yml is as follows:
directory: /tmp
status: /tmp/continuum-status.yml
port: 6666
email:
smtp-host: smtp.example.com:25
recipient: somebody@somewhere.net
sender: somebody@somewhere.net
success: true
one: true
modules:
- name: continuum
url: git@github.com:c4s4/continuum.git
branch: develop
command: |
set -e
make test
If configuration file is not passed on command line, it will be searched at
following locations:
- ~/.continuum.yml
- /etc/continuum.yml`
// FileExists tells if a given file exists.
func FileExists(file string) bool {
if _, err := os.Stat(file); err == nil {
return true
}
return false
}
// FindConfiguration looks for a configuration file as:
// - ~/.continuum.yml
// - /etc/continuum.yml
// If none of these is found, it stops the program and prints help, else it returns
// the path of the configuration file.
func FindConfiguration() string {
usr, _ := user.Current()
home := usr.HomeDir
config := path.Join(home, ".continuum.yml")
if FileExists(config) {
return config
}
config = "/etc/continuum.yml"
if FileExists(config) {
return config
}
fmt.Println("ERROR: no configuration file found")
fmt.Println(Help)
os.Exit(1)
return ""
}
// CheckArguments checks arguments passed on command line and prints help if
// -h or if an error occurs. Return the path of the configuration file.
func CheckArguments() string {
if len(os.Args) > 2 {
fmt.Println("ERROR: You must pass configuration file on command line")
fmt.Println(Help)
os.Exit(1)
}
if len(os.Args) == 2 {
if os.Args[1] == "-h" || os.Args[1] == "--help" {
fmt.Println(Help)
os.Exit(0)
return ""
}
return os.Args[1]
}
return FindConfiguration()
}
// main function that iterate on configuration files passed on command line.
func main() {
configFile := CheckArguments()
config := LoadConfig(configFile)
if IsAnotherInstanceRunning(config.Port) {
fmt.Fprintf(os.Stderr, "Another instance is already running, aborting")
os.Exit(0)
} else {
start := time.Now()
fmt.Println("Continuum running at", start.Format("2006-01-02 15:04"))
BuildModules(config)
duration := time.Since(start)
fmt.Println("Done in", duration)
}
}