如何读取外部变量
How to read external variables
我正在尝试读取由 --configFile=""
作为外部参数发送的变量,但我总是收到未找到的错误,即使我传递了绝对路径
变量:
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()
)
配置:
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
在此处替换代码https://github.com/rafal-szypulka/itm_exporter/blob/master/main.go#L51
与:
conf *Config
在此处替换代码https://github.com/rafal-szypulka/itm_exporter/blob/master/main.go#L354
与:
p := kingpin.MustParse(app.Parse(os.Args[1:]))
conf = c.getConf()
switch p {
我正在尝试读取由 --configFile=""
作为外部参数发送的变量,但我总是收到未找到的错误,即使我传递了绝对路径
变量:
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()
)
配置:
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
在此处替换代码https://github.com/rafal-szypulka/itm_exporter/blob/master/main.go#L51
与:
conf *Config
在此处替换代码https://github.com/rafal-szypulka/itm_exporter/blob/master/main.go#L354
与:
p := kingpin.MustParse(app.Parse(os.Args[1:]))
conf = c.getConf()
switch p {