-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref: Add OutputFormat type to enforce flag validation at parse time
- Loading branch information
Showing
4 changed files
with
71 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package config | ||
|
||
import "fmt" | ||
|
||
const ( | ||
JSON = "json" | ||
TEXT = "text" | ||
CSV = "csv" | ||
) | ||
|
||
// implements pflag.Value to enforce strict string matching for the output format during flag parsing | ||
type OutputFormat string | ||
|
||
func (o *OutputFormat) String() string { | ||
return string(*o) | ||
} | ||
|
||
func (o *OutputFormat) Set(value string) error { | ||
switch value { | ||
case JSON, TEXT, CSV: | ||
*o = OutputFormat(value) | ||
return nil | ||
default: | ||
return fmt.Errorf("invalid output format: %s (must be one of: json, text, csv)", value) | ||
} | ||
} | ||
|
||
func (o *OutputFormat) Type() string { | ||
return "OutputFormat" | ||
} |