Browse Source

implement argument with config file

tags/0.0.3
Vladimir Smagin 3 weeks ago
parent
commit
dac31e2a55
2 changed files with 20 additions and 8 deletions
  1. +2
    -2
      .gitignore
  2. +18
    -6
      main.go

+ 2
- 2
.gitignore View File

@@ -1,2 +1,2 @@
config.yaml
compare-pg
config-*.yaml
compare-pg*

+ 18
- 6
main.go View File

@@ -2,6 +2,7 @@ package main

import (
"database/sql"
"flag"
"fmt"
"io/ioutil"
"os"
@@ -11,7 +12,11 @@ import (
"gopkg.in/yaml.v2"
)

var logger *log.Logger
var (
configFile = flag.String("config", "", "Config file")
logger *log.Logger
pg pgConnections
)

type config struct {
Credentials map[string]string `yaml:"credentials"`
@@ -21,10 +26,10 @@ type pgConnections struct {
Handlers map[string]*sql.DB
}

func loadConfig() config {
func loadConfig(configFile string) config {
var conf config

yamldata, err := ioutil.ReadFile("config.yaml")
yamldata, err := ioutil.ReadFile(configFile)
if err != nil {
logger.Errorf("Error opening file %v", err)
os.Exit(1)
@@ -100,11 +105,18 @@ func inList(list []string, value string) bool {
return false
}

func main() {
func init() {
logger = log.New(os.Stderr).WithColor()
// Config
conf := loadConfig()
pg := pgConnections{}
flag.Parse()
if *configFile == "" {
logger.Errorf("provide -config")
os.Exit(1)
}
}

func main() {
conf := loadConfig(*configFile)

// Connect to DBs
pgConfigs := make(map[string]*sql.DB)


Loading…
Cancel
Save