forked from Telmate/proxmox-api-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
104 lines (89 loc) · 2.07 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"crypto/tls"
"io"
"log"
"os"
"regexp"
"github.com/Telmate/proxmox-api-go/cli"
_ "github.com/Telmate/proxmox-api-go/cli/command/commands"
"github.com/Telmate/proxmox-api-go/proxmox"
"github.com/joho/godotenv"
)
type AppConfig struct {
APIURL string
HTTPHeaders string
User string
Password string
OTP string
NewCLI bool
}
func loadAppConfig() AppConfig {
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: os.Getenv("NEW_CLI") == "true",
}
}
func initializeProxmoxClient(config AppConfig, secure bool, proxyURL string, taskTimeout int) (*proxmox.Client, error) {
tlsconf := &tls.Config{InsecureSkipVerify: !secure}
if secure {
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() {
loadAppConfig()
err := cli.Execute()
if err != nil {
failError(err)
}
os.Exit(0)
}
var rxUserRequiresToken = regexp.MustCompile("[a-z0-9]+@[a-z0-9]+![a-z0-9]+")
func userRequiresAPIToken(userID string) bool {
return rxUserRequiresToken.MatchString(userID)
}
// GetConfig get config from file
func GetConfig(configFile string) (configSource []byte) {
var err error
if configFile != "" {
configSource, err = os.ReadFile(configFile)
if err != nil {
log.Fatal(err)
}
} else {
configSource, err = io.ReadAll(os.Stdin)
if err != nil {
log.Fatal(err)
}
}
return
}
func failError(err error) {
if err != nil {
log.Fatal(err)
}
}