Skip to content

Commit

Permalink
Add Shared Guest Commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinyblargon committed Sep 13, 2022
1 parent e16a79f commit a12c5c5
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 10 deletions.
1 change: 1 addition & 0 deletions cli/command/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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/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)
}
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))
}

0 comments on commit a12c5c5

Please sign in to comment.