Skip to content

Commit

Permalink
Merge pull request #177 from Tinyblargon/CLI-Overhaul
Browse files Browse the repository at this point in the history
Cli overhaul
  • Loading branch information
mleone87 committed May 9, 2022
2 parents aee9f5e + b3c6385 commit 69c923c
Show file tree
Hide file tree
Showing 39 changed files with 1,272 additions and 3 deletions.
94 changes: 94 additions & 0 deletions cli/cobra.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package cli

import (
"crypto/tls"
"fmt"
"io/ioutil"
"os"
"regexp"

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

// Global else the nested folders dont work
var RootCmd = &cobra.Command{
Use: "proxmox-api-go",
Short: "Application to configure Proxmox from the Api",
}

func init() {
RootCmd.PersistentFlags().BoolP("insecure", "i", false, "TLS insecure mode")
RootCmd.PersistentFlags().BoolP("debug", "d", false, "debug mode")
RootCmd.PersistentFlags().IntP("timeout", "t", 300, "api task timeout in seconds")
RootCmd.PersistentFlags().StringP("file", "f", "", "file to get the config from")
RootCmd.PersistentFlags().StringP("proxyurl", "p", "", "proxy url to connect to")
}

func Execute() (err error) {
if err = RootCmd.Execute(); err != nil {
return
}
return
}

func NewClient()(c *proxmox.Client) {
c, err := Client("","","","")
LogFatalError(err)
return
}

func Client(apiUlr, userID, password, otp string) (c *proxmox.Client, err error) {
insecure, _ := RootCmd.Flags().GetBool("insecure")
timeout, _ := RootCmd.Flags().GetInt("timeout")
proxyUrl, _ := RootCmd.Flags().GetString("proxyurl")

tlsconf := &tls.Config{InsecureSkipVerify: true}
if !insecure {
tlsconf = nil
}
if apiUlr == "" {
apiUlr = os.Getenv("PM_API_URL")
}
if userID == "" {
userID = os.Getenv("PM_USER")
}
if password == "" {
password = os.Getenv("PM_PASS")
}
if otp == "" {
otp = os.Getenv("PM_OTP")
}
c, err = proxmox.NewClient(apiUlr, nil, tlsconf, proxyUrl, timeout)
LogFatalError(err)
if userRequiresAPIToken(userID) {
c.SetAPIToken(userID, password)
// As test, get the version of the server
_, err = c.GetVersion()
if err != nil {
err = fmt.Errorf("login error: %s", err)
}
} else {
err = c.Login(userID, password, otp)
}
return
}

var rxUserRequiresToken = regexp.MustCompile("[a-z0-9]+@[a-z0-9]+![a-z0-9]+")

func userRequiresAPIToken(userID string) bool {
return rxUserRequiresToken.MatchString(userID)
}

func NewConfig()(configSource []byte) {
var err error
file, _ := RootCmd.Flags().GetString("file")
if file != "" {
configSource, err = ioutil.ReadFile(file)
LogFatalError(err)
} else {
configSource, err = ioutil.ReadAll(RootCmd.InOrStdin())
LogFatalError(err)
}
return
}
11 changes: 11 additions & 0 deletions cli/command/commands/commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package commands

import (
_ "github.com/Telmate/proxmox-api-go/cli/command/create"
_ "github.com/Telmate/proxmox-api-go/cli/command/delete"
_ "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/list"
_ "github.com/Telmate/proxmox-api-go/cli/command/set"
_ "github.com/Telmate/proxmox-api-go/cli/command/update"
)
29 changes: 29 additions & 0 deletions cli/command/create/create-pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package create

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

var create_poolCmd = &cobra.Command{
Use: "pool POOLID [COMMENT]" ,
Short: "Creates a new pool",
RunE: func(cmd *cobra.Command, args []string) (err error) {
id := cli.ValidateIDset(args, 0 ,"PoolID")
var comment string
if len(args) > 1 {
comment = args[1]
}
c := cli.NewClient()
err = c.CreatePool(id, comment)
if err != nil {
return
}
cli.PrintItemCreated(createCmd.OutOrStdout() ,id, "Pool")
return
},
}

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

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

var createCmd = &cobra.Command{
Use: "create",
Short: "With this command you can create new items in proxmox",
}

func init() {
cli.RootCmd.AddCommand(createCmd)
}
17 changes: 17 additions & 0 deletions cli/command/delete/delete-metricserver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package delete

import (
"github.com/spf13/cobra"
)

var delete_metricserverCmd = &cobra.Command{
Use: "metricserver METRICSID",
Short: "Deletes the speciefied MetricServer",
RunE: func(cmd *cobra.Command, args []string) error {
return DeleteID(args, "MetricServer")
},
}

func init() {
deleteCmd.AddCommand(delete_metricserverCmd)
}
18 changes: 18 additions & 0 deletions cli/command/delete/delete-pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package delete

import (
"github.com/spf13/cobra"
)

var delete_poolCmd = &cobra.Command{
Use: "pool POOLID",
Short: "Deletes the Speciefied pool",
RunE: func(cmd *cobra.Command, args []string) (err error) {
err = DeleteID(args, "Pool")
return
},
}

func init() {
deleteCmd.AddCommand(delete_poolCmd)
}
18 changes: 18 additions & 0 deletions cli/command/delete/delete-user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package delete

import (
"github.com/spf13/cobra"
)

var delete_userCmd = &cobra.Command{
Use: "user USERID",
Short: "Deletes the speciefied User",
RunE: func(cmd *cobra.Command, args []string) (err error) {
err = DeleteID(args, "User")
return
},
}

func init() {
deleteCmd.AddCommand(delete_userCmd)
}
33 changes: 33 additions & 0 deletions cli/command/delete/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package delete

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

var deleteCmd = &cobra.Command{
Use: "delete",
Short: "With this command you can delete existing items from proxmox",
}

func init() {
cli.RootCmd.AddCommand(deleteCmd)
}

func DeleteID(args []string, IDtype string) (err error){
id := cli.ValidateIDset(args, 0, IDtype+"ID")
c := cli.NewClient()
switch IDtype {
case "MetricServer" :
err = c.DeleteMetricServer(id)
case "Pool" :
err = c.DeletePool(id)
case "User" :
err = c.DeleteUser(id)
}
if err != nil {
return
}
cli.PrintItemDeleted(deleteCmd.OutOrStdout(), id, IDtype)
return
}
15 changes: 15 additions & 0 deletions cli/command/example/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package example

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

var exampleCmd = &cobra.Command{
Use: "example",
Short: "This function show examples of fully populated config files",
}

func init() {
cli.RootCmd.AddCommand(exampleCmd)
}
31 changes: 31 additions & 0 deletions cli/command/get/get-pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package get

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

var get_poolCmd = &cobra.Command{
Use: "pool POOLID",
Short: "Gets the configuration of the specified Pool",
RunE: func(cmd *cobra.Command, args []string) (err error) {
id := cli.ValidateIDset(args, 0, "PoolID")
c := cli.NewClient()
poolinfo, err := c.GetPoolInfo(id)
if err != nil {
return
}
poolList, err := json.Marshal(poolinfo)
if err != nil {
return
}
fmt.Fprintln(getCmd.OutOrStdout(),string(poolList))
return
},
}

func init() {
getCmd.AddCommand(get_poolCmd)
}
17 changes: 17 additions & 0 deletions cli/command/get/get-user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package get

import (
"github.com/spf13/cobra"
)

var get_userCmd = &cobra.Command{
Use: "user USERID",
Short: "Gets the configuration of the specified User",
RunE: func(cmd *cobra.Command, args []string) error {
return GetConfig(args, "User")
},
}

func init() {
getCmd.AddCommand(get_userCmd)
}
31 changes: 31 additions & 0 deletions cli/command/get/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package get

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

var getCmd = &cobra.Command{
Use: "get",
Short: "get shows the current configuration an item in proxmox",
}

func init() {
cli.RootCmd.AddCommand(getCmd)
}

func GetConfig(args []string, IDtype string) (err error) {
id := cli.ValidateIDset(args, 0, IDtype+"ID")
c := cli.NewClient()
var config interface{}
switch IDtype {
case "User" :
config, err = proxmox.NewConfigUserFromApi(id, c)
}
if err != nil {
return
}
cli.PrintFormattedJson(getCmd.OutOrStdout(),config)
return
}
18 changes: 18 additions & 0 deletions cli/command/list/list-acmeaccounts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package list

import (
"github.com/spf13/cobra"
)

var list_acmeaccountsCmd = &cobra.Command{
Use: "acmeaccounts",
Short: "Prints a list of AcmeAccounts in raw json format",
Run: func(cmd *cobra.Command, args []string) {
ListRaw("AcmeAccounts")
},
}


func init() {
listCmd.AddCommand(list_acmeaccountsCmd)
}
18 changes: 18 additions & 0 deletions cli/command/list/list-acmeplugins.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package list

import (
"github.com/spf13/cobra"
)

var list_acmepluginsCmd = &cobra.Command{
Use: "acmeplugins",
Short: "Prints a list of AcmePlugins in raw json format",
Run: func(cmd *cobra.Command, args []string) {
ListRaw("AcmePlugins")
},
}


func init() {
listCmd.AddCommand(list_acmepluginsCmd)
}
17 changes: 17 additions & 0 deletions cli/command/list/list-guests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package list

import (
"github.com/spf13/cobra"
)

var list_qemuguestsCmd = &cobra.Command{
Use: "guests",
Short: "Prints a list of Qemu/Lxc Guests in raw json format",
Run: func(cmd *cobra.Command, args []string) {
ListRaw("Guests")
},
}

func init() {
listCmd.AddCommand(list_qemuguestsCmd)
}
Loading

0 comments on commit 69c923c

Please sign in to comment.