Skip to content

Commit

Permalink
Merge pull request #34 from carloscastrojumo/27-create-config-command
Browse files Browse the repository at this point in the history
27 create config command
  • Loading branch information
ruia authored Jan 23, 2023
2 parents c191400 + b9d2d11 commit 54e67c6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
19 changes: 19 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
@@ -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)
}
41 changes: 25 additions & 16 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"fmt"
"os"
"strconv"

"github.com/adrg/xdg"
prompt "github.com/carloscastrojumo/remindme/pkg/prompt"
Expand All @@ -15,6 +16,8 @@ import (

var appDir = xdg.Home + "/.config/remindme"

var config = &storage.Config{}

// InitConfig initializes the configuration
func InitConfig() {
viper.AddConfigPath(appDir)
Expand Down Expand Up @@ -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()
Expand All @@ -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))
}
}

0 comments on commit 54e67c6

Please sign in to comment.