-
Notifications
You must be signed in to change notification settings - Fork 14
/
utils.go
67 lines (55 loc) · 1.09 KB
/
utils.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
package cmd
import (
"errors"
"fmt"
"io"
neturl "net/url"
"os"
"path/filepath"
"strings"
"github.com/spf13/cobra"
)
const (
GROUP_BASIC = "basic"
GROUP_QUICK = "quick"
)
func exitWithError(err error) {
cobra.CheckErr(err)
}
func exitWithFormat(format string, a ...interface{}) {
cobra.CheckErr(fmt.Sprintf(format, a...))
}
func parseRepoStr(repoAndRef string) (repoUrl string, ref string, err error) {
comps := strings.Split(repoAndRef, "@")
if len(comps) == 1 {
repoUrl = repoAndRef
} else if len(comps) == 2 {
repoUrl = comps[0]
ref = comps[1]
} else {
err = errors.New("Invalid repository: " + repoAndRef)
}
return
}
func transformRepoUrl(base string, repo string) (string, error) {
url, err := neturl.Parse(repo)
if err != nil {
return "", err
}
if url.Scheme != "" {
return repo, nil
}
if strings.HasPrefix(repo, "/") {
return repo, nil
}
return filepath.Abs(filepath.Join(base, url.Path))
}
func isDirEmpty(dir string) bool {
f, err := os.Open(dir)
if err != nil {
return false
}
defer f.Close()
_, err = f.Readdirnames(1)
return err == io.EOF
}