From c07ee1d78a74f9ad00a2e2ed5f8267079b476808 Mon Sep 17 00:00:00 2001 From: Arijit Das Date: Fri, 3 Dec 2021 20:04:25 +0530 Subject: [PATCH 1/3] Take config through env. --- cmd/root.go | 26 ++++++++++++++++++-------- go.mod | 3 +-- go.sum | 2 ++ 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 4e8ac521..073653b4 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -19,11 +19,14 @@ package cmd import ( "fmt" "os" + "path/filepath" "strings" + "github.com/joho/godotenv" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/viper" + "github.com/vulcanize/ipld-eth-server/pkg/prom" ) @@ -121,14 +124,21 @@ func init() { } func initConfig() { - if cfgFile != "" { - viper.SetConfigFile(cfgFile) - if err := viper.ReadInConfig(); err == nil { - log.Printf("Using config file: %s", viper.ConfigFileUsed()) - } else { - log.Fatal(fmt.Sprintf("Couldn't read config file: %s", err.Error())) - } - } else { + if cfgFile == "" { log.Warn("No config file passed with --config flag") + return + } + + viper.SetConfigFile(cfgFile) + if err := viper.ReadInConfig(); err != nil { + log.Fatalf("Couldn't read config file: %s", err.Error()) + } + + log.Infof("Using config file: %s", viper.ConfigFileUsed()) + + if filepath.Ext(cfgFile) == ".env" { + if err := godotenv.Load(cfgFile); err != nil { + log.Fatalf("Failed to set environment variable from config file: %s", err.Error()) + } } } diff --git a/go.mod b/go.mod index 9fcc2576..b9ca1ec9 100644 --- a/go.mod +++ b/go.mod @@ -7,13 +7,12 @@ require ( github.com/fsnotify/fsnotify v1.5.1 // indirect github.com/graph-gophers/graphql-go v0.0.0-20201113091052-beb923fada29 github.com/ipfs/go-block-format v0.0.3 - github.com/ipfs/go-blockservice v0.1.7 github.com/ipfs/go-cid v0.0.7 - github.com/ipfs/go-ipfs v0.10.0 github.com/ipfs/go-ipfs-blockstore v1.0.1 github.com/ipfs/go-ipfs-ds-help v1.0.0 github.com/ipfs/go-ipld-format v0.2.0 github.com/jmoiron/sqlx v1.2.0 + github.com/joho/godotenv v1.4.0 github.com/lib/pq v1.10.4 github.com/machinebox/graphql v0.2.2 github.com/mailgun/groupcache/v2 v2.2.1 diff --git a/go.sum b/go.sum index 1c0fce9f..dd0fea5e 100644 --- a/go.sum +++ b/go.sum @@ -754,6 +754,8 @@ github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHW github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= github.com/jmoiron/sqlx v1.2.0 h1:41Ip0zITnmWNR/vHV+S4m+VoUivnWY5E4OJfLZjCJMA= github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks= +github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg= +github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ= -- 2.45.2 From 143d79fdfca85763cf9f9573b0e5f85816bf6715 Mon Sep 17 00:00:00 2001 From: Arijit Das Date: Mon, 6 Dec 2021 12:32:40 +0530 Subject: [PATCH 2/3] Update go.mod. --- go.mod | 2 ++ go.sum | 14 +++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index b9ca1ec9..a27fe1ef 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.15 require ( github.com/ethereum/go-ethereum v1.10.14 github.com/fsnotify/fsnotify v1.5.1 // indirect + github.com/go-sql-driver/mysql v1.6.0 // indirect github.com/graph-gophers/graphql-go v0.0.0-20201113091052-beb923fada29 github.com/ipfs/go-block-format v0.0.3 github.com/ipfs/go-cid v0.0.7 @@ -17,6 +18,7 @@ require ( github.com/machinebox/graphql v0.2.2 github.com/mailgun/groupcache/v2 v2.2.1 github.com/matryer/is v1.4.0 // indirect + github.com/mattn/go-sqlite3 v1.14.9 // indirect github.com/multiformats/go-multihash v0.0.15 github.com/onsi/ginkgo v1.16.5 github.com/onsi/gomega v1.13.0 diff --git a/go.sum b/go.sum index dd0fea5e..275b0024 100644 --- a/go.sum +++ b/go.sum @@ -324,8 +324,9 @@ github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh github.com/go-sourcemap/sourcemap v2.1.2+incompatible/go.mod h1:F8jJfvm2KbVjc5NqelyYJmf/v5J0dwNLS2mL4sNA1Jg= github.com/go-sourcemap/sourcemap v2.1.3+incompatible/go.mod h1:F8jJfvm2KbVjc5NqelyYJmf/v5J0dwNLS2mL4sNA1Jg= github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= -github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA= github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= +github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= +github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I= @@ -1204,8 +1205,9 @@ github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/Qd github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/mattn/go-sqlite3 v1.11.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= -github.com/mattn/go-sqlite3 v1.14.7 h1:fxWBnXkxfM6sRiuH3bqJ4CfzZojMOLVc0UTsTglEghA= github.com/mattn/go-sqlite3 v1.14.7/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= +github.com/mattn/go-sqlite3 v1.14.9 h1:10HX2Td0ocZpYEjhilsuo6WWtUqttj2Kb0KtD86/KYA= +github.com/mattn/go-sqlite3 v1.14.9/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= github.com/mattn/go-tty v0.0.0-20180907095812-13ff1204f104/go.mod h1:XPvLUNfbS4fJH25nqRHfWLMa1ONC8Amw+mIA639KxkE= github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= @@ -1839,6 +1841,13 @@ golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96b golang.org/x/net v0.0.0-20210423184538-5f58ad60dda6/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk= golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk= golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +<<<<<<< HEAD +||||||| parent of af4d66d4 (Update go.mod.) +golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d h1:LO7XpTYMwTqxjLcGWPijK3vRXg1aWdlNOVOHRq45d7c= +======= +golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +>>>>>>> af4d66d4 (Update go.mod.) golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 h1:CIJ76btIcR3eFI5EgSo6k1qKw9KJexJuRLI9G7Hp5wE= @@ -2081,7 +2090,6 @@ google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7 google.golang.org/appengine v1.6.1/go.mod h1:i06prIuMbXzDqacNJfV5OdTW448YApPu5ww/cMBSeb0= google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= google.golang.org/appengine v1.6.6/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= -google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c= google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= google.golang.org/genproto v0.0.0-20180831171423-11092d34479b/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= -- 2.45.2 From 316bf0990a7472a055d77e6875895e59ae271fe0 Mon Sep 17 00:00:00 2001 From: Arijit Das Date: Thu, 13 Jan 2022 23:14:03 +0530 Subject: [PATCH 3/3] Add env flag to load .env file. --- cmd/root.go | 39 ++++++++++++++++++++++++++------------- go.sum | 7 ------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 073653b4..f5d6401c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -32,6 +32,7 @@ import ( var ( cfgFile string + envFile string subCommand string logWithCommand log.Entry ) @@ -102,6 +103,7 @@ func init() { viper.AutomaticEnv() rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file location") + rootCmd.PersistentFlags().StringVar(&envFile, "env", "", "environment file location") rootCmd.PersistentFlags().String("client-ipcPath", "", "location of geth.ipc file") rootCmd.PersistentFlags().String("log-level", log.InfoLevel.String(), "log level (trace, debug, info, warn, error, fatal, panic)") @@ -124,21 +126,32 @@ func init() { } func initConfig() { - if cfgFile == "" { - log.Warn("No config file passed with --config flag") - return + if cfgFile == "" && envFile == "" { + log.Fatal("No configuration file specified, use --config , --env flag to provide configuration") } - viper.SetConfigFile(cfgFile) - if err := viper.ReadInConfig(); err != nil { - log.Fatalf("Couldn't read config file: %s", err.Error()) - } - - log.Infof("Using config file: %s", viper.ConfigFileUsed()) - - if filepath.Ext(cfgFile) == ".env" { - if err := godotenv.Load(cfgFile); err != nil { - log.Fatalf("Failed to set environment variable from config file: %s", err.Error()) + if cfgFile != "" { + if filepath.Ext(cfgFile) != ".toml" { + log.Fatal("Provide .toml file for --config flag") } + + viper.SetConfigFile(cfgFile) + if err := viper.ReadInConfig(); err != nil { + log.Fatalf("Couldn't read config file: %s", err.Error()) + } + + log.Infof("Using config file: %s", viper.ConfigFileUsed()) + } + + if envFile != "" { + if filepath.Ext(envFile) != ".env" { + log.Fatal("Provide .env file for --env flag") + } + + if err := godotenv.Load(envFile); err != nil { + log.Fatalf("Failed to set environment variable from env file: %s", err.Error()) + } + + log.Infof("Using env file: %s", envFile) } } diff --git a/go.sum b/go.sum index 275b0024..010afaaf 100644 --- a/go.sum +++ b/go.sum @@ -1841,13 +1841,6 @@ golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96b golang.org/x/net v0.0.0-20210423184538-5f58ad60dda6/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk= golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk= golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -<<<<<<< HEAD -||||||| parent of af4d66d4 (Update go.mod.) -golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d h1:LO7XpTYMwTqxjLcGWPijK3vRXg1aWdlNOVOHRq45d7c= -======= -golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= ->>>>>>> af4d66d4 (Update go.mod.) golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 h1:CIJ76btIcR3eFI5EgSo6k1qKw9KJexJuRLI9G7Hp5wE= -- 2.45.2