/
root.go
127 lines (106 loc) · 2.83 KB
/
root.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package cmd
import (
"context"
"errors"
"fmt"
"strings"
"github.com/google/go-github/github"
"github.com/shuntaka9576/kanban/api"
"github.com/shuntaka9576/kanban/internal/kanban/config"
"github.com/shuntaka9576/kanban/internal/kanban/ui"
"github.com/shuntaka9576/kanban/pkg/git"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"golang.org/x/oauth2"
)
var RootCmd = &cobra.Command{
Use: "kanban",
Short: "GitHub Project TUI",
Long: "GitHub Project Viewer",
SilenceErrors: true,
SilenceUsage: true,
RunE: runRootCmd,
}
type FlagError struct {
Err error
}
func (fe FlagError) Error() string {
return fe.Err.Error()
}
func (fe FlagError) Unwrap() error {
return fe.Err
}
var Version = "DEV"
var BuildDate = ""
var versionOutput = ""
var versionCmd = &cobra.Command{
Use: "version",
Hidden: true,
Run: func(cmd *cobra.Command, args []string) {
fmt.Print(versionOutput)
},
}
func init() {
RootCmd.AddCommand(versionCmd)
RootCmd.PersistentFlags().StringP("repo", "R", "", "Select another repository using the `OWNER/REPO` format")
RootCmd.PersistentFlags().Bool("help", false, "Show help for command")
RootCmd.Flags().StringP("search", "S", "", "Search project name string(default first project)")
if BuildDate == "" {
RootCmd.Version = Version
} else {
RootCmd.Version = fmt.Sprintf("%s (%s)", Version, BuildDate)
}
versionOutput = fmt.Sprintf("kanban version %s\n", RootCmd.Version)
RootCmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error {
if err == pflag.ErrHelp {
return err
}
return &FlagError{Err: err}
})
}
func runRootCmd(cmd *cobra.Command, args []string) error {
config := config.New()
token, err := config.AuthToken()
if err != nil {
return err
}
baseRepository, err := DetermineBaseRepository(cmd)
if err != nil {
return err
}
searchString, err := cmd.Flags().GetString("search")
if err != nil {
return err
}
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
tui := ui.NewTui(&ui.GhpjSettings{
Client: api.NewGitHubClient(token),
Repository: baseRepository,
SearchString: searchString,
V3Client: client,
})
tui.Run(ctx)
return nil
}
func DetermineBaseRepository(cmd *cobra.Command) (git.Repository, error) {
ownerRepoString, err := cmd.Flags().GetString("repo")
var repo git.Repository
if err != nil || ownerRepoString == "" {
repo, err = git.BaseRepoFromRemote()
if err != nil {
return nil, err
}
} else {
ownerRepoList := strings.Split(ownerRepoString, "/")
if len(ownerRepoList) != 2 {
return nil, FlagError{errors.New("invalid --repo value: " + ownerRepoString + "\nPlease set OWNER/REPO format")}
}
repo, err = git.Repo(ownerRepoList[0], ownerRepoList[1])
}
return repo, err
}