Skip to content

Commit

Permalink
annotation to override default value
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Feb 17, 2022
1 parent 8ced391 commit b3ecfe1
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 8 deletions.
2 changes: 2 additions & 0 deletions annotation/annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ const (
// CodeDelimiter specifies the char that will be converted as code backtick.
// Can be used on cmd for inheritance or a specific flag.
CodeDelimiter = "docs.code-delimiter"
// DefaultValue specifies the default value for a flag.
DefaultValue = "docs.default-value"
)
9 changes: 8 additions & 1 deletion clidocstool_md.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,14 @@ func mdCmdOutput(cmd *cobra.Command, old string) (string, error) {
}

var defval string
if f.DefValue != "" && (f.Value.Type() != "bool" && f.DefValue != "true") && f.DefValue != "[]" {
if v, ok := f.Annotations[annotation.DefaultValue]; ok && len(v) > 0 {
defval = v[0]
if cd, ok := f.Annotations[annotation.CodeDelimiter]; ok {
defval = strings.ReplaceAll(defval, cd[0], "`")
} else if cd, ok := cmd.Annotations[annotation.CodeDelimiter]; ok {
defval = strings.ReplaceAll(defval, cd, "`")
}
} else if f.DefValue != "" && (f.Value.Type() != "bool" && f.DefValue != "true") && f.DefValue != "[]" {
defval = "`" + f.DefValue + "`"
}

Expand Down
1 change: 1 addition & 0 deletions clidocstool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func init() {
buildxBuildFlags.StringArrayP("output", "o", []string{}, `Output destination (format: "type=local,dest=path")`)

buildxBuildFlags.StringArray("platform", []string{}, "Set target platform for build")
buildxBuildFlags.SetAnnotation("platform", annotation.DefaultValue, []string{"local"})

buildxBuildFlags.Bool("push", false, `Shorthand for "--output=type=registry"`)

Expand Down
22 changes: 17 additions & 5 deletions clidocstool_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,25 @@ func genFlagResult(cmd *cobra.Command, flags *pflag.FlagSet, anchors map[string]

flags.VisitAll(func(flag *pflag.Flag) {
opt = cmdOption{
Option: flag.Name,
ValueType: flag.Value.Type(),
DefaultValue: forceMultiLine(flag.DefValue, defaultValueMaxWidth),
Deprecated: len(flag.Deprecated) > 0,
Hidden: flag.Hidden,
Option: flag.Name,
ValueType: flag.Value.Type(),
Deprecated: len(flag.Deprecated) > 0,
Hidden: flag.Hidden,
}

var defval string
if v, ok := flag.Annotations[annotation.DefaultValue]; ok && len(v) > 0 {
defval = v[0]
if cd, ok := flag.Annotations[annotation.CodeDelimiter]; ok {
defval = strings.ReplaceAll(defval, cd[0], "`")
} else if cd, ok := cmd.Annotations[annotation.CodeDelimiter]; ok {
defval = strings.ReplaceAll(defval, cd, "`")
}
} else {
defval = flag.DefValue
}
opt.DefaultValue = forceMultiLine(defval, defaultValueMaxWidth)

usage := flag.Usage
if cd, ok := flag.Annotations[annotation.CodeDelimiter]; ok {
usage = strings.ReplaceAll(usage, cd[0], "`")
Expand Down
2 changes: 1 addition & 1 deletion fixtures/buildx_build.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Start a build
| `--load` | | | Shorthand for `--output=type=docker` |
| `--network` | `string` | `default` | Set the networking mode for the `RUN` instructions during build |
| `-o`, `--output` | `stringArray` | | Output destination (format: `type=local,dest=path`) |
| `--platform` | `stringArray` | | Set target platform for build |
| `--platform` | `stringArray` | local | Set target platform for build |
| `--push` | | | Shorthand for `--output=type=registry` |
| `-q`, `--quiet` | | | Suppress the build output and print image ID on success |
| `--secret` | `stringArray` | | Secret file to expose to the build (format: `id=mysecret,src=/local/secret`) |
Expand Down
2 changes: 1 addition & 1 deletion fixtures/docker_buildx_build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ options:
swarm: false
- option: platform
value_type: stringArray
default_value: '[]'
default_value: local
description: Set target platform for build
deprecated: false
hidden: false
Expand Down

0 comments on commit b3ecfe1

Please sign in to comment.