diff --git a/go.mod b/go.mod index d5ae66b..5764da0 100644 --- a/go.mod +++ b/go.mod @@ -2,6 +2,7 @@ module github.com/sj14/review-bot require ( github.com/google/go-github/v25 v25.0.2 + github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.3.0 github.com/xanzy/go-gitlab v0.11.6 golang.org/x/oauth2 v0.0.0-20181106182150-f42d05182288 diff --git a/go.sum b/go.sum index 5dafa6c..d1687f0 100644 --- a/go.sum +++ b/go.sum @@ -8,6 +8,8 @@ github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASu github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= diff --git a/main.go b/main.go index 97db4b4..e75ae5c 100644 --- a/main.go +++ b/main.go @@ -2,31 +2,80 @@ package main import ( "encoding/json" - "flag" "fmt" "io/ioutil" "log" + "os" "strings" "text/template" "github.com/sj14/review-bot/hoster/github" "github.com/sj14/review-bot/hoster/gitlab" "github.com/sj14/review-bot/slackermost" + "github.com/spf13/pflag" ) func main() { var ( - host = flag.String("host", "", "host address (e.g. github.com, gitlab.com or self-hosted gitlab url)") - token = flag.String("token", "", "host API token") - repo = flag.String("repo", "", "repository (format: 'owner/repo'), or project id (only gitlab)") - reviewersPath = flag.String("reviewers", "examples/reviewers.json", "path to the reviewers file") - templatePath = flag.String("template", "", "path to the template file") - webhook = flag.String("webhook", "", "slack/mattermost webhook URL") - channelOrUser = flag.String("channel", "", "mattermost channel (e.g. MyChannel) or user (e.g. @AnyUser)") + // Repository flags, available for all subcommands. + repoFlags = pflag.NewFlagSet("repo", pflag.ExitOnError) + githost = repoFlags.String("host", "", "host address (e.g. github.com, gitlab.com or self-hosted gitlab url)") + token = repoFlags.String("token", "", "host API token") + repo = repoFlags.String("repo", "", "repository (format: 'owner/repo'), or project id (only gitlab)") + reviewersPath = repoFlags.String("reviewers", "examples/reviewers.json", "path to the reviewers file") + templatePath = repoFlags.String("template", "", "path to the template file") + + // Slack/Mattermost flags + slackermostFlags = pflag.NewFlagSet("slackermost", pflag.ExitOnError) + webhook = slackermostFlags.String("webhook", "", "slack/mattermost webhook URL") + channelOrUser = slackermostFlags.String("channel", "", "mattermost channel (e.g. MyChannel) or user (e.g. @AnyUser)") + + // E-Mail flags + mailFlags = pflag.NewFlagSet("mail", pflag.ExitOnError) + // mailhost = mailFlags.String("mailhost", "", "...") + // user = mailFlags.String("user", "", "...") + // pass = mailFlags.String("pass", "", "...") ) - flag.Parse() - if *host == "" { + repoFlags.Usage = func() { + fmt.Fprintf(os.Stderr, "Available subcommands:\n\tslackermost | mail\n") + fmt.Fprintf(os.Stderr, "\tUse 'subcommand --help' for all flags of the specified command.\n") + fmt.Fprintf(os.Stderr, "Generic flags for all subcommands:\n") + repoFlags.PrintDefaults() + } + + log.Printf("got arg: %v", os.Args[1]) + + switch os.Args[1] { + case "slackermost": + slackermostFlags.AddFlagSet(repoFlags) + if err := slackermostFlags.Parse(os.Args[2:]); err != nil { + log.Fatalf("failed to parse slackermost flags: %v", err) + } + log.Println("called slackermost subcommand") + case "mail": + mailFlags.AddFlagSet(repoFlags) + if err := mailFlags.Parse(os.Args[2:]); err != nil { + log.Fatalf("failed to parse mail flags: %v", err) + } + log.Println("called mail subcommand") + default: + if err := repoFlags.Parse(os.Args[1:]); err != nil { + log.Fatalf("failed to parse default flags: %v", err) + } + + // Command not recognized. Print usage help and exit. + repoFlags.Usage() + os.Exit(1) + } + + // No comamnd given. Print usage help and exit. + if len(os.Args) < 2 { + repoFlags.Usage() + os.Exit(1) + } + + if *githost == "" { log.Fatalln("missing host") } if *repo == "" { @@ -38,14 +87,14 @@ func main() { var tmpl *template.Template if *templatePath != "" { tmpl = loadTemplate(*templatePath) - } else if *host == "github.com" { + } else if *githost == "github.com" { tmpl = github.DefaultTemplate() } else { tmpl = gitlab.DefaultTemplate() } var reminder string - if *host == "github.com" { + if *githost == "github.com" { ownerRespo := strings.SplitN(*repo, "/", 2) if len(ownerRespo) != 2 { log.Fatalln("wrong repo format (use 'owner/repo')") @@ -58,7 +107,7 @@ func main() { reminder = github.ExecTemplate(tmpl, repo, reminders) } else { - project, reminders := gitlab.AggregateReminder(*host, *token, *repo, reviewers) + project, reminders := gitlab.AggregateReminder(*githost, *token, *repo, reviewers) if len(reminders) == 0 { // prevent from sending the header only return