2021-04-22 09:36:11 +00:00
|
|
|
package config
|
|
|
|
|
2021-05-05 09:37:38 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"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"
|
2021-05-05 09:37:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-02-25 14:55:41 +00:00
|
|
|
configDirPath = "data"
|
|
|
|
configFileJSON = "data.json"
|
2021-05-05 09:37:38 +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
|
2021-04-22 09:36:11 +00:00
|
|
|
}
|
2021-05-05 09:37:38 +00:00
|
|
|
|
|
|
|
type configApps struct {
|
|
|
|
Apps map[string]App `json:"apps"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read from file
|
|
|
|
type App struct {
|
|
|
|
Paths []Path `json:"paths"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Path struct {
|
|
|
|
Internal string `json:"internal"`
|
2022-10-24 14:57:44 +00:00
|
|
|
External string `json:"external,omitempty"`
|
|
|
|
URL string `json:"url,omitempty"`
|
2021-05-05 09:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoadConfig return config, configDemo
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil, fmt.Errorf("failed to load config: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadConfigJSON(path string) (*configReal, *configDemo, error) {
|
|
|
|
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 14:55:41 +00:00
|
|
|
return nil, nil, fmt.Errorf("failed to read file%s: %w", configPathJSON, err)
|
2021-05-05 09:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var cfgApps configApps
|
|
|
|
if err = json.Unmarshal(bytes, &cfgApps); err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("failed to unmarshal: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cfgReal := configReal{
|
2022-10-24 14:57:44 +00:00
|
|
|
httpClient: &http.Client{
|
|
|
|
Timeout: time.Second * 5,
|
|
|
|
},
|
2021-05-05 09:37:38 +00:00
|
|
|
configApps: cfgApps,
|
|
|
|
}
|
|
|
|
|
|
|
|
cfgDemo := configDemo{
|
|
|
|
configApps: cfgApps,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &cfgReal, &cfgDemo, nil
|
|
|
|
}
|