Browse Source

Converted configuration from straight environment to Viper.

Frederic G. MARAND 5 years ago
parent
commit
d402315016
7 changed files with 33 additions and 25 deletions
  1. 7 0
      .gitignore
  2. 4 0
      cmd/kurzd/dist.config.yml
  3. 0 4
      cmd/kurzd/export.go
  4. 1 1
      cmd/kurzd/export_content.go
  5. 7 14
      cmd/kurzd/kurzd.go
  6. 13 5
      cmd/kurzd/rootCmd.go
  7. 1 1
      go.mod

+ 7 - 0
.gitignore

@@ -5,6 +5,9 @@
 *.o
 *.a
 *.so
+cmd/kurz/kurz
+cmd/kurzd/kurzd
+doc/*/*.svg
 
 # Folders
 _obj
@@ -25,3 +28,7 @@ _testmain.go
 *.exe
 *.test
 *.prof
+
+# Data and local files
+config.yml
+*.sql

+ 4 - 0
cmd/kurzd/dist.config.yml

@@ -0,0 +1,4 @@
+database:
+  # Code likely not to work on another engine because it adds ?parseTime=true
+  driver: mysql
+  dsn: <user>:<pass>@[<server>]/<database>

+ 0 - 4
cmd/kurzd/export.go

@@ -1,7 +1,6 @@
 package main
 
 import (
-	"fmt"
 	"github.com/spf13/cobra"
 )
 
@@ -9,9 +8,6 @@ var cmdExport = &cobra.Command{
 	Use:   "export",
 	Short: "Export configuration or content",
 	Long:  "Provides subcommands to export configuration as Kurz gets it or content found in the database",
-	Run: func(cmd *cobra.Command, args []string) {
-		fmt.Println("Please use one of the export subcommands")
-	},
 }
 
 func init() {

+ 1 - 1
cmd/kurzd/export_content.go

@@ -51,7 +51,7 @@ func exportContentHandler(cmd *cobra.Command, args []string) {
 	dbDriver, dbDsn := parseDbCred()
 	db, err := dbDial(dbDriver, dbDsn)
 	if err != nil {
-		panic("Could not open database")
+		panic(err)
 	}
 	defer db.Close()
 	entries, err := exportContentLoader(db)

+ 7 - 14
cmd/kurzd/kurzd.go

@@ -2,6 +2,7 @@
 package main
 
 import (
+	"github.com/spf13/viper"
 	"os"
 	"time"
 
@@ -51,19 +52,11 @@ func dbDial(dbDriver, dbDsn string) (*sql.DB, error) {
 }
 
 func parseDbCred() (driver, dsn string) {
-	const DefaultDriver = "mysql"
-	const DefaultDsn = "root:root@tcp(localhost:3306)/kurz"
+	viper.SetDefault("database.driver", "mysql")
+	viper.SetDefault("database.dsn", "root:root@tcp(localhost:3306)/kurz")
 
-	envDriver := os.Getenv("DB_DRIVER")
-	if envDriver == "" {
-		envDriver = DefaultDriver
-	}
-
-	envDsn := os.Getenv("DB_DSN")
-	if envDsn == "" {
-		envDsn = DefaultDsn
-	}
-	envDsn += "?parseTime=true"
-
-	return envDriver, envDsn
+	driver = viper.Get("database.driver").(string)
+	dsn = viper.Get("database.dsn").(string)
+	dsn += "?parseTime=true"
+	return
 }

+ 13 - 5
cmd/kurzd/rootCmd.go

@@ -5,23 +5,31 @@ import (
 	"os"
 
 	"github.com/spf13/cobra"
+	"github.com/spf13/viper"
 )
 
 var cmd = &cobra.Command{
 	Use:   "kurzd",
 	Short: "kurzd is the Kurz daemon and back-office command",
-	Long:  "kurzd is the actual engine for Kurz. Use it as the server for your Kurz clients",
-	Run: func(cmd *cobra.Command, args []string) {
-		panic("Not yet implemented")
-	},
+	Long:  `kurzd is the actual engine for Kurz. Use it as the server for your Kurz clients.
+Configure it by copying dist.config.yml to ~/.kurz/config.yml and editing the copy.`,
 }
+var verbose bool
 
 func initConfig() {
-
+	viper.SetConfigName("config")
+	viper.AddConfigPath(".")
+	viper.AddConfigPath("$HOME/.kurz")
+	err := viper.ReadInConfig()
+	if err != nil {
+		fmt.Fprintln(os.Stderr, err)
+		os.Exit(2)
+	}
 }
 
 func init() {
 	cobra.OnInitialize(initConfig)
+	verbose = *cmd.PersistentFlags().BoolP("verbose", "v", false, "Add -v for verbose operation")
 }
 
 func Execute() {

+ 1 - 1
go.mod

@@ -5,7 +5,7 @@ require (
 	github.com/mitchellh/go-homedir v1.0.0 // indirect
 	github.com/spf13/cobra v0.0.3
 	github.com/spf13/pflag v1.0.3 // indirect
-	github.com/spf13/viper v1.2.1 // indirect
+	github.com/spf13/viper v1.2.1
 	google.golang.org/appengine v1.3.0 // indirect
 	gopkg.in/yaml.v2 v2.2.1
 )