You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
3.5 KiB

8 years ago
8 years ago
8 years ago
8 years ago
  1. package cli
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "os"
  7. "path/filepath"
  8. "github.com/spf13/cobra"
  9. )
  10. // WriteConfigVals writes a toml file with the given values.
  11. // It returns an error if writing was impossible.
  12. func WriteConfigVals(dir string, vals map[string]string) error {
  13. data := ""
  14. for k, v := range vals {
  15. data += fmt.Sprintf("%s = \"%s\"\n", k, v)
  16. }
  17. cfile := filepath.Join(dir, "config.toml")
  18. return os.WriteFile(cfile, []byte(data), 0600)
  19. }
  20. // RunWithArgs executes the given command with the specified command line args
  21. // and environmental variables set. It returns any error returned from cmd.Execute()
  22. func RunWithArgs(cmd Executable, args []string, env map[string]string) error {
  23. oargs := os.Args
  24. oenv := map[string]string{}
  25. // defer returns the environment back to normal
  26. defer func() {
  27. os.Args = oargs
  28. for k, v := range oenv {
  29. os.Setenv(k, v)
  30. }
  31. }()
  32. // set the args and env how we want them
  33. os.Args = args
  34. for k, v := range env {
  35. // backup old value if there, to restore at end
  36. oenv[k] = os.Getenv(k)
  37. err := os.Setenv(k, v)
  38. if err != nil {
  39. return err
  40. }
  41. }
  42. // and finally run the command
  43. return cmd.Execute()
  44. }
  45. // RunCaptureWithArgs executes the given command with the specified command
  46. // line args and environmental variables set. It returns string fields
  47. // representing output written to stdout and stderr, additionally any error
  48. // from cmd.Execute() is also returned
  49. func RunCaptureWithArgs(cmd Executable, args []string, env map[string]string) (stdout, stderr string, err error) {
  50. oldout, olderr := os.Stdout, os.Stderr // keep backup of the real stdout
  51. rOut, wOut, _ := os.Pipe()
  52. rErr, wErr, _ := os.Pipe()
  53. os.Stdout, os.Stderr = wOut, wErr
  54. defer func() {
  55. os.Stdout, os.Stderr = oldout, olderr // restoring the real stdout
  56. }()
  57. // copy the output in a separate goroutine so printing can't block indefinitely
  58. copyStd := func(reader *os.File) *(chan string) {
  59. stdC := make(chan string)
  60. go func() {
  61. var buf bytes.Buffer
  62. // io.Copy will end when we call reader.Close() below
  63. io.Copy(&buf, reader) //nolint:errcheck //ignore error
  64. stdC <- buf.String()
  65. }()
  66. return &stdC
  67. }
  68. outC := copyStd(rOut)
  69. errC := copyStd(rErr)
  70. // now run the command
  71. err = RunWithArgs(cmd, args, env)
  72. // and grab the stdout to return
  73. wOut.Close()
  74. wErr.Close()
  75. stdout = <-*outC
  76. stderr = <-*errC
  77. return stdout, stderr, err
  78. }
  79. // NewCompletionCmd returns a cobra.Command that generates bash and zsh
  80. // completion scripts for the given root command. If hidden is true, the
  81. // command will not show up in the root command's list of available commands.
  82. func NewCompletionCmd(rootCmd *cobra.Command, hidden bool) *cobra.Command {
  83. flagZsh := "zsh"
  84. cmd := &cobra.Command{
  85. Use: "completion",
  86. Short: "Generate shell completion scripts",
  87. Long: fmt.Sprintf(`Generate Bash and Zsh completion scripts and print them to STDOUT.
  88. Once saved to file, a completion script can be loaded in the shell's
  89. current session as shown:
  90. $ . <(%s completion)
  91. To configure your bash shell to load completions for each session add to
  92. your $HOME/.bashrc or $HOME/.profile the following instruction:
  93. . <(%s completion)
  94. `, rootCmd.Use, rootCmd.Use),
  95. RunE: func(cmd *cobra.Command, _ []string) error {
  96. zsh, err := cmd.Flags().GetBool(flagZsh)
  97. if err != nil {
  98. return err
  99. }
  100. if zsh {
  101. return rootCmd.GenZshCompletion(cmd.OutOrStdout())
  102. }
  103. return rootCmd.GenBashCompletion(cmd.OutOrStdout())
  104. },
  105. Hidden: hidden,
  106. Args: cobra.NoArgs,
  107. }
  108. cmd.Flags().Bool(flagZsh, false, "Generate Zsh completion script")
  109. return cmd
  110. }