feat: support toml config (wip)

main
sudo pacman -Syu 2023-02-25 22:07:37 +07:00
parent c44b098afb
commit 9277635a5c
5 changed files with 53 additions and 13 deletions

1
go.mod
View File

@ -3,6 +3,7 @@ module github.com/haunt98/dotfiles
go 1.18
require (
github.com/BurntSushi/toml v1.2.1
github.com/make-go-great/color-go v0.4.1
github.com/make-go-great/copy-go v0.9.0
github.com/make-go-great/diff-go v0.0.5

2
go.sum
View File

@ -1,3 +1,5 @@
github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=

View File

@ -7,13 +7,18 @@ import (
"os"
"path/filepath"
"time"
"github.com/BurntSushi/toml"
)
const (
configDirPath = "data"
configFileJSON = "data.json"
configFileTOML = "data.toml"
)
var ErrConfigNotFound = fmt.Errorf("config not found")
type Config interface {
Install() error
Update() error
@ -22,19 +27,19 @@ type Config interface {
Download() error
}
type configApps struct {
Apps map[string]App `json:"apps"`
type ConfigApps struct {
Apps map[string]App `json:"apps" toml:"apps"`
}
// Read from file
type App struct {
Paths []Path `json:"paths"`
Paths []Path `json:"paths" toml:"paths"`
}
type Path struct {
Internal string `json:"internal"`
External string `json:"external,omitempty"`
URL string `json:"url,omitempty"`
Internal string `json:"internal" toml:"internal"`
External string `json:"external,omitempty" toml:"external"`
URL string `json:"url,omitempty" toml:"url"`
}
// LoadConfig return config, configDemo
@ -44,7 +49,12 @@ func LoadConfig(path string) (*configReal, *configDemo, error) {
return cfgReal, cfgDemo, nil
}
return nil, nil, fmt.Errorf("failed to load config: %w", err)
cfgReal, cfgDemo, err = loadConfigTOML(path)
if err == nil {
return cfgReal, cfgDemo, nil
}
return nil, nil, ErrConfigNotFound
}
func loadConfigJSON(path string) (*configReal, *configDemo, error) {
@ -52,10 +62,10 @@ func loadConfigJSON(path string) (*configReal, *configDemo, error) {
bytes, err := os.ReadFile(configPathJSON)
if err != nil {
return nil, nil, fmt.Errorf("failed to read file%s: %w", configPathJSON, err)
return nil, nil, fmt.Errorf("os: failed to read file [%s]: %w", configPathJSON, err)
}
var cfgApps configApps
var cfgApps ConfigApps
if err = json.Unmarshal(bytes, &cfgApps); err != nil {
return nil, nil, fmt.Errorf("failed to unmarshal: %w", err)
}
@ -64,11 +74,38 @@ func loadConfigJSON(path string) (*configReal, *configDemo, error) {
httpClient: &http.Client{
Timeout: time.Second * 5,
},
configApps: cfgApps,
ConfigApps: cfgApps,
}
cfgDemo := configDemo{
configApps: cfgApps,
ConfigApps: cfgApps,
}
return &cfgReal, &cfgDemo, nil
}
func loadConfigTOML(path string) (*configReal, *configDemo, error) {
configPathTOML := filepath.Join(path, configDirPath, configFileTOML)
bytes, err := os.ReadFile(configPathTOML)
if err != nil {
return nil, nil, fmt.Errorf("os: failed to read file [%s]: %w", configPathTOML, err)
}
var cfgApps ConfigApps
if err := toml.Unmarshal(bytes, &cfgApps); err != nil {
return nil, nil, fmt.Errorf("toml: failed to decode: %w", err)
}
cfgReal := configReal{
httpClient: &http.Client{
Timeout: time.Second * 5,
},
ConfigApps: cfgApps,
}
cfgDemo := configDemo{
ConfigApps: cfgApps,
}
return &cfgReal, &cfgDemo, nil

View File

@ -3,7 +3,7 @@ package config
import "fmt"
type configDemo struct {
configApps
ConfigApps
}
var _ Config = (*configDemo)(nil)

View File

@ -13,7 +13,7 @@ import (
type configReal struct {
httpClient *http.Client
configApps
ConfigApps
}
var _ Config = (*configReal)(nil)