Search code examples
stringgovar

How to read external variables


I am trying to read a variable sent as an external parameter by --configFile="", but I always get an error not found, even if I pass the absolute path

Vars:

var (
    c                          Config
    conf                       = c.getConf()
    app                        = kingpin.New("exporter", "Exporter for Prometheus.")
    configFile                 = app.Flag("configFile", "Configuration file destination (/etc/exporter/config.yaml)").Default("/etc/exporter/config.yaml").String()
)

Config:

func (c *Config) getConf() *Config {

    yamlFile, err := ioutil.ReadFile(string(*configFile))
    if err != nil {
        log.Errorf("yamlFile.Get err   #%v ", err)
    }
    err = yaml.Unmarshal(yamlFile, c)
    if err != nil {
        log.Errorf("Unmarshal: %v", err)
    }
    return c
}

Command/Output:

server:/etc/exporter # ./exporter --configFile="/etc/exporter/config.yaml"
ERRO[0000] yamlFile.Get err   #open : no such file or directory

ls -ltr:

total 14152
-rw------- 1 root root     1334 Sep 25 20:47 config.yaml
-rwxrwxr-x 1 root root 14466568 Sep 25 22:03 exporter

Solution

  • Replace code here https://github.com/rafal-szypulka/itm_exporter/blob/master/main.go#L51

    with:

    conf *Config
    

    Replace code here https://github.com/rafal-szypulka/itm_exporter/blob/master/main.go#L354

    with:

    p := kingpin.MustParse(app.Parse(os.Args[1:]))
    conf = c.getConf()
    switch p {