yaml supports commands with slashes

This commit is contained in:
Jim Schubert 2022-04-23 15:46:57 -04:00
parent a913e1fbab
commit 0bfd1c113a
2 changed files with 19 additions and 2 deletions

View file

@ -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 {

View file

@ -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)