Skip to content

Commit 4b49a34

Browse files
committed
added api wrapper cli parser
1 parent facd9ff commit 4b49a34

File tree

9 files changed

+108
-32
lines changed

9 files changed

+108
-32
lines changed

cmd/pve_api_wrapper/pve_api_wrapper.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,30 @@ package main
22

33
import (
44
"fmt"
5+
"log"
56
"os"
67

8+
"github.com/alexflint/go-arg"
79
"github.com/iolave/go-proxmox/internal/server"
810
)
911

1012
func main() {
11-
s := server.New("", "", "localhost", 8006)
13+
var app server.App
1214

13-
err := s.Start()
15+
p, err := arg.NewParser(arg.Config{Program: server.NAME}, &app)
1416
if err != nil {
15-
fmt.Printf("error: %s", err.Error())
17+
log.Fatalf("there was an error in the definition of the Go struct: %v\n", err)
18+
}
19+
err = p.Parse(os.Args[1:])
20+
switch {
21+
case err == arg.ErrHelp: // found "--help" on command line
22+
p.WriteHelp(os.Stdout)
23+
os.Exit(0)
24+
case err != nil:
25+
fmt.Printf("error: %v\n", err)
26+
p.WriteUsage(os.Stdout)
1627
os.Exit(1)
1728
}
1829

30+
app.Start()
1931
}

docs/api-wrapper.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
The pve api wrapper is an http server ment to be installed on the proxmox host server (but can definitely be installed somewhere else) that wraps the proxmox api and adds missing features to it.
1+
The pve api wrapper is an http server ment to be installed on the proxmox host server () that wraps the proxmox api and adds missing features to it.
2+
3+
_pve-api-wrapper can definitely be installed somewhere else but some functionalities might not work._
24

35
## Features
46
- Proper error responses on failed requests,
@@ -25,11 +27,18 @@ cd go-proxmox-latest
2527
make build
2628
```
2729

28-
29-
## Serve
30+
## Usage
3031
```bash
31-
pve-api-wrapper
32+
pve-api-wrapper [--version] [--pve-host PVE-HOST] [--pve-port PVE-PORT] [--pve-host PVE-HOST] [--pve-port PVE-PORT] [--crt CRT] [--key KEY]
3233
```
34+
### Options
35+
- `--version` displays the program version.
36+
- `--pve-host` proxmox virtual environment host (env:`PVE_HOST`, default:`localhost`).
37+
- `--pve-port` proxmox virtual environment port (env:`PVE_PORT`, default:`8006`).
38+
- `--host` api wrapper host (env:`WRAPPER_HOST`, default:`localhost`).
39+
- `--port` api wrapper port (env:`WRAPPER_PORT`, default:`8443`).
40+
- `--crt` api wrapper tls crt path (default:`/etc/pve/local/pve-ssl.pem`).
41+
- `--key` api wrapper tls key path (default:`/etc/pve/local/pve-ssl.key`).
3342

3443
[here]: https://github.com/iolave/go-proxmox/blob/latest/scripts/install.sh
3544
<!--

docs/go-client/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Go client
22
## Install
33
```bash
4-
go get github.com/iolave/go-proxmox@v0.4.0
4+
go get github.com/iolave/go-proxmox@v0.5.0
55
```
66

77
## Environment variables

go.mod

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ module github.com/iolave/go-proxmox
22

33
go 1.22.2
44

5-
require github.com/princjef/gomarkdoc v1.1.0
5+
require (
6+
github.com/alexflint/go-arg v1.5.1
7+
github.com/princjef/gomarkdoc v1.1.0
8+
)
69

710
require (
811
github.com/Microsoft/go-winio v0.5.0 // indirect
912
github.com/VividCortex/ewma v1.2.0 // indirect
13+
github.com/alexflint/go-scalar v1.2.0 // indirect
1014
github.com/cheggaaa/pb/v3 v3.0.8 // indirect
1115
github.com/emirpasic/gods v1.12.0 // indirect
1216
github.com/fatih/color v1.11.0 // indirect

go.sum

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ github.com/VividCortex/ewma v1.2.0 h1:f58SaIzcDXrSy3kWaHNvuJgJ3Nmz59Zji6XoJR/q1o
77
github.com/VividCortex/ewma v1.2.0/go.mod h1:nz4BbCtbLyFDeC9SUHbtcT5644juEuWfUAUnGx7j5l4=
88
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7 h1:uSoVVbwJiQipAclBbw+8quDsfcvFjOpI5iCf4p/cqCs=
99
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7/go.mod h1:6zEj6s6u/ghQa61ZWa/C2Aw3RkjiTBOix7dkqa1VLIs=
10+
github.com/alexflint/go-arg v1.5.1 h1:nBuWUCpuRy0snAG+uIJ6N0UvYxpxA0/ghA/AaHxlT8Y=
11+
github.com/alexflint/go-arg v1.5.1/go.mod h1:A7vTJzvjoaSTypg4biM5uYNTkJ27SkNTArtYXnlqVO8=
12+
github.com/alexflint/go-scalar v1.2.0 h1:WR7JPKkeNpnYIOfHRa7ivM21aWAdHD0gEWHCx+WQBRw=
13+
github.com/alexflint/go-scalar v1.2.0/go.mod h1:LoFvNMqS1CPrMVltza4LvnGKhaSpc3oyLEBUZVhhS2o=
1014
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 h1:kFOfPq6dUM1hTo4JG6LR5AXSUEsOjtdm0kw0FtQtMJA=
1115
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
1216
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
@@ -111,8 +115,9 @@ github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic
111115
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
112116
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
113117
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
114-
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
115118
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
119+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
120+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
116121
github.com/x-cray/logrus-prefixed-formatter v0.5.2 h1:00txxvfBM9muc0jiLIEAkAcIMJzfthRT6usrui8uGmg=
117122
github.com/x-cray/logrus-prefixed-formatter v0.5.2/go.mod h1:2duySbKsL6M18s5GU7VPsoEPHyzalCE06qoARUCeBBE=
118123
github.com/xanzy/ssh-agent v0.3.0 h1:wUMzuKtKilRgBAD1sUb8gOwwRr2FGoBVumcjoOACClI=
@@ -173,5 +178,7 @@ gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
173178
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
174179
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
175180
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
181+
gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA=
182+
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
176183
mvdan.cc/xurls/v2 v2.2.0 h1:NSZPykBXJFCetGZykLAxaL6SIpvbVy/UFEniIfHAa8A=
177184
mvdan.cc/xurls/v2 v2.2.0/go.mod h1:EV1RMtya9D6G5DMYPGD8zTQzaHet6Jh8gFlRgGRJeO8=

internal/server/cli.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package server
2+
3+
import (
4+
"fmt"
5+
"os"
6+
)
7+
8+
const (
9+
NAME = "pve-api-wrapper"
10+
// TODO: read and implement this https://akrabat.com/setting-the-version-of-a-go-application-when-building/
11+
VERSION = "v0.5.0"
12+
)
13+
14+
type App struct {
15+
Version *bool `arg:"--version" help:"display the program version"`
16+
PVEHost string `arg:"--pve-host,env:PVE_HOST" help:"Proxmox virtual environment host" default:"localhost"`
17+
PVEPort int `arg:"--pve-port,env:PVE_PORT" help:"Proxmox virtual environment port" default:"8006"`
18+
Host string `arg:"--pve-host,env:WRAPPER_HOST" help:"API wrapper host" default:"localhost"`
19+
Port int `arg:"--pve-port,env:WRAPPER_PORT" help:"API wrapper port" default:"8443"`
20+
TLSCrtPath string `arg:"--crt" help:"API wrapper tls crt path" default:"/etc/pve/local/pve-ssl.pem"`
21+
TLSKeyPath string `arg:"--key" help:"API wrapper tls key path" default:"/etc/pve/local/pve-ssl.key"`
22+
}
23+
24+
func (a App) Start() {
25+
if a.Version != nil && *a.Version == true {
26+
fmt.Println(VERSION)
27+
os.Exit(0)
28+
}
29+
30+
cfg := serverConfig{
31+
PVEHost: a.PVEHost,
32+
PVEPort: a.PVEPort,
33+
Host: a.Host,
34+
Port: a.Port,
35+
TLSKey: a.TLSKeyPath,
36+
TLSCrt: a.TLSCrtPath,
37+
}
38+
39+
s := New(cfg)
40+
41+
if err := s.Start(); err != nil {
42+
fmt.Printf("error: %s\n", err.Error())
43+
os.Exit(1)
44+
}
45+
}

internal/server/server.go

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,24 @@ import (
77
"net/http"
88
)
99

10-
type Server struct {
11-
host string
12-
port int
13-
tlsKey string
14-
tlsCrt string
15-
c *http.Client
16-
s *http.Server
10+
type server struct {
11+
c *http.Client
12+
s *http.Server
13+
cfg serverConfig
14+
}
15+
16+
type serverConfig struct {
17+
Host string
18+
Port int
19+
TLSKey string
20+
TLSCrt string
1721
PVEHost string
1822
PVEPort int
1923
}
2024

21-
func New(tlsCrt, tlsKey, pveHost string, pvePort int) *Server {
22-
server := new(Server)
23-
server.PVEHost = pveHost
24-
server.PVEPort = pvePort
25-
server.host = "localhost"
26-
server.port = 8443
27-
server.tlsCrt = tlsCrt
28-
server.tlsKey = tlsKey
25+
func New(cfg serverConfig) *server {
26+
server := new(server)
27+
server.cfg = cfg
2928

3029
c := &http.Client{
3130
Transport: &http.Transport{
@@ -39,12 +38,12 @@ func New(tlsCrt, tlsKey, pveHost string, pvePort int) *Server {
3938
return server
4039
}
4140

42-
func (s *Server) Start() error {
41+
func (s *server) Start() error {
4342
log.Println("starting server")
4443
s.s = &http.Server{
45-
Addr: fmt.Sprintf("%s:%d", s.host, s.port),
44+
Addr: fmt.Sprintf("%s:%d", s.cfg.Host, s.cfg.Port),
4645
Handler: getHandler(s),
4746
}
4847
defer s.s.Close()
49-
return s.s.ListenAndServeTLS(s.tlsCrt, s.tlsKey)
48+
return s.s.ListenAndServeTLS(s.cfg.TLSCrt, s.cfg.TLSKey)
5049
}

internal/server/client.go renamed to internal/server/server_client.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import (
1010
"github.com/iolave/go-proxmox/pkg/errors"
1111
)
1212

13-
func (s *Server) sendPVERequest(sr *http.Request) (*http.Response, *errors.HTTPError) {
13+
func (s *server) sendPVERequest(sr *http.Request) (*http.Response, *errors.HTTPError) {
1414
pveUrl, err := url.Parse(fmt.Sprintf(
1515
"https://%s:%d%s",
16-
s.PVEHost,
17-
s.PVEPort,
16+
s.cfg.PVEHost,
17+
s.cfg.PVEPort,
1818
sr.URL.Path,
1919
))
2020
b, err := io.ReadAll(sr.Body)

internal/server/handler.go renamed to internal/server/server_handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/iolave/go-proxmox/pkg/errors"
1010
)
1111

12-
func getHandler(s *Server) http.HandlerFunc {
12+
func getHandler(s *server) http.HandlerFunc {
1313
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1414
log.Println(r.Method, r.URL.Path, "started")
1515
res, httpErr := s.sendPVERequest(r)

0 commit comments

Comments
 (0)