forked from Telmate/proxmox-api-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Telmate#194 from Tinyblargon/CLI-Overhaul
Cli overhaul: guest commands (no tests yet)
- Loading branch information
Showing
14 changed files
with
310 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
Oops, something went wrong.