diff --git a/main.go b/main.go index c2e112ff..16a83393 100644 --- a/main.go +++ b/main.go @@ -20,93 +20,96 @@ import ( ) type AppConfig struct { - APIURL string - HTTPHeaders string - User string - Password string - OTP string - NewCLI bool + APIURL string + HTTPHeaders string + User string + Password string + OTP string + NewCLI bool } func loadAppConfig() AppConfig { - newCLI := os.Getenv("NEW_CLI") == "true" - - if err := godotenv.Load(); err != nil { - log.Printf("Warning: Failed to load .env file: %v", err) - } - - return AppConfig{ - APIURL: os.Getenv("PM_API_URL"), - HTTPHeaders: os.Getenv("PM_HTTP_HEADERS"), - User: os.Getenv("PM_USER"), - Password: os.Getenv("PM_PASS"), - OTP: os.Getenv("PM_OTP"), - NewCLI: newCLI, - } + newCLI := os.Getenv("NEW_CLI") == "true" + + if err := godotenv.Load(); err != nil { + log.Printf("Warning: Failed to load .env file: %v", err) + } + + return AppConfig{ + APIURL: os.Getenv("PM_API_URL"), + HTTPHeaders: os.Getenv("PM_HTTP_HEADERS"), + User: os.Getenv("PM_USER"), + Password: os.Getenv("PM_PASS"), + OTP: os.Getenv("PM_OTP"), + NewCLI: newCLI, + } } func initializeProxmoxClient(config AppConfig, insecure bool, proxyURL string, taskTimeout int) (*proxmox.Client, error) { - tlsconf := &tls.Config{InsecureSkipVerify: insecure} - if !insecure { - tlsconf = nil - } - - client, err := proxmox.NewClient(config.APIURL, nil, config.HTTPHeaders, tlsconf, proxyURL, taskTimeout) - if err != nil { - return nil, err - } - - if userRequiresAPIToken(config.User) { - client.SetAPIToken(config.User, config.Password) - _, err := client.GetVersion() - if err != nil { - return nil, err - } - } else { - err = client.Login(config.User, config.Password, config.OTP) - if err != nil { - return nil, err - } - } - - return client, nil + tlsconf := &tls.Config{InsecureSkipVerify: insecure} + if !insecure { + tlsconf = nil + } + + client, err := proxmox.NewClient(config.APIURL, nil, config.HTTPHeaders, tlsconf, proxyURL, taskTimeout) + if err != nil { + return nil, err + } + + if userRequiresAPIToken(config.User) { + client.SetAPIToken(config.User, config.Password) + _, err := client.GetVersion() + if err != nil { + return nil, err + } + } else { + err = client.Login(config.User, config.Password, config.OTP) + if err != nil { + return nil, err + } + } + + return client, nil } func main() { - config := loadAppConfig() - - if config.NewCLI { - err := cli.Execute() - if err != nil { - failError(err) - } - os.Exit(0) - } - - // Command-line flags - insecure := flag.Bool("insecure", false, "TLS insecure mode") - proxmox.Debug = flag.Bool("debug", false, "debug mode") - fConfigFile := flag.String("file", "", "file to get the config from") - taskTimeout := flag.Int("timeout", 300, "API task timeout in seconds") - proxyURL := flag.String("proxy", "", "proxy URL to connect to") - fvmid := flag.Int("vmid", -1, "custom VMID (instead of auto)") - flag.Parse() - - // Initialize Proxmox client + config := loadAppConfig() + + if config.NewCLI { + err := cli.Execute() + if err != nil { + failError(err) + } + os.Exit(0) + } + + // Command-line flags + insecure := flag.Bool("insecure", false, "TLS insecure mode") + proxmox.Debug = flag.Bool("debug", false, "debug mode") + fConfigFile := flag.String("file", "", "file to get the config from") + taskTimeout := flag.Int("timeout", 300, "API task timeout in seconds") + proxyURL := flag.String("proxy", "", "proxy URL to connect to") + fvmid := flag.Int("vmid", -1, "custom VMID (instead of auto)") + flag.Parse() + + // Initialize Proxmox client c, err := initializeProxmoxClient(config, *insecure, *proxyURL, *taskTimeout) + if err != nil { + log.Fatalf("Failed to initialize Proxmox client: %v", err) + } - vmid := *fvmid - if vmid < 0 { - if len(flag.Args()) > 1 { - vmid, err = strconv.Atoi(flag.Args()[1]) - if err != nil { + vmid := *fvmid + if vmid < 0 { + if len(flag.Args()) > 1 { + vmid, err = strconv.Atoi(flag.Args()[1]) + if err != nil { vmid = 0 } - } else if len(flag.Args()) == 0 || (flag.Args()[0] == "idstatus") { - vmid = 0 - } - } + } else if len(flag.Args()) == 0 || (flag.Args()[0] == "idstatus") { + vmid = 0 + } + } var jbody interface{} var vmr *proxmox.VmRef