Skip to content

Commit

Permalink
Merge pull request Telmate#194 from Tinyblargon/CLI-Overhaul
Browse files Browse the repository at this point in the history
Cli overhaul: guest commands (no tests yet)
  • Loading branch information
mleone87 authored Sep 20, 2022
2 parents 9b7928c + 0815cb9 commit 422dd16
Show file tree
Hide file tree
Showing 14 changed files with 310 additions and 51 deletions.
2 changes: 2 additions & 0 deletions cli/command/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
_ "github.com/Telmate/proxmox-api-go/cli/command/example"
_ "github.com/Telmate/proxmox-api-go/cli/command/get"
_ "github.com/Telmate/proxmox-api-go/cli/command/get/id"
_ "github.com/Telmate/proxmox-api-go/cli/command/guest"
_ "github.com/Telmate/proxmox-api-go/cli/command/guest/qemu"
_ "github.com/Telmate/proxmox-api-go/cli/command/list"
_ "github.com/Telmate/proxmox-api-go/cli/command/set"
_ "github.com/Telmate/proxmox-api-go/cli/command/update"
Expand Down
25 changes: 25 additions & 0 deletions cli/command/guest/guest-shutdown.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package guest

import (
"github.com/Telmate/proxmox-api-go/cli"
"github.com/Telmate/proxmox-api-go/proxmox"
"github.com/spf13/cobra"
)

var guest_shutdownCmd = &cobra.Command{
Use: "shutdown GUESTID",
Short: "Shuts the speciefid guest down",
RunE: func(cmd *cobra.Command, args []string) (err error) {
vmr := proxmox.NewVmRef(cli.ValidateIntIDset(args, "GuestID"))
c := cli.NewClient()
_, err = c.ShutdownVm(vmr)
if err == nil {
cli.PrintGuestStatus(GuestCmd.OutOrStdout(), vmr.VmId(), "stopped")
}
return
},
}

func init() {
GuestCmd.AddCommand(guest_shutdownCmd)
}
25 changes: 25 additions & 0 deletions cli/command/guest/guest-start.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package guest

import (
"github.com/Telmate/proxmox-api-go/cli"
"github.com/Telmate/proxmox-api-go/proxmox"
"github.com/spf13/cobra"
)

var guest_statusCmd = &cobra.Command{
Use: "start GUESTID",
Short: "Starts the speciefid guest",
RunE: func(cmd *cobra.Command, args []string) (err error) {
vmr := proxmox.NewVmRef(cli.ValidateIntIDset(args, "GuestID"))
c := cli.NewClient()
_, err = c.StartVm(vmr)
if err == nil {
cli.PrintGuestStatus(GuestCmd.OutOrStdout(), vmr.VmId(), "started")
}
return
},
}

func init() {
GuestCmd.AddCommand(guest_statusCmd)
}
27 changes: 27 additions & 0 deletions cli/command/guest/guest-status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package guest

import (
"fmt"

"github.com/Telmate/proxmox-api-go/cli"
"github.com/Telmate/proxmox-api-go/proxmox"
"github.com/spf13/cobra"
)

var guest_startCmd = &cobra.Command{
Use: "status GUESTID",
Short: "Gets the status of the speciefid guest",
RunE: func(cmd *cobra.Command, args []string) (err error) {
vmr := proxmox.NewVmRef(cli.ValidateIntIDset(args, "GuestID"))
c := cli.NewClient()
vmState, err := c.GetVmState(vmr)
if err == nil {
fmt.Fprintf(GuestCmd.OutOrStdout(), "Status of guest with id (%d) is %s\n", vmr.VmId(), vmState["status"].(string))
}
return
},
}

func init() {
GuestCmd.AddCommand(guest_startCmd)
}
25 changes: 25 additions & 0 deletions cli/command/guest/guest-stop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package guest

import (
"github.com/Telmate/proxmox-api-go/cli"
"github.com/Telmate/proxmox-api-go/proxmox"
"github.com/spf13/cobra"
)

var guest_stopCmd = &cobra.Command{
Use: "stop GUESTID",
Short: "Stops the speciefid guest",
RunE: func(cmd *cobra.Command, args []string) (err error) {
vmr := proxmox.NewVmRef(cli.ValidateIntIDset(args, "GuestID"))
c := cli.NewClient()
_, err = c.StopVm(vmr)
if err == nil {
cli.PrintGuestStatus(GuestCmd.OutOrStdout(), vmr.VmId(), "stopped")
}
return
},
}

func init() {
GuestCmd.AddCommand(guest_stopCmd)
}
27 changes: 27 additions & 0 deletions cli/command/guest/guest-uptime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package guest

import (
"fmt"

"github.com/Telmate/proxmox-api-go/cli"
"github.com/Telmate/proxmox-api-go/proxmox"
"github.com/spf13/cobra"
)

var guest_uptimeCmd = &cobra.Command{
Use: "uptime GUESTID",
Short: "Gets the uptime of the speciefid guest",
RunE: func(cmd *cobra.Command, args []string) (err error) {
vmr := proxmox.NewVmRef(cli.ValidateIntIDset(args, "GuestID"))
c := cli.NewClient()
vmState, err := c.GetVmState(vmr)
if err == nil {
fmt.Fprintf(GuestCmd.OutOrStdout(), "Uptime of guest with id (%d) is %d\n", vmr.VmId(), int(vmState["uptime"].(float64)))
}
return
},
}

func init() {
GuestCmd.AddCommand(guest_uptimeCmd)
}
15 changes: 15 additions & 0 deletions cli/command/guest/guest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package guest

import (
"github.com/Telmate/proxmox-api-go/cli"
"github.com/spf13/cobra"
)

var GuestCmd = &cobra.Command{
Use: "guest",
Short: "Commands to interact with the qemu and LXC guests on Proxmox",
}

func init() {
cli.RootCmd.AddCommand(GuestCmd)
}
25 changes: 25 additions & 0 deletions cli/command/guest/qemu/guest-qemu-hibernate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package qemu

import (
"github.com/Telmate/proxmox-api-go/cli"
"github.com/Telmate/proxmox-api-go/proxmox"
"github.com/spf13/cobra"
)

var qemu_hibernateCmd = &cobra.Command{
Use: "hibernate GUESTID",
Short: "Hibernates the speciefid guest",
RunE: func(cmd *cobra.Command, args []string) (err error) {
vmr := proxmox.NewVmRef(cli.ValidateIntIDset(args, "GuestID"))
c := cli.NewClient()
_, err = c.HibernateVm(vmr)
if err == nil {
cli.PrintGuestStatus(qemuCmd.OutOrStdout(), vmr.VmId(), "suspended to disk")
}
return
},
}

func init() {
qemuCmd.AddCommand(qemu_hibernateCmd)
}
25 changes: 25 additions & 0 deletions cli/command/guest/qemu/guest-qemu-pause.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package qemu

import (
"github.com/Telmate/proxmox-api-go/cli"
"github.com/Telmate/proxmox-api-go/proxmox"
"github.com/spf13/cobra"
)

var qemu_pauseCmd = &cobra.Command{
Use: "pause GUESTID",
Short: "Pauses the speciefid guest",
RunE: func(cmd *cobra.Command, args []string) (err error) {
vmr := proxmox.NewVmRef(cli.ValidateIntIDset(args, "GuestID"))
c := cli.NewClient()
_, err = c.PauseVm(vmr)
if err == nil {
cli.PrintGuestStatus(qemuCmd.OutOrStdout(), vmr.VmId(), "paused")
}
return
},
}

func init() {
qemuCmd.AddCommand(qemu_pauseCmd)
}
25 changes: 25 additions & 0 deletions cli/command/guest/qemu/guest-qemu-reset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package qemu

import (
"github.com/Telmate/proxmox-api-go/cli"
"github.com/Telmate/proxmox-api-go/proxmox"
"github.com/spf13/cobra"
)

var qemu_resetCmd = &cobra.Command{
Use: "reset GUESTID",
Short: "Resets the speciefid guest",
RunE: func(cmd *cobra.Command, args []string) (err error) {
vmr := proxmox.NewVmRef(cli.ValidateIntIDset(args, "GuestID"))
c := cli.NewClient()
_, err = c.StartVm(vmr)
if err == nil {
cli.PrintGuestStatus(qemuCmd.OutOrStdout(), vmr.VmId(), "reset")
}
return
},
}

func init() {
qemuCmd.AddCommand(qemu_resetCmd)
}
25 changes: 25 additions & 0 deletions cli/command/guest/qemu/guest-qemu-resume.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package qemu

import (
"github.com/Telmate/proxmox-api-go/cli"
"github.com/Telmate/proxmox-api-go/proxmox"
"github.com/spf13/cobra"
)

var qemu_resumeCmd = &cobra.Command{
Use: "resume GUESTID",
Short: "Resumes the speciefid guest",
RunE: func(cmd *cobra.Command, args []string) (err error) {
vmr := proxmox.NewVmRef(cli.ValidateIntIDset(args, "GuestID"))
c := cli.NewClient()
_, err = c.ResumeVm(vmr)
if err == nil {
cli.PrintGuestStatus(qemuCmd.OutOrStdout(), vmr.VmId(), "resumed")
}
return
},
}

func init() {
qemuCmd.AddCommand(qemu_resumeCmd)
}
15 changes: 15 additions & 0 deletions cli/command/guest/qemu/guest-qemu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package qemu

import (
"github.com/Telmate/proxmox-api-go/cli/command/guest"
"github.com/spf13/cobra"
)

var qemuCmd = &cobra.Command{
Use: "qemu",
Short: "Commands to interact with the qemu guests on Proxmox",
}

func init() {
guest.GuestCmd.AddCommand(qemuCmd)
}
24 changes: 14 additions & 10 deletions cli/print.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
package cli

import (
"encoding/json"
"fmt"
"io"
"encoding/json"
)

func PrintItemCreated(out io.Writer, id, text string){
func PrintGuestStatus(out io.Writer, id int, text string) {
fmt.Fprintf(out, "Guest with id (%d) has been %s\n", id, text)
}

func PrintItemCreated(out io.Writer, id, text string) {
fmt.Fprintf(out, "%s (%s) has been created\n", text, id)
}

func PrintItemUpdated(out io.Writer, id, text string){
func PrintItemUpdated(out io.Writer, id, text string) {
fmt.Fprintf(out, "%s (%s) has been updated\n", text, id)
}

func PrintItemDeleted(out io.Writer, id, text string){
func PrintItemDeleted(out io.Writer, id, text string) {
fmt.Fprintf(out, "%s (%s) has been deleted\n", text, id)
}

func PrintItemSet(out io.Writer, id, text string){
func PrintItemSet(out io.Writer, id, text string) {
fmt.Fprintf(out, "%s (%s) has been configured\n", text, id)
}

func PrintRawJson(out io.Writer, input interface{}){
func PrintRawJson(out io.Writer, input interface{}) {
list, err := json.Marshal(input)
LogFatalError(err)
fmt.Fprintln(out,string(list))
fmt.Fprintln(out, string(list))
}

func PrintFormattedJson(out io.Writer, input interface{}){
func PrintFormattedJson(out io.Writer, input interface{}) {
list, err := json.MarshalIndent(input, "", " ")
LogFatalError(err)
fmt.Fprintln(out,string(list))
}
fmt.Fprintln(out, string(list))
}
Loading

0 comments on commit 422dd16

Please sign in to comment.