-
Notifications
You must be signed in to change notification settings - Fork 7
/
spec.go
116 lines (104 loc) · 3.58 KB
/
spec.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package catalog
import (
"fmt"
"net/url"
"os"
"os/exec"
"path"
"strings"
"github.com/anz-bank/sysl/pkg/sysl"
)
// CopySyslModCache copies ALL the contents of the sysl module cache directory
// (typically found in ~/.sysl/github.com) and outputs it to the specified folder
func CopySyslModCache(targetDir string) error {
homeDir, err := os.UserHomeDir()
if err != nil {
return err
}
syslCacheDir := homeDir + "/.sysl/github.com"
cpSpecsToOutputDir := exec.Command("cp", "-r", syslCacheDir, targetDir)
if err := cpSpecsToOutputDir.Run(); err != nil {
return err
}
return nil
}
func IsOpenAPIFile(source *sysl.SourceContext) bool {
importPath := source.GetFile()
fileExt := path.Ext(importPath)
if fileExt == ".yaml" || fileExt == ".json" {
return true
}
return false
}
// BuildSpecURL takes a source context reference and builds an raw git URL for it
// It handles sourceContext paths which are from remote repos as well as in the same repo
func BuildSpecURL(source *sysl.SourceContext) (string, error) {
// Append the version tag to the repo name
var filePath string = source.GetFile()
if v := source.GetVersion(); v != "" {
filePath = AppendVersion(filePath, v)
}
filePath = strings.TrimPrefix(filePath, ".")
if !strings.HasPrefix(filePath, "/") {
filePath = "/" + filePath
}
return filePath, nil
}
// AppendVersion takes in a remote file import path and a version, and appends the version tag to the repo name separated by the '@' char
// e.g for an input
// - remoteFilePath github.com/anz-bank/sysl-examples/demos/grocerystore/grocerystore.sysl
// - version v0.0.0-c63b9e92813a
// it returns /github.com/anz-bank/sysl-examples@v0.0.0-c63b9e92813a/demos/grocerystore/grocerystore.sysl
func AppendVersion(remoteFilePath string, version string) string {
names := strings.FieldsFunc(remoteFilePath, func(c rune) bool {
return c == '/'
})
names[2] = names[2] + "@" + version
return path.Join(names...)
}
// GetRemoteFromGit gets the URL to the git remote
// e.g github.com/myorg/somerepo/
func GetRemoteFromGit() (string, error) {
cmd := exec.Command("git", "config", "--get", "remote.origin.url")
out, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("error getting git remote: is sysl-catalog running in a git repo? %w", err)
}
return StripExtension(string(out)), nil
}
// StripExtension removes spaces and suffixes
func StripExtension(input string) string {
noExt := strings.TrimSuffix(input, path.Ext(input))
noSpace := strings.TrimSpace(noExt)
return noSpace
}
// BuildGithubRawURL gets the base URL for raw content hosted on github.com or Github Enterprise
// For github.com it takes in https://github.com/anz-bank/sysl-catalog and returns https://raw.githubusercontent.com/anz-bank/sysl-catalog/master/
// For Github Enterprise it takes in https://github.myorg.com/anz-bank/sysl-catalog and returns https://github.myorg.com/raw/anz-bank/sysl-catalog/master/
func BuildGithubRawURL(repoURL string) (gitURL string) {
url, err := url.Parse(repoURL)
if err != nil {
panic(err)
}
switch url.Host {
case "github.com":
url.Host = "raw.githubusercontent.com"
url.Path = url.Path + "/master/"
gitURL = url.String()
default:
// Handles github enterprise which uses a different URL scheme for raw files
url.Path = "raw" + url.Path + "/master/"
gitURL = url.String()
}
return gitURL
}
// BuildGithubBlobURL creates a root URL for github blob
// it will not work for non github links.
func BuildGithubBlobURL(repoURL string) string {
url, err := url.Parse(repoURL)
if err != nil {
panic(err)
}
url.Path = path.Join(url.Path, "/blob/master/")
return url.String()
}