aboutsummaryrefslogtreecommitdiff
path: root/cmd/root.go
diff options
context:
space:
mode:
authorVidhu Kant Sharma <vidhukant@vidhukant.xyz>2022-08-16 21:07:10 +0530
committerVidhu Kant Sharma <vidhukant@vidhukant.xyz>2022-08-16 21:07:10 +0530
commit20c4f14750f18050f88aceef8adb788342576a25 (patch)
tree4a228d0f075c833a9743f1bbd815a9221b419d78 /cmd/root.go
parent5686c94f5fc24f7ec6927ab4b80b30d8644fba8f (diff)
reading config file with viper
Diffstat (limited to 'cmd/root.go')
-rw-r--r--cmd/root.go26
1 files changed, 25 insertions, 1 deletions
diff --git a/cmd/root.go b/cmd/root.go
index f1fba18..2c6ca56 100644
--- a/cmd/root.go
+++ b/cmd/root.go
@@ -20,8 +20,10 @@ package cmd
import (
"os"
+ "fmt"
"github.com/spf13/cobra"
+ "github.com/spf13/viper"
)
var (
@@ -32,7 +34,29 @@ var (
var rootCmd = &cobra.Command{
Use: "macli",
Short: "macli - Unofficial CLI-Based MyAnimeList Client.",
- Long: "macli is an unofficial MyAnimeClient for use inside the terminal.",
+ Long: "macli is an unofficial MyAnimeList Client for use inside the terminal.",
+}
+
+func init() {
+ viper.SetConfigName("macli")
+ viper.SetConfigType("yaml")
+ viper.AddConfigPath("$HOME/.config/macli")
+ viper.AddConfigPath("/etc/macli")
+
+ // dont show error if file not found
+ // macli doesnt need a config file to work properly
+ if err := viper.ReadInConfig(); err != nil {
+ // error if config file found but has errors
+ if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
+ fmt.Println("Error while reading macli config file:", err)
+ os.Exit(1)
+ }
+ }
+
+ name := viper.Get("name").(map[string]interface{})
+
+ fmt.Println("Config File Contents:", name["last"])
+ os.Exit(0)
}
func Execute() {