-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
63 lines (60 loc) · 1.36 KB
/
parse.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
package install
import (
"strings"
"github.com/cardil/ghet/pkg/config"
"github.com/cardil/ghet/pkg/github"
)
// Parse parses the installation arguments from the given spec.
//
// The spec is a string that contains the arguments in a format of
// "owner/repo[@version][::archive-name][!!binary-name]".
func Parse(spec string) Args {
const expectedParts = 2
parts := strings.SplitN(spec, "@", expectedParts)
p2 := strings.SplitN(parts[0], "/", expectedParts)
owner, repo := "", ""
if len(p2) > 1 {
owner, repo = p2[0], p2[1]
}
version := "latest"
archive := ""
binary := ""
if len(parts) >= expectedParts {
parts = strings.SplitN(parts[1], "::", expectedParts)
version = parts[0]
if len(parts) >= expectedParts {
archive = parts[1]
}
}
parts = strings.SplitN(archive, "!!", expectedParts)
if len(parts) >= expectedParts {
_, binary = parts[0], parts[1]
}
if binary == "" {
binary = repo
}
ext := ""
parts = strings.SplitN(binary, ".", expectedParts)
if len(parts) > 1 {
binary, ext = parts[0], parts[1]
}
args := Args{
Asset: github.Asset{
FileName: github.FileName{
BaseName: binary,
Extension: ext,
},
Release: github.Release{
Tag: version,
Repository: github.Repository{
Owner: owner,
Repo: repo,
},
},
},
Site: config.Site{
Type: config.TypeGitHub,
},
}
return args.WithDefaults()
}