/
init.go
152 lines (130 loc) · 3.9 KB
/
init.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
package appcmd
import (
"embed"
"fmt"
"github.com/gookit/gcli/v3"
"github.com/gookit/goutil"
"github.com/gookit/goutil/byteutil"
"github.com/gookit/goutil/cliutil"
"github.com/gookit/goutil/fsutil"
"github.com/gookit/goutil/structs"
"github.com/gookit/goutil/sysutil"
"github.com/inhere/kite-go"
"github.com/inhere/kite-go/internal/app"
"github.com/inhere/kite-go/internal/appconst"
)
var ikOpts = struct {
dryRun bool
force bool
}{}
// KiteInitCmd command
var KiteInitCmd = &gcli.Command{
Name: "init",
Desc: "init kite .env, config to the user home dir",
Config: func(c *gcli.Command) {
c.BoolOpt2(&ikOpts.dryRun, "dry-run, dry", "run workflow, dont real execute")
c.BoolOpt2(&ikOpts.force, "force, f", "force re-init kite app config")
},
Func: func(c *gcli.Command, args []string) error {
c.Warnln("Pre-check:")
uHome := sysutil.UserHomeDir()
c.Infoln("Found user home dir:", uHome)
baseDir := app.App().BaseDir
dotenvFile := fsutil.JoinPaths(baseDir, appconst.DotEnvFileName)
confFile := fsutil.JoinPaths(baseDir, appconst.KiteConfigName)
c.Infoln("Will init kite .env and config and more...")
if ikOpts.dryRun {
c.Warnln("TIP: on DRY-RUN mode, will not be execute any operation")
}
qr := goutil.NewQuickRun()
qr.Add(func(ctx *structs.Data) error {
idx := ctx.IntVal("index") + 1
c.Infof("%d. Make the kite base dir: %s\n", idx, baseDir)
if ikOpts.dryRun {
return nil
}
if !ikOpts.force && fsutil.IsFile(baseDir) {
c.Warnln(" Exists, skip write!")
return nil
}
return fsutil.Mkdir(baseDir, fsutil.DefaultDirPerm)
}, func(ctx *structs.Data) error {
idx := ctx.IntVal("index") + 1
c.Infof("%d. Init the .env file: %s\n", idx, dotenvFile)
if ikOpts.dryRun {
return nil
}
if !ikOpts.force && fsutil.IsFile(dotenvFile) {
c.Warnln(" Exists, skip write!")
return nil
}
text := byteutil.SafeString(kite.EmbedFs.ReadFile(".example.env"))
_, err := fsutil.PutContents(dotenvFile, text, fsutil.FsCWTFlags)
return err
}, func(ctx *structs.Data) error {
idx := ctx.IntVal("index") + 1
c.Infof("%d. Init the main config file: %s\n", idx, confFile)
if ikOpts.dryRun {
return nil
}
if !ikOpts.force && fsutil.IsFile(confFile) {
c.Warnln(" Exists, skip write!")
return nil
}
text := byteutil.SafeString(kite.EmbedFs.ReadFile("kite.example.yml"))
_, err := fsutil.PutContents(confFile, text, fsutil.FsCWTFlags)
return err
}, func(ctx *structs.Data) error {
subDirs := []string{"config", "data", "scripts", "plugins", "tmp"}
idx := ctx.IntVal("index") + 1
c.Infof("%d. Init subdir in %s: %v\n", idx, baseDir, subDirs)
if ikOpts.dryRun {
return nil
}
return fsutil.MkSubDirs(fsutil.DefaultDirPerm, baseDir, subDirs...)
}, func(ctx *structs.Data) error {
idx := ctx.IntVal("index") + 1
c.Infof("%d. Init kite config files to %s/config\n", idx, baseDir)
if ikOpts.dryRun {
return nil
}
return exportEmbedDir(kite.EmbedFs, "config", baseDir+"/config", true)
})
cliutil.Warnln("\nStarting init:")
err := qr.Run()
if err == nil {
cliutil.Successln("✅ Init Completed")
}
return err
},
}
func exportEmbedDir(efs embed.FS, dirPath, dstDir string, exportSub bool) error {
entries, err := efs.ReadDir(dirPath)
if err != nil {
return err
}
for _, entry := range entries {
name := entry.Name()
path := dirPath + "/" + name
if entry.IsDir() {
if exportSub {
err = exportEmbedDir(efs, path, dstDir+"/"+name, exportSub)
}
continue
}
fmt.Print(" Read and init the ", path)
dstFile := dstDir + "/" + name
if !ikOpts.force && fsutil.IsFile(dstFile) {
cliutil.Warnln(" Exists")
continue
}
text := byteutil.SafeString(kite.EmbedFs.ReadFile(path))
_, err = fsutil.PutContents(dstFile, text, fsutil.FsCWTFlags)
if err != nil {
cliutil.Errorln(" ERROR")
return err
}
cliutil.Successln(" OK")
}
return nil
}