Skip to content

Commit

Permalink
yaml supports commands with slashes
Browse files Browse the repository at this point in the history
  • Loading branch information
jimschubert committed May 17, 2022
1 parent a913e1f commit 0bfd1c1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
3 changes: 1 addition & 2 deletions doc/yaml_docs.go
Expand Up @@ -19,7 +19,6 @@ import (
"os"
"path/filepath"
"sort"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -66,7 +65,7 @@ func GenYamlTreeCustom(cmd *cobra.Command, dir string, filePrepender, linkHandle
}
}

basename := strings.ReplaceAll(cmd.CommandPath(), " ", "_") + ".yaml"
basename := cleanCommandName(cmd.CommandPath()) + ".yaml"
filename := filepath.Join(dir, basename)
f, err := os.Create(filename)
if err != nil {
Expand Down
18 changes: 18 additions & 0 deletions doc/yaml_docs_test.go
Expand Up @@ -57,6 +57,24 @@ func TestGenYamlTree(t *testing.T) {
}
}

func TestGenYamlTreeSlashCommands(t *testing.T) {
c := &cobra.Command{Use: "run/first [OPTIONS] arg1 arg2"}

tmpdir, err := ioutil.TempDir("", "test-gen-yaml-tree-slash-commands")
if err != nil {
t.Fatalf("Failed to create tmpdir: %s", err.Error())
}
defer os.RemoveAll(tmpdir)

if err := GenYamlTree(c, tmpdir); err != nil {
t.Fatalf("GenYamlTree failed: %s", err.Error())
}

if _, err := os.Stat(filepath.Join(tmpdir, "run_first.yaml")); err != nil {
t.Fatalf("Expected file 'run_first.yaml' to exist")
}
}

func TestGenYamlDocRunnable(t *testing.T) {
// Testing a runnable command: should contain the "usage" field
buf := new(bytes.Buffer)
Expand Down

0 comments on commit 0bfd1c1

Please sign in to comment.