forked from ki38sato/ansible-http
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkspace.go
39 lines (33 loc) · 1004 Bytes
/
workspace.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
package main
import (
"fmt"
"os"
"os/exec"
"path"
"github.com/pkg/errors"
"github.com/satori/go.uuid"
)
func CreateWorkspace(name string, uuid uuid.UUID) (string, error) {
dirname := fmt.Sprintf("%s/%s-%s", config.WorkDir, name, uuid)
if err := os.MkdirAll(dirname, 0777); err != nil {
return "", err
}
return dirname, nil
}
func GitCloneWorkspace(dirname string, destpath string, repositoryAddr string, branchName string) (string, error) {
// create clone dest directory
sourceDir := path.Clean(fmt.Sprintf("%s/%s", dirname, destpath))
if err := os.MkdirAll(sourceDir, 0777); err != nil {
return "", err
}
// git clone
out, err := exec.Command("git", "clone", "--depth", "1", "-b", branchName, repositoryAddr, sourceDir).CombinedOutput()
if err != nil {
return "", errors.Wrap(err, string(out))
}
fmt.Printf("[success] git clone --depth 1 -b %s %s %s\n", branchName, repositoryAddr, sourceDir)
return sourceDir, nil
}
func CreateUuid() uuid.UUID {
return uuid.NewV4()
}