dotfiles/internal/config/config.go

115 lines
2.3 KiB
Go
Raw Normal View History

package config
import (
"encoding/json"
2023-02-25 15:36:50 +00:00
"errors"
"fmt"
2022-10-24 14:57:44 +00:00
"net/http"
"os"
"path/filepath"
2022-10-24 14:57:44 +00:00
"time"
2023-02-25 15:07:37 +00:00
"github.com/BurntSushi/toml"
)
const (
2023-02-25 14:55:41 +00:00
configDirPath = "data"
configFileJSON = "data.json"
2023-02-25 15:07:37 +00:00
configFileTOML = "data.toml"
)
2023-02-25 15:36:50 +00:00
var ErrConfigNotFound = errors.New("config not found")
2023-02-25 15:07:37 +00:00
type Config interface {
Install() error
Update() error
Clean() error
2022-05-31 09:27:06 +00:00
Diff() error
2022-10-24 14:57:44 +00:00
Download() error
2023-02-25 15:30:20 +00:00
Validate() error
}
2023-02-25 15:07:37 +00:00
type ConfigApps struct {
Apps map[string]App `json:"apps" toml:"apps"`
}
// Read from file
type App struct {
2023-02-25 15:07:37 +00:00
Paths []Path `json:"paths" toml:"paths"`
}
type Path struct {
2023-02-25 15:07:37 +00:00
Internal string `json:"internal" toml:"internal"`
External string `json:"external,omitempty" toml:"external"`
URL string `json:"url,omitempty" toml:"url"`
}
// LoadConfig return config, configDemo
2023-02-25 15:44:26 +00:00
func LoadConfig(path string) (*ConfigReal, *ConfigDemo, error) {
2023-02-25 14:55:41 +00:00
cfgReal, cfgDemo, err := loadConfigJSON(path)
if err == nil {
return cfgReal, cfgDemo, nil
}
2023-02-25 15:07:37 +00:00
cfgReal, cfgDemo, err = loadConfigTOML(path)
if err == nil {
return cfgReal, cfgDemo, nil
}
return nil, nil, ErrConfigNotFound
2023-02-25 14:55:41 +00:00
}
2023-02-25 15:44:26 +00:00
func loadConfigJSON(path string) (*ConfigReal, *ConfigDemo, error) {
2023-02-25 14:55:41 +00:00
configPathJSON := filepath.Join(path, configDirPath, configFileJSON)
bytes, err := os.ReadFile(configPathJSON)
if err != nil {
2023-02-25 15:07:37 +00:00
return nil, nil, fmt.Errorf("os: failed to read file [%s]: %w", configPathJSON, err)
}
2023-02-25 15:07:37 +00:00
var cfgApps ConfigApps
if err = json.Unmarshal(bytes, &cfgApps); err != nil {
2023-02-25 15:36:50 +00:00
return nil, nil, fmt.Errorf("json: failed to unmarshal: %w", err)
}
2023-02-25 15:44:26 +00:00
cfgReal := ConfigReal{
2022-10-24 14:57:44 +00:00
httpClient: &http.Client{
Timeout: time.Second * 5,
},
2023-02-25 15:07:37 +00:00
ConfigApps: cfgApps,
}
2023-02-25 15:44:26 +00:00
cfgDemo := ConfigDemo{
2023-02-25 15:07:37 +00:00
ConfigApps: cfgApps,
}
return &cfgReal, &cfgDemo, nil
}
2023-02-25 15:44:26 +00:00
func loadConfigTOML(path string) (*ConfigReal, *ConfigDemo, error) {
2023-02-25 15:07:37 +00:00
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)
}
2023-02-25 15:44:26 +00:00
cfgReal := ConfigReal{
2023-02-25 15:07:37 +00:00
httpClient: &http.Client{
Timeout: time.Second * 5,
},
ConfigApps: cfgApps,
}
2023-02-25 15:44:26 +00:00
cfgDemo := ConfigDemo{
2023-02-25 15:07:37 +00:00
ConfigApps: cfgApps,
}
return &cfgReal, &cfgDemo, nil
}