From 0bfd1c113adc31124cf1ceaca5d39367ba7009cd Mon Sep 17 00:00:00 2001 From: Jim Schubert Date: Sat, 23 Apr 2022 15:46:57 -0400 Subject: [PATCH] yaml supports commands with slashes --- doc/yaml_docs.go | 3 +-- doc/yaml_docs_test.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/doc/yaml_docs.go b/doc/yaml_docs.go index a79fa40e3..8089a3442 100644 --- a/doc/yaml_docs.go +++ b/doc/yaml_docs.go @@ -19,7 +19,6 @@ import ( "os" "path/filepath" "sort" - "strings" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -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 { diff --git a/doc/yaml_docs_test.go b/doc/yaml_docs_test.go index d08fa4f82..d20ebf40e 100644 --- a/doc/yaml_docs_test.go +++ b/doc/yaml_docs_test.go @@ -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)