/
init.go
51 lines (42 loc) · 1.08 KB
/
init.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
package books
import (
"os"
"os/user"
"path"
"regexp"
"strings"
"text/template"
)
var (
absroot = "~/.goplay"
identityfile = path.Join(absroot, "ansible_rsa.pub")
playfile = path.Join(absroot, "Playfile.yml")
playfiletpl = template.Must(template.ParseFiles("./Playfile.yml"))
// ansible
rversion = regexp.MustCompile(`^ansible +?([\d.]+?)[\d.]*?`)
rconfig = regexp.MustCompile(`config file\W*?=\W*?([\w/.]+)`)
rinventory = regexp.MustCompile(`#?inventory\W*?=\W*?[\w/.]+`)
defaultVersion = "2"
defaultConfigFile = path.Join(absroot, "ansible.cfg")
)
func init() {
absroot = abspath(absroot)
identityfile = abspath(identityfile)
playfile = abspath(playfile)
defaultConfigFile = abspath(defaultConfigFile)
err := os.MkdirAll(absroot, 0755)
if err != nil {
panic(err)
}
}
func abspath(filename string) string {
filename = path.Clean(filename)
if !strings.HasPrefix(filename, "~/") {
return filename
}
cu, err := user.Current()
if err == nil {
filename = strings.Replace(filename, "~", cu.HomeDir, 1)
}
return filename
}