virtigoctl/main.go

106 lines
2.0 KiB
Go

// Copyright 2024 WIT.COM Inc Licensed GPL 3.0
package main
import (
"embed"
"os"
"path/filepath"
"strings"
"go.wit.com/dev/alexflint/arg"
"go.wit.com/log"
)
var Version string
//go:embed resources/*
var resources embed.FS
var urlbase string = "http://localhost:8080"
func main() {
var pp *arg.Parser
pp = arg.MustParse(&argv)
if pp == nil {
pp.WriteHelp(os.Stdout)
os.Exit(0)
}
if argv.Dump != nil {
if argv.Dump.Droplets {
dumpDroplets(false)
os.Exit(0)
}
if argv.Dump.DropletsFull {
dumpStdout("/dumpdropletsfull")
dumpDroplets(true)
os.Exit(0)
}
if argv.Dump.Uptime {
dumpStdout("/uptime")
os.Exit(0)
}
if argv.Dump.Hypervisors {
dumpStdout("/dumphypervisors")
os.Exit(0)
}
log.Info("dump something here")
os.Exit(0)
}
if argv.Create != nil {
dir := filepath.Dir(argv.Create.Filename)
filename := filepath.Base(argv.Create.Filename)
if err := createFilename(dir, filename); err != nil {
log.Info("create failed", err)
os.Exit(-1)
}
log.Info("virtigoctl create end")
os.Exit(0)
}
if argv.Start != nil {
log.Info("start the vm's here:", argv.Start)
os.Exit(0)
}
}
func dumpDroplets(full bool) error {
log.DaemonMode(true)
log.Info("dump droplets here ==", argv.Dump.Droplets)
var url string
if full {
url = urlbase + "/dumpdropletsfull"
} else {
url = urlbase + "/dumpdroplets"
}
body, err := httpPost(url, nil)
if err != nil {
log.Info("httpPost() failed:", err)
return err
}
test := strings.TrimSpace(string(body))
// log.Info("virtigo returned body:", test)
for _, line := range strings.Split(test, "\n") {
log.Info("GOT:", line)
}
return nil
}
func dumpStdout(route string) error {
log.DaemonMode(true)
url := urlbase + route
log.Info("dump url here ==", url)
body, err := httpPost(url, nil)
if err != nil {
log.Info("httpPost() failed:", err)
return err
}
test := strings.TrimSpace(string(body))
for _, line := range strings.Split(test, "\n") {
log.Info("GOT:", line)
}
return nil
}