Version control system wrapper allowing the developer to worry about only having to learn one command set to manage all types of repositories. Derivative of the Python project http://code.revolvingcow.com/RevolvingCow/pycode

app.go 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package cmd
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "os"
  8. "os/exec"
  9. "strings"
  10. )
  11. // The application
  12. type App struct {
  13. Args []string
  14. Stdin io.Reader
  15. Stderr io.Writer
  16. Stdout io.Writer
  17. Directory string
  18. }
  19. // Create a new application instance
  20. func NewApp() *App {
  21. return &App{
  22. Args: os.Args[1:],
  23. Stdin: os.Stdin,
  24. Stderr: os.Stderr,
  25. Stdout: os.Stdout,
  26. Directory: getWorkingDirectory(),
  27. }
  28. }
  29. // Run the application
  30. func (a *App) Run() error {
  31. if len(a.Args) < 1 {
  32. a.Args = append(a.Args, "help")
  33. }
  34. executed := false
  35. for _, s := range getVersionControlSystems() {
  36. // Determine if the directory is version controlled (skip if it is not)
  37. err := isVersionControlled(s, a.Directory)
  38. if err != nil {
  39. continue
  40. }
  41. // Execute the subcommand
  42. err = a.executeSubcommand(s, a.Args[0], a.Args[1:]...)
  43. executed = true
  44. }
  45. // If nothing was executed inform the user there is no repository found
  46. if !executed {
  47. return errors.New("No repository found")
  48. }
  49. return nil
  50. }
  51. // Get the working directory
  52. func getWorkingDirectory() string {
  53. d, _ := os.Getwd()
  54. return d
  55. }
  56. // Extract an array of version control systems available on the system
  57. func getVersionControlSystems() []string {
  58. return strings.Split(os.Getenv("CODE_VCS"), ";")
  59. }
  60. // Determine if the directory is part of the version control system
  61. func isVersionControlled(vcs, directory string) error {
  62. env := os.Getenv(fmt.Sprintf("CODE_%s_CHECK", strings.ToUpper(vcs)))
  63. if env == "" {
  64. return errors.New(fmt.Sprintf("CODE_%s_CHECK is not set", strings.ToUpper(vcs)))
  65. }
  66. // Execute the command and swallow any output
  67. var out bytes.Buffer
  68. actions := strings.Split(env, " ")
  69. cmd := exec.Command(vcs, actions...)
  70. cmd.Stdout = &out
  71. return cmd.Run()
  72. }
  73. // Execute a subcommand of the given version control system while passing along all arguments
  74. func (a *App) executeSubcommand(vcs, subcommand string, args ...string) error {
  75. command := []string{}
  76. env := os.Getenv(fmt.Sprintf("CODE_%s_%s", strings.ToUpper(vcs), strings.ToUpper(subcommand)))
  77. // If there is an override to the subcommand we will utilize it
  78. // otherwise, we simply pass everything to the VCS in raw format
  79. if env == "" {
  80. command = append(command, subcommand)
  81. command = append(command, args...)
  82. } else {
  83. actions := strings.Split(env, " ")
  84. command = append(command, actions...)
  85. command = append(command, args...)
  86. }
  87. // Execute the subcommand and output everything to standard system resources
  88. cmd := exec.Command(vcs, command...)
  89. cmd.Stdout = a.Stdout
  90. cmd.Stderr = a.Stderr
  91. cmd.Stdin = a.Stdin
  92. return cmd.Run()
  93. }