diff --git a/cmd/config.go b/cmd/config.go new file mode 100644 index 0000000..d9aa0c3 --- /dev/null +++ b/cmd/config.go @@ -0,0 +1,19 @@ +package cmd + +import ( + "github.com/carloscastrojumo/remindme/pkg/config" + "github.com/spf13/cobra" +) + +var configCmd = &cobra.Command{ + Use: "config", + Short: "Prints the current configuration file to screen", + Long: `Prints the current configuration file to screen`, + Run: func(cmd *cobra.Command, args []string) { + config.GetConfig() + }, +} + +func init() { + rootCmd.AddCommand(configCmd) +} diff --git a/pkg/config/config.go b/pkg/config/config.go index f35d980..54df02c 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" @@ -15,6 +16,8 @@ import ( var appDir = xdg.Home + "/.config/remindme" +var config = &storage.Config{} + // InitConfig initializes the configuration func InitConfig() { viper.AddConfigPath(appDir) @@ -53,7 +56,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() @@ -67,50 +70,56 @@ 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.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.StorageConfig = &yamlConfig - return initNoteService(storageConfig) default: color.Red("No storage type found") os.Exit(1) } - return nil + return initNoteService(config) } func initNoteService(storageConfig *storage.Config) *storage.NoteService { storeService := storage.GetStorage(storageConfig) return storage.NewNoteService(storeService) } + +// GetConfig prints the current configuration to screen +func GetConfig() { + 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)) + } +}