|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "fmt" |
| 4 | + "context" |
5 | 5 | "os" |
6 | | - "runtime/debug" |
| 6 | + "os/signal" |
| 7 | + "syscall" |
7 | 8 |
|
| 9 | + "github.com/amberpixels/git-undo/cmd/shared" |
8 | 10 | "github.com/amberpixels/git-undo/internal/app" |
| 11 | + "github.com/urfave/cli/v3" |
9 | 12 | ) |
10 | 13 |
|
11 | 14 | // version is set by the build ldflags |
12 | 15 | // The default value is "dev+dirty" but it should never be used. In success path, it's always overwritten. |
13 | 16 | var version = "dev+dirty" |
| 17 | +var versionSource = "hardcoded" |
14 | 18 |
|
15 | | -func main() { |
16 | | - var verbose, dryRun bool |
17 | | - for _, arg := range os.Args[1:] { |
18 | | - if arg == "-v" || arg == "--verbose" { |
19 | | - verbose = true |
20 | | - } |
21 | | - if arg == "--dry-run" { |
22 | | - dryRun = true |
23 | | - } |
24 | | - } |
| 19 | +const ( |
| 20 | + appNameGitUndo = "git-undo" |
| 21 | +) |
25 | 22 |
|
26 | | - // When running binary that was installed via `go install`, here we'll get the proper version |
27 | | - if bi, ok := debug.ReadBuildInfo(); ok && bi.Main.Version != "" { |
28 | | - version = bi.Main.Version |
| 23 | +func main() { |
| 24 | + // Create a context that can be cancelled with Ctrl+C |
| 25 | + ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) |
| 26 | + defer cancel() |
| 27 | + |
| 28 | + version, versionSource = app.HandleAppVersion(version, versionSource) |
| 29 | + |
| 30 | + cmd := &cli.Command{ |
| 31 | + Name: appNameGitUndo, |
| 32 | + Usage: "Universal \"Ctrl + Z\" for Git commands", |
| 33 | + DisableSliceFlagSeparator: true, |
| 34 | + HideHelp: true, |
| 35 | + Flags: shared.CommonFlags(), |
| 36 | + Action: func(ctx context.Context, c *cli.Command) error { |
| 37 | + application := app.NewAppGitUndo(version, versionSource) |
| 38 | + if c.Bool("version") { |
| 39 | + return application.HandleVersion(c.Bool("verbose")) |
| 40 | + } |
| 41 | + |
| 42 | + // Use the new structured approach with parsed options |
| 43 | + opts := app.RunOptions{ |
| 44 | + Verbose: c.Bool("verbose"), |
| 45 | + DryRun: c.Bool("dry-run"), |
| 46 | + HookCommand: c.String("hook"), |
| 47 | + ShowLog: c.Bool("log"), |
| 48 | + Args: c.Args().Slice(), |
| 49 | + } |
| 50 | + |
| 51 | + return application.Run(ctx, opts) |
| 52 | + }, |
29 | 53 | } |
30 | | - application := app.NewAppGitUndo(version, verbose, dryRun) |
31 | 54 |
|
32 | | - if err := application.Run(os.Args[1:]); err != nil { |
33 | | - _, _ = fmt.Fprintln(os.Stderr, redColor+appNameGitUndo+" ❌: "+grayColor+err.Error()+resetColor) |
34 | | - os.Exit(1) |
| 55 | + if err := cmd.Run(ctx, os.Args); err != nil { |
| 56 | + app.HandleError(appNameGitUndo, err) |
35 | 57 | } |
36 | 58 | } |
37 | | - |
38 | | -const ( |
39 | | - grayColor = "\033[90m" |
40 | | - redColor = "\033[31m" |
41 | | - resetColor = "\033[0m" |
42 | | - |
43 | | - appNameGitUndo = "git-undo" |
44 | | -) |
|
0 commit comments