-
Notifications
You must be signed in to change notification settings - Fork 31
/
initialiser.git.go
77 lines (69 loc) · 1.73 KB
/
initialiser.git.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"path"
)
// GitInitialiserConfig holds the configurations for the GitInitialiser
type GitInitialiserConfig struct {
Path string
}
// InitGitInitialiser initialises the Git initialiser that assists in
// initialising a directory as a Git repository
func InitGitInitialiser(config *GitInitialiserConfig) *GitInitialiser {
gi := &GitInitialiser{
Key: ".git",
Path: config.Path,
logger: InitLogger(&LoggerConfig{
Format: "raw",
}),
}
return gi
}
// GitInitialiser assists in initialising a directory as a Git repository
type GitInitialiser struct {
Key string
Path string
logger *Logger
}
// Check verifies that the path exists
func (gi *GitInitialiser) Check() bool {
return directoryExists(path.Join(gi.Path, "/.git"))
}
// Confirm seeks advice from the user whether we should proceed with the
// Git repository initialisation
func (gi *GitInitialiser) Confirm(reader *bufio.Reader) bool {
return confirm(
reader,
Color("white", fmt.Sprintf("godev> initialise git repository at '%s'?", gi.Path)),
false,
Color("bold", Color("red", initialiserRetryText)),
)
}
// GetKey returns the key of this initialiser
func (gi *GitInitialiser) GetKey() string {
return gi.Key
}
// Handle processes the initialiser (initialises the Git repository)
func (gi *GitInitialiser) Handle(skip ...bool) error {
if len(skip) > 0 && skip[0] {
gi.logger.Info(
Color("gray",
fmt.Sprintf("godev> skipping git repository initialisation at '%s'", gi.Path),
),
)
return nil
}
var err error
if _, err = exec.LookPath("git"); err != nil {
return err
}
cmd := exec.Command("git", "init")
cmd.Dir = gi.Path
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
err = cmd.Run()
return err
}