-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup.go
More file actions
188 lines (146 loc) · 3.27 KB
/
setup.go
File metadata and controls
188 lines (146 loc) · 3.27 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"errors"
"fmt"
"io/fs"
"os"
"github.com/fatih/color"
"github.com/manifoldco/promptui"
)
// needsSetup checks if the setup process needs to be executed.
func needsSetup() (need bool, err error) {
isFolder, err := isDevcertFolder()
if err != nil {
return
}
if isFolder == false {
return true, nil
}
isCA, err := isValidCA()
if err != nil {
return
}
if isCA == false {
return true, nil
}
return false, nil
}
// isDevcertFolder checks if the necessary folder exists.
func isDevcertFolder() (is bool, err error) {
devcertDir, err := buildDevcertDir()
if err != nil {
return false, err
}
_, err = os.Stat(devcertDir)
notExist := errors.Is(err, fs.ErrNotExist)
if err != nil && notExist == false {
return false, err
}
is = notExist == false
return is, nil
}
// isValidCA check if the certificate authority files are valid.
func isValidCA() (is bool, err error) {
ca, err := loadCA()
if err != nil {
return
}
is = ca.Valid
return
}
// setupPrompt will ask the user to continue or not.
func setupPrompt() (ok bool, err error) {
prompt := promptui.Prompt{
Label: "Do you want to continue?",
IsConfirm: true,
}
_, err = prompt.Run()
if err != nil {
if errors.Is(err, promptui.ErrAbort) {
err = nil
return
}
return
}
return true, nil
}
// setup will start the setup process.
func setup() (err error) {
devcertDir, err := buildDevcertDir()
if err != nil {
return
}
color.Set(color.FgWhite, color.Bold)
fmt.Printf("devcert needs to execute the setup process first:")
color.Unset()
fmt.Printf("\n - It will create the")
color.Set(color.FgCyan)
fmt.Printf(" %s ", devcertDir)
color.Unset()
fmt.Printf("directory.")
fmt.Printf("\n - It will create a local certificate authority (CA) to sign future certificates.")
fmt.Printf("\n - It will mark the CA as trusted locally.\n")
promptOK, err := setupPrompt()
if err != nil {
return
}
if promptOK == false {
os.Exit(0)
return
}
// Continue the setup process.
err = createDevcertDir()
if err != nil {
err = fmt.Errorf("setup failed: %w", err)
attemptCleanupDevcertDir()
return
}
err = createCA()
if err != nil {
err = fmt.Errorf("setup failed: %w", err)
attemptCleanupDevcertDir()
return
}
err = trustCA()
if err != nil {
err = fmt.Errorf("setup failed: %w", err)
attemptCleanupCA()
return
}
return
}
// createDevcertDir creates the .devcert directory in the user's home directory.
func createDevcertDir() (err error) {
fmt.Printf("Creating directory...\n")
devcertDir, err := buildDevcertDir()
if err != nil {
err = fmt.Errorf("creating .devcert directory failed: %w", err)
return
}
isDevcertDir, err := isDevcertFolder()
if err != nil {
err = fmt.Errorf("creating .devcert directory failed: %w", err)
return
}
// The directory already exists.
if isDevcertDir == true {
fmt.Printf("Directory")
color.Set(color.FgCyan)
fmt.Printf(" %s ", devcertDir)
color.Unset()
fmt.Printf("is already created.\n")
return
}
// Create the directory
err = os.MkdirAll(devcertDir, 0755)
if err != nil {
err = fmt.Errorf("creating .devcert directory failed: %w", err)
return
}
fmt.Printf("Directory")
color.Set(color.FgCyan)
fmt.Printf(" %s ", devcertDir)
color.Unset()
fmt.Printf("created.\n")
return
}