diff --git a/cmd/generate/cmd.go b/cmd/generate/cmd.go new file mode 100644 index 0000000..59d22fa --- /dev/null +++ b/cmd/generate/cmd.go @@ -0,0 +1,46 @@ +package generate + +import ( + "fmt" + "github.com/spf13/afero" + "github.com/spf13/cobra" + "log" +) + +var MakeCommand = &cobra.Command{ + Use: "make:command", + Args: cobra.MinimumNArgs(2), + RunE: makeCommand, +} + +func makeCommand(cmd *cobra.Command, args []string) error { + PackageName = args[0] + name := args[1] + + PackageRoot = "art" + + hasDir, _ := afero.DirExists(afero.NewOsFs(), PackageRoot) + + if !hasDir { + afero.NewOsFs().Mkdir(PackageRoot, 0755) + } + + fs := afero.NewBasePathFs(afero.NewOsFs(), PackageRoot) + + createCMDFolders(fs, name) + createCmdFiles(fs, name) + + log.Println("Successfully generated Command file for " + Title(name)) + + return nil +} + + +func createCMDFolders(fs afero.Fs, name string) { + fs.Mkdir(name, 0755) + fs.Mkdir(name+"/commands", 0755) +} + +func createCmdFiles(fs afero.Fs, name string) { + createFile(fs, name, "stubs/cmd.stub", name+"/commands/"+name+"Command.go") +} diff --git a/cmd/generate/stubs/cmd.stub b/cmd/generate/stubs/cmd.stub new file mode 100644 index 0000000..dab91c1 --- /dev/null +++ b/cmd/generate/stubs/cmd.stub @@ -0,0 +1,14 @@ +package commands + +import ( + "github.com/spf13/cobra" +) + +var {{TitleName}}Cmd = &cobra.Command{ + Use: "{{PackageName}}", + RunE: {{TitleName}}CommandHandle, +} + +func {{TitleName}}CommandHandle(cmd *cobra.Command, args []string) error { + return nil +} \ No newline at end of file diff --git a/cmd/main.go b/cmd/main.go index f142203..8072c1d 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -6,7 +6,7 @@ import ( "log" ) -var rootCmd = &cobra.Command{ +var RootCmd = &cobra.Command{ Use: "art", Short: "A simple artifact command", Long: `A simple artifact command`, @@ -17,12 +17,17 @@ func hello(cmd *cobra.Command, args []string) { log.Println("art command") } +// add command if necessary +func AddCommand(cmd *cobra.Command) { + RootCmd.AddCommand(cmd) +} + func init() { - rootCmd.AddCommand(generate.CrudCmd) + RootCmd.AddCommand(generate.CrudCmd) } func Execute() { - if err := rootCmd.Execute(); err != nil { + if err := RootCmd.Execute(); err != nil { log.Fatal(err) } } diff --git a/readme.md b/readme.md index fbef5b9..a80a408 100644 --- a/readme.md +++ b/readme.md @@ -256,4 +256,53 @@ var TodoCollection artifact.MongoCollection = artifact.Mongo.Collection("todos") TodoCollection.Find(bson.M{}) ``` +## Custom Command + +```go + +// Define the struct +var TestCmd = &cobra.Command{ + Use: "test", + RunE: testCommand, +} + +// Write the command function +func testCommand(cmd *cobra.Command, args []string) error { + log.Print("test command") + return nil +} + +func main() { + cmd.AddCommand(TestCmd) // Add the command + + cmd.Execute() +} + + +``` + +You can run command to generate Custom Command file + +```go +go run ./art make:command module_name command_name + +``` +And register in ./art/main.go +```go +package main + +import ( + "github.com/shipu/artifact/cmd" + "github.com/tenminschool/enrolment-service/art/test/commands" +) + +func init() { + cmd.AddCommand(commands.TestCmd) // Register the Custom command +} + +func main() { + cmd.Execute() +} +``` + All [Go Mongo Driver](https://docs.mongodb.com/drivers/go/current/) Support. \ No newline at end of file