-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
172 lines (156 loc) · 5.04 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
package main
import (
"context"
"flag"
"fmt"
"log/slog"
"os"
"strconv"
"strings"
"time"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/aurelian-one/au/cmd/au/commentcmd"
"github.com/aurelian-one/au/cmd/au/common"
"github.com/aurelian-one/au/cmd/au/devcmd"
"github.com/aurelian-one/au/cmd/au/todocmd"
"github.com/aurelian-one/au/cmd/au/workspacecmd"
"github.com/aurelian-one/au/pkg/au"
)
var rootCmd = &cobra.Command{
Use: "au",
Short: "au is the core CLI for interacting with aurelian task management",
Long: strings.TrimSpace(`
A core CLI for interacting with aurelian task management workspaces manually in single-operation mode.
This CLI can be used for simple human tasks or be called by other processes in order to accomplish machine-driven goals 🤖. Development and debug commands are also provided.
Find more information at: https://github.com/aurelian-one/au`),
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if err := setupLogger(cmd); err != nil {
return err
}
if err := resolveConfigDirectoryAndWorkspace(cmd, "directory", "current-workspace", "author"); err != nil {
return err
}
return nil
},
SilenceErrors: true,
SilenceUsage: true,
}
var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version information of the au binary",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
intTimestamp, _ := strconv.Atoi(au.CommitTimestamp)
_, _ = fmt.Fprintf(os.Stdout, "%s (%s @ %s)\n", au.Version, au.Commit, time.Unix(int64(intTimestamp), 0))
},
}
func setupLogger(cmd *cobra.Command) error {
handlerOptions := &slog.HandlerOptions{AddSource: false, Level: slog.LevelError}
if debugValue, err := cmd.Flags().GetInt("debug"); err != nil {
return err
} else if debugValue == 1 {
handlerOptions.Level = slog.LevelDebug
} else if debugValue == 2 {
handlerOptions.AddSource = true
handlerOptions.Level = slog.LevelDebug
} else if debugValue != 0 {
return fmt.Errorf("debug value must be 0, 1, or 2")
}
slogHandler := slog.NewTextHandler(cmd.ErrOrStderr(), handlerOptions)
slogLogger := slog.New(slogHandler)
slog.SetDefault(slogLogger)
if slog.Default().Enabled(context.Background(), slog.LevelDebug) {
slog.Debug("debug logging is enabled")
}
return nil
}
func resolveConfigDirectoryAndWorkspace(cmd *cobra.Command, directoryFlag string, workspaceFlag string, authorFlag string) error {
directoryValue, err := cmd.Flags().GetString(directoryFlag)
if err != nil {
return err
}
directoryValue, err = au.ResolveConfigDirectory(directoryValue)
if err != nil {
return err
}
directoryStorage, err := au.NewDirectoryStorage(directoryValue)
if err != nil {
return err
}
workspaceValue, err := cmd.Flags().GetString(workspaceFlag)
if err != nil {
return err
}
workspaceValue, err = au.ResolveWorkspaceUid(workspaceValue)
if err != nil {
return err
}
if workspaceValue == "" {
if r, err := directoryStorage.GetCurrentWorkspace(cmd.Context()); err != nil {
return err
} else {
workspaceValue = r
}
}
authorValue, _ := cmd.Flags().GetString(authorFlag)
currentAuthor, err := au.ResolveAuthor(authorValue)
if err != nil {
return err
}
cmd.SetContext(context.WithValue(cmd.Context(), common.StorageContextKey, directoryStorage))
cmd.SetContext(context.WithValue(cmd.Context(), common.CurrentWorkspaceIdContextKey, workspaceValue))
cmd.SetContext(context.WithValue(cmd.Context(), common.CurrentAuthorContextKey, currentAuthor))
return nil
}
func init() {
rootCmd.PersistentFlags().IntP(
"debug", "d", 0,
"Enable debug logging by setting this value > 0. Valid values are 0, 1, or 2.",
)
rootCmd.PersistentFlags().String(
"directory", "",
strings.TrimSpace(fmt.Sprintf(`
The path of the config directory to operate in. If no value is provided, this will fallback to $%s before falling back to %s".`,
au.ConfigDirEnvironmentVariable, au.DefaultConfigDir,
)),
)
rootCmd.PersistentFlags().String(
"current-workspace", "",
strings.TrimSpace(fmt.Sprintf(`
The id of the target workspace to operate in. If no value is provided, this will fallback to $%s before falling back to 'current' file".`,
au.WorkspaceUidEnvironmentVariable,
)),
)
rootCmd.PersistentFlags().String(
"current-author", "",
strings.TrimSpace(fmt.Sprintf(`
The 'Name <email>' of the author for any Todo or Comment changes. If no value is provided, this will fallback to $%s before falling back to 'author' file".`,
au.AuthorEnvironmentVariable,
)),
)
rootCmd.AddGroup(&cobra.Group{Title: "Core", ID: "core"})
rootCmd.AddCommand(
workspacecmd.Command,
todocmd.Command,
commentcmd.Command,
devcmd.Command,
versionCmd,
)
}
func main() {
if err := mainInner(); err != nil && !errors.Is(err, flag.ErrHelp) {
if v, _ := rootCmd.Flags().GetInt("debug"); v >= 2 {
_, _ = fmt.Fprintf(rootCmd.ErrOrStderr(), "%+v\n", err)
}
if ee := new(common.ExitWithCode); errors.As(err, &ee) {
os.Exit(ee.Code)
} else {
_, _ = fmt.Fprintf(rootCmd.ErrOrStderr(), "Error: %v\n", err)
os.Exit(1)
}
}
}
func mainInner() error {
return rootCmd.Execute()
}