Improve usability and feedback
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Danny Bessems 2022-12-23 12:45:12 +01:00
parent 15105ee1ea
commit 5fa5530e0a
4 changed files with 37 additions and 31 deletions

View File

@ -3,7 +3,7 @@ type: kubernetes
name: 'Golang Build'
environment:
BINARY_VERSION: v0.2.0
BINARY_VERSION: v0.3.0
clone:
disable: true

View File

@ -2,47 +2,49 @@ package main
import (
"context"
"flag"
"log"
"os"
"github.com/jessevdk/go-flags"
"spamasaurus.com/m/pkg/hypervisor"
)
type Input struct {
FQDN string
Username string
Password string
Datacenter string
VirtualMachine string
Network string
}
func main() {
var input Input
var opts struct {
FQDN string `short:"s" long:"server" description:"FQDN of the vCenter appliance" required:"true"`
Username string `short:"u" long:"username" description:"Username to authenticate with" required:"true"`
Password string `short:"p" long:"password" description:"Password to authenticate with" required:"true"`
flag.StringVar(&input.FQDN, "server", "", "FQDN of the vCenter appliance")
flag.StringVar(&input.Username, "username", "", "Username to authenticate with")
flag.StringVar(&input.Password, "password", "", "Password to authenticate with")
flag.StringVar(&input.Datacenter, "dc", "", "Name of datacenter")
flag.StringVar(&input.VirtualMachine, "vm", "", "Name of VM")
flag.StringVar(&input.Network, "network", "", "Name of network portgroup")
flag.Parse()
Datacenter string `short:"d" long:"datacenter" description:"Name of datacenter" required:"true"`
VirtualMachine string `short:"v" long:"virtualmachine" description:"Name of virtual machine" required:"true"`
Network string `short:"n" long:"network" description:"Name of network portgroup" required:"true"`
}
_, err := flags.Parse(&opts)
if err != nil {
if e, ok := err.(*flags.Error); ok {
if e.Type == flags.ErrHelp {
os.Exit(0)
}
}
os.Exit(1)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
clt, err := hypervisor.NewClient(ctx, input.FQDN, input.Username, input.Password, true)
clt, err := hypervisor.NewClient(ctx, opts.FQDN, opts.Username, opts.Password, true)
if err != nil {
log.Fatalf("Login failed: %s", err)
log.Fatalf("[ERROR] Login failed: %s", err)
}
fnd, err := hypervisor.DatacenterFinder(ctx, clt, input.Datacenter)
fnd, err := hypervisor.DatacenterFinder(ctx, clt, opts.Datacenter)
if err != nil {
log.Fatalf("Foo indeed: %s", err)
log.Fatalf("[ERROR] Unable to determine datacenter: %s", err)
}
if err := hypervisor.SetVirtualMachineProperties(ctx, fnd, input.VirtualMachine, input.Network); err != nil {
log.Fatalf("Could not apply vApp properties: %s", err)
if err := hypervisor.SetVirtualMachineProperties(ctx, fnd, opts.VirtualMachine, opts.Network); err != nil {
log.Fatalf("[ERROR] Could not apply vApp properties: %s", err)
}
}

7
go.mod
View File

@ -2,6 +2,9 @@ module spamasaurus.com/m
go 1.19
require github.com/vmware/govmomi v0.30.0
require (
github.com/jessevdk/go-flags v1.5.0
github.com/vmware/govmomi v0.30.0
)
require github.com/davecgh/go-spew v1.1.1 // indirect
require golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4 // indirect

7
go.sum
View File

@ -1,6 +1,7 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc=
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
github.com/vmware/govmomi v0.30.0 h1:Fm8ugPnnlMSTSceDKY9goGvjmqc6eQLPUSUeNXdpeXA=
github.com/vmware/govmomi v0.30.0/go.mod h1:F7adsVewLNHsW/IIm7ziFURaXDaHEwcc+ym4r3INMdY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4 h1:EZ2mChiOa8udjfp6rRmswTbtZN/QzUQp4ptM4rnjHvc=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=