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

View file

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

View file

@ -2,7 +2,7 @@
Copyright © 2024 Antoine Harle antoine.harle@proton.me Copyright © 2024 Antoine Harle antoine.harle@proton.me
*/ */
package cmd package controller
import ( import (
"fmt" "fmt"
@ -13,21 +13,17 @@ import (
// serveCmd represents the serve command // serveCmd represents the serve command
var serveCmd = &cobra.Command{ var serveCmd = &cobra.Command{
Use: "serve", Use: "serve",
Short: "A brief description of your command", Short: "serve command descritpion",
Long: `A longer description that spans multiple lines and likely contains examples Long: `serve command descritpion`,
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) { 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() { func init() {
controllerCmd.AddCommand(serveCmd) controllerCmd.AddCommand(serveCmd)
motorCmd.AddCommand(serveCmd) // motorCmd.AddCommand(serveCmd)
// Here you will define your flags and configuration settings. // 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 // Cobra supports local flags which will only run when this command
// is called directly, e.g.: // is called directly, e.g.:
serveCmd.Flags().Uint16P("port", "p", 8080, "Port") 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 Copyright © 2024 Antoine Harle antoine.harle@proton.me
*/ */
package cmd package controller
import ( import (
"fmt" "fmt"

View file

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

View file

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

View file

@ -2,7 +2,7 @@
Copyright © 2024 Antoine Harle antoine.harle@proton.me Copyright © 2024 Antoine Harle antoine.harle@proton.me
*/ */
package cmd package motor
import ( import (
"fmt" "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 var cfgFile string
// rootCmd represents the base command when called without any subcommands // RootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{ var RootCmd = &cobra.Command{
Use: "src", Use: "src",
Short: "Multi Motors Controller", Short: "Multi Motors Controller",
Long: `Root command of 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. // 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() { func Execute() {
err := rootCmd.Execute() err := RootCmd.Execute()
if err != nil { if err != nil {
os.Exit(1) os.Exit(1)
} }
@ -41,13 +41,13 @@ func init() {
// Cobra supports persistent flags, which, if defined here, // Cobra supports persistent flags, which, if defined here,
// will be global for your application. // will be global for your application.
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.cobra.yaml)") RootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.cobra.yaml)")
//rootCmd.MarkFlagRequired("config") //RootCmd.MarkFlagRequired("config")
// viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config")) // viper.BindPFlag("config", RootCmd.PersistentFlags().Lookup("config"))
// Cobra also supports local flags, which will only run // Cobra also supports local flags, which will only run
// when this action is called directly. // 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. // 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 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() { func main() {
cmd.Execute() cmd.Execute()