rework config
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"os"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
"github.com/sst/opencode/pkg/client"
|
||||
)
|
||||
|
||||
type State struct {
|
||||
@@ -15,44 +16,15 @@ type State struct {
|
||||
Model string `toml:"model"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Theme string `toml:"theme"`
|
||||
Provider string `toml:"provider"`
|
||||
Model string `toml:"model"`
|
||||
Keybinds map[string]string `toml:"keybinds"`
|
||||
}
|
||||
|
||||
func NewState() *State {
|
||||
return &State{
|
||||
Theme: "opencode",
|
||||
}
|
||||
}
|
||||
|
||||
func NewConfig() *Config {
|
||||
keybinds := make(map[string]string)
|
||||
keybinds["leader"] = "ctrl+x"
|
||||
return &Config{
|
||||
Keybinds: keybinds,
|
||||
}
|
||||
}
|
||||
|
||||
func ConfigToState(config *Config) *State {
|
||||
return &State{
|
||||
Theme: config.Theme,
|
||||
Provider: config.Provider,
|
||||
Model: config.Model,
|
||||
}
|
||||
}
|
||||
|
||||
func MergeState(state *State, config *Config) *Config {
|
||||
if config.Theme == "" {
|
||||
config.Theme = state.Theme
|
||||
}
|
||||
if config.Provider == "" {
|
||||
config.Provider = state.Provider
|
||||
}
|
||||
if config.Model == "" {
|
||||
config.Model = state.Model
|
||||
func MergeState(state *State, config *client.ConfigInfo) *client.ConfigInfo {
|
||||
if config.Theme == nil {
|
||||
config.Theme = &state.Theme
|
||||
}
|
||||
return config
|
||||
}
|
||||
@@ -79,19 +51,6 @@ func SaveState(filePath string, state *State) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadConfig reads a Config struct from the specified TOML file.
|
||||
// It returns a pointer to the Config struct and an error if any issues occur.
|
||||
func LoadConfig(filePath string) (*Config, error) {
|
||||
var config Config
|
||||
if _, err := toml.DecodeFile(filePath, &config); err != nil {
|
||||
if _, statErr := os.Stat(filePath); os.IsNotExist(statErr) {
|
||||
return nil, fmt.Errorf("config file not found at %s: %w", filePath, statErr)
|
||||
}
|
||||
return nil, fmt.Errorf("failed to decode TOML from file %s: %w", filePath, err)
|
||||
}
|
||||
return &config, nil
|
||||
}
|
||||
|
||||
// LoadState loads the state from the specified TOML file.
|
||||
// It returns a pointer to the State struct and an error if any issues occur.
|
||||
func LoadState(filePath string) (*State, error) {
|
||||
|
||||
Reference in New Issue
Block a user