Skip to content

Commit 02747f4

Browse files
committed
Expose flag names as public consts
1 parent a9f491d commit 02747f4

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

client.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func (c *client) Call(
173173
if len(args) == 0 {
174174
args = []string{procedure.Path()}
175175
}
176-
args = append(args, "--"+flagFormat, c.format.String())
176+
args = append(args, "--"+FormatFlagName, c.format.String())
177177
if err := c.runner.Run(
178178
ctx,
179179
Env{
@@ -198,7 +198,7 @@ func (c *client) getSpecUncached(ctx context.Context) (Spec, error) {
198198
if err := c.runner.Run(
199199
ctx,
200200
Env{
201-
Args: []string{"--" + flagSpec, "--" + flagFormat, c.format.String()},
201+
Args: []string{"--" + SpecFlagName, "--" + FormatFlagName, c.format.String()},
202202
Stdout: stdout,
203203
Stderr: c.stderr,
204204
},
@@ -207,11 +207,11 @@ func (c *client) getSpecUncached(ctx context.Context) (Spec, error) {
207207
}
208208
data := stdout.Bytes()
209209
if len(data) == 0 {
210-
return nil, fmt.Errorf("--%s did not return a spec", flagSpec)
210+
return nil, fmt.Errorf("--%s did not return a spec", SpecFlagName)
211211
}
212212
protoSpec := &pluginrpcv1.Spec{}
213213
if err := unmarshalSpec(c.format, data, protoSpec); err != nil {
214-
return nil, fmt.Errorf("--%s did not return a properly-formed spec: %w", flagSpec, err)
214+
return nil, fmt.Errorf("--%s did not return a properly-formed spec: %w", SpecFlagName, err)
215215
}
216216
return NewSpecForProto(protoSpec)
217217
}
@@ -222,7 +222,7 @@ func (c *client) checkProtocolVersion(ctx context.Context) error {
222222
return err
223223
}
224224
if version != protocolVersion {
225-
return fmt.Errorf("--%s returned unknown protocol version %d", flagProtocol, version)
225+
return fmt.Errorf("--%s returned unknown protocol version %d", ProtocolFlagName, version)
226226
}
227227
return nil
228228
}
@@ -232,7 +232,7 @@ func (c *client) getProtocolVersionUncached(ctx context.Context) (int, error) {
232232
if err := c.runner.Run(
233233
ctx,
234234
Env{
235-
Args: []string{"--" + flagProtocol},
235+
Args: []string{"--" + ProtocolFlagName},
236236
Stdout: stdout,
237237
Stderr: c.stderr,
238238
},
@@ -241,11 +241,11 @@ func (c *client) getProtocolVersionUncached(ctx context.Context) (int, error) {
241241
}
242242
data := stdout.Bytes()
243243
if len(data) == 0 {
244-
return 0, fmt.Errorf("--%s did not return a protocol version", flagProtocol)
244+
return 0, fmt.Errorf("--%s did not return a protocol version", ProtocolFlagName)
245245
}
246246
version, err := unmarshalProtocol(data)
247247
if err != nil {
248-
return 0, fmt.Errorf("--%s did not return a properly-formed protocol version: %w", flagProtocol, err)
248+
return 0, fmt.Errorf("--%s did not return a properly-formed protocol version: %w", ProtocolFlagName, err)
249249
}
250250
return version, nil
251251
}

flags.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@ import (
2424
)
2525

2626
const (
27+
// ProtocolFlagName is the name of the protocol bool flag.
28+
ProtocolFlagName = "protocol"
29+
// SpecFlagName is the name of the spec bool flag.
30+
SpecFlagName = "spec"
31+
// FormatFlagName is the name of the format string flag.
32+
FormatFlagName = "format"
33+
2734
protocolVersion = 1
28-
flagProtocol = "protocol"
29-
flagSpec = "spec"
30-
flagFormat = "format"
3135
)
3236

3337
type flags struct {
@@ -41,20 +45,20 @@ func parseFlags(output io.Writer, args []string) (*flags, []string, error) {
4145
var formatString string
4246
flagSet := pflag.NewFlagSet("plugin", pflag.ContinueOnError)
4347
flagSet.SetOutput(output)
44-
flagSet.BoolVar(&flags.printProtocol, flagProtocol, false, "Print the protocol to stdout and exit.")
45-
flagSet.BoolVar(&flags.printSpec, flagSpec, false, "Print the spec to stdout in the specified format and exit.")
46-
flagSet.StringVar(&formatString, flagFormat, formatBinaryString, fmt.Sprintf("The format to use for requests, responses, and specs. Must be one of [%q, %q].", formatBinaryString, formatJSONString))
48+
flagSet.BoolVar(&flags.printProtocol, ProtocolFlagName, false, "Print the protocol to stdout and exit.")
49+
flagSet.BoolVar(&flags.printSpec, SpecFlagName, false, "Print the spec to stdout in the specified format and exit.")
50+
flagSet.StringVar(&formatString, FormatFlagName, formatBinaryString, fmt.Sprintf("The format to use for requests, responses, and specs. Must be one of [%q, %q].", formatBinaryString, formatJSONString))
4751
if err := flagSet.Parse(args); err != nil {
4852
return nil, nil, err
4953
}
5054
if flags.printProtocol && flags.printSpec {
51-
return nil, nil, fmt.Errorf("cannot specify both --%s and --%s", flagProtocol, flagSpec)
55+
return nil, nil, fmt.Errorf("cannot specify both --%s and --%s", ProtocolFlagName, SpecFlagName)
5256
}
5357
format := FormatBinary
5458
if formatString != "" {
5559
format = FormatForString(formatString)
5660
if format == 0 {
57-
return nil, nil, fmt.Errorf("invalid value for --%s: %q", flagFormat, formatString)
61+
return nil, nil, fmt.Errorf("invalid value for --%s: %q", FormatFlagName, formatString)
5862
}
5963
}
6064
if err := validateFormat(format); err != nil {

0 commit comments

Comments
 (0)