infctl-cli/config/project.go

109 lines
3.1 KiB
Go
Raw Permalink Normal View History

update: Added Longhorn installation process and updated memory allocation for VMs update: Added 'git' and 'vagrant' to required tools in pre-flight checks fix: configured k3s install to use internal nic for flanel network update: Added Longhorn installation process and updated memory allocation for VMs update: Added 'git' and 'vagrant' to required tools in pre-flight checks fix: configured k3s install to use internal nic for flanel network fix: corrected JSON formatting for config json update: reduce VM memory allocation to 2GB, add Longhorn installation scripts and prerequisites, and implement checks for existing pods fix: merge issues fix: merge issues update: Added Longhorn installation process and updated memory allocation for VMs update: Added 'git' and 'vagrant' to required tools in pre-flight checks fix: configured k3s install to use internal nic for flanel network update: Added Longhorn installation process and updated memory allocation for VMs update: Added 'git' and 'vagrant' to required tools in pre-flight checks fix: configured k3s install to use internal nic for flanel network fix: corrected JSON formatting for config json update: reduce VM memory allocation to 2GB, add Longhorn installation scripts and prerequisites, and implement checks for existing pods update: improve error logging in RunJsonDeployment and RunCommand functions update: add jq installation to provision script update: add version flag bump version fix: improve error messages for config file reading feat: add Windows gitbash installation support and improve binary download process clean up tmp code fix: increase timeout for some slower windows clients feat: add Ingress and Service configurations for nginx deployment, and implement MetalLB and Traeik installation scripts refactor: remove obsolete Traefik installation script feat: add environment checks and configurations for Vagrant setup, including dnsmasq MetalLB and ingress feat: add deployment and installation scripts for infmon-cli, including Kubernetes configurations feat: refactor customer project creation and add success/failure job scripts refactor: rename customer references to project in configuration and application logic feat: enhance JSON deployment handling with retry logic and command execution improvements feat: enhance RunJsonDeployment with error handling and retry logic; add tests for configuration reading feat: add automatic creation of base and config JSON files from examples if they do not exist refactor: remove database package and related functionality; update app state initialization and error handling refactor: update deployment handling to use ProjectConfig; improve error messages and logging feat: enhance RunJsonDeployment retry logic with configurable delay; improve logging for retries feat: implement LoadConfigs function for improved configuration loading; add logger setup refactor: remove unused fields from BaseConfig and ProjectConfig structs for cleaner configuration management refactor: clean up tests by removing obsolete functions and simplifying test cases chore: update version to v0.0.5 in install script feat: implement default configuration creation for BaseConfig and ProjectConfig; enhance validation logic fix: enhance configuration parsing and loading; streamline flag handling and error reporting refactor: remove obsolete configuration download logic from installation script
2025-08-16 18:00:28 +01:00
package config
import (
"encoding/json"
"fmt"
"os"
)
type ProjectConfig struct {
LogFormat string `json:"log_format"`
DeploymentFile string `json:"deployment_file"`
DeploymentType string `json:"deployment_type"`
DeploymentMode string `json:"deployment_mode"`
}
func ValidateProjectConfig(config ProjectConfig) error {
if config.LogFormat != "full" && config.LogFormat != "basic" && config.LogFormat != "none" {
return fmt.Errorf("invalid LogFormat: %s (must be 'full', 'basic', or 'none')", config.LogFormat)
}
if _, err := os.Stat(config.DeploymentFile); os.IsNotExist(err) {
return fmt.Errorf("deployment file does not exist: %s", config.DeploymentFile)
} else if err != nil {
return fmt.Errorf("error checking deployment file: %w", err)
}
if config.DeploymentMode != "json" && config.DeploymentMode != "api" {
return fmt.Errorf("invalid DeploymentMode: %s (must be 'json' or 'api')", config.DeploymentMode)
}
fmt.Printf("DeploymentType: %s\n", config.DeploymentType)
if config.DeploymentType != "development" && config.DeploymentType != "pre-production" && config.DeploymentType != "production" {
return fmt.Errorf("invalid DeploymentType: %s (must be 'development', 'pre-production', or 'production')", config.DeploymentType)
}
return nil
}
func CreateDefaultJsonConfig(path string, depploymentFile string) error {
defaultConfig := ProjectConfig{
LogFormat: "full",
DeploymentType: "development",
DeploymentFile: depploymentFile,
DeploymentMode: "json",
}
data, err := json.MarshalIndent(defaultConfig, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal default config: %w", err)
}
if err := os.WriteFile(path, data, 0644); err != nil {
return fmt.Errorf("failed to write default config to file: %w", err)
}
return nil
}
func ReadProjectConfig(path string, pipelineFile *string) (ProjectConfig, error) {
var config ProjectConfig
if pipelineFile == nil || *pipelineFile == "" {
return ProjectConfig{}, fmt.Errorf("no deployment file specified, please use -f or --deployment-file flag")
}
config.DeploymentFile = *pipelineFile
if _, err := os.Stat(path); os.IsNotExist(err) {
if err := CreateDefaultJsonConfig(path, config.DeploymentFile); err != nil {
return ProjectConfig{}, fmt.Errorf("failed to create default config: %w", err)
}
}
data, err := os.ReadFile(path)
if err != nil {
return ProjectConfig{}, fmt.Errorf("failed to read project configfile: %w", err)
}
var proj ProjectConfig
if err := json.Unmarshal(data, &proj); err != nil {
return ProjectConfig{}, fmt.Errorf("failed to unmarshal JSON: %w", err)
}
if proj.DeploymentMode != "" {
config.DeploymentMode = proj.DeploymentMode
} else {
config.DeploymentMode = "json"
}
deploymentModeEnv := os.Getenv("DEPLOYMENT_MODE")
if deploymentModeEnv != "" {
config.DeploymentMode = deploymentModeEnv
}
config.LogFormat = proj.LogFormat
if config.LogFormat == "" {
config.LogFormat = "full"
}
config.DeploymentType = proj.DeploymentType
if err := ValidateProjectConfig(config); err != nil {
return ProjectConfig{}, err
}
return config, nil
}