diff --git a/cmd/pypihub/main.go b/cmd/pypihub/main.go index 3de5f0d..cee3f34 100644 --- a/cmd/pypihub/main.go +++ b/cmd/pypihub/main.go @@ -1,7 +1,13 @@ package main -import "fmt" +import ( + "fmt" + + "github.com/brettlangdon/pypihub" +) func main() { - fmt.Println("PyPIHub") + var config pypihub.Config + config = pypihub.ParseConfig() + fmt.Printf("%#v\r\n", config) } diff --git a/config.go b/config.go new file mode 100644 index 0000000..4d4c54d --- /dev/null +++ b/config.go @@ -0,0 +1,35 @@ +package pypihub + +import ( + "os" + "strings" + + arg "github.com/alexflint/go-arg" +) + +type Config struct { + Username string `arg:"-u,--username,env:PYPIHUB_USERNAME,required,help:Username of GitHub user to login as (env: PYPIHUB_USERNAME)"` + AccessToken string `arg:"-a,--access-token,env:PYPIHUB_ACCESS_TOKEN,required,help:GitHub personal access token to use for authenticating (env: PYPIHUB_ACCESS_TOKEN)"` + RepoNames []string `arg:"positional,help:list of '/' repos to proxy for (env: PYPIHUB_REPOS)"` + Bind string `arg:"-b,--bind,env:PYPIHUB_BIND,help:[
]: to bind the server to (default: ':8287') (env: PYPIHUB_BIND)"` +} + +func ParseConfig() Config { + var config = Config{ + Bind: ":8287", + RepoNames: make([]string, 0), + } + + arg.MustParse(&config) + + if val, ok := os.LookupEnv("PYPIHUB_REPOS"); ok { + config.RepoNames = append(config.RepoNames, strings.Split(val, " ")...) + } + for i := range config.RepoNames { + config.RepoNames[i] = strings.Trim(config.RepoNames[i], " ") + } + + config.RepoNames = uniqueSlice(config.RepoNames) + + return config +} diff --git a/utils.go b/utils.go new file mode 100644 index 0000000..6036711 --- /dev/null +++ b/utils.go @@ -0,0 +1,14 @@ +package pypihub + +func uniqueSlice(s []string) []string { + var m = make(map[string]bool) + for _, v := range s { + m[v] = true + } + + var o = make([]string, 0) + for v := range m { + o = append(o, v) + } + return o +}