Skip to content

Commit

Permalink
docs: support admonitions in command descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
majori committed Dec 21, 2023
1 parent 33b7d2e commit c597f69
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
30 changes: 29 additions & 1 deletion cmd/docs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"embed"
"fmt"
"os"
"regexp"
"strings"
"text/template"

"github.com/Masterminds/sprig"
Expand Down Expand Up @@ -103,11 +105,14 @@ func checkErr(err error) {

func mapCommandInfos(cmds []*cobra.Command) []CommandInfo {
infos := make([]CommandInfo, len(cmds))

for i, c := range cmds {
description := replaceAdmonition(c.Long)

info := CommandInfo{
Name: c.Name(),
Aliases: c.Aliases,
Description: c.Long,
Description: description,
Usage: c.Use,
Example: c.Example,
Flags: make([]Flag, 0),
Expand Down Expand Up @@ -137,3 +142,26 @@ func valueTypeToString(v pflag.Value) string {
return t
}
}

var admonitionRegExp *regexp.Regexp = regexp.MustCompile("\n((Note|Tip|Info|Warning|Danger): (.+))")

func replaceAdmonition(s string) string {
description := s
admonitions := admonitionRegExp.FindAllStringSubmatch(description, -1)
if len(admonitions) > 0 {
for _, a := range admonitions {
description = strings.Replace(
description,
a[0],
fmt.Sprintf(
"\n:::%s\n\n%s\n\n:::",
strings.ToLower(a[2]),
a[3],
),
1,
)
}
}

return description
}
2 changes: 1 addition & 1 deletion cmd/docs/templates/_schema.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
| `templateExtension` | `string` | | File extension of files in "templates" directory which should be templated. Files not matched by this extension will be copied as-is. If left empty (the default), all files will be templated. |
| `initHelp` | `string` | | A message which will be showed to an user after a succesful recipe execution. Can be used to guide the user what should be done next in the project directory. |
| `ignorePatterns` | `[]string` | | Glob patterns for ignoring generated files from future recipe upgrades. Ignored files will not be regenerated even if their templates change in future versions of the recipe. |
| `vars` | [`[]Variable`](#variable) | | An array of variables which can be used in templates. The user will be prompted to provide the value for the variable if not predefined with `--set` flag. |
| `vars` | [`[]Variable`](#variable) | | An array of variables which can be used in templates. The user will be prompted to provide the value for the variable if not set with `--set` flag. |

### Variable

Expand Down
8 changes: 6 additions & 2 deletions internal/cli/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ jalapeno execute oci://ghcr.io/user/my-recipe:latest
# Execute recipe to different directory
jalapeno execute path/to/recipe --dir other/dir
# Predefine variable values
jalapeno execute path/to/recipe --set MY_VAR=foo --set MY_OTHER_VAR=bar`,
# Set variable values with flags
jalapeno execute path/to/recipe --set MY_VAR=foo --set MY_OTHER_VAR=bar
# Set variable values with environment variables
export JALAPENO_VAR_MY_VAR=foo
jalapeno execute path/to/recipe`,
}

if err := option.ApplyFlags(&opts, cmd.Flags()); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/option/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Values struct {
}

func (opts *Values) ApplyFlags(fs *pflag.FlagSet) {
fs.StringArrayVarP(&opts.Flags, "set", "s", []string{}, "Predefine values to be used in the templates. Example: `--set \"MY_VAR=foo\"`")
fs.StringArrayVarP(&opts.Flags, "set", "s", []string{}, "Set values to be used in the templates. Example: `--set \"MY_VAR=foo\"`")
fs.StringVar(&opts.delimiter, "delimiter", ",", "Delimiter used when setting table variables")
fs.BoolVarP(&opts.ReuseSauceValues, "reuse-sauce-values", "r", false, "By default each sauce has their own set of values even if the variable names are same in both recipes. Setting this to `true` will reuse previous sauce values if the variable name match")
fs.BoolVar(&opts.NoInput, "no-input", false, "If set to true, the program will exit with an error code if it needs to wait for any user input. This is useful when running the program in CI/CD environment")
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jalapeno upgrade oci://ghcr.io/user/my-recipe:v2.0.0
# Upgrade recipe to different directory
jalapeno upgrade path/to/recipe --dir other/dir
# Predefine values for new variables introduced in the upgrade
# Set values for new variables introduced in the upgrade
jalapeno upgrade path/to/recipe --set NEW_VAR=foo`,
}

Expand Down

0 comments on commit c597f69

Please sign in to comment.