Skip to content

Commit

Permalink
Beginning of a new cli using cobra
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinyblargon committed Apr 27, 2022
1 parent a4743a8 commit 8d8762b
Show file tree
Hide file tree
Showing 13 changed files with 176 additions and 3 deletions.
25 changes: 25 additions & 0 deletions cli/cobra.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cli

import (
"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() {
if err := RootCmd.Execute(); err != nil {
LogFatalError(err)
}
}
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"
)
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)
}
15 changes: 15 additions & 0 deletions cli/command/delete/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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)
}
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)
}
15 changes: 15 additions & 0 deletions cli/command/get/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package get

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

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

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

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

var listCmd = &cobra.Command{
Use: "list",
Short: "Lists all items of the same kind from proxmox",
}

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

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

var setCmd = &cobra.Command{
Use: "set",
Short: "This command sets the current configuration of an item",
Long: `This command sets the current configuration of an item.
Depending on if the item already exists the item will be created or updated.`,
}

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

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

var updateCmd = &cobra.Command{
Use: "update",
Short: "With this command you can update existing items within proxmox",
}

func init() {
cli.RootCmd.AddCommand(updateCmd)
}
11 changes: 11 additions & 0 deletions cli/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package cli

import (
"log"
)

func LogFatalError(err error) {
if err != nil {
log.Fatal(err)
}
}
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ module github.com/Telmate/proxmox-api-go

go 1.17

require github.com/stretchr/testify v1.6.1
require (
github.com/spf13/cobra v1.4.0
github.com/stretchr/testify v1.6.1
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q=
github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
9 changes: 7 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ import (
"io/ioutil"

"github.com/Telmate/proxmox-api-go/proxmox"
"github.com/Telmate/proxmox-api-go/cli"
_ "github.com/Telmate/proxmox-api-go/cli/command/commands"
)

func main() {
var insecure *bool
insecure = flag.Bool("insecure", false, "TLS insecure mode")
if os.Getenv("NEW_CLI") == "true" {
cli.Execute()
os.Exit(0)
}
insecure := flag.Bool("insecure", false, "TLS insecure mode")
proxmox.Debug = flag.Bool("debug", false, "debug mode")
fConfigFile := flag.String("file", "", "file to get the config from")
taskTimeout := flag.Int("timeout", 300, "api task timeout in seconds")
Expand Down

0 comments on commit 8d8762b

Please sign in to comment.