-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
98 lines (84 loc) · 1.93 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
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"gopkg.in/yaml.v3"
)
var (
outDir = flag.String("o", ".", "path to output directory")
cfgPath = flag.String("c", "", "path to Twist configuration")
renderREADME = flag.Bool("readme", false, "toggle README rendering - requires configuration")
)
type pkg struct {
Canonical string
Source string
}
//go:generate go run github.com/UnnoTed/fileb0x b0x.yml
func main() {
flag.Parse()
// if only one argument, we are dealing with a command
if len(flag.Args()) == 1 {
switch arg := flag.Arg(0); arg {
case "help":
showHelp()
os.Exit(0)
case "config":
if *cfgPath == "" {
*cfgPath = "./twist.yml"
}
b, err := yaml.Marshal(newConfig())
if err != nil {
panic(err)
}
if err := ioutil.WriteFile(*cfgPath, b, os.ModePerm); err != nil {
panic(err)
}
fmt.Printf("config generated in '%s'\n", *cfgPath)
os.Exit(0)
default:
println("insufficient arguments provided")
os.Exit(1)
}
}
// otherwise generate
var cfg config
if *cfgPath == "" {
if len(flag.Args()) == 0 {
println("insufficient arguments provided")
os.Exit(1)
}
generate(flag.Arg(0), canonical{Path: flag.Arg(1), Subpackages: flag.Args()[2:]})
} else {
b, err := ioutil.ReadFile(*cfgPath)
if err != nil {
panic(err)
}
if err := yaml.Unmarshal(b, &cfg); err != nil {
panic(err)
}
for s, c := range cfg.Packages {
generate(s, c)
}
}
if *renderREADME {
if cfg.Packages == nil {
panic("no configuration found")
}
generateREADME(&cfg)
}
}
func showHelp() {
println(`twist is a tool for generating static, serverless canonical imports for Go packages.
usage:
twist -c twist.yml
twist [source] [canonical] [subpackages...]
other commands:
config generate a twist configuration file
help show help text
flags:
`)
flag.PrintDefaults()
println("\nsee https://go.bobheadxi.dev/twist for more documentation.")
}