diff options
| -rw-r--r-- | cmd/lulu/creds.go | 22 | ||||
| -rw-r--r-- | cmd/lulu/main.go | 2 | ||||
| -rw-r--r-- | lulu.go | 16 | ||||
| -rw-r--r-- | lulu_test.go | 31 |
4 files changed, 31 insertions, 40 deletions
diff --git a/cmd/lulu/creds.go b/cmd/lulu/creds.go index e26f0ce..abb904e 100644 --- a/cmd/lulu/creds.go +++ b/cmd/lulu/creds.go @@ -7,6 +7,8 @@ import ( "strings" "github.com/adrg/xdg" + + "git.samanthony.xyz/lulu" ) var ( @@ -17,26 +19,22 @@ var ( sandboxSecretFile = filepath.Join(configDir, "sandbox", "secret") ) -// Credentials contains the client-key and client-secret used to -// authenticate to the API. -type Credentials struct { - key, secret string -} - -func readCreds(sandbox bool) (Credentials, error) { +func readCreds(sandbox bool) (lulu.Credentials, error) { keyf, secf := keyFile, secretFile if sandbox { keyf, secf = sandboxKeyFile, sandboxSecretFile } - key, err := readFile(keyf) + var creds lulu.Credentials + var err error + creds.Key, err = readFile(keyf) if err != nil { - return Credentials{}, fmt.Errorf("error reading client-key file: %w", err) + return creds, fmt.Errorf("error reading client-key file: %w", err) } - sec, err := readFile(secf) + creds.Secret, err = readFile(secf) if err != nil { - return Credentials{}, fmt.Errorf("error reading client-secret file: %w", err) + return creds, fmt.Errorf("error reading client-secret file: %w", err) } - return Credentials{key, sec}, nil + return creds, nil } func readFile(path string) (string, error) { diff --git a/cmd/lulu/main.go b/cmd/lulu/main.go index 6a9bcba..0609279 100644 --- a/cmd/lulu/main.go +++ b/cmd/lulu/main.go @@ -24,7 +24,7 @@ func main() { creds, err := readCreds(cli.Sandbox) ctx.FatalIfErrorf(err) - clnt, err := lulu.NewClient(context.Background(), creds.key, creds.secret) + clnt, err := lulu.NewClient(context.Background(), creds) ctx.FatalIfErrorf(err) err = ctx.Run(cli.Globals, clnt) @@ -58,17 +58,23 @@ type Client struct { c *http.Client } -// NewClient returns a client that will use the given client-key and -// client-secret to connect to the API server. -func NewClient(ctx context.Context, key, secret string) (*Client, error) { +// Credentials contains the client-key and client-secret used to +// authenticate to the API. +type Credentials struct { + Key, Secret string +} + +// NewClient returns a client that will use the given credentials to +// connect to the API server. +func NewClient(ctx context.Context, creds Credentials) (*Client, error) { tokenUrl, err := url.JoinPath(ApiUrl, tokenPath) if err != nil { return nil, pkgErrf(err, "error creating client") } cfg := &clientcredentials.Config{ - ClientID: key, - ClientSecret: secret, + ClientID: creds.Key, + ClientSecret: creds.Secret, TokenURL: tokenUrl, } return &Client{ctx, cfg.Client(ctx)}, nil diff --git a/lulu_test.go b/lulu_test.go index 420033c..063d5aa 100644 --- a/lulu_test.go +++ b/lulu_test.go @@ -2,10 +2,9 @@ package lulu import ( "context" + _ "embed" "encoding/json" "flag" - "fmt" - "os" "strings" "testing" "time" @@ -14,9 +13,6 @@ import ( ) const ( - clientKeyPath = "testdata/clientkey" - clientSecretPath = "testdata/clientsecret" - interiorUrl = "https://www.dropbox.com/sh/p3zh22vzsaegiri/AACOUn3LFKsITDzylh13bQpsa/161025/thesis2.pdf?dl=1" coverUrl = "https://www.dropbox.com/sh/p3zh22vzsaegiri/AADP367j0bTWlt8fCu-_tm2ia/161025/139056_cover.pdf?dl=1" @@ -24,35 +20,26 @@ const ( ) var ( - clientKey string + //go:embed testdata/clientkey + clientKey string + //go:embed testdata/clientsecret clientSecret string ) func TestMain(m *testing.M) { flag.BoolVar(&Debug, "d", false, "Print debug info to stderr.") flag.Parse() - Sandbox() - - if b, err := os.ReadFile(clientKeyPath); err == nil { - clientKey = strings.TrimSpace(string(b)) - } else { - fmt.Fprintf(os.Stderr, "%v\nCopy and paste your \"client key\" from the API Keys page into %s\n%s\n", err, clientKeyPath, SandboxApiKeyPage) - os.Exit(1) - } - if b, err := os.ReadFile(clientSecretPath); err == nil { - clientSecret = strings.TrimSpace(string(b)) - } else { - fmt.Fprintf(os.Stderr, "%v\nCopy and paste your \"client secret\" from the API Keys page into %s\n%s\n", err, clientSecretPath, SandboxApiKeyPage) - os.Exit(1) - } - m.Run() } func newClient(t *testing.T) *Client { t.Helper() - c, err := NewClient(t.Context(), clientKey, clientSecret) + creds := Credentials{ + Key: strings.TrimSpace(clientKey), + Secret: strings.TrimSpace(clientSecret), + } + c, err := NewClient(t.Context(), creds) require.NoError(t, err) return c } |