-
Notifications
You must be signed in to change notification settings - Fork 12
/
config.go
112 lines (89 loc) · 2.42 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package config
import (
_ "embed"
"encoding/json"
"fmt"
"os"
"path/filepath"
"github.com/pomdtr/sunbeam/internal/utils"
"github.com/pomdtr/sunbeam/pkg/schemas"
"github.com/pomdtr/sunbeam/pkg/types"
)
//go:embed config.json
var configBytes []byte
type Config struct {
Schema string `json:"$schema,omitempty"`
Oneliners []Oneliner `json:"oneliners,omitempty"`
Extensions map[string]ExtensionConfig `json:"extensions,omitempty"`
}
type ExtensionConfig struct {
Origin string `json:"origin,omitempty"`
Preferences types.Preferences `json:"preferences,omitempty"`
Root []types.RootItem `json:"root,omitempty"`
}
func (e *ExtensionConfig) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err == nil {
e.Origin = s
return nil
}
var extensionRef struct {
Origin string `json:"origin,omitempty"`
Preferences map[string]any `json:"preferences,omitempty"`
Root []types.RootItem
}
if err := json.Unmarshal(b, &extensionRef); err == nil {
e.Origin = extensionRef.Origin
e.Preferences = extensionRef.Preferences
e.Root = extensionRef.Root
return nil
}
return fmt.Errorf("invalid extension ref: %s", string(b))
}
func (cfg Config) Aliases() []string {
var aliases []string
for alias := range cfg.Extensions {
aliases = append(aliases, alias)
}
return aliases
}
type Oneliner struct {
Title string `json:"title"`
Command string `json:"command"`
Exit bool `json:"exit"`
}
func Path() string {
if env, ok := os.LookupEnv("SUNBEAM_CONFIG"); ok {
return env
}
return filepath.Join(utils.ConfigHome(), "config.json")
}
func Load() (Config, error) {
configPath := Path()
if _, err := os.Stat(configPath); err != nil {
if err := os.MkdirAll(filepath.Dir(configPath), 0755); err != nil {
return Config{}, err
}
f, err := os.Create(configPath)
if err != nil {
return Config{}, err
}
defer f.Close()
if _, err := f.Write(configBytes); err != nil {
return Config{}, err
}
}
var configBytes []byte
configBytes, err := os.ReadFile(configPath)
if err != nil {
return Config{}, err
}
if err := schemas.ValidateConfig(configBytes); err != nil {
return Config{}, fmt.Errorf("invalid config: %w", err)
}
var config Config
if err := json.Unmarshal(configBytes, &config); err != nil {
return Config{}, fmt.Errorf("failed to unmarshal config: %w", err)
}
return config, nil
}