Home > Software engineering >  Variable for a directory but get error when calling it
Variable for a directory but get error when calling it

Time:12-30

I'm trying to assign a variable to read a directory but I get this error:

"cannot use &home (value of type *[]fs.FileInfo) as string value in argument to viper.AddConfigPath"

I also tried the pointer inside viper.AddConfigPath(&home) but to no avail as well

Here is a snippet of the code:

// Find home directory
        home, err := ioutil.ReadDir("../db-dump")
        cobra.CheckErr(err)

        // Search config in home directory with name ".config" (without extension)
        viper.AddConfigPath(home)

I'm looking to transform the type *[]fs.FileInfo into a string so viper.AddConfigPath()can read it.

Update

I've tried now this way and I'm getting no error compiling, but I can't really test the code yet because of runtime errors:

home := "working directory"
        fileSystem := os.DirFS(home)

        fs.WalkDir(fileSystem, ".", func(path string, d fs.DirEntry, err error) error {
            if err != nil {
                log.Fatal(err)
            }
            fmt.Println(path)
            return nil
        })

        // Search config in home directory with name ".config" (without extension)
        viper.AddConfigPath(home)

CodePudding user response:

Your mistake is that you have type mismatch: home is of type []fs.FileInfo (reference) while viper.AddConfigPath() expects a string (reference).

CodePudding user response:

1. You can read the error message as:

cannot use &home because value of type *[]fs.FileInfo is inserted instead of as string value ...

Meaning that viper.AddConfigPath requires a variable of type string and not a pointer to an array of fs.FileInfo.

2. A quick search here listed this another topic that may have the same issue and probably a tip to your question.

CodePudding user response:

first create new config file (just for test code):

echo "key1: value" > AppName.yaml

and run the code

package main

import (
    "errors"
    "fmt"
    "log"

    "github.com/spf13/viper"
)

func main() {
    v := viper.GetViper()
    if err := readConfig(v, ""); err != nil {
        log.Fatal(err)
    }
    fmt.Println(v.AllKeys())

}

func readConfig(v *viper.Viper, configPath string) error {
    var err error

    if configPath != "" {
        v.SetConfigFile(configPath)
        if err := v.ReadInConfig(); err != nil {
            return fmt.Errorf("unable to read application config=%q : %w", configPath, err)
        }
        return nil
    }
    // look for .<AppName>.yaml (in the current directory)
    v.AddConfigPath(".")
    v.SetConfigName("AppName")
    if err = v.ReadInConfig(); err == nil {
        return nil
    } else if !errors.As(err, &viper.ConfigFileNotFoundError{}) {
        return fmt.Errorf("unable to parse config=%q: %w", v.ConfigFileUsed(), err)
    }

    return nil
}


out:

[key1]

You can load config by path:

    if err := readConfig(v, "app.yml"); err != nil {
        log.Fatal(err)
    }
  • Related