Skip to content

Commit

Permalink
feat: goauth: add ParseOptions(), Options.NewClient()
Browse files Browse the repository at this point in the history
  • Loading branch information
grokify committed Sep 23, 2024
1 parent 766fccb commit 76c385c
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions cli.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,40 @@
package goauth

// Options is a struct to be used with `github.com/jessevdk/go-flags`.
// It can be embedded in another struct.
import (
"context"
"net/http"

"github.com/grokify/mogo/errors/errorsutil"
"github.com/jessevdk/go-flags"
)

// Options is a struct to be used with `ParseOptions()` or `github.com/jessevdk/go-flags`.
// It can be embedded in another struct and used directly with `github.com/jessevdk/go-flags`.
type Options struct {
CredsPath string `long:"creds" description:"Environment File Path" required:"true"`
Account string `long:"account" description:"Environment Variable Name"`
Token string `long:"token" description:"Token"`
CLI []bool `long:"cli" description:"CLI"`
}

func ParseOptions() (*Options, error) {
opts := &Options{}
_, err := flags.Parse(opts)
return opts, err
}

func (opts *Options) Credentials() (Credentials, error) {
return ReadFileCredentialsFromCredentialsSet(opts.CredsPath, opts.Account)
}

func (opts *Options) NewClient(ctx context.Context) (*http.Client, error) {
if creds, err := opts.Credentials(); err != nil {
return nil, errorsutil.Wrap(err, "error in `goauth.Options.NewClient() call to self as `goauth.Options.Credentials()`")
} else {
return creds.NewClient(ctx)
}
}

func (opts *Options) UseCLI() bool {
return len(opts.CLI) > 0
}

0 comments on commit 76c385c

Please sign in to comment.