From cebea824b413dedb63607bb081947df6f84dce5d Mon Sep 17 00:00:00 2001 From: Rui Almeida Date: Fri, 20 Jan 2023 00:20:12 +0000 Subject: [PATCH 1/4] Created config command and started implementing configuration output --- cmd/config.go | 41 +++++++++++++++++++++++++++++++++++++++++ pkg/config/config.go | 42 ++++++++++++++++++++++++++++++------------ 2 files changed, 71 insertions(+), 12 deletions(-) create mode 100644 cmd/config.go diff --git a/cmd/config.go b/cmd/config.go new file mode 100644 index 0000000..8c5e1aa --- /dev/null +++ b/cmd/config.go @@ -0,0 +1,41 @@ +package cmd + +import ( + "github.com/carloscastrojumo/remindme/pkg/config" + "github.com/spf13/cobra" +) + +var configCmd = &cobra.Command{ + Use: "config", + Short: "Manage application configuration", + Long: `Helps manage application configuration file.`, + Run: func(cmd *cobra.Command, args []string) { + // var searchLocations []string + + // cmd.Flags().Visit(func(f *pflag.Flag) { + // searchLocations = append(searchLocations, f.Name) + // }) + + // if len(searchLocations) == 0 { + // searchLocations = append(searchLocations, "command", "description", "tags") + // } + + // if len(args) < 1 { + // return errors.New("the word-to-search argument is required") + // } + + // notes, err := noteService.Search(args[0], searchLocations) + // if err != nil { + // color.Red("Error while getting notes by tags: %s", err) + // } + // output.Print(notes) + config.GetConfig() + }, +} + +func init() { + // searchCmd.Flags().BoolP("tags", "t", false, "Search in tags") + // searchCmd.Flags().BoolP("command", "c", false, "Search in commands") + // searchCmd.Flags().BoolP("description", "d", false, "Search in description") + rootCmd.AddCommand(configCmd) +} diff --git a/pkg/config/config.go b/pkg/config/config.go index f35d980..ad75266 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -15,6 +15,8 @@ import ( var appDir = xdg.Home + "/.config/remindme" +var config = &storage.Config{} + // InitConfig initializes the configuration func InitConfig() { viper.AddConfigPath(appDir) @@ -53,7 +55,7 @@ func promptConfigFile() { viper.Set("mongo.collection", prompt.ForString("Mongo collection")) case "yaml": dataFilename := prompt.ForString("YAML file name (current directory: " + appDir + ")") - viper.Set("yaml.name", appDir + "/" + dataFilename) + viper.Set("yaml.name", appDir+"/"+dataFilename) } saveConfigFile() @@ -80,12 +82,15 @@ func GetNoteService() *storage.NoteService { os.Exit(1) } - storageConfig := &storage.Config{ - StorageType: "mongo", - StorageConfig: &mongoConfig, - } + // storageConfig := &storage.Config{ + // StorageType: "mongo", + // StorageConfig: &mongoConfig, + // } - return initNoteService(storageConfig) + config.StorageType = "mongo" + config.StorageConfig = &mongoConfig + + // return initNoteService(storageConfig) case "yaml": color.Blue("Using YAML storage") var yamlConfig yaml.Config @@ -96,21 +101,34 @@ func GetNoteService() *storage.NoteService { os.Exit(1) } - storageConfig := &storage.Config{ - StorageType: "yaml", - StorageConfig: &yamlConfig, - } + // storageConfig := &storage.Config{ + // StorageType: "yaml", + // StorageConfig: &yamlConfig, + // } + config.StorageType = "yaml" + config.StorageConfig = &yamlConfig - return initNoteService(storageConfig) + // return initNoteService(storageConfig) default: color.Red("No storage type found") os.Exit(1) } - return nil + // return nil + return initNoteService(config) } func initNoteService(storageConfig *storage.Config) *storage.NoteService { storeService := storage.GetStorage(storageConfig) return storage.NewNoteService(storeService) } + +func GetConfig() { + fmt.Println("Configuration:") + // simply print config file??? + + // fmt.Println("File: " + viper.ConfigFileUsed()) + // fmt.Println("Type: " + config.StorageType) + // fmt.Println("Content: " + config.StorageConfig.(*yaml.Config).Name) + +} From 3124922274239c5c97dbca5f3bfa33b2b511fbdd Mon Sep 17 00:00:00 2001 From: Rui Almeida Date: Sat, 21 Jan 2023 22:18:25 +0000 Subject: [PATCH 2/4] Finished simple print config, few tweaks on the code too --- cmd/config.go | 26 ++------------------------ pkg/config/config.go | 40 +++++++++++++++------------------------- 2 files changed, 17 insertions(+), 49 deletions(-) diff --git a/cmd/config.go b/cmd/config.go index 8c5e1aa..d9aa0c3 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -7,35 +7,13 @@ import ( var configCmd = &cobra.Command{ Use: "config", - Short: "Manage application configuration", - Long: `Helps manage application configuration file.`, + Short: "Prints the current configuration file to screen", + Long: `Prints the current configuration file to screen`, Run: func(cmd *cobra.Command, args []string) { - // var searchLocations []string - - // cmd.Flags().Visit(func(f *pflag.Flag) { - // searchLocations = append(searchLocations, f.Name) - // }) - - // if len(searchLocations) == 0 { - // searchLocations = append(searchLocations, "command", "description", "tags") - // } - - // if len(args) < 1 { - // return errors.New("the word-to-search argument is required") - // } - - // notes, err := noteService.Search(args[0], searchLocations) - // if err != nil { - // color.Red("Error while getting notes by tags: %s", err) - // } - // output.Print(notes) config.GetConfig() }, } func init() { - // searchCmd.Flags().BoolP("tags", "t", false, "Search in tags") - // searchCmd.Flags().BoolP("command", "c", false, "Search in commands") - // searchCmd.Flags().BoolP("description", "d", false, "Search in description") rootCmd.AddCommand(configCmd) } diff --git a/pkg/config/config.go b/pkg/config/config.go index ad75266..ba2ebfd 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -3,6 +3,7 @@ package config import ( "fmt" "os" + "strconv" "github.com/adrg/xdg" prompt "github.com/carloscastrojumo/remindme/pkg/prompt" @@ -69,52 +70,38 @@ func saveConfigFile() { // GetNoteService returns a new note service func GetNoteService() *storage.NoteService { - storageType := viper.GetString("storageType") + config.StorageType = viper.GetString("storageType") - switch storageType { + switch config.StorageType { case "mongo": color.Blue("Using Mongo storage") var mongoConfig mongo.Config err := viper.UnmarshalKey("mongo", &mongoConfig) if err != nil { - color.Red("Could not read %s configuration: '%s'", storageType, err) + color.Red("Could not read %s configuration: '%s'", config.StorageType, err) os.Exit(1) } - // storageConfig := &storage.Config{ - // StorageType: "mongo", - // StorageConfig: &mongoConfig, - // } - - config.StorageType = "mongo" config.StorageConfig = &mongoConfig - // return initNoteService(storageConfig) case "yaml": color.Blue("Using YAML storage") var yamlConfig yaml.Config err := viper.UnmarshalKey("yaml", &yamlConfig) if err != nil { - color.Red("Whoops. Could not read %s configuration: '%s'", storageType, err) + color.Red("Whoops. Could not read %s configuration: '%s'", config.StorageType, err) os.Exit(1) } - // storageConfig := &storage.Config{ - // StorageType: "yaml", - // StorageConfig: &yamlConfig, - // } - config.StorageType = "yaml" config.StorageConfig = &yamlConfig - // return initNoteService(storageConfig) default: color.Red("No storage type found") os.Exit(1) } - // return nil return initNoteService(config) } @@ -124,11 +111,14 @@ func initNoteService(storageConfig *storage.Config) *storage.NoteService { } func GetConfig() { - fmt.Println("Configuration:") - // simply print config file??? - - // fmt.Println("File: " + viper.ConfigFileUsed()) - // fmt.Println("Type: " + config.StorageType) - // fmt.Println("Content: " + config.StorageConfig.(*yaml.Config).Name) - + color.Blue("Configuration file: %s\n", color.GreenString(viper.ConfigFileUsed())) + switch config.StorageType { + case "yaml": + color.Blue("Data file: %s\n", color.GreenString(config.StorageConfig.(*yaml.Config).Name)) + case "mongo": + color.Blue("Host: %s\n", color.GreenString(config.StorageConfig.(*mongo.Config).Host)) + color.Blue("Port: %s\n", color.GreenString(strconv.Itoa(config.StorageConfig.(*mongo.Config).Port))) + color.Blue("Database: %s\n", color.GreenString(config.StorageConfig.(*mongo.Config).Database)) + color.Blue("Collection: %s\n", color.GreenString(config.StorageConfig.(*mongo.Config).Collection)) + } } From 1154a7e33b270d6e6a88c3e122c5be7d274b3a1a Mon Sep 17 00:00:00 2001 From: Rui Almeida Date: Mon, 23 Jan 2023 00:45:59 +0000 Subject: [PATCH 3/4] Fix syntax suggestion --- pkg/config/config.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/config/config.go b/pkg/config/config.go index ba2ebfd..01c5ddb 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -110,6 +110,7 @@ func initNoteService(storageConfig *storage.Config) *storage.NoteService { return storage.NewNoteService(storeService) } +// Prints the current configuration to screen func GetConfig() { color.Blue("Configuration file: %s\n", color.GreenString(viper.ConfigFileUsed())) switch config.StorageType { From b9d2d115befd2090605286569af30d5b10cd19e3 Mon Sep 17 00:00:00 2001 From: Rui Almeida Date: Mon, 23 Jan 2023 02:12:25 +0000 Subject: [PATCH 4/4] Missing function name --- pkg/config/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 01c5ddb..54df02c 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -110,7 +110,7 @@ func initNoteService(storageConfig *storage.Config) *storage.NoteService { return storage.NewNoteService(storeService) } -// Prints the current configuration to screen +// GetConfig prints the current configuration to screen func GetConfig() { color.Blue("Configuration file: %s\n", color.GreenString(viper.ConfigFileUsed())) switch config.StorageType {