-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathsetup.go
93 lines (71 loc) · 2.39 KB
/
setup.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
package cmd
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/semaphoreui/semaphore/cli/setup"
"github.com/semaphoreui/semaphore/db"
"github.com/semaphoreui/semaphore/db/factory"
"github.com/semaphoreui/semaphore/util"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(setupCmd)
}
var setupCmd = &cobra.Command{
Use: "setup",
Short: "Perform interactive setup",
Run: func(cmd *cobra.Command, args []string) {
doSetup()
},
}
// nolint: gocyclo
func doSetup() int {
config := &util.ConfigType{}
config.GenerateSecrets()
setup.InteractiveSetup(config)
resultConfigPath := setup.SaveConfig(config, "config.json", persistentFlags.configPath)
util.ConfigInit(resultConfigPath, false)
fmt.Println(" Pinging db..")
store := factory.CreateStore()
defer store.Close("setup")
store.Connect("setup")
fmt.Println("Running db Migrations..")
if err := db.Migrate(store, nil); err != nil {
fmt.Printf("Database migrations failed!\n %v\n", err.Error())
os.Exit(1)
}
stdin := bufio.NewReader(os.Stdin)
var user db.UserWithPwd
user.Username = readNewline("\n\n > Username: ", stdin)
user.Username = strings.ToLower(user.Username)
user.Email = readNewline(" > Email: ", stdin)
user.Email = strings.ToLower(user.Email)
existingUser, err := store.GetUserByLoginOrEmail(user.Username, user.Email)
util.LogWarning(err)
if existingUser.ID > 0 {
// user already exists
fmt.Printf("\n Welcome back, %v! (a user with this username/email is already set up..)\n\n", existingUser.Name)
} else {
user.Name = readNewline(" > Your name: ", stdin)
user.Pwd = readNewline(" > Password: ", stdin)
user.Admin = true
if _, err := store.CreateUser(user); err != nil {
fmt.Printf(" Inserting user failed. If you already have a user, you can disregard this error.\n %v\n", err.Error())
os.Exit(1)
}
fmt.Printf("\n You are all setup %v!\n", user.Name)
}
fmt.Printf(" Re-launch this program pointing to the configuration file\n\n./semaphore server --config %v\n\n", resultConfigPath)
fmt.Printf(" To run as daemon:\n\nnohup ./semaphore server --config %v &\n\n", resultConfigPath)
fmt.Printf(" You can login with %v or %v.\n", user.Email, user.Username)
return 0
}
func readNewline(pre string, stdin *bufio.Reader) string {
fmt.Print(pre)
str, err := stdin.ReadString('\n')
util.LogWarning(err)
str = strings.Replace(strings.Replace(str, "\n", "", -1), "\r", "", -1)
return str
}