-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
121 lines (102 loc) · 3.72 KB
/
main.go
File metadata and controls
121 lines (102 loc) · 3.72 KB
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
package main
import (
"flag"
"fmt"
"github.com/stoewer/go-strcase"
"os"
"path/filepath"
"strings"
)
type Module string
const (
ModuleFolder Module = "version"
ModuleVersion Module = "0.4.0-beta"
ModulePackage Module = "github.com/gerardforcada/structera"
)
func main() {
if err := cli(flag.CommandLine); err != nil {
_, err = fmt.Fprintf(os.Stderr, "Error: %v\n", err)
if err != nil {
panic(err)
}
os.Exit(1)
}
}
func cli(flagset *flag.FlagSet) error {
var (
fileName string
structName string
outputDir string
showVersion bool
showHelp bool
force bool
)
// Define both long and short flag versions
flagset.StringVar(&fileName, "file", "", "Path to the Go file containing the struct")
flagset.StringVar(&fileName, "f", "", "Path to the Go file containing the struct (shorthand)")
flagset.StringVar(&structName, "struct", "", "Name of the struct")
flagset.StringVar(&structName, "s", "", "Name of the struct (shorthand)")
flagset.StringVar(&outputDir, "output", "", "Output directory (optional)")
flagset.StringVar(&outputDir, "o", "", "Output directory (optional) (shorthand)")
flagset.BoolVar(&showHelp, "help", false, "Print the help page and exit")
flagset.BoolVar(&showHelp, "h", false, "Print the help page and exit (shorthand)")
flagset.BoolVar(&showVersion, "version", false, "Print the version of Structera and exit")
flagset.BoolVar(&showVersion, "v", false, "Print the version of Structera and exit (shorthand)")
flagset.BoolVar(&force, "force", false, "Replace existing versioned struct files")
flagset.BoolVar(&force, "F", false, "Replace existing versioned struct files (shorthand)")
err := flagset.Parse(os.Args[1:])
if err != nil {
return err
}
if showVersion {
fmt.Printf("Structera version %s\n", ModuleVersion)
return nil
}
// Check if the required flags are set
if fileName == "" || structName == "" || showHelp {
fmt.Printf("Structera version %s\n\n", ModuleVersion)
fmt.Println("Structera is a command-line tool for versioning Go structs.")
fmt.Printf("For more information, updates, or contributions, visit https://%s\n\n", ModulePackage)
fmt.Println("Usage:")
fmt.Println(" structera -f <path-to-struct-file> -s <StructName> [-o <output-directory>]")
fmt.Println(" structera --file <path-to-struct-file> --struct <StructName> [--output <output-directory>]")
fmt.Println("\nOptions:")
fmt.Println(" --file, -f Path to the Go file containing the struct")
fmt.Println(" --force, -F Replace existing versioned struct files")
fmt.Println(" --struct, -s Name of the struct to version")
fmt.Println(" --output, -o (Optional) Output directory for the versioned struct files")
fmt.Println(" --help, -h Prints this page and exit")
fmt.Println(" --version, -v Print the version of Structera and exit")
fmt.Println("\nExample:")
fmt.Println(" structera -f ./models/user.go -s User")
fmt.Println(" structera -f ./models/user.go -s User -o ./models/versioned")
fmt.Println(" structera --file ./models/user.go --struct User")
fmt.Println(" structera --file ./models/user.go --struct User --output ./models/versioned")
fmt.Println()
if showHelp {
return nil
}
return fmt.Errorf("missing required flags")
}
if outputDir == "" {
outputDir = filepath.Dir(fileName)
}
generator := Generator{
Format: &Format{},
Resolver: &Resolver{},
Filename: fileName,
StructName: StructName{
Original: structName,
Lower: strings.ToLower(structName),
Snake: strcase.SnakeCase(structName),
},
OutputDir: outputDir,
Package: string(ModuleFolder),
Replace: force,
}
if err := generator.VersionedStructs(); err != nil {
return err
}
fmt.Println("Versioned structs generated successfully.")
return nil
}