feat: switch config json to toml

main
sudo pacman -Syu 2023-02-25 22:44:26 +07:00
parent 1b322b7c17
commit 962a4523b9
7 changed files with 81 additions and 109 deletions

View File

@ -1,84 +0,0 @@
{
"apps": {
"nvim": {
"paths": [
{
"internal": "data/nvim/init.lua",
"external": "~/.config/nvim/init.lua"
}
]
},
"kitty": {
"paths": [
{
"internal": "data/kitty/kitty.conf",
"external": "~/.config/kitty/kitty.conf"
},
{
"internal": "data/kitty/mocha.conf",
"external": "~/.config/kitty/mocha.conf"
},
{
"internal": "data/kitty/kitty-dark.icns",
"external": "~/.config/kitty/kitty.app.icns"
},
{
"internal": "data/kitty/mocha.conf",
"url": "https://raw.githubusercontent.com/catppuccin/kitty/main/mocha.conf"
},
{
"internal": "data/kitty/kitty-dark.icns",
"url": "https://github.com/DinkDonk/kitty-icon/blob/main/kitty-dark.icns?raw=true"
},
{
"internal": "data/kitty/kitty-light.icns",
"url": "https://github.com/DinkDonk/kitty-icon/blob/main/kitty-light.icns?raw=true"
}
]
},
"alacritty": {
"paths": [
{
"internal": "data/alacritty/alacritty.yml",
"external": "~/.config/alacritty/alacritty.yml"
},
{
"internal": "data/alacritty/catppuccin-mocha.yml",
"external": "~/.config/alacritty/catppuccin-mocha.yml"
},
{
"internal": "data/alacritty/catppuccin-mocha.yml",
"url": "https://raw.githubusercontent.com/catppuccin/alacritty/main/catppuccin-mocha.yml"
}
]
},
"bat": {
"paths": [
{
"internal": "data/bat",
"external": "~/.config/bat"
},
{
"internal": "data/bat/themes/Catppuccin-mocha.tmTheme",
"url": "https://raw.githubusercontent.com/catppuccin/bat/main/Catppuccin-mocha.tmTheme"
}
]
},
"fd": {
"paths": [
{
"internal": "data/fd/.ignore",
"external": "~/.ignore"
}
]
},
"zsh": {
"paths": [
{
"internal": "data/zsh/extra-zshrc",
"external": "~/.config/zsh/extra-zshrc"
}
]
}
}
}

55
data/data.toml Normal file
View File

@ -0,0 +1,55 @@
[[apps.nvim.paths]]
internal = "data/nvim/init.lua"
external = "~/.config/nvim/init.lua"
[[apps.kitty.paths]]
internal = "data/kitty/kitty.conf"
external = "~/.config/kitty/kitty.conf"
[[apps.kitty.paths]]
internal = "data/kitty/mocha.conf"
external = "~/.config/kitty/mocha.conf"
[[apps.kitty.paths]]
internal = "data/kitty/kitty-dark.icns"
external = "~/.config/kitty/kitty.app.icns"
[[apps.kitty.paths]]
internal = "data/kitty/mocha.conf"
url = "https://raw.githubusercontent.com/catppuccin/kitty/main/mocha.conf"
[[apps.kitty.paths]]
internal = "data/kitty/kitty-dark.icns"
url = "https://github.com/DinkDonk/kitty-icon/blob/main/kitty-dark.icns?raw=true"
[[apps.kitty.paths]]
internal = "data/kitty/kitty-light.icns"
url = "https://github.com/DinkDonk/kitty-icon/blob/main/kitty-light.icns?raw=true"
[[apps.alacritty.paths]]
internal = "data/alacritty/alacritty.yml"
external = "~/.config/alacritty/alacritty.yml"
[[apps.alacritty.paths]]
internal = "data/alacritty/catppuccin-mocha.yml"
external = "~/.config/alacritty/catppuccin-mocha.yml"
[[apps.alacritty.paths]]
internal = "data/alacritty/catppuccin-mocha.yml"
url = "https://raw.githubusercontent.com/catppuccin/alacritty/main/catppuccin-mocha.yml"
[[apps.bat.paths]]
internal = "data/bat"
external = "~/.config/bat"
[[apps.bat.paths]]
internal = "data/bat/themes/Catppuccin-mocha.tmTheme"
url = "https://raw.githubusercontent.com/catppuccin/bat/main/Catppuccin-mocha.tmTheme"
[[apps.fd.paths]]
internal = "data/fd/.ignore"
external = "~/.ignore"
[[apps.zsh.paths]]
internal = "data/zsh/extra-zshrc"
external = "~/.config/zsh/extra-zshrc"

View File

@ -101,12 +101,13 @@ func (a *action) runValidate(c *cli.Context) error {
func (a *action) loadConfig(c *cli.Context, command string) (config.Config, error) { func (a *action) loadConfig(c *cli.Context, command string) (config.Config, error) {
a.getFlags(c) a.getFlags(c)
a.log("start %s with flags %+v\n", command, a.flags) a.log("Start command [%s] with flags [%+v]\n", command, a.flags)
cfgReal, cfgDemo, err := config.LoadConfig(currentDir) cfgReal, cfgDemo, err := config.LoadConfig(currentDir)
if err != nil { if err != nil {
return nil, fmt.Errorf("config: failed to load: %w", err) return nil, fmt.Errorf("config: failed to load: %w", err)
} }
a.log("Config apps [%+v]\n", cfgReal.Apps)
if a.flags.dryRun { if a.flags.dryRun {
return cfgDemo, nil return cfgDemo, nil

View File

@ -167,7 +167,7 @@ func NewApp() *App {
func (a *App) Run() { func (a *App) Run() {
// Prevent running at runtime // Prevent running at runtime
if _, ok := denyOSes[runtime.GOOS]; ok { if _, ok := denyOSes[runtime.GOOS]; ok {
color.PrintAppError(appName, fmt.Sprintf("OS %s is not supported right now", runtime.GOOS)) color.PrintAppError(appName, fmt.Sprintf("OS [%s] is not supported right now", runtime.GOOS))
return return
} }

View File

@ -45,7 +45,7 @@ type Path struct {
} }
// LoadConfig return config, configDemo // LoadConfig return config, configDemo
func LoadConfig(path string) (*configReal, *configDemo, error) { func LoadConfig(path string) (*ConfigReal, *ConfigDemo, error) {
cfgReal, cfgDemo, err := loadConfigJSON(path) cfgReal, cfgDemo, err := loadConfigJSON(path)
if err == nil { if err == nil {
return cfgReal, cfgDemo, nil return cfgReal, cfgDemo, nil
@ -59,7 +59,7 @@ func LoadConfig(path string) (*configReal, *configDemo, error) {
return nil, nil, ErrConfigNotFound return nil, nil, ErrConfigNotFound
} }
func loadConfigJSON(path string) (*configReal, *configDemo, error) { func loadConfigJSON(path string) (*ConfigReal, *ConfigDemo, error) {
configPathJSON := filepath.Join(path, configDirPath, configFileJSON) configPathJSON := filepath.Join(path, configDirPath, configFileJSON)
bytes, err := os.ReadFile(configPathJSON) bytes, err := os.ReadFile(configPathJSON)
@ -72,21 +72,21 @@ func loadConfigJSON(path string) (*configReal, *configDemo, error) {
return nil, nil, fmt.Errorf("json: failed to unmarshal: %w", err) return nil, nil, fmt.Errorf("json: failed to unmarshal: %w", err)
} }
cfgReal := configReal{ cfgReal := ConfigReal{
httpClient: &http.Client{ httpClient: &http.Client{
Timeout: time.Second * 5, Timeout: time.Second * 5,
}, },
ConfigApps: cfgApps, ConfigApps: cfgApps,
} }
cfgDemo := configDemo{ cfgDemo := ConfigDemo{
ConfigApps: cfgApps, ConfigApps: cfgApps,
} }
return &cfgReal, &cfgDemo, nil return &cfgReal, &cfgDemo, nil
} }
func loadConfigTOML(path string) (*configReal, *configDemo, error) { func loadConfigTOML(path string) (*ConfigReal, *ConfigDemo, error) {
configPathTOML := filepath.Join(path, configDirPath, configFileTOML) configPathTOML := filepath.Join(path, configDirPath, configFileTOML)
bytes, err := os.ReadFile(configPathTOML) bytes, err := os.ReadFile(configPathTOML)
@ -99,14 +99,14 @@ func loadConfigTOML(path string) (*configReal, *configDemo, error) {
return nil, nil, fmt.Errorf("toml: failed to decode: %w", err) return nil, nil, fmt.Errorf("toml: failed to decode: %w", err)
} }
cfgReal := configReal{ cfgReal := ConfigReal{
httpClient: &http.Client{ httpClient: &http.Client{
Timeout: time.Second * 5, Timeout: time.Second * 5,
}, },
ConfigApps: cfgApps, ConfigApps: cfgApps,
} }
cfgDemo := configDemo{ cfgDemo := ConfigDemo{
ConfigApps: cfgApps, ConfigApps: cfgApps,
} }

View File

@ -2,13 +2,13 @@ package config
import "fmt" import "fmt"
type configDemo struct { type ConfigDemo struct {
ConfigApps ConfigApps
} }
var _ Config = (*configDemo)(nil) var _ Config = (*ConfigDemo)(nil)
func (c *configDemo) Install() error { func (c *ConfigDemo) Install() error {
for _, app := range c.Apps { for _, app := range c.Apps {
for _, p := range app.Paths { for _, p := range app.Paths {
if p.External == "" { if p.External == "" {
@ -22,7 +22,7 @@ func (c *configDemo) Install() error {
return nil return nil
} }
func (c *configDemo) Update() error { func (c *ConfigDemo) Update() error {
for _, app := range c.Apps { for _, app := range c.Apps {
for _, p := range app.Paths { for _, p := range app.Paths {
if p.External == "" { if p.External == "" {
@ -36,7 +36,7 @@ func (c *configDemo) Update() error {
return nil return nil
} }
func (c *configDemo) Download() error { func (c *ConfigDemo) Download() error {
for _, app := range c.Apps { for _, app := range c.Apps {
for _, p := range app.Paths { for _, p := range app.Paths {
if p.URL == "" { if p.URL == "" {
@ -50,7 +50,7 @@ func (c *configDemo) Download() error {
return nil return nil
} }
func (c *configDemo) Clean() error { func (c *ConfigDemo) Clean() error {
unusedDirs, err := getUnusedDirs(c.Apps) unusedDirs, err := getUnusedDirs(c.Apps)
if err != nil { if err != nil {
return err return err
@ -63,10 +63,10 @@ func (c *configDemo) Clean() error {
return nil return nil
} }
func (c *configDemo) Diff() error { func (c *ConfigDemo) Diff() error {
return nil return nil
} }
func (c *configDemo) Validate() error { func (c *ConfigDemo) Validate() error {
return nil return nil
} }

View File

@ -14,15 +14,15 @@ import (
var ErrConfigInvalid = errors.New("config invalid") var ErrConfigInvalid = errors.New("config invalid")
type configReal struct { type ConfigReal struct {
httpClient *http.Client httpClient *http.Client
ConfigApps ConfigApps
} }
var _ Config = (*configReal)(nil) var _ Config = (*ConfigReal)(nil)
// Install internal -> external // Install internal -> external
func (c *configReal) Install() error { func (c *ConfigReal) Install() error {
for _, app := range c.Apps { for _, app := range c.Apps {
for _, p := range app.Paths { for _, p := range app.Paths {
if p.External == "" { if p.External == "" {
@ -39,7 +39,7 @@ func (c *configReal) Install() error {
} }
// Update external -> internal // Update external -> internal
func (c *configReal) Update() error { func (c *ConfigReal) Update() error {
for _, app := range c.Apps { for _, app := range c.Apps {
for _, p := range app.Paths { for _, p := range app.Paths {
if p.External == "" { if p.External == "" {
@ -55,7 +55,7 @@ func (c *configReal) Update() error {
return nil return nil
} }
func (c *configReal) Download() error { func (c *ConfigReal) Download() error {
for _, app := range c.Apps { for _, app := range c.Apps {
for _, p := range app.Paths { for _, p := range app.Paths {
if p.URL == "" { if p.URL == "" {
@ -91,7 +91,7 @@ func (c *configReal) Download() error {
} }
// Clean remove unused config inside config dir // Clean remove unused config inside config dir
func (c *configReal) Clean() error { func (c *ConfigReal) Clean() error {
unusedDirs, err := getUnusedDirs(c.Apps) unusedDirs, err := getUnusedDirs(c.Apps)
if err != nil { if err != nil {
return err return err
@ -133,7 +133,7 @@ func getUnusedDirs(apps map[string]App) (map[string]struct{}, error) {
return unusedDirs, nil return unusedDirs, nil
} }
func (c *configReal) Diff() error { func (c *ConfigReal) Diff() error {
for _, app := range c.Apps { for _, app := range c.Apps {
for _, p := range app.Paths { for _, p := range app.Paths {
if p.External == "" { if p.External == "" {
@ -149,7 +149,7 @@ func (c *configReal) Diff() error {
return nil return nil
} }
func (c *configReal) Validate() error { func (c *ConfigReal) Validate() error {
for _, app := range c.Apps { for _, app := range c.Apps {
for _, p := range app.Paths { for _, p := range app.Paths {
if p.Internal == "" { if p.Internal == "" {