Change CLI command structure

This commit is contained in:
AntoineH 2024-09-03 16:50:05 +02:00
parent 42b89b1586
commit 0f5d2df8a5
10 changed files with 74 additions and 30 deletions

View file

@ -2,11 +2,13 @@
Copyright © 2024 Antoine Harle antoine.harle@proton.me
*/
package cmd
package controller
import (
"fmt"
"github.com/AntoineHX/multi-motors-controller/src/cmd"
"github.com/spf13/cobra"
)
@ -21,7 +23,7 @@ var controllerCmd = &cobra.Command{
}
func init() {
rootCmd.AddCommand(controllerCmd)
cmd.RootCmd.AddCommand(controllerCmd)
// Here you will define your flags and configuration settings.

View file

@ -2,7 +2,7 @@
Copyright © 2024 Antoine Harle antoine.harle@proton.me
*/
package cmd
package controller
import (
"fmt"

View file

@ -2,7 +2,7 @@
Copyright © 2024 Antoine Harle antoine.harle@proton.me
*/
package cmd
package controller
import (
"fmt"
@ -13,21 +13,17 @@ import (
// serveCmd represents the serve command
var serveCmd = &cobra.Command{
Use: "serve",
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.`,
Short: "serve command descritpion",
Long: `serve command descritpion`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("serve called with: Port ", cmd.Flag("port").Value, " - ID ", cmd.Flag("id").Value)
fmt.Println("serve called with: Port ", cmd.Flag("port").Value)
fmt.Println(cmd.CommandPath())
},
}
func init() {
controllerCmd.AddCommand(serveCmd)
motorCmd.AddCommand(serveCmd)
// motorCmd.AddCommand(serveCmd)
// Here you will define your flags and configuration settings.
@ -38,6 +34,4 @@ func init() {
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
serveCmd.Flags().Uint16P("port", "p", 8080, "Port")
//TODO: Support Motor ID flag
}
}

View file

@ -2,7 +2,7 @@
Copyright © 2024 Antoine Harle antoine.harle@proton.me
*/
package cmd
package controller
import (
"fmt"

View file

@ -2,7 +2,7 @@
Copyright © 2024 Antoine Harle antoine.harle@proton.me
*/
package cmd
package motor
import (
"fmt"

View file

@ -2,11 +2,13 @@
Copyright © 2024 Antoine Harle antoine.harle@proton.me
*/
package cmd
package motor
import (
"fmt"
"github.com/AntoineHX/multi-motors-controller/src/cmd"
"github.com/spf13/cobra"
)
@ -21,7 +23,7 @@ var motorCmd = &cobra.Command{
}
func init() {
rootCmd.AddCommand(motorCmd)
cmd.RootCmd.AddCommand(motorCmd)
// Here you will define your flags and configuration settings.

View file

@ -2,7 +2,7 @@
Copyright © 2024 Antoine Harle antoine.harle@proton.me
*/
package cmd
package motor
import (
"fmt"

42
src/cmd/motor/serve.go Normal file
View file

@ -0,0 +1,42 @@
/*
Copyright © 2024 Antoine Harle antoine.harle@proton.me
*/
package motor
import (
"fmt"
"github.com/spf13/cobra"
)
// serveCmd represents the serve command
var serveCmd = &cobra.Command{
Use: "serve",
Short: "serve command descritpion",
Long: `serve command descritpion`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("serve called with ID ", cmd.Flag("id").Value)
fmt.Println(cmd.CommandPath())
},
}
func init() {
motorCmd.AddCommand(serveCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// serveCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// serveCmd.Flags().Uint16P("port", "p", 8080, "Port")
//TODO: Support Motor ID flag
}
func controller() {
}

View file

@ -14,8 +14,8 @@ import (
var cfgFile string
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "src",
Short: "Multi Motors Controller",
Long: `Root command of Multi Motors Controller`,
@ -26,9 +26,9 @@ var rootCmd = &cobra.Command{
}
// 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.
// This is called by main.main(). It only needs to happen once to the RootCmd.
func Execute() {
err := rootCmd.Execute()
err := RootCmd.Execute()
if err != nil {
os.Exit(1)
}
@ -41,13 +41,13 @@ func init() {
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.cobra.yaml)")
//rootCmd.MarkFlagRequired("config")
// viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config"))
RootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.cobra.yaml)")
//RootCmd.MarkFlagRequired("config")
// viper.BindPFlag("config", RootCmd.PersistentFlags().Lookup("config"))
// 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")
RootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
// initConfig reads in config file and ENV variables if set.

View file

@ -4,7 +4,11 @@ Copyright © 2024 Antoine Harle antoine.harle@proton.me
*/
package main
import "github.com/AntoineHX/multi-motors-controller/src/cmd"
import (
"github.com/AntoineHX/multi-motors-controller/src/cmd"
_ "github.com/AntoineHX/multi-motors-controller/src/cmd/controller"
_ "github.com/AntoineHX/multi-motors-controller/src/cmd/motor"
)
func main() {
cmd.Execute()