From 68c2c29fa977244c6b59e7063b282bd23b1f7bca Mon Sep 17 00:00:00 2001 From: Matthew Stobbs Date: Fri, 29 Mar 2024 14:48:21 -0600 Subject: [PATCH] add cobra for cli management --- cmd/clusterctl/LICENSE | 9 +++++++ cmd/clusterctl/cmd/config.go | 40 +++++++++++++++++++++++++++ cmd/clusterctl/cmd/daemon.go | 34 +++++++++++++++++++++++ cmd/clusterctl/cmd/root.go | 52 ++++++++++++++++++++++++++++++++++++ cmd/clusterctl/main.go | 11 ++++++++ cmd/clustvirt.go | 29 -------------------- go.mod | 3 +++ go.sum | 9 +++++++ 8 files changed, 158 insertions(+), 29 deletions(-) create mode 100644 cmd/clusterctl/LICENSE create mode 100644 cmd/clusterctl/cmd/config.go create mode 100644 cmd/clusterctl/cmd/daemon.go create mode 100644 cmd/clusterctl/cmd/root.go create mode 100644 cmd/clusterctl/main.go delete mode 100644 cmd/clustvirt.go diff --git a/cmd/clusterctl/LICENSE b/cmd/clusterctl/LICENSE new file mode 100644 index 0000000..4286601 --- /dev/null +++ b/cmd/clusterctl/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) 2024 stobbsm + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/cmd/clusterctl/cmd/config.go b/cmd/clusterctl/cmd/config.go new file mode 100644 index 0000000..dee68e6 --- /dev/null +++ b/cmd/clusterctl/cmd/config.go @@ -0,0 +1,40 @@ +/* +Copyright © 2024 NAME HERE + +*/ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +// configCmd represents the config command +var configCmd = &cobra.Command{ + Use: "config", + Short: "A brief description of your command", + Long: `A longer description that spans multiple lines and likely contains examples +and usage of using your command. For example: + +Cobra is a CLI library for Go that empowers applications. +This application is a tool to generate the needed files +to quickly create a Cobra application.`, + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("config called") + }, +} + +func init() { + rootCmd.AddCommand(configCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // configCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // configCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/cmd/clusterctl/cmd/daemon.go b/cmd/clusterctl/cmd/daemon.go new file mode 100644 index 0000000..42c42ca --- /dev/null +++ b/cmd/clusterctl/cmd/daemon.go @@ -0,0 +1,34 @@ +/* +Copyright © 2024 NAME HERE +*/ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +// daemonCmd represents the daemon command +var daemonCmd = &cobra.Command{ + Use: "daemon", + Short: "Control the clustvirt daemon process", + Long: `Used to start, stop, restart and reload the clustvirt daemon process`, + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("daemon called") + }, +} + +func init() { + rootCmd.AddCommand(daemonCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // daemonCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // daemonCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/cmd/clusterctl/cmd/root.go b/cmd/clusterctl/cmd/root.go new file mode 100644 index 0000000..497453d --- /dev/null +++ b/cmd/clusterctl/cmd/root.go @@ -0,0 +1,52 @@ +// Copyright Matthew Stobbs +// +// Package cmd contains the main application file for ClustVirt. +// This command is used for everything, implementing the command pattern +// with the help of spf13/cobra. +// After building, the executable's first argument must be one of the valid +// commands, such as `daemon`, `api`, `query`, `host`, etc. This pattern allows +// for a simple way to access functionality, reducing the need to use the +// REST api directly. +package cmd + +import ( + "os" + + "github.com/spf13/cobra" +) + +// rootCmd represents the base command when called without any subcommands +var rootCmd = &cobra.Command{ + Use: "clusterctl", + Short: "A brief description of your application", + Long: `A longer description that spans multiple lines and likely contains +examples and usage of using your application. For example: + +Cobra is a CLI library for Go that empowers applications. +This application is a tool to generate the needed files +to quickly create a Cobra application.`, + // Uncomment the following line if your bare application + // has an action associated with it: + // Run: func(cmd *cobra.Command, args []string) { }, +} + +// Execute adds all child commands to the root command and sets flags appropriately. +// This is called by main.main(). It only needs to happen once to the rootCmd. +func Execute() { + err := rootCmd.Execute() + if err != nil { + os.Exit(1) + } +} + +func init() { + // Here you will define your flags and configuration settings. + // Cobra supports persistent flags, which, if defined here, + // will be global for your application. + + // rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.clusterctl.yaml)") + + // Cobra also supports local flags, which will only run + // when this action is called directly. + rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/cmd/clusterctl/main.go b/cmd/clusterctl/main.go new file mode 100644 index 0000000..dc9bc7c --- /dev/null +++ b/cmd/clusterctl/main.go @@ -0,0 +1,11 @@ +/* +Copyright © 2024 NAME HERE + +*/ +package main + +import "git.staur.ca/stobbsm/clustvirt/cmd/clusterctl/cmd" + +func main() { + cmd.Execute() +} diff --git a/cmd/clustvirt.go b/cmd/clustvirt.go deleted file mode 100644 index 8ba1012..0000000 --- a/cmd/clustvirt.go +++ /dev/null @@ -1,29 +0,0 @@ -// Package cmd contains the main application file for ClustVirt. -// This command is used for everything, implementing the command pattern -// with the help of spf13/cobra. -// After building, the executable's first argument must be one of the valid -// commands, such as `daemon`, `api`, `query`, `host`, etc. This pattern allows -// for a simple way to access functionality, reducing the need to use the -// REST api directly. -package cmd - -// Global Arguments: -// All commands can use the following arguments: -// --output none|json|xml: Set the output type, none being just plain log, -// json outputing as a JSON document, and XML outputing as an XML document - -// Commands planned: -// config: actions associated with the configuration of clustvirt -// set: set a configuration value -// get: get the current value of a configuration options -// list: list available configuration options -// daemon: actions associated with running clustvirt in daemon mode -// start: start the daemon -// stop: safely stop the daemon -// status: query the status of the daemon -// api: query the REST api. There must a clustvirt daemon running for this to function -// host: actions associated with a specific host in the cluster -// vm: actions asociated with a specifc vm in the cluster -// storagepool: actions associated with storage pools -// auth: actions associated with authentication -// network: actions to define networks diff --git a/go.mod b/go.mod index 0e87644..fbcac33 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( github.com/go-chi/docgen v1.2.0 // indirect github.com/go-chi/jwtauth v1.2.0 // indirect github.com/goccy/go-json v0.3.5 // indirect + github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/lestrrat-go/backoff/v2 v2.0.7 // indirect github.com/lestrrat-go/httpcc v1.0.0 // indirect github.com/lestrrat-go/iter v1.0.0 // indirect @@ -25,6 +26,8 @@ require ( github.com/mattn/go-isatty v0.0.20 // indirect github.com/mitchellh/go-homedir v1.0.0 // indirect github.com/pkg/errors v0.9.1 // indirect + github.com/spf13/cobra v1.8.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect golang.org/x/sys v0.15.0 // indirect ) diff --git a/go.sum b/go.sum index 42e455f..5ae1ca6 100644 --- a/go.sum +++ b/go.sum @@ -3,6 +3,7 @@ github.com/a-h/templ v0.2.598/go.mod h1:SA7mtYwVEajbIXFRh3vKdYm/4FYyLQAtPH1+KxzG github.com/a-h/templ v0.2.646 h1:F4+tGmPavtPMdWF3ofCDkPx/31qtInztsmUdzu6EzVU= github.com/a-h/templ v0.2.646/go.mod h1:SA7mtYwVEajbIXFRh3vKdYm/4FYyLQAtPH1+KxzGPA8= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= +github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-chi/chi v1.5.1/go.mod h1:REp24E+25iKvxgeTfHmdUoL5x15kBiDBlnIl5bCwe2k= github.com/go-chi/chi/v5 v5.0.1/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= @@ -20,6 +21,8 @@ github.com/goccy/go-json v0.3.5/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGF github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/jaypipes/pcidb v1.0.0 h1:vtZIfkiCUE42oYbJS0TAq9XSfSmcsgo9IdxSm9qzYU8= github.com/jaypipes/pcidb v1.0.0/go.mod h1:TnYUvqhPBzCKnH34KrIX22kAeEbDCSRJ9cqLRCuNDfk= github.com/lestrrat-go/backoff/v2 v2.0.7 h1:i2SeK33aOFJlUNJZzf2IpXRBvqBBnaGXfY5Xaop/GsE= @@ -49,6 +52,11 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0= github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0= +github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho= +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.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= @@ -92,6 +100,7 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= libvirt.org/go/libvirt v1.10001.0 h1:lEVDNE7xfzmZXiDEGIS8NvJSuaz11OjRXw+ufbQEtPY= libvirt.org/go/libvirt v1.10001.0/go.mod h1:1WiFE8EjZfq+FCVog+rvr1yatKbKZ9FaFMZgEqxEJqQ= libvirt.org/go/libvirtxml v1.10001.0 h1:r9WBs24r3mxIG3/hAMRRwDMy4ZaPHmhHjw72o/ceXic=