-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
76 lines (60 loc) · 1.8 KB
/
Copy pathmain.go
File metadata and controls
76 lines (60 loc) · 1.8 KB
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
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/rtfpessoa/clitr/internal/log"
"github.com/rtfpessoa/clitr/internal/utils"
"github.com/spf13/cobra"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const (
verboseFlagKeyShort = "v"
verboseFlagKeyLong = "verbose"
dataDirFlagKeyShort = "d"
dataDirFlagKeyLong = "data-dir"
defaultDataDir = "~/.local/share/clitr"
)
type rootConfig struct {
dataDir string
eventsDir string
verbose bool
}
func main() {
config := &rootConfig{}
defer log.Sync()
rootCmd := &cobra.Command{
Use: "clitr",
Short: "Trade Republic transaction exporter",
Long: "A CLI tool to fetch and export transaction history from Trade Republic",
}
rootCmd.PersistentFlags().BoolVarP(&config.verbose, verboseFlagKeyLong, verboseFlagKeyShort, false, "Enable verbose debug output")
rootCmd.PersistentFlags().StringVarP(&config.dataDir, dataDirFlagKeyLong, dataDirFlagKeyShort, defaultDataDir, fmt.Sprintf("Directory to save data (default: %s)", defaultDataDir))
// This runs before any subcommands
rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
if config.verbose {
log.SetLevel(zapcore.DebugLevel)
}
if config.dataDir == "" {
return fmt.Errorf("--%s cannot be empty, to use the default value do not pass the flag", dataDirFlagKeyLong)
}
resolvedDataDir, err := utils.ResolvePath(config.dataDir)
if err != nil {
return fmt.Errorf("failed to resolve %s: %w", dataDirFlagKeyLong, err)
}
config.dataDir = resolvedDataDir
config.eventsDir = filepath.Join(config.dataDir, "events")
return nil
}
rootCmd.AddCommand(
NewFetchCmd(config),
NewExportCmd(config),
NewPatchCmd(config),
NewServeCmd(config),
)
if err := rootCmd.Execute(); err != nil {
log.Error("Command failed", zap.Error(err))
os.Exit(1)
}
}