Skip to content

Commit

Permalink
only error on missing smugmug credentials when required (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
bzimmer authored Apr 22, 2023
1 parent f2d774b commit df0ba9a
Show file tree
Hide file tree
Showing 21 changed files with 134 additions and 230 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ jobs:
with:
skipBuild: false
skipCoverage: false
secrets: inherit

vuln:
uses: bzimmer/actions/.github/workflows/vuln.yaml@main
1 change: 1 addition & 0 deletions .github/workflows/mkdocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ jobs:

mkdocs:
uses: bzimmer/actions/.github/workflows/mkdocs.yaml@main
secrets: inherit
1 change: 1 addition & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ jobs:

release:
uses: bzimmer/actions/.github/workflows/release.yaml@main
secrets: inherit
1 change: 1 addition & 0 deletions .gitleaks-baseline.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
14 changes: 13 additions & 1 deletion Taskfile-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ tasks:
module:
sh: go mod edit -json | jq -r .Module.Path
cmds:
- goimports -w -local "{{.module}}" .
- goimports -w -local "{{.module}}/" .

docs:build:
desc: Statically generate the documentation
Expand All @@ -110,3 +110,15 @@ tasks:
deps: [docs:generate]
cmds:
- "{{.ROOT_DIR}}/env/bin/mkdocs serve"

gitleaks:
desc: Run gitleaks
deps: [dist]
cmds:
- gitleaks detect --no-banner --baseline-path {{.ROOT_DIR}}/.gitleaks-baseline.json --report-path {{.DIST}}/gitleaks-report.json

gitleaks:baseline:
desc: Run gitleaks
deps: [dist]
cmds:
- gitleaks detect --no-banner --report-path {{.ROOT_DIR}}/.gitleaks-baseline.json
130 changes: 0 additions & 130 deletions cmd/ma/clienttrace.go

This file was deleted.

114 changes: 60 additions & 54 deletions cmd/ma/main.go
Original file line number Diff line number Diff line change
@@ -1,53 +1,51 @@
package main

//go:generate go run main.go manual -o ../../docs/commands.md ../../docs/commands

import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/http/httptrace"
"os"
"time"

"github.com/armon/go-metrics"
"github.com/bzimmer/httpwares"
"github.com/bzimmer/manual"
"github.com/bzimmer/smugmug"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/afero"
"github.com/urfave/cli/v2"
"golang.org/x/text/language"

"github.com/bzimmer/manual"

"github.com/bzimmer/ma"
)

func flags() []cli.Flag {
return []cli.Flag{
&cli.StringFlag{
Name: "smugmug-client-key",
Required: true,
Usage: "smugmug client key",
EnvVars: []string{"SMUGMUG_CLIENT_KEY"},
Name: "smugmug-client-key",
Usage: "smugmug client key",
EnvVars: []string{"SMUGMUG_CLIENT_KEY"},
},
&cli.StringFlag{
Name: "smugmug-client-secret",
Required: true,
Usage: "smugmug client secret",
EnvVars: []string{"SMUGMUG_CLIENT_SECRET"},
Name: "smugmug-client-secret",
Usage: "smugmug client secret",
EnvVars: []string{"SMUGMUG_CLIENT_SECRET"},
},
&cli.StringFlag{
Name: "smugmug-access-token",
Required: true,
Usage: "smugmug access token",
EnvVars: []string{"SMUGMUG_ACCESS_TOKEN"},
Name: "smugmug-access-token",
Usage: "smugmug access token",
EnvVars: []string{"SMUGMUG_ACCESS_TOKEN"},
},
&cli.StringFlag{
Name: "smugmug-token-secret",
Required: true,
Usage: "smugmug token secret",
EnvVars: []string{"SMUGMUG_TOKEN_SECRET"},
Name: "smugmug-token-secret",
Usage: "smugmug token secret",
EnvVars: []string{"SMUGMUG_TOKEN_SECRET"},
},
&cli.BoolFlag{
Name: "json",
Expand All @@ -68,13 +66,6 @@ func flags() []cli.Flag {
Usage: "enable debugging of http requests",
Value: false,
},
&cli.BoolFlag{
Name: "trace",
Required: false,
Usage: "enable http client tracing",
Value: false,
Hidden: true,
},
}
}

Expand All @@ -96,6 +87,28 @@ func initLogging(c *cli.Context) error {
return nil
}

func mg(c *cli.Context) func() *smugmug.Client {
return func() *smugmug.Client {
httpclient, err := smugmug.NewHTTPClient(
c.String("smugmug-client-key"),
c.String("smugmug-client-secret"),
c.String("smugmug-access-token"),
c.String("smugmug-token-secret"))
if err != nil {
panic(err)
}
client, err := smugmug.NewClient(
smugmug.WithConcurrency(c.Int("concurrency")),
smugmug.WithHTTPClient(httpclient),
smugmug.WithPretty(c.Bool("debug")),
smugmug.WithHTTPTracing(c.Bool("debug")))
if err != nil {
panic(err)
}
return client
}
}

func main() {
app := &cli.App{
Name: "ma",
Expand Down Expand Up @@ -128,32 +141,14 @@ func main() {
grab.Transport = &httpwares.VerboseTransport{}
}

httpclient, err := smugmug.NewHTTPClient(
c.String("smugmug-client-key"),
c.String("smugmug-client-secret"),
c.String("smugmug-access-token"),
c.String("smugmug-token-secret"))
if err != nil {
return err
}

client, err := smugmug.NewClient(
smugmug.WithConcurrency(c.Int("concurrency")),
smugmug.WithHTTPClient(httpclient),
smugmug.WithPretty(c.Bool("debug")),
smugmug.WithHTTPTracing(c.Bool("debug")))
if err != nil {
return err
}

writer := io.Discard
if c.Bool("json") {
writer = c.App.Writer
}

c.App.Metadata = map[string]any{
ma.RuntimeKey: &ma.Runtime{
Client: client,
Smugmug: mg(c),
Sink: sink,
Grab: grab,
Metrics: metric,
Expand All @@ -165,10 +160,6 @@ func main() {
},
}

if c.Bool("trace") {
c.Context = httptrace.WithClientTrace(c.Context, ClientTrace())
}

return nil
},
After: ma.Metrics,
Expand All @@ -191,9 +182,24 @@ func main() {
manual.Manual(),
},
}
ctx := context.Background()
if err := app.RunContext(ctx, os.Args); err != nil {
os.Exit(1)
}
os.Exit(0)
var err error
defer func() {
if r := recover(); r != nil {
switch v := r.(type) {
case error:
log.Error().Err(v).Msg(app.Name)
case string:
log.Error().Err(errors.New(v)).Msg(app.Name)
default:
log.Error().Err(fmt.Errorf("%v", v)).Msg(app.Name)
}
os.Exit(1)
}
if err != nil {
log.Error().Err(err).Msg(app.Name)
os.Exit(1)
}
os.Exit(0)
}()
err = app.RunContext(context.Background(), os.Args)
}
1 change: 0 additions & 1 deletion docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ All your media archiving needs!
|json|j||emit all results as JSON and print to stdout|
|monochrome|||disable colored output|
|debug|||enable debugging of http requests|
|trace|||enable http client tracing|
|help|h||show help|

## Commands
Expand Down
2 changes: 1 addition & 1 deletion export.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func export(c *cli.Context) error {
return fmt.Errorf("expected two arguments, not {%d}", c.NArg())
}
x := &exporter{
mg: runtime(c).Client,
mg: runtime(c).Smugmug(),
fs: runtime(c).Fs,
grab: runtime(c).Grab,
force: c.Bool("force"),
Expand Down
2 changes: 1 addition & 1 deletion find.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func find(c *cli.Context) error {
mg := runtime(c).Client
mg := runtime(c).Smugmug()
scope := c.String("scope")
if scope == "" {
user, err := mg.User.AuthUser(c.Context)
Expand Down
Loading

0 comments on commit df0ba9a

Please sign in to comment.