Skip to content

Commit

Permalink
Merge pull request #12 from ploxiln/flagset_test
Browse files Browse the repository at this point in the history
define command-line flags in new mainFlagSet() function
  • Loading branch information
ploxiln authored Jan 6, 2019
2 parents 2da0c32 + cd447d6 commit 604b6dc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
16 changes: 11 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import (
"github.com/mreiferson/go-options"
)

func main() {
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
func mainFlagSet() *flag.FlagSet {
flagSet := flag.NewFlagSet("oauth2_proxy", flag.ExitOnError)

emailDomains := StringArray{}
Expand All @@ -24,9 +23,6 @@ func main() {
googleGroups := StringArray{}
gitlabGroups := StringArray{}

config := flagSet.String("config", "", "path to config file")
showVersion := flagSet.Bool("version", false, "print version string")

flagSet.String("http-address", "127.0.0.1:4180", "[http://]<addr>:<port> or unix://<path> to listen on for HTTP clients")
flagSet.String("https-address", ":443", "<addr>:<port> to listen on for HTTPS clients")
flagSet.String("tls-cert", "", "path to certificate file")
Expand Down Expand Up @@ -85,6 +81,16 @@ func main() {

flagSet.String("signature-key", "", "GAP-Signature request signature key (algorithm:secretkey)")

return flagSet
}

func main() {
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
flagSet := mainFlagSet()

config := flagSet.String("config", "", "path to config file")
showVersion := flagSet.Bool("version", false, "print version string")

flagSet.Parse(os.Args[1:])

if *showVersion {
Expand Down
18 changes: 18 additions & 0 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"
"time"

"github.com/mreiferson/go-options"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -72,6 +73,23 @@ func TestInitializedOptions(t *testing.T) {
assert.Equal(t, nil, o.Validate())
}

func TestMultiGitLabGroupOptions(t *testing.T) {
flagSet := mainFlagSet()
flagSet.Parse([]string{"--gitlab-group=one", "-gitlab-group=two"})
opts := NewOptions()
cfg := make(EnvOptions)
options.Resolve(opts, flagSet, cfg)

assert.Equal(t, []string{"one", "two"}, opts.GitLabGroups)

flagSet = mainFlagSet()
flagSet.Parse([]string{"--upstream=http://127.0.0.1:2000"})
opts = NewOptions()
options.Resolve(opts, flagSet, cfg)

assert.Equal(t, 0, len(opts.GitLabGroups))
}

// Note that it's not worth testing nonparseable URLs, since url.Parse()
// seems to parse damn near anything.
func TestRedirectURL(t *testing.T) {
Expand Down

0 comments on commit 604b6dc

Please sign in to comment.