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.

128 lines
3.5 KiB

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