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#177 from Tinyblargon/CLI-Overhaul
Cli overhaul
- Loading branch information
Showing
39 changed files
with
1,272 additions
and
3 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
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 | ||
} |
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,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" | ||
) |
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,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) | ||
} |
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 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) | ||
} |
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,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) | ||
} |
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,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) | ||
} |
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,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) | ||
} |
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,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 | ||
} |
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 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) | ||
} |
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,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) | ||
} |
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,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) | ||
} |
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,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 | ||
} |
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,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) | ||
} |
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,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) | ||
} |
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,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) | ||
} |
Oops, something went wrong.