99 "path/filepath"
1010
1111 tea "github.com/charmbracelet/bubbletea"
12+ "github.com/pimg/certguard/config"
1213 "github.com/pimg/certguard/internal/adapter/db"
1314 "github.com/pimg/certguard/internal/ports/models"
1415 cmds "github.com/pimg/certguard/internal/ports/models/commands"
@@ -17,31 +18,40 @@ import (
1718 "github.com/spf13/cobra"
1819)
1920
21+ var v * config.ViperConfig
22+
2023func init () {
21- rootCmd .PersistentFlags ().Bool ("debug" , false , "enables debug logging to a file located in ~/.local/certguard/debug.log" )
22- rootCmd .PersistentFlags ().String ("theme" , "dracula" , "set the theme of the application. Allowed values: 'dracula', 'gruvbox'" )
24+ v = config .NewViperConfig ()
25+ rootCmd .PersistentFlags ().BoolVarP (& v .Config ().Log .Debug , "debug" , "d" , false , "enables debug logging to a file located in ~/.local/certguard/debug.log" )
26+ rootCmd .PersistentFlags ().StringVarP (& v .Config ().Theme .Name , "theme" , "t" , "dracula" , "set the theme of the application. Allowed values: 'dracula', 'gruvbox'" )
27+
28+ // bind Cobra flags to viper config
29+ _ = v .BindPFlag ("config.theme.name" , rootCmd .PersistentFlags ().Lookup ("theme" ))
30+ _ = v .BindPFlag ("config.log.debug" , rootCmd .PersistentFlags ().Lookup ("debug" ))
31+
2332}
2433
2534var rootCmd = & cobra.Command {
2635 Version : "v0.0.1" ,
2736 Use : "certguard" ,
2837 Long : "Certguard can download, store and inspect x.509 Certificate Revocation Lists" ,
2938 Example : "certguard" ,
30- RunE : runInteractiveCertGuard ,
39+ PersistentPreRunE : func (cmd * cobra.Command , args []string ) error {
40+ return v .InitializeConfig ()
41+ },
42+ RunE : runInteractiveCertGuard ,
3143}
3244
3345func runInteractiveCertGuard (cmd * cobra.Command , args []string ) error {
34- debug , _ := cmd . Flags ().GetBool ( "debug" )
35- theme , _ := cmd . Flags ().GetString ( "theme" )
46+ debug := v . Config ().Log . Debug
47+ theme := v . Config ().Theme . Name
3648
3749 if debug {
38- homeDir , err := os . UserHomeDir ()
50+ logDir , err := logDir ()
3951 if err != nil {
40- fmt .Println ("fatal:" , err )
41- os .Exit (1 )
52+ return err
4253 }
4354
44- logDir := filepath .Join (homeDir , ".local" , "share" , "certguard" )
4555 err = os .MkdirAll (logDir , 0o777 )
4656 if err != nil {
4757 return err
@@ -55,7 +65,12 @@ func runInteractiveCertGuard(cmd *cobra.Command, args []string) error {
5565 defer f .Close ()
5666 }
5767
58- cacheDir , err := determineCacheDir ()
68+ cacheDir , err := cacheDir ()
69+ if err != nil {
70+ return err
71+ }
72+
73+ importDir , err := importDir ()
5974 if err != nil {
6075 return err
6176 }
@@ -86,7 +101,7 @@ func runInteractiveCertGuard(cmd *cobra.Command, args []string) error {
86101 return err
87102 }
88103
89- storage , err := crl .NewStorage (libsqlStorage , cacheDir )
104+ storage , err := crl .NewStorage (libsqlStorage , cacheDir , importDir )
90105 if err != nil {
91106 return err
92107 }
@@ -107,7 +122,41 @@ func Execute() error {
107122 return rootCmd .Execute ()
108123}
109124
110- func determineCacheDir () (string , error ) {
125+ func cacheDir () (string , error ) {
126+ if v .Config ().CacheDirectory != "" {
127+ return v .Config ().CacheDirectory , nil
128+ }
129+
130+ return defaultDir ()
131+ }
132+
133+ func importDir () (string , error ) {
134+ if v .Config ().ImportDirectory != "" {
135+ return v .Config ().ImportDirectory , nil
136+ }
137+
138+ dir , err := defaultDir ()
139+ if err != nil {
140+ return "" , err
141+ }
142+
143+ return filepath .Join (dir , "import" ), nil
144+ }
145+
146+ func logDir () (string , error ) {
147+ if v .Config ().Log .Directory != "" {
148+ return v .Config ().Log .Directory , nil
149+ }
150+
151+ homeDir , err := os .UserHomeDir ()
152+ if err != nil {
153+ return "" , errors .New ("could not create file path to User home dir, logging will not be enabled" )
154+ }
155+
156+ return filepath .Join (homeDir , ".local" , "share" , "certguard" ), nil
157+ }
158+
159+ func defaultDir () (string , error ) {
111160 homeDir , err := os .UserHomeDir ()
112161 if err != nil {
113162 return "" , errors .New ("could not create file path to User home dir, Cache will not be enabled" )
0 commit comments