2024-06-25 12:39:39 +01:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/utilities"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2024-08-18 21:13:08 +01:00
|
|
|
configFileName string = "config.json"
|
|
|
|
|
|
|
|
defaultHTTPTimeout int = 5
|
|
|
|
defaultHTTPMediaTimeout int = 30
|
|
|
|
defaultLineWrapMaxWidth int = 80
|
2024-06-25 12:39:39 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
CredentialsFile string `json:"credentialsFile"`
|
|
|
|
CacheDirectory string `json:"cacheDirectory"`
|
|
|
|
LineWrapMaxWidth int `json:"lineWrapMaxWidth"`
|
|
|
|
HTTP HTTPConfig `json:"http"`
|
|
|
|
Integrations Integrations `json:"integrations"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type HTTPConfig struct {
|
|
|
|
Timeout int `json:"timeout"`
|
|
|
|
MediaTimeout int `json:"mediaTimeout"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Integrations struct {
|
|
|
|
Browser string `json:"browser"`
|
|
|
|
Editor string `json:"editor"`
|
|
|
|
Pager string `json:"pager"`
|
|
|
|
ImageViewer string `json:"imageViewer"`
|
|
|
|
VideoPlayer string `json:"videoPlayer"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewConfigFromFile(configDir string) (*Config, error) {
|
2024-08-18 21:13:08 +01:00
|
|
|
path, err := configPath(configDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to calculate the path to your config file: %w", err)
|
|
|
|
}
|
2024-06-25 12:39:39 +01:00
|
|
|
|
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to open %s: %w", path, err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
var config Config
|
|
|
|
|
|
|
|
if err := json.NewDecoder(file).Decode(&config); err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to decode the JSON data: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func FileExists(configDir string) (bool, error) {
|
2024-08-18 21:13:08 +01:00
|
|
|
path, err := configPath(configDir)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("unable to calculate the path to your config file: %w", err)
|
|
|
|
}
|
2024-06-25 12:39:39 +01:00
|
|
|
|
|
|
|
return utilities.FileExists(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func SaveDefaultConfigToFile(configDir string) error {
|
2024-08-18 21:13:08 +01:00
|
|
|
path, err := configPath(configDir)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to calculate the path to your config file: %w", err)
|
|
|
|
}
|
2024-06-25 12:39:39 +01:00
|
|
|
|
|
|
|
file, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to create the file at %s: %w", path, err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
2024-06-29 16:02:19 +01:00
|
|
|
config := defaultConfig()
|
|
|
|
|
2024-08-18 21:13:08 +01:00
|
|
|
credentialsFilePath, err := defaultCredentialsConfigFile(configDir)
|
2024-06-29 16:02:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to calculate the path to the credentials file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config.CredentialsFile = credentialsFilePath
|
|
|
|
|
2024-06-25 12:39:39 +01:00
|
|
|
encoder := json.NewEncoder(file)
|
|
|
|
encoder.SetIndent("", " ")
|
|
|
|
|
|
|
|
if err := encoder.Encode(config); err != nil {
|
|
|
|
return fmt.Errorf("unable to save the JSON data to the config file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-18 21:13:08 +01:00
|
|
|
func configPath(configDir string) (string, error) {
|
|
|
|
configDir, err := utilities.CalculateConfigDir(configDir)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("unable to get the config directory: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return filepath.Join(configDir, configFileName), nil
|
2024-06-25 12:39:39 +01:00
|
|
|
}
|
|
|
|
|
2024-06-29 16:02:19 +01:00
|
|
|
func defaultConfig() Config {
|
2024-06-25 12:39:39 +01:00
|
|
|
return Config{
|
2024-06-29 16:02:19 +01:00
|
|
|
CredentialsFile: "",
|
2024-06-25 12:39:39 +01:00
|
|
|
CacheDirectory: "",
|
|
|
|
HTTP: HTTPConfig{
|
2024-08-18 21:13:08 +01:00
|
|
|
Timeout: defaultHTTPTimeout,
|
|
|
|
MediaTimeout: defaultHTTPMediaTimeout,
|
2024-06-25 12:39:39 +01:00
|
|
|
},
|
2024-08-18 21:13:08 +01:00
|
|
|
LineWrapMaxWidth: defaultLineWrapMaxWidth,
|
2024-06-25 12:39:39 +01:00
|
|
|
Integrations: Integrations{
|
|
|
|
Browser: "",
|
|
|
|
Editor: "",
|
|
|
|
Pager: "",
|
|
|
|
ImageViewer: "",
|
|
|
|
VideoPlayer: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|