-
Notifications
You must be signed in to change notification settings - Fork 55
/
helpers.go
76 lines (58 loc) · 1.65 KB
/
helpers.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
// Copyright (C) 2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package apmintegration
import (
"errors"
"fmt"
"net/url"
"path"
"strings"
"github.com/ava-labs/avalanche-cli/pkg/constants"
)
func removeSlashes(str string) string {
return strings.TrimSuffix(strings.TrimPrefix(str, "/"), "/")
}
func getGitOrg(gitURL *url.URL) (string, error) {
org, repo := path.Split(gitURL.Path)
org = removeSlashes(org)
repo = removeSlashes(repo)
if org == "" || repo == "" {
return "", errors.New("invalid url format, unable to find org: " + gitURL.Path)
}
return org, nil
}
func getGitRepo(gitURL *url.URL) (string, error) {
org, repo := path.Split(gitURL.Path)
org = removeSlashes(org)
repo = removeSlashes(repo)
if org == "" || repo == "" {
return "", errors.New("invalid url format, unable to find repo name: " + gitURL.Path)
}
return strings.TrimSuffix(repo, constants.GitExtension), nil
}
func getAlias(url *url.URL) (string, error) {
org, err := getGitOrg(url)
if err != nil {
return "", fmt.Errorf("unable to create alias: %w", err)
}
repo, err := getGitRepo(url)
if err != nil {
return "", fmt.Errorf("unable to create alias: %w", err)
}
return makeAlias(org, repo), nil
}
func makeAlias(org, repo string) string {
return org + "/" + repo
}
func MakeKey(alias, subnet string) string {
return alias + ":" + subnet
}
func splitKey(subnetKey string) (string, string, error) {
splitSubnet := strings.Split(subnetKey, ":")
if len(splitSubnet) != 2 {
return "", "", fmt.Errorf("invalid subnet key: %s", subnetKey)
}
repo := splitSubnet[0]
subnetName := splitSubnet[1]
return repo, subnetName, nil
}