-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
204 lines (169 loc) · 4.83 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package main
import (
"errors"
"fmt"
"io"
"log/slog"
"os"
"os/user"
"path/filepath"
"strings"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/log"
"github.com/prgrs/clickup/api"
"github.com/prgrs/clickup/internal/config"
"github.com/prgrs/clickup/pkg/cache"
"github.com/prgrs/clickup/ui"
"github.com/prgrs/clickup/ui/context"
"github.com/spf13/pflag"
"golang.design/x/clipboard"
)
const (
// Version is the current version
AppVersion = "0.0.1-pre-alpha"
// Name is the name of the application
AppName = "clickup-tui"
// Description is the description of the application
AppDescription = "A terminal user interface for ClickUp"
// DefaultCachePath is the default cache path
DefaultCachePath = "./cache"
)
var (
flag *pflag.FlagSet = pflag.NewFlagSet(AppName, pflag.ContinueOnError)
flagDebug *bool = flag.Bool("debug", false, "Enable debug mode")
flagDebugDeep *bool = flag.Bool("debug-deep", false, "Enable deep debug mode")
flagHelp *bool = flag.BoolP("help", "h", false, "Show help")
flagVersion *bool = flag.BoolP("version", "v", false, "Show version")
flagConfig *string = flag.StringP("config", "c", "", "A config filename")
flagCleanCache *bool = flag.Bool("clean-cache", false, "Cleans cache data")
flagCleanCacheOnly *bool = flag.Bool("clean-cache-only", false, "Cleans cache data and exits")
flagCachePath *string = flag.String("cache-path", DefaultCachePath, "The path to the cache directory")
flagUsage func() string = func() string {
s := strings.Builder{}
s.WriteString(fmt.Sprintf("%s - %s\n", AppName, AppDescription))
s.WriteString("Usage:\n")
s.WriteString(fmt.Sprintf(" %s [flags]\n", AppName))
s.WriteString("Flags:\n")
s.WriteString(flag.FlagUsages())
return s.String()
}
termLogger *log.Logger = log.NewWithOptions(os.Stderr, log.Options{
ReportCaller: false,
Level: log.InfoLevel,
})
)
func main() {
if err := flag.Parse(os.Args[1:]); err != nil {
fmt.Println(flagUsage())
os.Exit(2)
}
if *flagHelp {
fmt.Println(flagUsage())
return
}
if *flagVersion {
fmt.Printf("%s %s\n", AppName, AppVersion)
return
}
logger := log.NewWithOptions(os.Stderr, log.Options{
ReportCaller: *flagDebugDeep,
Level: func() log.Level {
lvl := log.InfoLevel
if *flagDebug {
lvl = log.DebugLevel
}
return lvl
}(),
ReportTimestamp: true,
})
f, err := os.OpenFile("debug.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
if err != nil {
logger.Fatal(err)
}
defer f.Close()
logger.SetOutput(f)
termLogger.SetOutput(io.MultiWriter(os.Stderr, f))
logger.Info("Starting up...")
logger.Info("Initializing config...")
cfg, err := initConfig(*flagConfig)
if err != nil {
termLogger.Fatal(err)
}
logger.Info("Initializing cache...")
cache := cache.NewCache(
slog.New(logger.WithPrefix("Cache")),
*flagCachePath,
)
defer func() {
if err := cache.Close(); err != nil {
termLogger.Fatal(err)
}
}()
if *flagCleanCache || *flagCleanCacheOnly {
logger.Info("Cleaning cache...")
if err := cache.Invalidate(); err != nil {
termLogger.Fatal(err)
}
if *flagCleanCacheOnly {
return
}
}
logger.Info("Loading cache...")
if err := cache.Load(); err != nil {
termLogger.Fatal(err)
}
logger.Info("Initializing clipboard...")
if err := clipboard.Init(); err != nil {
termLogger.Fatal(err)
}
logger.Info("Initializing api...")
api := api.NewApi(logger, cache, cfg.Token)
defer api.Close()
logger.Info("Initializing user context...")
ctx := context.NewUserContext(logger, api, cfg)
logger.Info("Initializing main model...")
mainModel := ui.InitialModel(&ctx, logger)
logger.Info("Initializing program...")
p := tea.NewProgram(mainModel, tea.WithAltScreen())
if _, err := p.Run(); err != nil {
termLogger.Fatal(err)
}
}
func initConfig(path string) (*config.Config, error) {
if path == "" {
usr, err := user.Current()
if err != nil {
return nil, err
}
defaultConfigPath := filepath.Join(usr.HomeDir, config.DefaultPathPrefix)
paths := []string{
".",
"/etc/clickup-tui",
"/home/user/clickup-tui",
defaultConfigPath,
}
filename := config.DefaultFilename
configPath, err := config.FindConfigFile(filename, paths)
if err != nil {
fmt.Println("Config file not found, creating a new...")
configPath = defaultConfigPath
if err = config.CreateEmptyCfgFile(filename, configPath); err != nil {
return nil, err
}
configPath = filepath.Join(configPath, filename)
}
path = configPath
fmt.Println("Loading config file from:", path)
}
cfg, err := config.Init(path)
if err != nil {
if errors.Is(err, config.ErrMissingToken) {
fmt.Println(config.HowToGetToken)
if err := cfg.Save(); err != nil {
return nil, err
}
}
return nil, err
}
return cfg, nil
}