package config import ( "encoding/json" "flag" "fmt" "os" ) const Version = "v0.0.4" type BaseConfig struct { ProjectsDirectory string `json:"projects_directory"` Env string `json:"env"` StaticImages string `json:"static_images"` PublicImages string `json:"public_images"` PhpConf string `json:"php_conf"` Exports string `json:"exports"` Logs string `json:"logs"` PreviewPath string `json:"preview_path"` DataWww string `json:"data_www"` NginxConf string `json:"nginx_conf"` AdminURL string `json:"admin_url"` PreviewURL string `json:"preview_url"` AppImage string `json:"app_image"` WebserverImage string `json:"webserver_image"` EmptyDB string `json:"empty_db"` DB string `json:"db"` EmptyImages string `json:"empty_imaages"` DeploymentType string `json:"deployment_type"` DeploymentFile string `json:"deployment_file"` Port int `json:"port"` } func ReadBaseConfig(path string) (BaseConfig, error) { deploymentType := os.Getenv("DEPLOYMENT_TYPE") deploymentFile := flag.String("deployment-file", "", "path to config file") deploymentFileShorthand := flag.String("f", "", "shorthand for -deployment-file") helpFlag := flag.Bool("help", false, "show help") versionFlag := flag.Bool("version", false, "show version") vFlag := flag.Bool("v", false, "show version (shorthand)") flag.Parse() if *helpFlag { fmt.Println("Usage: infctl-cli --deployment-file=") fmt.Println("DEPLOYMENT_TYPE environment variable must be set to 'json' or 'api (api is not implemented yet)'") os.Exit(0) } // Handle version flags if *versionFlag || *vFlag { fmt.Println("infctl-cli version:", Version) os.Exit(0) } var config BaseConfig if *deploymentFileShorthand != "" { config.DeploymentFile = *deploymentFileShorthand } else if *deploymentFile != "" { config.DeploymentFile = *deploymentFile } data, err := os.ReadFile(path) if err != nil { return BaseConfig{}, fmt.Errorf("failed to read the config file: %w", err) } if err := json.Unmarshal(data, &config); err != nil { return BaseConfig{}, fmt.Errorf("failed to unmarshal JSON: %w", err) } config.DeploymentType = deploymentType return config, nil }