feat: toml config support (#9)
## What's new - TOML config support - add config init command to justfile https://github.com/versenilvis/IRIS/pull/9/commits/a5bb6c23d74ca73385b3590d2eb275ce6429906b - add copy binary file to local bin on reload https://github.com/versenilvis/IRIS/pull/9/commits/4da3acdbc58c28f20fa0021d15fec82823ba392b - init config on setup https://github.com/versenilvis/IRIS/pull/9/commits/fb48b6bb832f38e24fab9211d92858bab9a58a07 ## Fix - add lsof to support macos because /proc filesystem is Linux-specific and does not exist on macOS 0439d829573864fcf902fc4127f7d45384bfab01 - git push doesnt suggest current branch, fix https://github.com/versenilvis/IRIS/pull/9/commits/b498532b401b764a4a1a2c70b3cb44b7caf72320 - use timeout context for lsof command in reload handler https://github.com/versenilvis/IRIS/pull/9/commits/1a2baee951e6538a729da12aedfe53960e51102d
This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
type Duration time.Duration
|
||||
|
||||
var (
|
||||
_ encoding.TextUnmarshaler = (*Duration)(nil)
|
||||
_ encoding.TextMarshaler = (*Duration)(nil)
|
||||
)
|
||||
|
||||
func (d *Duration) UnmarshalText(text []byte) error {
|
||||
dur, err := time.ParseDuration(string(text))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*d = Duration(dur)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d Duration) MarshalText() ([]byte, error) {
|
||||
return []byte(time.Duration(d).String()), nil
|
||||
}
|
||||
|
||||
type CoreConfig struct {
|
||||
Version int `toml:"version"`
|
||||
Shell string `toml:"shell"`
|
||||
Mode string `toml:"mode"`
|
||||
Debug bool `toml:"debug"`
|
||||
}
|
||||
|
||||
type UIConfig struct {
|
||||
GhostText bool `toml:"ghost-text"`
|
||||
MaxSuggestions int `toml:"max-suggestions"`
|
||||
MaxHeight int `toml:"max-height"`
|
||||
}
|
||||
|
||||
type GitConfig struct {
|
||||
FilterActiveBranch bool `toml:"filter-active-branch"`
|
||||
DeduplicateBranches bool `toml:"deduplicate-branches"`
|
||||
}
|
||||
|
||||
type UpdaterConfig struct {
|
||||
CheckOnStartup bool `toml:"check-on-startup"`
|
||||
Channel string `toml:"channel"`
|
||||
CheckInterval Duration `toml:"check-interval"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Core CoreConfig `toml:"core"`
|
||||
UI UIConfig `toml:"ui"`
|
||||
Git GitConfig `toml:"git"`
|
||||
Updater UpdaterConfig `toml:"updater"`
|
||||
}
|
||||
|
||||
var (
|
||||
activeConfig *Config
|
||||
once sync.Once
|
||||
)
|
||||
|
||||
func Get() *Config {
|
||||
once.Do(func() {
|
||||
if activeConfig == nil {
|
||||
activeConfig = DefaultConfig()
|
||||
}
|
||||
})
|
||||
return activeConfig
|
||||
}
|
||||
|
||||
func Init(cfg *Config) {
|
||||
activeConfig = cfg
|
||||
once.Do(func() {})
|
||||
}
|
||||
|
||||
func Load() (*Config, error) {
|
||||
cfg := DefaultConfig()
|
||||
|
||||
path, err := ConfigPath()
|
||||
if err == nil {
|
||||
if _, statErr := os.Stat(path); statErr == nil {
|
||||
data, readErr := os.ReadFile(path)
|
||||
if readErr != nil {
|
||||
return cfg, fmt.Errorf("config: read %s: %w", path, readErr)
|
||||
}
|
||||
if _, decodeErr := toml.Decode(string(data), cfg); decodeErr != nil {
|
||||
return cfg, fmt.Errorf("config: parse %s: %w", path, decodeErr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
applyEnv(cfg)
|
||||
|
||||
if err := validate(cfg); err != nil {
|
||||
return cfg, fmt.Errorf("config: invalid value: %w", err)
|
||||
}
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func Save(cfg *Config) error {
|
||||
path, err := ConfigPath()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = os.MkdirAll(filepath.Dir(path), 0755)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
enc := toml.NewEncoder(file)
|
||||
if err := enc.Encode(cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func validate(cfg *Config) error {
|
||||
validModes := map[string]bool{"last": true, "spec": true, "history": true}
|
||||
if cfg.Core.Mode != "" && !validModes[cfg.Core.Mode] {
|
||||
return fmt.Errorf("core.mode: invalid value %q (want: last|spec|history)", cfg.Core.Mode)
|
||||
}
|
||||
|
||||
validShells := map[string]bool{"": true, "bash": true, "zsh": true, "fish": true}
|
||||
if !validShells[cfg.Core.Shell] {
|
||||
return fmt.Errorf("core.shell: invalid value %q (want: bash|zsh|fish)", cfg.Core.Shell)
|
||||
}
|
||||
|
||||
validChannels := map[string]bool{"stable": true, "nightly": true}
|
||||
if !validChannels[cfg.Updater.Channel] {
|
||||
return fmt.Errorf("updater.channel: invalid value %q (want: stable|nightly)", cfg.Updater.Channel)
|
||||
}
|
||||
|
||||
if cfg.UI.MaxSuggestions < 1 || cfg.UI.MaxSuggestions > 500 {
|
||||
return fmt.Errorf("ui.max-suggestions: must be between 1 and 500")
|
||||
}
|
||||
|
||||
if cfg.UI.MaxHeight < 3 || cfg.UI.MaxHeight > 50 {
|
||||
return fmt.Errorf("ui.max-height: must be between 3 and 50")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package config
|
||||
|
||||
import "time"
|
||||
|
||||
func DefaultConfig() *Config {
|
||||
return &Config{
|
||||
Core: CoreConfig{
|
||||
Version: 1,
|
||||
Shell: "",
|
||||
Mode: "last",
|
||||
Debug: false,
|
||||
},
|
||||
UI: UIConfig{
|
||||
GhostText: true,
|
||||
MaxSuggestions: 100,
|
||||
MaxHeight: 15,
|
||||
},
|
||||
Git: GitConfig{
|
||||
FilterActiveBranch: true,
|
||||
DeduplicateBranches: true,
|
||||
},
|
||||
Updater: UpdaterConfig{
|
||||
CheckOnStartup: true,
|
||||
Channel: "stable",
|
||||
CheckInterval: Duration(24 * time.Hour),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func DefaultState() *State {
|
||||
return &State{
|
||||
LastMode: "spec",
|
||||
Updater: UpdaterState{
|
||||
LastCheckTime: time.Time{},
|
||||
SeenVersion: "",
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
func applyEnv(cfg *Config) {
|
||||
if val := os.Getenv("IRIS_CORE_DEBUG"); val != "" {
|
||||
if b, err := strconv.ParseBool(val); err == nil {
|
||||
cfg.Core.Debug = b
|
||||
}
|
||||
}
|
||||
if val := os.Getenv("IRIS_CORE_SHELL"); val != "" {
|
||||
cfg.Core.Shell = val
|
||||
}
|
||||
if val := os.Getenv("IRIS_CORE_MODE"); val != "" {
|
||||
cfg.Core.Mode = val
|
||||
}
|
||||
if val := os.Getenv("IRIS_UI_GHOST_TEXT"); val != "" {
|
||||
if b, err := strconv.ParseBool(val); err == nil {
|
||||
cfg.UI.GhostText = b
|
||||
}
|
||||
}
|
||||
if val := os.Getenv("IRIS_UI_MAX_SUGGESTIONS"); val != "" {
|
||||
if i, err := strconv.Atoi(val); err == nil {
|
||||
cfg.UI.MaxSuggestions = i
|
||||
}
|
||||
}
|
||||
if val := os.Getenv("IRIS_UI_MAX_HEIGHT"); val != "" {
|
||||
if i, err := strconv.Atoi(val); err == nil {
|
||||
cfg.UI.MaxHeight = i
|
||||
}
|
||||
}
|
||||
if val := os.Getenv("IRIS_UPDATER_CHANNEL"); val != "" {
|
||||
cfg.Updater.Channel = val
|
||||
}
|
||||
if val := os.Getenv("IRIS_UPDATER_INTERVAL"); val != "" {
|
||||
if dur, err := time.ParseDuration(val); err == nil {
|
||||
cfg.Updater.CheckInterval = Duration(dur)
|
||||
}
|
||||
}
|
||||
if val := os.Getenv("IRIS_UPDATER_CHECK_ON_STARTUP"); val != "" {
|
||||
if b, err := strconv.ParseBool(val); err == nil {
|
||||
cfg.Updater.CheckOnStartup = b
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
type legacyState struct {
|
||||
Mode string `json:"mode"`
|
||||
}
|
||||
|
||||
type legacyUpdateState struct {
|
||||
SeenVersion string `json:"seen_version"`
|
||||
LastCheck int64 `json:"last_check"`
|
||||
}
|
||||
|
||||
func MigrateFromLegacyJSON() error {
|
||||
statePath, err := StatePath()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, statErr := os.Stat(statePath); statErr == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
home, homeErr := os.UserHomeDir()
|
||||
if homeErr != nil {
|
||||
return homeErr
|
||||
}
|
||||
|
||||
legacyDir := filepath.Join(home, ".iris")
|
||||
legacyStatePath := filepath.Join(legacyDir, "state.json")
|
||||
legacyUpdatePath := filepath.Join(legacyDir, "update_state.json")
|
||||
|
||||
hasLegacyState := false
|
||||
if _, err := os.Stat(legacyStatePath); err == nil {
|
||||
hasLegacyState = true
|
||||
}
|
||||
hasLegacyUpdate := false
|
||||
if _, err := os.Stat(legacyUpdatePath); err == nil {
|
||||
hasLegacyUpdate = true
|
||||
}
|
||||
|
||||
if !hasLegacyState && !hasLegacyUpdate {
|
||||
return nil
|
||||
}
|
||||
|
||||
state := DefaultState()
|
||||
|
||||
if hasLegacyState {
|
||||
data, err := os.ReadFile(legacyStatePath)
|
||||
if err == nil {
|
||||
var ls legacyState
|
||||
if err := json.Unmarshal(data, &ls); err == nil {
|
||||
if ls.Mode == "history" || ls.Mode == "spec" {
|
||||
state.LastMode = ls.Mode
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if hasLegacyUpdate {
|
||||
data, err := os.ReadFile(legacyUpdatePath)
|
||||
if err == nil {
|
||||
var lu legacyUpdateState
|
||||
if err := json.Unmarshal(data, &lu); err == nil {
|
||||
state.Updater.SeenVersion = lu.SeenVersion
|
||||
if lu.LastCheck > 0 {
|
||||
state.Updater.LastCheckTime = time.Unix(lu.LastCheck, 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err := SaveState(state); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if hasLegacyState {
|
||||
_ = os.Rename(legacyStatePath, legacyStatePath+".bak")
|
||||
}
|
||||
if hasLegacyUpdate {
|
||||
_ = os.Rename(legacyUpdatePath, legacyUpdatePath+".bak")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func ConfigPath() (string, error) {
|
||||
dir, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return filepath.Join(dir, "iris", "config.toml"), nil
|
||||
}
|
||||
|
||||
func StatePath() (string, error) {
|
||||
dataHome := os.Getenv("XDG_DATA_HOME")
|
||||
if dataHome == "" {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
dataHome = filepath.Join(home, ".local", "share")
|
||||
}
|
||||
return filepath.Join(dataHome, "iris", "state.toml"), nil
|
||||
}
|
||||
|
||||
func CachePath() (string, error) {
|
||||
dir, err := os.UserCacheDir()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return filepath.Join(dir, "iris"), nil
|
||||
}
|
||||
|
||||
func CrashDir() (string, error) {
|
||||
cache, err := CachePath()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return filepath.Join(cache, "crashes"), nil
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
type UpdaterState struct {
|
||||
LastCheckTime time.Time `toml:"last-check-time"`
|
||||
SeenVersion string `toml:"seen-version"`
|
||||
}
|
||||
|
||||
type State struct {
|
||||
LastMode string `toml:"last-mode"`
|
||||
Updater UpdaterState `toml:"updater"`
|
||||
}
|
||||
|
||||
func LoadState() *State {
|
||||
s := DefaultState()
|
||||
|
||||
path, err := StatePath()
|
||||
if err != nil {
|
||||
return s
|
||||
}
|
||||
|
||||
if _, statErr := os.Stat(path); os.IsNotExist(statErr) {
|
||||
return s
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return s
|
||||
}
|
||||
|
||||
if _, err := toml.Decode(string(data), s); err != nil {
|
||||
return s
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
func SaveState(s *State) error {
|
||||
path, err := StatePath()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = os.MkdirAll(filepath.Dir(path), 0755)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
enc := toml.NewEncoder(file)
|
||||
if err := enc.Encode(s); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user