-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
90 lines (82 loc) · 2.27 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"bytes"
"errors"
"fmt"
"os"
git "github.com/go-git/go-git/v5"
cli "github.com/urfave/cli/v2"
)
func main() {
app := &cli.App{
Name: "wise",
Description: "Smarter version control than git",
Authors: []*cli.Author{
{
Name: "Charles Julian Knight",
Email: "[email protected]",
},
},
Commands: []*cli.Command{
{
Name: "status",
Aliases: []string{"s"},
Action: withRepo(status),
Description: "Check the current status of the working tree",
},
},
// Action: withRepo(status),
}
err := app.Run(os.Args)
if err == nil {
return
}
if _, ok := err.(FriendlyError); ok {
fmt.Println("It's unlikely this is an error with Wise.")
} else {
// TODO: improve experience
fmt.Println("It's likely this is an error with Wise itself. Consider reporting it by opening an issue.")
panic(err)
}
}
func withRepo(handler func(ctx *cli.Context, repo *git.Repository, wt *git.Worktree) error) cli.ActionFunc {
return func(ctx *cli.Context) error {
dir, err := os.Getwd()
if err != nil {
return fmt.Errorf("unable to determine current directory: %w", err)
}
repo, err := git.PlainOpenWithOptions(dir, &git.PlainOpenOptions{
DetectDotGit: true,
})
if err != nil {
if errors.Is(err, git.ErrRepositoryNotExists) {
return notARepositoryError(dir)
}
return fmt.Errorf("get repo: %w", err)
}
wt, err := repo.Worktree()
if err != nil {
// if errors.Is(err, git.ErrIsBareRepository) {
// // TODO: handle this
// }
return fmt.Errorf("get worktree: %w", err)
}
return handler(ctx, repo, wt)
}
}
func notARepositoryError(cwd string) error {
// TODO: improve directions on how to resolve this issue
buf := bytes.NewBuffer(nil)
fmt.Fprintf(buf, "The current directory `%s` does not appear to be in a git repository.\n\n", cwd)
fmt.Fprint(buf, "It could be that you aren't in the directory of your project, or it could be that you want to create a new repository.")
return NewFriendlyError(buf.String())
}
func status(ctx *cli.Context, repo *git.Repository, wt *git.Worktree) error {
status, err := wt.Status()
if err != nil {
return fmt.Errorf("get status: %w", err)
}
// TODO: print meaningful information about the current state
fmt.Println(status.String())
return nil
}