Skip to content

Commit 2c893bc

Browse files
committed
config change
1 parent 680788e commit 2c893bc

File tree

3 files changed

+13
-28
lines changed

3 files changed

+13
-28
lines changed

cmd/server/main.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,15 @@
11
package main
22

33
import (
4-
"flag"
54
"fmt"
65
"os"
76

87
"github.com/aaabhilash97/aadhaar_scrapper_apis/internal/appconfig"
98
cmd "github.com/aaabhilash97/aadhaar_scrapper_apis/internal/cmd/server"
109
)
1110

12-
var gitCommit, gitTag string
13-
1411
func init() {
15-
version := flag.Bool("version", false, "Show version information")
16-
flag.Parse()
17-
18-
if *version {
19-
msg := fmt.Sprintf(`Git Tag: %s
20-
Git commit: %s`, gitTag, gitCommit)
2112

22-
fmt.Println(msg)
23-
os.Exit(0)
24-
}
2513
}
2614

2715
func main() {

configs/configs.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
11
package configs
22

33
import (
4-
"embed"
54
_ "embed"
6-
"fmt"
75
)
86

9-
//go:embed *.yml
10-
var f embed.FS
11-
12-
func GetYamlConfig(name string) ([]byte, error) {
13-
return f.ReadFile(fmt.Sprintf("%s.yml", name))
14-
}
15-
167
//go:embed default.yml
178
var defaultConf []byte
189

internal/appconfig/appconfig.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/aaabhilash97/aadhaar_scrapper_apis/configs"
88
"github.com/aaabhilash97/aadhaar_scrapper_apis/pkg/logger"
9+
"github.com/spf13/pflag"
910
"github.com/spf13/viper"
1011
"go.uber.org/zap"
1112
)
@@ -14,25 +15,30 @@ func Init() *Config {
1415
fn := "appconfig.Init"
1516

1617
log := logger.InitLogger(logger.Config{
17-
LogLevel: "debug",
18-
LogOutputPaths: "stdout",
18+
LogLevel: "debug",
19+
LogOutputPaths: "stdout",
20+
DisableStackTrace: true,
1921
})
2022

2123
var config Config
2224
{
25+
pflag.String("conf", "", "config path")
26+
pflag.Parse()
27+
2328
viperConfig := viper.New()
24-
viperConfig.SetConfigType("yaml")
29+
viperConfig.AutomaticEnv()
30+
_ = viperConfig.BindPFlags(pflag.CommandLine)
31+
viperConfig.SetConfigType("yml")
2532
err := viperConfig.ReadConfig(bytes.NewBuffer(configs.GetDefaultConfig()))
2633
if err != nil {
2734
log.Fatal(fn, zap.Error(err))
2835
}
29-
if confName := os.Getenv("conf"); confName != "" {
30-
envConfig, err := configs.GetYamlConfig(confName)
36+
if confPath := viperConfig.GetString("conf"); confPath != "" {
37+
confPath, err := os.Open(confPath)
3138
if err != nil {
3239
log.Fatal(fn, zap.NamedError("config not found", err))
3340
}
34-
err = viperConfig.MergeConfig(
35-
bytes.NewBuffer(envConfig))
41+
err = viperConfig.MergeConfig(confPath)
3642
if err != nil {
3743
log.Fatal(fn, zap.Error(err))
3844
}

0 commit comments

Comments
 (0)