-
-
Notifications
You must be signed in to change notification settings - Fork 184
/
cmd_create.go
89 lines (75 loc) · 1.42 KB
/
cmd_create.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
package main
import (
"fmt"
"io"
"os"
"github.com/urfave/cli/v2"
)
func doCreate(c *cli.Context) error {
var (
name = c.Args().First()
vcs = c.String("vcs")
w = c.App.Writer
bare = c.Bool("bare")
)
if name == "" {
return fmt.Errorf("repository name is required")
}
u, err := newURL(name, false, true)
if err != nil {
return err
}
localRepo, err := LocalRepositoryFromURL(u, bare)
if err != nil {
return err
}
p := localRepo.FullPath
ok, err := isNotExistOrEmpty(p)
if err != nil {
return err
}
if !ok {
return fmt.Errorf("directory %q already exists and not empty", p)
}
remoteRepo, err := NewRemoteRepository(u)
if err != nil {
return err
}
vcsBackend, ok := vcsRegistry[vcs]
if !ok {
vcsBackend, _, err = remoteRepo.VCS()
if err != nil {
return err
}
}
if vcsBackend == nil {
return fmt.Errorf("failed to init: unsupported VCS")
}
initFunc := vcsBackend.Init
if initFunc == nil {
return fmt.Errorf("failed to init: unsupported VCS")
}
if err := os.MkdirAll(p, 0755); err != nil {
return err
}
if err := initFunc(p); err != nil {
return err
}
_, err = fmt.Fprintln(w, p)
return err
}
func isNotExistOrEmpty(name string) (bool, error) {
f, err := os.Open(name)
if err != nil {
if os.IsNotExist(err) {
return true, nil
}
return false, err
}
defer f.Close()
_, err = f.Readdirnames(1)
if err == io.EOF {
return true, nil
}
return false, err
}