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,39 @@ 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" ))
2331}
2432
2533var rootCmd = & cobra.Command {
2634 Version : "v0.0.1" ,
2735 Use : "certguard" ,
2836 Long : "Certguard can download, store and inspect x.509 Certificate Revocation Lists" ,
2937 Example : "certguard" ,
30- RunE : runInteractiveCertGuard ,
38+ PersistentPreRunE : func (cmd * cobra.Command , args []string ) error {
39+ return v .InitializeConfig ()
40+ },
41+ RunE : runInteractiveCertGuard ,
3142}
3243
3344func runInteractiveCertGuard (cmd * cobra.Command , args []string ) error {
34- debug , _ := cmd . Flags ().GetBool ( "debug" )
35- theme , _ := cmd . Flags ().GetString ( "theme" )
45+ debug := v . Config ().Log . Debug
46+ theme := v . Config ().Theme . Name
3647
3748 if debug {
38- homeDir , err := os . UserHomeDir ()
49+ logDir , err := logDir ()
3950 if err != nil {
40- fmt .Println ("fatal:" , err )
41- os .Exit (1 )
51+ return err
4252 }
4353
44- logDir := filepath .Join (homeDir , ".local" , "share" , "certguard" )
4554 err = os .MkdirAll (logDir , 0o777 )
4655 if err != nil {
4756 return err
@@ -55,7 +64,12 @@ func runInteractiveCertGuard(cmd *cobra.Command, args []string) error {
5564 defer f .Close ()
5665 }
5766
58- cacheDir , err := determineCacheDir ()
67+ cacheDir , err := cacheDir ()
68+ if err != nil {
69+ return err
70+ }
71+
72+ importDir , err := importDir ()
5973 if err != nil {
6074 return err
6175 }
@@ -86,7 +100,7 @@ func runInteractiveCertGuard(cmd *cobra.Command, args []string) error {
86100 return err
87101 }
88102
89- storage , err := crl .NewStorage (libsqlStorage , cacheDir )
103+ storage , err := crl .NewStorage (libsqlStorage , cacheDir , importDir )
90104 if err != nil {
91105 return err
92106 }
@@ -107,7 +121,41 @@ func Execute() error {
107121 return rootCmd .Execute ()
108122}
109123
110- func determineCacheDir () (string , error ) {
124+ func cacheDir () (string , error ) {
125+ if v .Config ().CacheDirectory != "" {
126+ return v .Config ().CacheDirectory , nil
127+ }
128+
129+ return defaultDir ()
130+ }
131+
132+ func importDir () (string , error ) {
133+ if v .Config ().ImportDirectory != "" {
134+ return v .Config ().ImportDirectory , nil
135+ }
136+
137+ dir , err := defaultDir ()
138+ if err != nil {
139+ return "" , err
140+ }
141+
142+ return filepath .Join (dir , "import" ), nil
143+ }
144+
145+ func logDir () (string , error ) {
146+ if v .Config ().Log .Directory != "" {
147+ return v .Config ().Log .Directory , nil
148+ }
149+
150+ homeDir , err := os .UserHomeDir ()
151+ if err != nil {
152+ return "" , errors .New ("could not create file path to User home dir, logging will not be enabled" )
153+ }
154+
155+ return filepath .Join (homeDir , ".local" , "share" , "certguard" ), nil
156+ }
157+
158+ func defaultDir () (string , error ) {
111159 homeDir , err := os .UserHomeDir ()
112160 if err != nil {
113161 return "" , errors .New ("could not create file path to User home dir, Cache will not be enabled" )
0 commit comments