2020-09-10 17:27:46 +00:00
|
|
|
package flags
|
|
|
|
|
|
|
|
import (
|
2020-09-12 01:38:18 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-09-10 18:43:38 +00:00
|
|
|
"net/url"
|
2020-09-10 17:27:46 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2020-09-12 01:38:18 +00:00
|
|
|
"go.jolheiser.com/vanity/api"
|
|
|
|
|
2020-09-10 17:27:46 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
"go.jolheiser.com/beaver"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
configPath string
|
2020-09-12 01:38:18 +00:00
|
|
|
baseURL string
|
2020-09-10 17:27:46 +00:00
|
|
|
include cli.StringSlice
|
|
|
|
exclude cli.StringSlice
|
2020-09-12 01:38:18 +00:00
|
|
|
override cli.StringSlice
|
|
|
|
|
|
|
|
Port int
|
|
|
|
Domain string
|
|
|
|
Service string
|
|
|
|
BaseURL *url.URL
|
|
|
|
Namespace string
|
|
|
|
Token string
|
|
|
|
Include []*regexp.Regexp
|
|
|
|
Exclude []*regexp.Regexp
|
|
|
|
Private bool
|
|
|
|
Fork bool
|
|
|
|
Mirror bool
|
|
|
|
Archive bool
|
|
|
|
Override = make(map[string]string)
|
|
|
|
Interval time.Duration
|
|
|
|
Debug bool
|
|
|
|
|
|
|
|
ConfigPackages []*api.Package
|
2020-09-10 17:27:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var Flags = []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "config",
|
|
|
|
Usage: "Path to a config file",
|
|
|
|
EnvVars: []string{"VANITY_CONFIG"},
|
|
|
|
Destination: &configPath,
|
|
|
|
},
|
|
|
|
&cli.IntFlag{
|
|
|
|
Name: "port",
|
|
|
|
Usage: "Port to run the vanity server on",
|
|
|
|
Value: 7777,
|
|
|
|
EnvVars: []string{"VANITY_PORT"},
|
2020-09-12 01:38:18 +00:00
|
|
|
Destination: &Port,
|
2020-09-10 17:27:46 +00:00
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "domain",
|
2020-09-10 18:43:38 +00:00
|
|
|
Usage: "Vanity domain, e.g. go.domain.tld",
|
2020-09-10 17:27:46 +00:00
|
|
|
EnvVars: []string{"VANITY_DOMAIN"},
|
|
|
|
Required: true,
|
2020-09-12 01:38:18 +00:00
|
|
|
Destination: &Domain,
|
2020-09-10 17:27:46 +00:00
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "service",
|
|
|
|
Usage: "Service type (Gitea, GitHub, GitLab)",
|
|
|
|
Value: "gitea",
|
|
|
|
EnvVars: []string{"VANITY_SERVICE"},
|
2020-09-12 01:38:18 +00:00
|
|
|
Destination: &Service,
|
2020-09-10 17:27:46 +00:00
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "base-url",
|
2020-09-12 01:38:18 +00:00
|
|
|
Usage: "Base URL to service",
|
2020-09-10 17:27:46 +00:00
|
|
|
EnvVars: []string{"VANITY_BASE_URL"},
|
2020-09-12 01:38:18 +00:00
|
|
|
Destination: &baseURL,
|
2020-09-10 17:27:46 +00:00
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "namespace",
|
|
|
|
Usage: "Owner namespace",
|
|
|
|
EnvVars: []string{"VANITY_NAMESPACE"},
|
2020-09-12 01:38:18 +00:00
|
|
|
Destination: &Namespace,
|
2020-09-10 17:27:46 +00:00
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "token",
|
|
|
|
Usage: "Access token",
|
|
|
|
EnvVars: []string{"VANITY_TOKEN"},
|
2020-09-12 01:38:18 +00:00
|
|
|
Destination: &Token,
|
2020-09-10 17:27:46 +00:00
|
|
|
},
|
|
|
|
&cli.StringSliceFlag{
|
|
|
|
Name: "include",
|
|
|
|
Usage: "Repository names to include (regex)",
|
|
|
|
EnvVars: []string{"VANITY_INCLUDE"},
|
|
|
|
Destination: &include,
|
|
|
|
},
|
|
|
|
&cli.StringSliceFlag{
|
|
|
|
Name: "exclude",
|
|
|
|
Usage: "Repository names to exclude (regex)",
|
|
|
|
EnvVars: []string{"VANITY_EXCLUDE"},
|
|
|
|
Destination: &exclude,
|
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "private",
|
|
|
|
Usage: "Include private repositories",
|
|
|
|
EnvVars: []string{"VANITY_PRIVATE"},
|
2020-09-12 01:38:18 +00:00
|
|
|
Destination: &Private,
|
2020-09-10 17:27:46 +00:00
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "fork",
|
|
|
|
Usage: "Include forked repositories",
|
|
|
|
EnvVars: []string{"VANITY_FORK"},
|
2020-09-12 01:38:18 +00:00
|
|
|
Destination: &Fork,
|
2020-09-10 17:27:46 +00:00
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "mirror",
|
|
|
|
Usage: "Include mirrored repositories",
|
|
|
|
EnvVars: []string{"VANITY_MIRROR"},
|
2020-09-12 01:38:18 +00:00
|
|
|
Destination: &Mirror,
|
2020-09-10 17:27:46 +00:00
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "archive",
|
|
|
|
Usage: "Include archived repositories",
|
|
|
|
EnvVars: []string{"VANITY_ARCHIVE"},
|
2020-09-12 01:38:18 +00:00
|
|
|
Destination: &Archive,
|
|
|
|
},
|
|
|
|
&cli.StringSliceFlag{
|
|
|
|
Name: "override",
|
|
|
|
Usage: "Repository name to override (NAME=OVERRIDE)",
|
|
|
|
EnvVars: []string{"VANITY_OVERRIDE"},
|
|
|
|
Destination: &override,
|
2020-09-10 17:27:46 +00:00
|
|
|
},
|
|
|
|
&cli.DurationFlag{
|
|
|
|
Name: "interval",
|
|
|
|
Usage: "Interval between updating repositories",
|
|
|
|
Value: time.Minute * 15,
|
|
|
|
EnvVars: []string{"VANITY_INTERVAL"},
|
2020-09-12 01:38:18 +00:00
|
|
|
Destination: &Interval,
|
2020-09-10 17:27:46 +00:00
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "debug",
|
|
|
|
Usage: "Debug logging",
|
|
|
|
EnvVars: []string{"VANITY_DEBUG"},
|
2020-09-12 01:38:18 +00:00
|
|
|
Destination: &Debug,
|
2020-09-10 17:27:46 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func Before(ctx *cli.Context) error {
|
|
|
|
setConfig(ctx)
|
|
|
|
|
2020-09-12 01:38:18 +00:00
|
|
|
var defaultURL string
|
|
|
|
var configOnly bool
|
|
|
|
switch strings.ToLower(Service) {
|
|
|
|
case "gitea":
|
|
|
|
defaultURL = "https://gitea.com"
|
|
|
|
case "github":
|
|
|
|
defaultURL = "https://github.com"
|
|
|
|
case "gitlab":
|
|
|
|
defaultURL = "https://gitlab.com"
|
|
|
|
case "off":
|
|
|
|
configOnly = true
|
|
|
|
beaver.Infof("Running in config-only mode")
|
|
|
|
defaultURL = "https://domain.tld"
|
|
|
|
default:
|
|
|
|
return errors.New("unrecognized service type")
|
|
|
|
}
|
|
|
|
|
|
|
|
if baseURL == "" {
|
|
|
|
baseURL = defaultURL
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
BaseURL, err = url.Parse(baseURL)
|
2020-09-10 18:43:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-12 01:38:18 +00:00
|
|
|
if !configOnly {
|
|
|
|
errs := make([]string, 0, 2)
|
|
|
|
if Namespace == "" {
|
|
|
|
errs = append(errs, "namespace")
|
|
|
|
}
|
|
|
|
if Token == "" {
|
|
|
|
errs = append(errs, "token")
|
|
|
|
}
|
|
|
|
if len(errs) > 0 {
|
|
|
|
return fmt.Errorf("%s is required with a service", strings.Join(errs, ", "))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Include = make([]*regexp.Regexp, len(include.Value()))
|
2020-09-10 17:27:46 +00:00
|
|
|
for idx, i := range include.Value() {
|
2020-09-12 01:38:18 +00:00
|
|
|
Include[idx] = regexp.MustCompile(i)
|
2020-09-10 17:27:46 +00:00
|
|
|
}
|
|
|
|
|
2020-09-12 01:38:18 +00:00
|
|
|
Exclude = make([]*regexp.Regexp, len(exclude.Value()))
|
2020-09-10 17:27:46 +00:00
|
|
|
for idx, e := range exclude.Value() {
|
2020-09-12 01:38:18 +00:00
|
|
|
Exclude[idx] = regexp.MustCompile(e)
|
2020-09-10 17:27:46 +00:00
|
|
|
}
|
|
|
|
|
2020-09-12 01:38:18 +00:00
|
|
|
if Debug {
|
2020-09-10 17:27:46 +00:00
|
|
|
beaver.Console.Level = beaver.DEBUG
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|