-
Notifications
You must be signed in to change notification settings - Fork 0
/
execution.go
95 lines (73 loc) · 2.34 KB
/
execution.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package execution
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"github.com/carrchang/handy-ci/config"
)
type Execution struct {
Command string
Path string
Args []string
Skip bool
}
type Parser interface {
CheckArgs(command *cobra.Command, args []string) error
Parse(
command *cobra.Command, args []string,
workspace config.Workspace, group config.Group, repository config.Repository) ([]Execution, error)
}
type ParseError struct {
message string
}
func (e ParseError) Error() string {
return fmt.Sprintf("%s", e.message)
}
func GroupPath(workspace config.Workspace, group config.Group) string {
workspacePath := filepath.FromSlash(workspace.Path)
workspacePath = strings.ReplaceAll(workspacePath, "$HANDY_CI_ROOT", os.Getenv("HANDY_CI_ROOT"))
homeDir, _ := os.UserHomeDir()
if homeDir != "" {
workspacePath = strings.ReplaceAll(workspacePath, "$HOME", homeDir)
}
workspacePath = strings.TrimSuffix(workspacePath, string(os.PathSeparator))
if len(group.Path) > 0 {
if strings.HasPrefix(group.Path, string(os.PathSeparator)) {
return strings.TrimSuffix(group.Path, string(os.PathSeparator))
} else {
return fmt.Sprintf("%s"+string(os.PathSeparator)+"%s", workspacePath, group.Path)
}
}
if group.NameIgnoredInPath {
return workspacePath
} else {
return fmt.Sprintf("%s"+string(os.PathSeparator)+"%s", workspacePath, group.Name)
}
}
func RepositoryPath(workspace config.Workspace, group config.Group, repository config.Repository) string {
groupPath := GroupPath(workspace, group)
groupPath = strings.TrimSuffix(groupPath, string(os.PathSeparator))
if len(repository.Path) > 0 {
if strings.HasPrefix(repository.Path, string(os.PathSeparator)) {
return strings.TrimSuffix(repository.Path, string(os.PathSeparator))
} else {
return fmt.Sprintf("%s"+string(os.PathSeparator)+"%s", groupPath, repository.Path)
}
}
if repository.NameIgnoredInPath {
return groupPath
} else {
return fmt.Sprintf("%s"+string(os.PathSeparator)+"%s", groupPath, repository.Name)
}
}
func RepositoryRemoteURL(repository config.Repository, remoteName string) string {
var remoteUrl string
for _, remote := range repository.Remotes {
if remote.Name == remoteName {
remoteUrl = remote.URL
}
}
return remoteUrl
}