Skip to content

Commit

Permalink
handle error on the caller
Browse files Browse the repository at this point in the history
  • Loading branch information
lesomnus committed Nov 26, 2024
1 parent 6ba8085 commit fe8d974
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 29 deletions.
2 changes: 1 addition & 1 deletion bringer/smb.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (b *smbBringer) bring(ctx context.Context, t thing.Thing, opts ...Option) (
v.share = v.share.WithContext(ctx)
v.File, err = v.share.Open(p)
if err != nil {
return nil, fmt.Errorf("open: %w", err)
return v, fmt.Errorf("open: %w", err)
}

return v, nil
Expand Down
48 changes: 22 additions & 26 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/urfave/cli/v3"
)

func NewApp() *cli.Command {
func NewApp(l **slog.Logger) *cli.Command {
root := NewCmdBring()
flags := []cli.Flag{
&cli.BoolFlag{
Expand Down Expand Up @@ -69,17 +69,31 @@ Example:
},
Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
conf_path := "things.yaml"
if v := cmd.String("conf"); v != "" {
conf_path = v
conf_path_opt := cmd.HasName("conf")
if conf_path_opt {
conf_path = cmd.String("conf")
}

conf, err := config.LoadFromFilepath(conf_path)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf("load config: %w", err)
conf = config.New()
if !errors.Is(err, os.ErrNotExist) || conf_path_opt {
// There is an error
// - config on default path
// - config on explicitly given path
err := fmt.Errorf("load config: %w", err)
*l = conf.Log.Logger()
return nil, err
}

conf = config.New()
// There is no config on default path, so use default config.
defer func() {
(*l).Info("use default config")
}()
} else {
defer func() {
(*l).Info("load config from the file", slog.String("path", conf_path))
}()
}
if cmd.Bool("verbose") {
conf.Log.Level = "info"
Expand All @@ -91,30 +105,12 @@ Example:
conf.Log.Format = v
}

l := conf.Log.Logger()
if err != nil {
l.Info("use default config")
} else {
l.Info("load config from the file", slog.String("path", conf_path))
}
*l = conf.Log.Logger()

ctx = config.Into(ctx, conf)
ctx = log.Into(ctx, l)
ctx = log.Into(ctx, *l)
return ctx, nil
},
Action: root.Action,

ExitErrHandler: func(ctx context.Context, cmd *cli.Command, err error) {
l := log.From(ctx)

var e cli.ExitCoder
if errors.As(err, &e) {
l.Error(e.Error())
os.Exit(e.ExitCode())
} else if err != nil {
l.Error(err.Error())
os.Exit(1)
}
},
}
}
24 changes: 22 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,32 @@ package main

import (
"context"
"errors"
"log/slog"
"os"

"github.com/lesomnus/bring/cmd"
"github.com/urfave/cli/v3"
)

func main() {
app := cmd.NewApp()
app.Run(context.Background(), os.Args)
var l *slog.Logger
handleError := func(err error) {
l.Error(err.Error())

var e cli.ExitCoder
if errors.As(err, &e) {
os.Exit(e.ExitCode())
} else {
os.Exit(1)
}
}

app := cmd.NewApp(&l)
app.ExitErrHandler = func(ctx context.Context, cmd *cli.Command, err error) {
handleError(err)
}
if err := app.Run(context.Background(), os.Args); err != nil {
handleError(err)
}
}

0 comments on commit fe8d974

Please sign in to comment.