Skip to content

Commit

Permalink
handle dry-run by hook
Browse files Browse the repository at this point in the history
  • Loading branch information
lesomnus committed Nov 26, 2024
1 parent a1cc501 commit 6ba8085
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 36 deletions.
35 changes: 16 additions & 19 deletions cmd/bring.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ import (
)

func NewCmdBring() *cli.Command {
executor := &executor{
DryRun: false,
}
dry_run := false

return &cli.Command{
Name: "bring",
Expand All @@ -29,9 +27,9 @@ func NewCmdBring() *cli.Command {
&cli.BoolFlag{
Name: "dry-run",
Usage: "Stops before write things to file",
Value: executor.DryRun,
Value: dry_run,

Destination: &executor.DryRun,
Destination: &dry_run,
},
},
Action: func(ctx context.Context, cmd *cli.Command) error {
Expand All @@ -56,20 +54,19 @@ func NewCmdBring() *cli.Command {
return fmt.Errorf("destination must be specified in the config file or given by argument")
}

if s, err := c.Secret.Open(ctx); err != nil {
return fmt.Errorf("open secret store: %w", err)
} else {
executor.Secret = s
}

executor.NewHook = func(ctx context.Context, t task.Task) hook.Hook {
return hook.Tie(
&sinkHookMw{D: t.Dest},
hook.Forward(hook.Join(
exe := executor{
NewHook: func(ctx context.Context, t task.Task) hook.Hook {
hs := []hook.Mw{}
if !dry_run {
hs = append(hs, &sinkHookMw{D: t.Dest})
}
hs = append(hs, hook.Forward(hook.Join(
&hooks.LogHook{T: t, L: log.From(ctx)},
&hooks.PrintHook{T: t, O: os.Stdout},
)),
)
)))

return hook.Tie(hs...)
},
}

num_tasks := c.Things.Len()
Expand All @@ -86,14 +83,14 @@ func NewCmdBring() *cli.Command {
Dest: p,
}

if err := c.Secret.OpenTo(ctx, t.Url, &executor.Secret); err != nil {
if err := c.Secret.OpenTo(ctx, t.Url, &exe.Secret); err != nil {
return err
}

ctx, cancel := c.Each.ApplyBringTimeout(ctx)
defer cancel()

if r, err := executor.Execute(ctx, task); err != nil {
if r, err := exe.Run(ctx, task); err != nil {
num_errors++
} else if r != nil {
defer r.Close()
Expand Down
7 changes: 3 additions & 4 deletions cmd/digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,19 @@ func NewCmdDigest() *cli.Command {

c := config.From(ctx)
l := log.From(ctx)
executor := &executor{
DryRun: true,
exe := &executor{
NewHook: func(ctx context.Context, t task.Task) hook.Hook {
return &hooks.LogHook{T: t, L: l}
},
}
if err := c.Secret.OpenTo(ctx, u, &executor.Secret); err != nil {
if err := c.Secret.OpenTo(ctx, u, &exe.Secret); err != nil {
return err
}

ctx, cancel := c.Each.ApplyBringTimeout(ctx)
defer cancel()

r, err := executor.Execute(ctx, task.Task{
r, err := exe.Run(ctx, task.Task{
Thing: thing.Thing{Url: u},

BringConfig: c.Each,
Expand Down
11 changes: 2 additions & 9 deletions cmd/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ import (
)

type executor struct {
Secret secret.Store

DryRun bool
Secret secret.Store
NewHook func(ctx context.Context, t task.Task) hook.Hook
}

Expand All @@ -29,7 +27,7 @@ func (e *executor) secret() secret.Store {
return secret.NopStore()
}

func (e *executor) Execute(ctx context.Context, t task.Task) (io.ReadCloser, error) {
func (e *executor) Run(ctx context.Context, t task.Task) (io.ReadCloser, error) {
hook := e.NewHook(ctx, t)
hook.OnStart()
defer hook.OnFinish()
Expand Down Expand Up @@ -79,11 +77,6 @@ func (e *executor) Execute(ctx context.Context, t task.Task) (io.ReadCloser, err
return nil, err
}

if e.DryRun {
hook.OnDone(nil)
return r, nil
}

hook.OnDone(r)
return r, nil
}
Expand Down
5 changes: 1 addition & 4 deletions internal/hooks/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,7 @@ func (h *PrintHook) OnSkip() {
}

func (h *PrintHook) OnDone(r io.Reader) {
h.sym = "◦"
if r != nil {
h.sym = color.New(color.FgHiGreen).Sprint("✓")
}
h.sym = color.New(color.FgHiGreen).Sprint("✓")
}

func (h *PrintHook) OnError(err error) {
Expand Down

0 comments on commit 6ba8085

Please sign in to comment.