Debian Codegraph @

tools/antithesis-go-instrumentor/args/args.go

raw
1 package args
2
3 import (
4 "flag"
5 "fmt"
6 "os"
7 "os/exec"
8 "regexp"
9 "strings"
10
11 "github.com/antithesishq/antithesis-sdk-go/tools/antithesis-go-instrumentor/common"
12 )
13
14 // Args holds the parsed command-line arguments.
15 type Args struct {
16 ExcludeFile string
17 SymPrefix string
18 InputDir string
19 OutputDir string
20 InstrumentorVersion string
21 LocalSDKPath string
22 VersionText string
23 LogFile string
24 VerbosityLevel common.Verbosity
25 ShowVersion bool
26 InvalidArgs bool
27 WantsInstrumentor bool
28 SkipTestFiles bool
29 SkipProtoBufFiles bool
30 SourceEditing bool
31 }
32
33 func ParseArgs(versionText string, thisVersion string) *Args {
34 versionPtr := flag.Bool("version", false, "the current version of this application")
35 exclusionsPtr := flag.String("exclude", "", "the path to a file listing files and directories to exclude from instrumentation (optional)")
36 prefixPtr := flag.String("prefix", "", "a string to prepend to the symbol table (optional)")
37 logfilePtr := flag.String("logfile", "", "file path to log into (default=stderr)")
38 verbosePtr := flag.Int("V", 0, "verbosity level (default to 0)")
39 assertOnlyPtr := flag.Bool("assert_only", false, "generate assertion catalog ONLY - no coverage instrumentation (default to false)")
40 catalogDirPtr := flag.String("catalog_dir", "", "(deprecated, ignored)")
41 instrVersionPtr := flag.String("instrumentor_version", thisVersion, "version of the SDK instrumentation package to require")
42 localSDKPathPtr := flag.String("local_sdk_path", "", "path to the local Antithesis SDK")
43 skipTestFilesPtr := flag.Bool("skip_test_files", false, "Skip instrumentation and cataloging for '*_test.go' files (default to false)")
44 skipProtoBufFilesPtr := flag.Bool("skip_protobuf_files", false, "Skip instrumentation and cataloging for '*.pb.go' files (default to false)")
45 sourceEditingPtr := flag.Bool("source_editing", false, "use the source-editing (edit-buffer) coverage instrumentor instead of the AST-rewriting one; preserves comments and source line numbers (experimental, default to false)")
46 flag.Parse()
47
48 args := Args{
49 InvalidArgs: false,
50 ShowVersion: *versionPtr,
51 }
52
53 if args.ShowVersion {
54 return &args
55 }
56
57 args.LogFile = strings.TrimSpace(*logfilePtr)
58 args.VerbosityLevel = common.Verbosity(*verbosePtr)
59 args.WantsInstrumentor = !*assertOnlyPtr
60 args.SymPrefix = strings.TrimSpace(*prefixPtr)
61 args.ExcludeFile = strings.TrimSpace(*exclusionsPtr)
62 args.InstrumentorVersion = strings.TrimSpace(*instrVersionPtr)
63 args.LocalSDKPath = strings.TrimSpace(*localSDKPathPtr)
64 args.VersionText = versionText
65 args.SkipTestFiles = *skipTestFilesPtr
66 args.SkipProtoBufFiles = *skipProtoBufFilesPtr
67 args.SourceEditing = *sourceEditingPtr
68
69 // Verify we have the expected number of positional arguments
70 numArgsRequired := 1
71 if args.WantsInstrumentor {
72 numArgsRequired++
73 }
74
75 if *catalogDirPtr != "" {
76 fmt.Fprintf(os.Stderr, "Warning: -catalog_dir is deprecated and will be ignored\n")
77 }
78
79 if flag.NArg() < numArgsRequired {
80 fmt.Fprintf(os.Stderr, "%s", strings.TrimSpace(versionText))
81 fmt.Fprintf(os.Stderr, "\n\n")
82 fmt.Fprintf(os.Stderr, "For assertions support:\n")
83 fmt.Fprintf(os.Stderr, " $ antithesis-go-instrumentor -assert_only [options] go_project_dir\n")
84 fmt.Fprintf(os.Stderr, "\n")
85 fmt.Fprintf(os.Stderr, " - The go_project_dir should contain a valid go.mod file\n")
86 fmt.Fprintf(os.Stderr, "\n\n")
87 fmt.Fprintf(os.Stderr, "For full instrumentations (including assertions support):\n")
88 fmt.Fprintf(os.Stderr, " $ antithesis-go-instrumentor [options] go_project_dir target_dir\n")
89 fmt.Fprintf(os.Stderr, "\n")
90 fmt.Fprintf(os.Stderr, " - The go_project_dir should contain a valid go.mod file\n")
91 fmt.Fprintf(os.Stderr, " - The target_dir should be an existing, but empty directory\n")
92 fmt.Fprintf(os.Stderr, "\n\n")
93 fmt.Fprintf(os.Stderr, "The Assertions catalog will be registered in a generated file:\n")
94 fmt.Fprintf(os.Stderr, " antithesis_catalog.go\n")
95 fmt.Fprintf(os.Stderr, "\n")
96 fmt.Fprintf(os.Stderr, " - For assertions support, the catalog will be created in the go_project_dir\n")
97 fmt.Fprintf(os.Stderr, " - For full instrumentation, the catalog will be created under the target_dir\n")
98 fmt.Fprintf(os.Stderr, "\n\n")
99 flag.Usage()
100 args.InvalidArgs = true
101 return &args
102 }
103
104 if args.SymPrefix != "" {
105 m, _ := regexp.MatchString(`^[a-z]+$`, *prefixPtr)
106 if !m {
107 fmt.Fprint(os.Stderr, "A prefix must consist of lower-case ASCII letters.\n")
108 args.InvalidArgs = true
109 return &args
110 }
111 }
112
113 args.InputDir = flag.Arg(0)
114 if args.WantsInstrumentor {
115 args.OutputDir = flag.Arg(1)
116 }
117
118 if !isGoAvailable() {
119 fmt.Fprint(os.Stderr, "Go toolchain not available\n")
120 args.InvalidArgs = true
121 }
122
123 return &args
124 }
125
126 func (args *Args) ShowArguments() {
127 logger := common.Logger
128 logger.Printf(common.Normal, "inputDir: %q", args.InputDir)
129 if args.LocalSDKPath != "" {
130 logger.Printf(common.Normal, "localSDKPath: %q", args.LocalSDKPath)
131 }
132 if args.WantsInstrumentor {
133 logger.Printf(common.Normal, "outputDir: %q", args.OutputDir)
134 if args.ExcludeFile != "" {
135 logger.Printf(common.Normal, "excludeFile: %q", args.ExcludeFile)
136 }
137 if args.SymPrefix != "" {
138 logger.Printf(common.Normal, "symPrefix: %q", args.SymPrefix)
139 }
140 }
141
142 // Intentional: no need to show anything if not skipping
143 if args.SkipTestFiles {
144 logger.Printf(common.Normal, "skipTestFiles: %t", args.SkipTestFiles)
145 }
146 if args.SkipProtoBufFiles {
147 logger.Printf(common.Normal, "skipProtoBufFiles: %t", args.SkipProtoBufFiles)
148 }
149 if args.SourceEditing {
150 logger.Printf(common.Normal, "sourceEditing: %t", args.SourceEditing)
151 }
152 }
153
154 func isGoAvailable() bool {
155 cmd := exec.Command("go", "version")
156 output, err := cmd.Output()
157 if err != nil {
158 return false
159 }
160
161 // go version is expected to output 1 line containing 4 space-delimited items
162 // Typical output expected is:
163 //
164 // go version go1.21.5 linux/amd64
165 //
166 // verify we get this 'shape' output
167 parts := strings.Split(strings.TrimSpace(string(output)), " ")
168 if len(parts) < 4 {
169 return false
170 }
171 return (parts[0] == "go") && (parts[1] == "version") && strings.HasPrefix(parts[2], "go")
172 }