-
Notifications
You must be signed in to change notification settings - Fork 5
/
rc.go
141 lines (118 loc) · 3.03 KB
/
rc.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package scaffold
import (
"errors"
"fmt"
"io"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/go-git/go-git/v5/plumbing/transport"
githttp "github.com/go-git/go-git/v5/plumbing/transport/http"
"gopkg.in/yaml.v3"
)
type ScaffoldRC struct {
// Defaults define a default value for a variable.
// name: myproject
// git_user: hay-kot
//
// These are injected into the template as variables for
// every scaffold.
Defaults map[string]any `yaml:"defaults"`
// Aliases define a alias for a repository.
// or filepath.
//
// component: https://githublcom/hay-kot/scaffold-go-component
// cli: https://github.com/hay-kot/scaffold-go-cli
Aliases map[string]string `yaml:"aliases"`
// Shorts define a short name for a repository.
// the key will be expanded into the value.
// gh: https://github.com
// gt: https://gitea.com
//
// This will allow you to use the short name in the scaffold
// gh:myorg/myrepo
Shorts map[string]string `yaml:"shorts"`
// Auth defines a list of auth entries that can be used to
// authenticate with a remote SCM.
Auth []AuthEntry `yaml:"auth"`
}
type AuthEntry struct {
Match regexp.Regexp `yaml:"match"`
Basic BasicAuth `yaml:"basic"`
Token string `yaml:"token"`
}
type BasicAuth struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
}
type RCValidationError struct {
Key string
Cause error
}
type RcValidationErrors []RCValidationError
func (e RcValidationErrors) Error() string {
return "invalid scaffold rc"
}
func NewScaffoldRC(r io.Reader) (*ScaffoldRC, error) {
var out ScaffoldRC
err := yaml.NewDecoder(r).Decode(&out)
if err != nil {
if errors.Is(err, io.EOF) {
// Assume empty file and return empty struct
return &out, nil
}
return nil, err
}
errs := []RCValidationError{}
for k, v := range out.Shorts {
_, err := url.ParseRequestURI(v)
if err != nil {
errs = append(errs, RCValidationError{
Key: k,
Cause: fmt.Errorf("parse url failed: %w", err),
})
}
}
for k, v := range out.Aliases {
// Shorts must be absolute path or relative to ~ or a URL
_, err := url.ParseRequestURI(v)
if err != nil {
if !filepath.IsAbs(v) && !strings.HasPrefix(v, "~") {
errs = append(errs, RCValidationError{
Key: k,
Cause: fmt.Errorf("invalid short path: %w", err),
})
}
}
}
if len(errs) > 0 {
return nil, RcValidationErrors(errs)
}
return &out, nil
}
func expandEnvVars(s string) string {
if !strings.HasPrefix(s, "${") && !strings.HasSuffix(s, "}") {
return s
}
return os.Getenv(s[2 : len(s)-1])
}
func (rc *ScaffoldRC) Authenticator(pkgurl string) (transport.AuthMethod, bool) {
for _, auth := range rc.Auth {
if auth.Match.MatchString(pkgurl) {
if auth.Basic.Username != "" {
return &githttp.BasicAuth{
Username: expandEnvVars(auth.Basic.Username),
Password: expandEnvVars(auth.Basic.Password),
}, true
}
if auth.Token != "" {
return &githttp.TokenAuth{
Token: expandEnvVars(auth.Token),
}, true
}
}
}
return nil, false
}