mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-12-04 23:56:30 +00:00
56a854fe14
* update github.com/docker/cli * update github.com/docker/distribution * update github.com/docker/docker * update github.com/gin-gonic/gin * update github.com/golang-jwt/jwt/v4 * update github.com/golangci/golangci-lint * update github.com/gorilla/securecookie * update github.com/mattn/go-sqlite3 * update github.com/moby/moby * update github.com/prometheus/client_golang * update github.com/xanzy/go-gitlab
70 lines
1.2 KiB
Go
70 lines
1.2 KiB
Go
package importas
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"regexp"
|
|
)
|
|
|
|
type Config struct {
|
|
RequiredAlias map[string]string
|
|
Rules []*Rule
|
|
DisallowUnaliased bool
|
|
DisallowExtraAliases bool
|
|
}
|
|
|
|
func (c *Config) CompileRegexp() error {
|
|
rules := make([]*Rule, 0, len(c.RequiredAlias))
|
|
for path, alias := range c.RequiredAlias {
|
|
reg, err := regexp.Compile(fmt.Sprintf("^%s$", path))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
rules = append(rules, &Rule{
|
|
Regexp: reg,
|
|
Alias: alias,
|
|
})
|
|
}
|
|
|
|
c.Rules = rules
|
|
return nil
|
|
}
|
|
|
|
func (c *Config) findRule(path string) *Rule {
|
|
for _, rule := range c.Rules {
|
|
if rule.Regexp.MatchString(path) {
|
|
return rule
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *Config) AliasFor(path string) (string, bool) {
|
|
rule := c.findRule(path)
|
|
if rule == nil {
|
|
return "", false
|
|
}
|
|
|
|
alias, err := rule.aliasFor(path)
|
|
if err != nil {
|
|
return "", false
|
|
}
|
|
|
|
return alias, true
|
|
}
|
|
|
|
type Rule struct {
|
|
Alias string
|
|
Regexp *regexp.Regexp
|
|
}
|
|
|
|
func (r *Rule) aliasFor(path string) (string, error) {
|
|
str := r.Regexp.FindString(path)
|
|
if len(str) > 0 {
|
|
return r.Regexp.ReplaceAllString(str, r.Alias), nil
|
|
}
|
|
|
|
return "", errors.New("mismatch rule")
|
|
}
|