forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config-sites.go
197 lines (184 loc) · 5.26 KB
/
config-sites.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
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
189
190
191
192
193
194
195
196
197
package cmd
import (
"fmt"
"strconv"
"strings"
"github.com/manifoldco/promptui"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/config"
"github.com/pydio/cells/common/proto/install"
)
var sitesCmd = &cobra.Command{
Use: "sites",
Short: "Manage sites where application is exposed",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
sites, e := config.LoadSites(true)
fatalQuitIfError(cmd, e)
if len(sites) == 0 {
fmt.Println("No site is currently configured. Cells exposes automatically the following URLs : ")
ss, _ := config.LoadSites()
autoSite := ss[0]
for _, u := range autoSite.GetBindURLs() {
fmt.Println(" - " + u)
}
p := &promptui.Prompt{
Label: "Do you want to create a new site? It will replace the automatic one",
IsConfirm: true,
}
if _, e := p.Run(); e == nil {
sitesAdd.Run(cmd, args)
}
} else {
fmt.Println("The following sites are currently defined:")
listSites(cmd, sites)
editString := "Edit an existing site"
deleteString := "Delete an existing site"
if len(sites) == 1 {
editString = "Edit current site"
deleteString = "Delete current site (will fallback to defaults)"
}
actionP := promptui.Select{
Items: []string{
"Add a new site",
editString,
deleteString,
"Quit",
},
Label: "What do you want to do",
}
action, _, e := actionP.Run()
fatalQuitIfError(cmd, e)
switch action {
case 0:
sitesAdd.Run(cmd, args)
case 1:
index := 0
if len(sites) > 1 {
p := &promptui.Prompt{
Label: "Provide the site number to edit",
Validate: func(s string) error {
i, e := strconv.ParseInt(s, 10, 64)
if e != nil {
return e
}
if int(i) >= len(sites) {
return fmt.Errorf("please provide a number smaller than %d", len(sites))
}
return nil
},
}
if n, e := p.Run(); e != nil || n == "" {
return
} else if idx, e := strconv.ParseInt(n, 10, 64); e == nil && int(idx) < len(sites) {
index = int(idx)
}
}
e := promptSite(sites[index], true)
fatalQuitIfError(cmd, e)
e = confirmAndSave(cmd, sites)
fatalQuitIfError(cmd, e)
case 2:
index := "0"
if len(sites) > 1 {
p := &promptui.Prompt{
Label: "Provide the site number to be remove",
Validate: func(s string) error {
i, e := strconv.ParseInt(s, 10, 64)
if e != nil {
return e
}
if int(i) >= len(sites) {
return fmt.Errorf("please provide a number smaller than %d", len(sites))
}
return nil
},
}
if n, e := p.Run(); e != nil || n == "" {
return
} else if idx, e := strconv.ParseInt(n, 10, 64); e == nil && int(idx) < len(sites) {
index = n
}
}
sitesDelete.Run(cmd, []string{index})
case 3:
return
}
cmd.Run(cmd, args)
}
},
}
func listSites(cmd *cobra.Command, sites []*install.ProxyConfig) {
oneHasMaintenance := false
for _, s := range sites {
if s.Maintenance {
oneHasMaintenance = true
break
}
}
table := tablewriter.NewWriter(cmd.OutOrStdout())
table.SetRowLine(true)
headers := []string{"#", "Bind(s)", "TLS", "External URL"}
if oneHasMaintenance {
headers = append(headers, "Maintenance Mode")
}
table.SetHeader(headers)
for i, s := range sites {
tlsString := "No Tls"
if s.TLSConfig != nil {
// TLSConfig
switch s.TLSConfig.(type) {
case *install.ProxyConfig_SelfSigned:
tlsString = "Self-signed"
case *install.ProxyConfig_LetsEncrypt:
tlsString = "Lets Encrypt"
case *install.ProxyConfig_Certificate:
tlsString = "Custom Certificate"
}
}
data := []string{fmt.Sprintf("%d", i), strings.Join(s.GetBindURLs(), ", "), tlsString, s.ReverseProxyURL}
if oneHasMaintenance {
m := ""
if s.Maintenance {
m = "On"
if len(s.MaintenanceConditions) > 0 {
m = strings.Join(s.MaintenanceConditions, ",")
}
}
data = append(data, m)
}
table.Append(data)
}
table.Render()
}
func confirmAndSave(cmd *cobra.Command, sites []*install.ProxyConfig) error {
// Reprint before saving
cmd.Println("*************************************************")
cmd.Println(" Please review your parameters before saving ")
cmd.Println("*************************************************")
listSites(cmd, sites)
confirm := promptui.Prompt{Label: "Do you want to save this configuration", IsConfirm: true}
if _, e := confirm.Run(); e == nil {
e = config.SaveSites(sites, common.PydioSystemUsername, "Updating config sites")
if e != nil {
cmd.Println("***********************************************")
cmd.Println("[ERROR] Could not save config : " + e.Error())
cmd.Println("***********************************************")
return e
} else {
cmd.Println("***********************************************")
cmd.Println(" Config has been updated, please restart now! ")
cmd.Println("***********************************************")
}
} else {
cmd.Println("***********************************************")
cmd.Println(" Operation aborted, nothing has been saved ")
cmd.Println("***********************************************")
}
return nil
}
func init() {
ConfigCmd.AddCommand(sitesCmd)
}