Skip to content

Commit

Permalink
wip: debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoisaiah committed May 4, 2024
1 parent 194effd commit 6d15419
Show file tree
Hide file tree
Showing 10 changed files with 528 additions and 136 deletions.
73 changes: 62 additions & 11 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package app
import (
"fmt"
"io"
"log/slog"
"net/http"
"os"
"strings"
Expand Down Expand Up @@ -69,6 +70,11 @@ func getDefaultOptsCtx() *cli.Context {
var defaultCtx *cli.Context

if optsEnv, exists := os.LookupEnv(EnvDefaultOpts); exists {
slog.Debug(
"found default options in environment",
slog.String("default_opts", optsEnv),
)

defaultOpts := make([]string, len(os.Args))

copy(defaultOpts, os.Args)
Expand All @@ -85,14 +91,24 @@ func getDefaultOptsCtx() *cli.Context {
}

// Run needs to be called here so that `defaultCtx` is populated
// It errors out when // TODO: complete this
// The only expected error is if the provided flags or arguments
// are incorrect
err := app.Run(defaultOpts)
if err != nil {
// TODO: Decide what to do here
slog.Debug("default options parse error",
slog.String("error", fmt.Sprintf("%v", err)),
)

pterm.Fprintln(
os.Stderr,
pterm.Error.Sprintf("error parsing default optons: %v", err),
pterm.Error.Sprintf(
"error parsing %s: %v",
EnvDefaultOpts,
err,
),
)

os.Exit(1)
}
}

Expand Down Expand Up @@ -155,15 +171,18 @@ func Get(reader io.Reader, writer io.Writer) *cli.App {

app.Before = func(ctx *cli.Context) error {
if ctx.Bool("no-color") {
slog.Debug("disabling styling")
pterm.DisableStyling()
}

if ctx.Bool("quiet") {
slog.Debug("disabling output")
pterm.DisableOutput()
}

// print short help and exit if no arguments or flags are present
if ctx.NumFlags() == 0 && !ctx.Args().Present() {
slog.Debug("print short help and exit")
pterm.Println(ShortHelp(ctx.App))
os.Exit(1)
}
Expand All @@ -172,6 +191,10 @@ func Get(reader io.Reader, writer io.Writer) *cli.App {
app.Metadata["writer"] = writer

if ctx.NumFlags() == 0 {
slog.Debug(
"simple mode detected",
slog.Int("num_flags", ctx.NumFlags()),
)
app.Metadata["simple-mode"] = true
}

Expand All @@ -181,21 +204,45 @@ func Get(reader io.Reader, writer io.Writer) *cli.App {
return nil
}

// TODO: simplify
for _, opt := range supportedDefaultOptions {
value := fmt.Sprintf("%v", defaultCtx.Value(opt))
for _, defaultOpt := range supportedDefaultOptions {
defaultValue := fmt.Sprintf("%v", defaultCtx.Value(defaultOpt))

if ctx.IsSet(defaultOpt) && defaultCtx.IsSet(defaultOpt) {
cliValue := fmt.Sprintf("%v", ctx.Value(defaultOpt))
slog.Debug(
fmt.Sprintf(
"command line flag overrides default option for: %s",
defaultOpt,
),
slog.String("flag", defaultOpt),
slog.String("command_line_value", cliValue),
slog.String("default_value", defaultValue),
)

continue
}

if !ctx.IsSet(opt) && defaultCtx.IsSet(opt) {
if x, ok := defaultCtx.Value(opt).(cli.StringSlice); ok {
value = strings.Join(x.Value(), "|")
if !ctx.IsSet(defaultOpt) && defaultCtx.IsSet(defaultOpt) {
if x, ok := defaultCtx.Value(defaultOpt).(cli.StringSlice); ok {
defaultValue = strings.Join(x.Value(), "|")
}

err := ctx.Set(opt, value)
slog.Debug(
fmt.Sprintf("set default option for flag: %s", defaultOpt),
slog.String("flag", defaultOpt),
slog.String("default_value", defaultValue),
)

err := ctx.Set(defaultOpt, defaultValue)
if err != nil {
slog.Debug("failed to set default option for: %s",
slog.String("flag", defaultOpt),
slog.String("default_value", defaultValue),
)
pterm.Fprintln(os.Stderr,
pterm.Warning.Sprintf(
"Unable to set default option for: %s",
opt,
defaultOpt,
),
)
}
Expand Down Expand Up @@ -231,6 +278,10 @@ or: FIND [REPLACE] [PATHS TO FILES AND DIRECTORIES...]`
DefaultText: "<path/to/csv/file>",
TakesFile: true,
},
&cli.BoolFlag{
Name: "debug",
Usage: "Enable debug mode",
},
&cli.StringFlag{
Name: "exiftool-opts",
Usage: "Provide custom options when using ExifTool variables",
Expand Down
19 changes: 18 additions & 1 deletion cmd/f2/main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
package main

import (
"log/slog"
"os"

"github.com/pterm/pterm"

"github.com/ayoisaiah/f2"

slogctx "github.com/veqryn/slog-context"
)

func initLogger() {
opts := &slog.HandlerOptions{
Level: slog.LevelInfo,
}

h := slogctx.NewHandler(slog.NewJSONHandler(os.Stderr, opts), nil)

l := slog.New(h)

slog.SetDefault(l)
}

func main() {
app := f2.GetApp(os.Stdin, os.Stdout)
initLogger()

app := f2.New(os.Stdin, os.Stdout)

err := app.Run(os.Args)
if err != nil {
Expand Down
26 changes: 17 additions & 9 deletions f2.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package f2

import (
"errors"
"fmt"
"io"
"log/slog"

"github.com/urfave/cli/v2"

Expand All @@ -21,11 +23,15 @@ var errConflictDetected = errors.New(

// run starts a new renaming operation.
func run(ctx *cli.Context) error {
// TODO: Log the final context

conf, err := config.Init(ctx)
if err != nil {
return err
}

slog.Debug("configuration", slog.Any("config", conf))

report.Stdout = conf.Stdout
report.Stderr = conf.Stderr

Expand All @@ -39,15 +45,25 @@ func run(ctx *cli.Context) error {
}

if len(matches) == 0 {
slog.Debug("no matches were found", slog.Any("find_matches", matches))
report.NoMatches(conf.JSON)

return nil
}

slog.Debug(
fmt.Sprintf("found %d matches", len(matches)),
slog.Any("find_matches", matches),
slog.Int("num_matches", len(matches)),
)

changes, err := replace.Replace(conf, matches)
if err != nil {
return err
}

slog.Debug("replacements done", slog.Any("changes", changes))

conflicts := validate.Validate(
changes,
conf.AutoFixConflicts,
Expand All @@ -63,17 +79,9 @@ func run(ctx *cli.Context) error {
return rename.Rename(conf, changes)
}

func GetApp(reader io.Reader, writer io.Writer) *cli.App {
func New(reader io.Reader, writer io.Writer) *cli.App {
f2App := app.Get(reader, writer)
f2App.Action = run

return f2App
}

// NewApp creates a new app instance.
func NewApp() *cli.App {
f2App := app.New()
f2App.Action = run

return f2App
}
Loading

0 comments on commit 6d15419

Please sign in to comment.