-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
230 lines (188 loc) · 6.95 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"gopkg.in/alecthomas/kingpin.v2"
)
var version string
var (
// kingpin
app = kingpin.New(
filepath.Base(os.Args[0]),
"Different modding tools by Depressed DST Modders for Klei's game Don't Starve Together.",
)
cfg = NewConfig()
appConfig = app.Flag("config", "Path to configuration file.").Short('c').Default(".modcli").String()
changelogCmd = app.Command("changelog", "Changelog tools.")
changelogCmdPath = changelogCmd.Arg("path", "Path.").Default("CHANGELOG.md").String()
changelogCmdCount = changelogCmd.Flag("count", "Show total number of releases.").Bool()
changelogCmdFirst = changelogCmd.Flag("first", "Show first release.").Short('f').Bool()
changelogCmdLatest = changelogCmd.Flag("latest", "Show latest release.").Short('l').Bool()
changelogCmdList = changelogCmd.Flag("list", "Show list of releases without changes.").Bool()
changelogCmdListVersions = changelogCmd.Flag("list-versions", "Show list of versions.").Bool()
doctorCmd = app.Command("doctor", "Check health of this CLI app.").Hidden()
formatCmd = app.Command("format", "Code formatting tools: Prettier and StyLua.")
formatCmdDocker = formatCmd.Flag("docker", "Run through Docker.").Short('d').Bool()
formatCmdFix = formatCmd.Flag("fix", "Fix issues automatically. Beware!").Short('f').Bool()
formatCmdPrettier = formatCmd.Flag("prettier", "Run Prettier.").Short('p').Bool()
formatCmdStyLua = formatCmd.Flag("stylua", "Run StyLua.").Short('s').Bool()
infoCmd = app.Command("info", "Mod info tools.")
infoCmdPath = infoCmd.Arg("path", "Path to modinfo.lua.").Default("modinfo.lua").String()
infoCmdCompatibility = infoCmd.Flag("compatibility", "Show compatibility fields.").Bool()
infoCmdConfiguration = infoCmd.Flag("configuration", "Show configuration options with their default values.").Bool()
infoCmdConfigurationMarkdown = infoCmd.Flag("configuration-markdown", "Show configuration options with their default values as a Markdown table.").Short('m').Bool()
infoCmdDescription = infoCmd.Flag("description", "Show description.").Short('d').Bool()
infoCmdField = infoCmd.Flag("field", "Show specific field value. Supports multiple flags.").Short('f').Strings()
infoCmdFirstLine = infoCmd.Flag("first-line", "Show first lines for values.").Bool()
infoCmdGeneral = infoCmd.Flag("general", "Show general fields.").Short('g').Bool()
infoCmdNames = infoCmd.Flag("names", "Show variable names or options data instead of their descriptions.").Short('n').Bool()
infoCmdOther = infoCmd.Flag("other", "Show other fields.").Short('o').Bool()
lintCmd = app.Command("lint", "Code linting tools: Luacheck.")
lintCmdDocker = lintCmd.Flag("docker", "Run through Docker.").Short('d').Bool()
lintCmdLuacheck = lintCmd.Flag("luacheck", "Run Luacheck.").Short('l').Bool()
lintCmdOriginal = lintCmd.Flag("original", "Show original output instead.").Short('o').Bool()
testCmd = app.Command("test", "Testing tools: Busted.")
workshopCmd = app.Command("workshop", "Steam Workshop tools.")
workshopCmdPath = workshopCmd.Arg("path", "Path to mod directory.").Default(".").ExistingDir()
workshopCmdList = workshopCmd.Flag("list", "Show only files that are going to be included.").Short('l').Bool()
workshopCmdName = workshopCmd.Flag("name", "Name of destination directory/archive.").Default("workshop").Short('n').String()
workshopCmdZip = workshopCmd.Flag("zip", "Create a ZIP archive instead.").Short('z').Bool()
)
func enableConfigBool(value *bool, docker *bool) {
if !*value && *docker {
*value = true
}
}
func loadConfig() {
if len(*appConfig) > 0 && *appConfig != ".modcli" {
errMsg := "failed to load config"
stat, err := os.Stat(*appConfig)
if errors.Is(err, os.ErrNotExist) {
fatalError(
errMsg,
fmt.Errorf("open %s: no such file", *appConfig),
)
}
if stat.IsDir() {
fatalError(
errMsg,
fmt.Errorf("open %s: expected file but got directory", *appConfig),
)
}
}
if _, err := os.Stat(*appConfig); err == nil {
errMsg := "failed to load config"
f, err := os.OpenFile(*appConfig, os.O_RDONLY, 0644)
if err != nil {
fatalError(errMsg, err)
}
if err := cfg.load(f); err != nil {
fatalError(errMsg, err)
}
}
// format
enableConfigBool(&cfg.Format.Prettier.Docker, formatCmdDocker)
enableConfigBool(&cfg.Format.StyLua.Docker, formatCmdDocker)
enableConfigBool(&cfg.Format.Prettier.Enabled, formatCmdPrettier)
enableConfigBool(&cfg.Format.StyLua.Enabled, formatCmdStyLua)
enableConfigBool(&cfg.Format.Prettier.Fix, formatCmdFix)
enableConfigBool(&cfg.Format.StyLua.Fix, formatCmdFix)
// lint
enableConfigBool(&cfg.Lint.Luacheck.Docker, lintCmdDocker)
enableConfigBool(&cfg.Lint.Luacheck.Enabled, lintCmdLuacheck)
}
func runChangelog() {
c := NewChangelog()
c.Count = *changelogCmdCount
c.First = *changelogCmdFirst
c.Latest = *changelogCmdLatest
c.List = *changelogCmdList
c.ListVersions = *changelogCmdListVersions
if err := c.run(*changelogCmdPath); err != nil {
fatalError("failed to run changelog command", err)
}
}
func runDoctor() {
d := NewDoctor(cfg)
if err := d.run(); err != nil {
fatalError("failed to run doctor command", err)
}
}
func runFormat() {
f, err := NewFormat(cfg)
if err != nil {
fatalError(err.Error())
}
f.run()
}
func runInfo() {
i := NewInfo()
i.Compatibility = *infoCmdCompatibility
i.Configuration = *infoCmdConfiguration
i.ConfigurationMarkdown = *infoCmdConfigurationMarkdown
i.Description = *infoCmdDescription
i.Fields = *infoCmdField
i.FirstLine = *infoCmdFirstLine
i.General = *infoCmdGeneral
i.Names = *infoCmdNames
i.Other = *infoCmdOther
if err := i.run(*infoCmdPath); err != nil {
fatalError("failed to run info command", err)
}
}
func runLint() {
l, err := NewLint(cfg)
l.Original = *lintCmdOriginal
if err != nil {
fatalError(err.Error())
}
l.run()
}
func runTest() {
l, err := NewTest(cfg)
if err != nil {
fatalError(err.Error())
}
l.run()
}
func runWorkshop() {
w := NewWorkshop(cfg)
w.destName = *workshopCmdName
w.list = *workshopCmdList
w.path = *workshopCmdPath
w.zip = *workshopCmdZip
if err := w.run(); err != nil {
fatalError("failed to run workshop command", err)
}
}
func main() {
// kingpin
app.UsageTemplate(kingpin.DefaultUsageTemplate).Version(version)
app.HelpFlag.Short('h')
app.VersionFlag.Short('v')
command, err := app.Parse(os.Args[1:])
if err != nil {
fatalError("failed to parse arguments", err)
}
// config
loadConfig()
// commands
switch kingpin.MustParse(command, err) {
case changelogCmd.FullCommand():
runChangelog()
case doctorCmd.FullCommand():
runDoctor()
case formatCmd.FullCommand():
runFormat()
case infoCmd.FullCommand():
runInfo()
case lintCmd.FullCommand():
runLint()
case testCmd.FullCommand():
runTest()
case workshopCmd.FullCommand():
runWorkshop()
}
}