This repository has been archived by the owner on Aug 1, 2022. It is now read-only.
/
vcs.go
96 lines (83 loc) · 2.25 KB
/
vcs.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
96
// Copyright 2016 Cutehacks AS. All rights reserved.
// License can be found in the LICENSE file.
package vcs
import (
"fmt"
"os"
"qpm.io/common"
msg "qpm.io/common/messages"
)
// Installer - generic interface to functionality needed to install packages
type Installer interface {
Install(repository *msg.Package_Repository, version *msg.Package_Version, destination string) (*common.PackageWrapper, error)
}
func CreateInstaller(repository *msg.Package_Repository) (Installer, error) {
switch repository.Type {
case msg.RepoType_GIT:
git := NewGit()
if err := git.Test(); err == nil {
return git, nil
}
case msg.RepoType_GITHUB:
git := NewGit()
if err := git.Test(); err == nil {
return git, nil
}
return NewGitHub(), nil
case msg.RepoType_MERCURIAL:
hg := NewMercurial()
if err := hg.Test(); err == nil {
return hg, nil
}
}
return nil, fmt.Errorf("Repository type %s is not supported", msg.RepoType_name[int32(repository.Type)])
}
// Publisher - generic interface to VCS functionality need to publish packages
type Publisher interface {
Test() error
CreateTag(name string) error
ValidateCommit(commit string) error
RepositoryURL() (string, error)
LastCommitRevision() (string, error)
LastCommitAuthorName() (string, error)
LastCommitEmail() (string, error)
RepositoryFileList() ([]string, error)
}
func CreatePublisher(repository *msg.Package_Repository) (Publisher, error) {
switch repository.Type {
case msg.RepoType_GIT:
fallthrough
case msg.RepoType_GITHUB:
git := NewGit()
if err := git.Test(); err != nil {
return nil, err
}
return git, nil
case msg.RepoType_MERCURIAL:
hg := NewMercurial()
if err := hg.Test(); err != nil {
return nil, err
}
return hg, nil
}
return nil, fmt.Errorf("Repository type %s is not supported", msg.RepoType_name[int32(repository.Type)])
}
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}
func RepoType() (msg.RepoType, error) {
if yes, _ := exists(".git"); yes {
return msg.RepoType_GIT, nil
}
if yes, _ := exists(".hg"); yes {
return msg.RepoType_MERCURIAL, nil
}
return msg.RepoType_AUTO, fmt.Errorf("Could not auto-detect the repository type")
}