command.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. The command package provides the command structure for kurz.
  3. Each command is implemented in its own file: the main command.go file only
  4. contains information for the root command, its flags, and helpers.
  5. Command files are expected to contain the following, for a command "foo"
  6. - the command itself, as a *command.Cobra instance, named FooCommand
  7. - the command flag variables, named fooSomething. Always prefix by the command name to avoid name conflicts
  8. - a init() function which will bind the flags to the command, and the command itself to other commands.
  9. - optional:
  10. - for commands usable without a subcommand, the command callable, as fooCommand(c *cobra.Command, args []string).
  11. - commands needing subcommands do not need a callable
  12. First-level commands bind to kurz, other commands may bind to the other FooCommand variables.
  13. */
  14. package command
  15. import (
  16. "fmt"
  17. "github.com/spf13/cobra"
  18. "os"
  19. )
  20. var kurz = &cobra.Command{
  21. Use: "kurz",
  22. Short: "kurz CLI",
  23. Long: "The kurz command uses the kurz API to provide a web-based URL shortener/aliaser",
  24. }
  25. var Verbose bool
  26. const ConfigName = ".kurz.js"
  27. func verbose() rune {
  28. if Verbose {
  29. return 'Y'
  30. } else {
  31. return 'N'
  32. }
  33. }
  34. /*
  35. Build() assembles the command structure from the various command files.
  36. It is the only function the main package needs to be aware of.
  37. */
  38. func Build() {
  39. kurz.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "Be verbose")
  40. err := kurz.Execute()
  41. if err != nil {
  42. fmt.Print("Error initializing commands: %+v\n", err)
  43. os.Exit(1)
  44. }
  45. }