Repository navigation
Expand file tree
/
Copy pathcommandInit.go
More file actions
103 lines (83 loc) · 2.06 KB
/
Copy pathcommandInit.go
File metadata and controls
103 lines (83 loc) · 2.06 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
package main
import (
"path/filepath"
flag "github.com/spf13/pflag"
"github.com/tehmoon/errors"
"github.com/spf13/cobra"
"os"
"github.com/spf13/viper"
"github.com/abiosoft/ishell"
)
type CommandInit struct {
password string
flags *viper.Viper
config *viper.Viper
}
func (cmd *CommandInit) Init(set *flag.FlagSet, args []string) (error) {
path := cmd.flags.GetString("path")
if len(args) == 1 {
path = args[0]
}
err := InitDirectories(path)
if err != nil {
return err
}
return nil
}
func CheckInitDirectory(base string) (error) {
d, err := os.Open(filepath.Join(base, ".easy-ca"))
if err != nil {
return err
}
defer d.Close()
return nil
}
func InitDirectories(base string) (error) {
err := createDirectory(base)
if err != nil {
return errors.Wrap(err, "Error creating base directory")
}
err = createDirectory(filepath.Join(base, "certificates"))
if err != nil {
return errors.Wrap(err, "Error creating certificates directory")
}
err = createDirectory(filepath.Join(base, "keys"))
if err != nil {
return errors.Wrap(err, "Error creating keys directory")
}
err = createDirectory(filepath.Join(base, "revoked"))
if err != nil {
return errors.Wrap(err, "Error creating revoked directory")
}
f, err := os.Create(filepath.Join(base, ".easy-ca"))
if err != nil {
return errors.Wrap(err, "Unable to create \".easy-ca\" file")
}
defer f.Close()
return nil
}
func (cmd CommandInit) Config() (*viper.Viper) {
return cmd.config
}
func (cmd CommandInit) Flags() (*viper.Viper) {
return cmd.flags
}
func (cmd CommandInit) Do() (error) {
path := cmd.flags.GetString("path")
dk, err := InitPasswordFile(filepath.Join(path, ".pass"), cmd.config.GetString("password"))
if err != nil {
return errors.Wrap(err, "Error creating the \".pass\" file")
}
cmd.config.Set("key", dk)
return nil
}
func NewCommandInit(config, flags *viper.Viper, ctx *ishell.Context) (*cobra.Command) {
return &cobra.Command{
Use: "init [path]",
Args: cobra.MaximumNArgs(1),
RunE: ExecuteCommand(&CommandInit{
flags: flags,
config: config,
}, ctx),
}
}