-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
182 lines (171 loc) · 5.04 KB
/
root.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
// Package cmd /*
package command
import (
"context"
"embed"
"encoding/json"
"errors"
"fmt"
"github.com/fatih/color"
"github.com/kcmvp/gob/cmd/gbc/artifact"
"github.com/kcmvp/gob/utils"
"github.com/samber/lo"
"github.com/spf13/cobra"
"github.com/tidwall/gjson"
"os" //nolint
"path/filepath"
"strings" //nolint
"sync" //nolint
)
const (
resourceDir = "resources"
tmplDir = "tmpl"
)
var (
//go:embed resources/*
resources embed.FS
//go:embed tmpl/*
templates embed.FS
once sync.Once
usage string
)
func usageTemplate() string {
once.Do(func() {
bytes, _ := templates.ReadFile(fmt.Sprintf("%s/usage.tmpl", tmplDir))
usage = color.YellowString(string(bytes))
})
return usage
}
func parseArtifacts(cmd *cobra.Command, args []string, name string) (gjson.Result, error) {
var result gjson.Result
var data []byte
var err error
if test, uqf := utils.TestCaller(); test {
data, err = os.ReadFile(filepath.Join(artifact.CurProject().Root(), "target", uqf, "config.json"))
} else {
data, err = resources.ReadFile(fmt.Sprintf("%s/config.json", resourceDir))
}
if err != nil {
return result, err
}
key := strings.ReplaceAll(cmd.CommandPath(), " ", "_")
result = gjson.GetBytes(data, fmt.Sprintf("%s.%s", key, name))
if !result.Exists() {
result = gjson.GetBytes(data, fmt.Sprintf("%s_%s.%s", key, strings.Join(args, "_"), name))
}
return result, nil
}
func installPlugins(cmd *cobra.Command, args []string) error {
result, err := parseArtifacts(cmd, args, "plugins")
if result.Exists() {
var data []byte
var plugins []artifact.Plugin
err = json.Unmarshal([]byte(result.Raw), &plugins)
for _, plugin := range plugins {
if err = artifact.CurProject().InstallPlugin(plugin); err != nil {
return err
}
if len(plugin.Config) > 0 {
if _, err = os.Stat(filepath.Join(artifact.CurProject().Root(), plugin.Config)); err != nil {
if data, err = resources.ReadFile(fmt.Sprintf("%s/%s", resourceDir, plugin.Config)); err == nil {
if err = os.WriteFile(filepath.Join(artifact.CurProject().Root(), plugin.Config), data, os.ModePerm); err != nil {
break
}
} else {
break
}
}
}
}
if err != nil {
return errors.New(color.RedString(err.Error()))
}
}
return err
}
func installDeps(cmd *cobra.Command, args []string) error {
result, err := parseArtifacts(cmd, args, "deps")
if result.Exists() {
var cfgDeps []string
err = json.Unmarshal([]byte(result.Raw), &cfgDeps)
for _, dep := range lo.Filter(cfgDeps, func(url string, _ int) bool {
return !lo.Contains(artifact.CurProject().Dependencies(), url)
}) {
if err = artifact.CurProject().InstallDependency(dep); err != nil {
break
}
}
}
if err != nil {
return errors.New(color.RedString(err.Error()))
}
return err
}
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "gbc",
Short: color.GreenString(`Go project boot command line`),
Long: color.GreenString(`Go project boot command line`),
ValidArgs: validBuilderArgs(),
Args: func(cmd *cobra.Command, args []string) error {
if err := OnlyValidArgs(cmd, args); err != nil {
return err
}
return MinimumNArgs(1)(cmd, args)
},
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
currentDir, _ := os.Getwd()
if artifact.CurProject().Root() != currentDir {
return fmt.Errorf(color.RedString("Please execute the command in the project root dir"))
}
if err := installPlugins(cmd, args); err != nil {
return err
}
if err := installDeps(cmd, args); err != nil {
return err
}
return artifact.CurProject().SetupHooks(false)
},
RunE: func(cmd *cobra.Command, args []string) error {
for _, arg := range lo.Uniq(args) {
if err := execute(cmd, arg); err != nil {
return errors.New(color.RedString("%s \n", err.Error()))
}
}
return nil
},
}
func rootExample() string {
format := fmt.Sprintf(" %%-%ds %%s", rootCmd.NamePadding())
builtIn := lo.Map(lo.Filter(buildActions(), func(item Action, _ int) bool {
return !strings.Contains(item.A, "_")
}), func(action Action, _ int) string {
return fmt.Sprintf(format, action.A, action.C)
})
lo.ForEach(artifact.CurProject().Plugins(), func(plugin artifact.Plugin, _ int) {
if !lo.ContainsBy(builtIn, func(item string) bool {
return strings.HasPrefix(strings.TrimSpace(item), strings.TrimSpace(plugin.Alias))
}) {
builtIn = append(builtIn, fmt.Sprintf(format, plugin.Alias, plugin.Description))
}
})
return strings.Join(builtIn, "\n")
}
func Execute() error {
ctx := context.Background()
if err := rootCmd.ExecuteContext(ctx); err != nil {
return fmt.Errorf(color.RedString(err.Error()))
}
return nil
}
func init() {
rootCmd.Example = rootExample()
rootCmd.SetUsageTemplate(usageTemplate())
rootCmd.SetErrPrefix(color.RedString("Error:"))
rootCmd.SetFlagErrorFunc(func(command *cobra.Command, err error) error {
return lo.IfF(err != nil, func() error {
return fmt.Errorf(color.RedString(err.Error()))
}).Else(nil)
})
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}