Skip to content

Commit a993c33

Browse files
committed
feat(config): json config
1 parent d67470c commit a993c33

2 files changed

Lines changed: 42 additions & 6 deletions

File tree

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ API_BASE_URL=https://your-unraid-server:port/graphql
3434
API_SKIP_TLS_VERIFY=true
3535
```
3636

37+
It also supports a config file located at `~/.config/unraidctl/config.json` with the following format:
38+
39+
```json
40+
{
41+
"api": {
42+
"api_key": "<apikey>",
43+
"base_url": "https://<unraid>:<port>/graphql",
44+
"skip_tls_verify": true
45+
}
46+
}
47+
```
48+
3749
Once installed, you can run commands like:
3850

3951
```bash

internal/config.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,44 @@
11
package internal
22

33
import (
4+
"encoding/json"
5+
"os"
6+
"path/filepath"
7+
48
"github.com/caarlos0/env/v11"
59
"github.com/joho/godotenv"
610
)
711

812
type Config struct {
913
Api struct {
10-
BaseUrl string `env:"BASE_URL,required"`
11-
ApiKey string `env:"KEY,required"`
12-
SkipTlsVerify bool `env:"SKIP_TLS_VERIFY" envDefault:"true"`
13-
} `envPrefix:"API_"`
14+
BaseUrl string `env:"BASE_URL,required" json:"base_url"`
15+
ApiKey string `env:"KEY,required" json:"api_key"`
16+
SkipTlsVerify bool `env:"SKIP_TLS_VERIFY" envDefault:"true" json:"skip_tls_verify"`
17+
} `envPrefix:"API_" json:"api"`
1418
}
1519

1620
func GetConfig() (*Config, error) {
1721
_ = godotenv.Load()
18-
config, err := env.ParseAs[Config]()
19-
return &config, err
22+
23+
cfg := &Config{}
24+
25+
home, err := os.UserHomeDir()
26+
if err != nil {
27+
return nil, err
28+
}
29+
30+
jsonPath := filepath.Join(home, ".config", "unraidctl", "config.json")
31+
if b, err := os.ReadFile(jsonPath); err == nil {
32+
err = json.Unmarshal(b, cfg)
33+
if err != nil {
34+
return nil, err
35+
}
36+
37+
return cfg, nil
38+
}
39+
40+
if err := env.Parse(cfg); err != nil {
41+
return nil, err
42+
}
43+
return cfg, nil
2044
}

0 commit comments

Comments
 (0)