Dan Anglin
77d782af21
- disable some linters - all executors now uses the config value - some variable renaming based on golangci-lint feedback - add path to the credentials file to the config so users can define the path to the credentials file
107 lines
2.5 KiB
Go
107 lines
2.5 KiB
Go
package config
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/utilities"
|
|
)
|
|
|
|
const (
|
|
configFileName = "config.json"
|
|
)
|
|
|
|
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) {
|
|
path := configFile(configDir)
|
|
|
|
fileExists, err := utilities.FileExists(path)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("unable to check if the config file exists: %w", err)
|
|
}
|
|
|
|
if !fileExists {
|
|
if err := saveDefaultConfigToFile(configDir, path); err != nil {
|
|
return nil, fmt.Errorf("unable to save the default config to file: %w", err)
|
|
}
|
|
}
|
|
|
|
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 configFile(configDir string) string {
|
|
return filepath.Join(utilities.CalculateConfigDir(configDir), configFileName)
|
|
}
|
|
|
|
func saveDefaultConfigToFile(configDir, configPath string) error {
|
|
file, err := os.Create(configPath)
|
|
if err != nil {
|
|
return fmt.Errorf("unable to create the file at %s: %w", configPath, err)
|
|
}
|
|
defer file.Close()
|
|
|
|
config := defaultConfig(configDir)
|
|
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
|
|
}
|
|
|
|
func defaultConfig(configDir string) Config {
|
|
credentialsFilePath := defaultCredentialsConfigFile(configDir)
|
|
|
|
return Config{
|
|
CredentialsFile: credentialsFilePath,
|
|
CacheDirectory: "",
|
|
HTTP: HTTPConfig{
|
|
Timeout: 5,
|
|
MediaTimeout: 30,
|
|
},
|
|
LineWrapMaxWidth: 80,
|
|
Integrations: Integrations{
|
|
Browser: "",
|
|
Editor: "",
|
|
Pager: "",
|
|
ImageViewer: "",
|
|
VideoPlayer: "",
|
|
},
|
|
}
|
|
}
|