Adding current project state
This commit is contained in:
77
config.go
Normal file
77
config.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type AppConfig struct {
|
||||
Storage struct {
|
||||
InstallDir string `json:"install_dir"`
|
||||
InstancesDir string `json:"instances_dir"`
|
||||
BackupDir string `json:"backup_dir"`
|
||||
ConfigTemplatesDir string `json:"config_templates_dir"`
|
||||
} `json:"storage"`
|
||||
|
||||
Daemon struct {
|
||||
UseNixOs bool `json:"use_nixos"`
|
||||
}
|
||||
}
|
||||
|
||||
func DefaultConfig() *AppConfig {
|
||||
home, _ := os.UserHomeDir()
|
||||
basePath := filepath.Join(home, ".local", "share", "vs-manager")
|
||||
|
||||
cfg := &AppConfig{}
|
||||
cfg.Storage.InstallDir = filepath.Join(basePath, "installs")
|
||||
cfg.Storage.InstancesDir = filepath.Join(basePath, "instances")
|
||||
cfg.Storage.BackupDir = filepath.Join(basePath, "backups")
|
||||
cfg.Storage.ConfigTemplatesDir = filepath.Join(basePath, "config_templates")
|
||||
|
||||
return cfg
|
||||
}
|
||||
|
||||
func LoadOrCreateConfig(configPath string) (*AppConfig, error) {
|
||||
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
||||
cfg := DefaultConfig()
|
||||
|
||||
if err := os.MkdirAll(filepath.Dir(configPath), 0755); err != nil {
|
||||
return nil, fmt.Errorf("Failed to create config directory: %w", err)
|
||||
}
|
||||
|
||||
data, err := json.MarshalIndent(cfg, "", " ")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := os.WriteFile(configPath, data, 0644); err != nil {
|
||||
return nil, fmt.Errorf("Failed to write config file at %s\n", configPath)
|
||||
}
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var cfg AppConfig
|
||||
if err := json.Unmarshal(data, &cfg); err != nil {
|
||||
return nil, fmt.Errorf("Invalid JSON layout in config file: %w", err)
|
||||
}
|
||||
|
||||
dirs := []string{
|
||||
cfg.Storage.InstallDir,
|
||||
cfg.Storage.InstancesDir,
|
||||
cfg.Storage.BackupDir,
|
||||
cfg.Storage.ConfigTemplatesDir,
|
||||
}
|
||||
for _, dir := range dirs {
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
return nil, fmt.Errorf("Failed creating workspace directory %s: %w", dir, err)
|
||||
}
|
||||
}
|
||||
|
||||
return &cfg, nil
|
||||
}
|
||||
Reference in New Issue
Block a user