LoadConfigFromFile reads the local configuration from the specified file.
(fileName string)
| 129 | |
| 130 | // LoadConfigFromFile reads the local configuration from the specified file. |
| 131 | func LoadConfigFromFile(fileName string) (*LocalConfig, error) { |
| 132 | f, err := os.Open(fileName) //nolint:gosec |
| 133 | if err != nil { |
| 134 | return nil, errors.Wrap(err, "error loading config file") |
| 135 | } |
| 136 | defer f.Close() //nolint:errcheck |
| 137 | |
| 138 | var lc LocalConfig |
| 139 | |
| 140 | if err := json.NewDecoder(f).Decode(&lc); err != nil { |
| 141 | return nil, errors.Wrap(err, "error decoding config json") |
| 142 | } |
| 143 | |
| 144 | // cache directory is stored as relative to config file name, resolve it to absolute. |
| 145 | if lc.Caching != nil { |
| 146 | if lc.Caching.CacheDirectory != "" && !ospath.IsAbs(lc.Caching.CacheDirectory) { |
| 147 | lc.Caching.CacheDirectory = filepath.Join(filepath.Dir(fileName), lc.Caching.CacheDirectory) |
| 148 | } |
| 149 | |
| 150 | // override cache directory from the environment variable. |
| 151 | if cd := os.Getenv("KOPIA_CACHE_DIRECTORY"); cd != "" && ospath.IsAbs(cd) { |
| 152 | lc.Caching.CacheDirectory = cd |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | if lc.PermissiveCacheLoading && os.Getenv("KOPIA_UPGRADE_LOCK_ENABLED") == "" { |
| 157 | return nil, errors.New("must have set KOPIA_UPGRADE_LOCK_ENABLED when connecting to repository with permissive cache loading") |
| 158 | } |
| 159 | |
| 160 | return &lc, nil |
| 161 | } |