-
Notifications
You must be signed in to change notification settings - Fork 918
/
main.go
45 lines (39 loc) 路 854 Bytes
/
main.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
package importresolver
import (
"errors"
"fmt"
"io/ioutil"
"strings"
"github.com/Masterminds/vcs"
)
type Args struct {
Import string
}
func VCSRemoteForImport(args *Args) (interface{}, error) {
remote := args.Import
scheme := strings.Split(remote, ":")[0]
switch scheme {
case "http", "https":
return resolve(remote)
default:
schemes := []string{"https://", "http://"}
for _, scheme := range schemes {
repo, err := resolve(scheme + remote)
if err == nil {
return repo, nil
}
}
return nil, errors.New(fmt.Sprintf("Could not resolve url for: %v", remote))
}
}
func resolve(remote string) (interface{}, error) {
local, err := ioutil.TempDir("", "unused-vcs-local-dir")
if err != nil {
return nil, err
}
repo, err := vcs.NewRepo(remote, local)
if err != nil {
return nil, err
}
return repo.Remote(), nil
}