109 lines
3.1 KiB
Go
109 lines
3.1 KiB
Go
|
|
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
|
||
|
|
}
|