Created
September 5, 2024 18:14
-
-
Save ignaciogutierrez/d495bbe5f66271fc8a5ccc242e4944cb to your computer and use it in GitHub Desktop.
Golang Cobra Example
This file contains hidden or 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
package cmd | |
import ( | |
"fmt" | |
"os" | |
"github.com/spf13/cobra" | |
) | |
func init() { | |
rootCmd.AddCommand(versionCmd) | |
rootCmd.AddCommand(migrateCmd) | |
} | |
var versionCmd = &cobra.Command{ | |
Use: "version", | |
Short: "Print the version number of Hugo", | |
Long: `All software has versions. This is Hugo's`, | |
Run: func(cmd *cobra.Command, args []string) { | |
fmt.Println("Hugo Static Site Generator v1.9 ") | |
}, | |
} | |
var migrateCmd = &cobra.Command{ | |
Use: "migratedb", | |
Short: "Actualiza la DB ejecutando comandos en folder /migrations", | |
Long: `Algo largo aqui`, | |
Run: func(cmd *cobra.Command, args []string) { | |
fmt.Println("Ejecutando migraciones ...") | |
}, | |
} | |
var rootCmd = &cobra.Command{ | |
Use: "cobra", | |
Short: "Cobra es una prueba de cobra pkg", | |
Long: `A Fast and Flexible Static Site Generator built with | |
love by spf13 and friends in Go. | |
Complete documentation is available at http://hugo.spf13.com`, | |
Run: func(cmd *cobra.Command, args []string) { | |
fmt.Printf("Running API Cobra...") | |
}, | |
} | |
func Execute() { | |
if err := rootCmd.Execute(); err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
} |
This file contains hidden or 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
package main | |
import ( | |
"fmt" | |
"sait/cobra/cmd" | |
) | |
func main() { | |
cmd.Execute() | |
fmt.Printf("Adios mundo cruel !\n") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment