loadConfig returns the server's parsed config file, locating it using the provided arg. The arg may be of the form: - empty, to mean automatic (will write a default high-level config if no cloud config is available) - a filepath absolute or relative to the user's configuration directory, - a URL
(arg string)
| 137 | // - a filepath absolute or relative to the user's configuration directory, |
| 138 | // - a URL |
| 139 | func loadConfig(arg string) (*serverinit.Config, error) { |
| 140 | if strings.HasPrefix(arg, "http://") || strings.HasPrefix(arg, "https://") { |
| 141 | contents, err := slurpURL(arg, 256<<10) |
| 142 | if err != nil { |
| 143 | return nil, err |
| 144 | } |
| 145 | return serverinit.Load(contents) |
| 146 | } |
| 147 | var absPath string |
| 148 | switch { |
| 149 | case arg == "": |
| 150 | absPath = osutil.UserServerConfigPath() |
| 151 | _, err := wkfs.Stat(absPath) |
| 152 | if err == nil { |
| 153 | break |
| 154 | } |
| 155 | if !os.IsNotExist(err) { |
| 156 | return nil, err |
| 157 | } |
| 158 | conf, err := serverinit.DefaultEnvConfig() |
| 159 | if err != nil || conf != nil { |
| 160 | return conf, err |
| 161 | } |
| 162 | configDir, err := osutil.PerkeepConfigDir() |
| 163 | if err != nil { |
| 164 | return nil, err |
| 165 | } |
| 166 | if err := wkfs.MkdirAll(configDir, 0700); err != nil { |
| 167 | return nil, err |
| 168 | } |
| 169 | log.Printf("Generating template config file %s", absPath) |
| 170 | if err := serverinit.WriteDefaultConfigFile(absPath); err != nil { |
| 171 | return nil, err |
| 172 | } |
| 173 | case filepath.IsAbs(arg): |
| 174 | absPath = arg |
| 175 | default: |
| 176 | configDir, err := osutil.PerkeepConfigDir() |
| 177 | if err != nil { |
| 178 | return nil, err |
| 179 | } |
| 180 | absPath = filepath.Join(configDir, arg) |
| 181 | } |
| 182 | return serverinit.LoadFile(absPath) |
| 183 | } |
| 184 | |
| 185 | // If cert/key files are specified, and found, use them. |
| 186 | // If cert/key files are specified, not found, and the default values, generate |
no test coverage detected