Quellcode durchsuchen

initial translation of application from python

bmallred vor 10 Jahren
Ursprung
Commit
3f31c70a02
3 geänderte Dateien mit 203 neuen und 0 gelöschten Zeilen
  1. 110 0
      cmd/app.go
  2. 75 0
      cmd/app_test.go
  3. 18 0
      code.go

+ 110 - 0
cmd/app.go

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

+ 75 - 0
cmd/app_test.go

@ -0,0 +1,75 @@
1
package cmd
2
3
import (
4
	"bytes"
5
	"os"
6
	"testing"
7
)
8
9
func TestExtension(t *testing.T) {
10
	var buffer bytes.Buffer
11
12
	os.Setenv("CODE_GIT_INCOMING", "log ..@{u}")
13
	app := App{
14
		Args:      []string{"incoming"},
15
		Stdin:     &buffer,
16
		Stderr:    &buffer,
17
		Stdout:    &buffer,
18
		Directory: getWorkingDirectory(),
19
	}
20
21
	err := app.Run()
22
	if err != nil {
23
		t.FailNow()
24
	}
25
}
26
27
func TestRawCommand(t *testing.T) {
28
	var buffer bytes.Buffer
29
	app := App{
30
		Args:      []string{"status"},
31
		Stdin:     &buffer,
32
		Stderr:    &buffer,
33
		Stdout:    &buffer,
34
		Directory: getWorkingDirectory(),
35
	}
36
37
	err := app.Run()
38
	if err != nil {
39
		t.FailNow()
40
	}
41
}
42
43
func TestMissingCommand(t *testing.T) {
44
	var buffer bytes.Buffer
45
	app := App{
46
		Args:      []string{"nonexistant", "command"},
47
		Stdin:     &buffer,
48
		Stderr:    &buffer,
49
		Stdout:    &buffer,
50
		Directory: getWorkingDirectory(),
51
	}
52
53
	err := app.Run()
54
	if err != nil {
55
		t.FailNow()
56
	}
57
}
58
59
func TestMissingRepository(t *testing.T) {
60
	os.Chdir(os.TempDir())
61
62
	var buffer bytes.Buffer
63
	app := App{
64
		Args:      []string{"status"},
65
		Stdin:     &buffer,
66
		Stderr:    &buffer,
67
		Stdout:    &buffer,
68
		Directory: getWorkingDirectory(),
69
	}
70
71
	err := app.Run()
72
	if err == nil {
73
		t.FailNow()
74
	}
75
}

+ 18 - 0
code.go

@ -0,0 +1,18 @@
1
package main
2
3
import (
4
	"log"
5
	"os"
6
7
	"code.revolvingcow.com/revolvingcow/go-code/cmd"
8
)
9
10
// Main entry point to the application
11
func main() {
12
	err := cmd.NewApp().Run()
13
14
	if err != nil {
15
		log.Fatal(err)
16
		os.Exit(1)
17
	}
18
}