2021-04-22 09:36:11 +00:00
|
|
|
package config
|
|
|
|
|
2021-05-05 09:37:38 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-02-25 15:36:50 +00:00
|
|
|
"errors"
|
2021-05-05 09:37:38 +00:00
|
|
|
"fmt"
|
2022-10-24 14:57:44 +00:00
|
|
|
"net/http"
|
2021-05-05 09:37:38 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2022-10-24 14:57:44 +00:00
|
|
|
"time"
|
2023-02-25 15:07:37 +00:00
|
|
|
|
|
|
|
"github.com/BurntSushi/toml"
|
2021-05-05 09:37:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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"
|
2021-05-05 09:37:38 +00:00
|
|
|
)
|
|
|
|
|
2023-02-25 15:36:50 +00:00
|
|
|
var ErrConfigNotFound = errors.New("config not found")
|
2023-02-25 15:07:37 +00:00
|
|
|
|
2021-04-22 09:36:11 +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
|
2021-04-22 09:36:11 +00:00
|
|
|
}
|
2021-05-05 09:37:38 +00:00
|
|
|
|
2023-02-25 15:07:37 +00:00
|
|
|
type ConfigApps struct {
|
|
|
|
Apps map[string]App `json:"apps" toml:"apps"`
|
2021-05-05 09:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read from file
|
|
|
|
type App struct {
|
2023-02-25 15:07:37 +00:00
|
|
|
Paths []Path `json:"paths" toml:"paths"`
|
2021-05-05 09:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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"`
|
2021-05-05 09:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2021-05-05 09:37:38 +00:00
|
|
|
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)
|
2021-05-05 09:37:38 +00:00
|
|
|
}
|
|
|
|
|
2023-02-25 15:07:37 +00:00
|
|
|
var cfgApps ConfigApps
|
2021-05-05 09:37:38 +00:00
|
|
|
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)
|
2021-05-05 09:37:38 +00:00
|
|
|
}
|
|
|
|
|
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,
|
2021-05-05 09:37:38 +00:00
|
|
|
}
|
|
|
|
|
2023-02-25 15:44:26 +00:00
|
|
|
cfgDemo := ConfigDemo{
|
2023-02-25 15:07:37 +00:00
|
|
|
ConfigApps: cfgApps,
|
2021-05-05 09:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &cfgReal, &cfgDemo, nil
|
|
|
|
}
|