-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
135 lines (107 loc) · 2.61 KB
/
main.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
package main
import (
"embed"
"os"
"os/exec"
"path/filepath"
"time"
"github.com/local-deploy/dl/command"
"github.com/local-deploy/dl/utils"
"github.com/pterm/pterm"
"github.com/spf13/viper"
)
var version = "dev"
//go:embed templates/*
var templates embed.FS
func main() {
pterm.ThemeDefault.SecondaryStyle = pterm.Style{pterm.FgDefault, pterm.BgDefault}
// forwarding file variable to package
utils.Templates = templates
if !dockerCheck() {
return
}
if utils.IsNeedInstall() {
firstStart()
}
if !utils.IsCertPathExists() {
createCertDirectory()
}
initConfig()
command.Execute()
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
configDir := utils.ConfigDir()
viper.AddConfigPath(configDir)
viper.SetConfigType("yaml")
viper.SetConfigName("config")
err := viper.ReadInConfig()
if err != nil {
pterm.FgRed.Printfln("Error config file: %s \n", err)
os.Exit(1)
}
if viper.GetString("version") != version {
viper.Set("version", version)
err = viper.WriteConfig()
if err != nil {
pterm.FgRed.Printfln("Error config file: %s \n", err)
os.Exit(1)
}
}
viper.AutomaticEnv()
}
func firstStart() {
err := createConfigFile()
if err != nil {
pterm.FgRed.Printfln("Unable to create config file: %s \n", err)
os.Exit(1)
}
err = utils.CreateTemplates(true)
if err != nil {
pterm.FgRed.Printfln("Unable to create template files: %s \n", err)
os.Exit(1)
}
// remove old template directory
err = utils.RemovePath(filepath.Join(utils.ConfigDir(), "config-files"))
if err != nil {
pterm.FgRed.Printfln("Unable to remove old template files: %s \n", err)
}
}
func createConfigFile() error {
configDir := utils.ConfigDir()
err := utils.CreateDirectory(configDir)
if err != nil {
return err
}
// do not overwrite the config if it exists
if utils.PathExists(filepath.Join(configDir, "config.yaml")) {
return nil
}
viper.AddConfigPath(configDir)
viper.SetConfigType("yaml")
viper.SetConfigName("config")
viper.Set("version", version)
viper.Set("locale", "en")
viper.Set("repo", "ghcr.io")
viper.Set("check-updates", time.Now())
errWrite := viper.SafeWriteConfig()
if errWrite != nil {
return errWrite
}
return errWrite
}
func createCertDirectory() {
err := utils.CreateDirectory(filepath.Join(utils.CertDir(), "conf"))
if err != nil {
pterm.FgRed.Printfln("Unable to create certs directory: %s \n", err)
os.Exit(1)
}
}
func dockerCheck() bool {
_, err := exec.LookPath("docker")
if err != nil {
pterm.FgRed.Printfln("Docker not found. Please install it. https://docs.docker.com/engine/install/")
return false
}
return true
}