2025-07-09 13:19:43 +01:00
|
|
|
package config
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"flag"
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
)
|
|
|
|
|
|
2025-08-16 18:00:28 +01:00
|
|
|
const Version = "v0.0.4"
|
|
|
|
|
|
2025-07-09 13:19:43 +01:00
|
|
|
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")
|
2025-08-16 11:45:47 +01:00
|
|
|
deploymentFileShorthand := flag.String("f", "", "shorthand for -deployment-file")
|
|
|
|
|
|
2025-07-09 13:19:43 +01:00
|
|
|
helpFlag := flag.Bool("help", false, "show help")
|
2025-08-16 18:00:28 +01:00
|
|
|
versionFlag := flag.Bool("version", false, "show version")
|
|
|
|
|
vFlag := flag.Bool("v", false, "show version (shorthand)")
|
2025-07-09 13:19:43 +01:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
|
|
if *helpFlag {
|
|
|
|
|
fmt.Println("Usage: infctl-cli --deployment-file=<path_to_config_file>")
|
|
|
|
|
fmt.Println("DEPLOYMENT_TYPE environment variable must be set to 'json' or 'api (api is not implemented yet)'")
|
|
|
|
|
os.Exit(0)
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-16 18:00:28 +01:00
|
|
|
// Handle version flags
|
|
|
|
|
if *versionFlag || *vFlag {
|
|
|
|
|
fmt.Println("infctl-cli version:", Version)
|
|
|
|
|
os.Exit(0)
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-09 13:19:43 +01:00
|
|
|
var config BaseConfig
|
2025-08-16 11:45:47 +01:00
|
|
|
if *deploymentFileShorthand != "" {
|
|
|
|
|
config.DeploymentFile = *deploymentFileShorthand
|
|
|
|
|
} else if *deploymentFile != "" {
|
2025-07-09 13:19:43 +01:00
|
|
|
config.DeploymentFile = *deploymentFile
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data, err := os.ReadFile(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return BaseConfig{}, fmt.Errorf("failed to read 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
|
|
|
|
|
}
|