-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.go
100 lines (77 loc) · 1.72 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
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
package main
import (
"io/ioutil"
"encoding/json"
"os"
)
var folders [3]string = [...]string{"root", "intermediates", "clients"}
func getHelpInit() string {
return `Usage: simpleca init
Init the current folder as a simpleca repository.
It will create root/, intermediates/ and clients/ folders as well as an empty state file and a generic configuration file.
You can run it multiple times, it won't overwrite or delete your content.`
}
// Do some cheap checks to know if we're in a simpleca folder
func isRepo() bool {
var err error
if _, err = os.Stat(statePath); os.IsNotExist(err) {
return false
}
if _, err = os.Stat(confPath); os.IsNotExist(err) {
return false
}
for _, f := range folders {
if _, err = os.Stat(f); os.IsNotExist(err) {
return false
}
}
return true
}
// Can't call it init() because of go
func init_() error {
var err error
// Create and chmod all subfolders
for _, f := range folders {
err = os.Mkdir(f, 0700)
if err != nil && ! os.IsExist(err) {
return err
}
err = os.Chmod(f, 0700)
if err != nil {
return err
}
}
// Init empty State and configuration if needed
if _, err = os.Stat(statePath); os.IsNotExist(err) {
f, err := os.Create(statePath)
if err != nil {
return err
}
_, err = f.WriteString("{}\n")
if err != nil {
return err
}
}
if _, err = os.Stat(confPath); os.IsNotExist(err) {
_, err := os.Create(confPath)
if err != nil {
return err
}
// No config file: create one
var conf Conf = Conf{
36,
"SimpleCA",
"France",
"Paris",
}
b, err := json.MarshalIndent(conf, "", " ")
if err != nil {
return err
}
err = ioutil.WriteFile(confPath, b, 0644)
if err != nil {
return err
}
}
return nil
}