Skip to content

Commit

Permalink
Merge pull request #4 from sagar290/RegisterCommand
Browse files Browse the repository at this point in the history
Feature: 🚀 Register Custom Command
  • Loading branch information
Shipu authored Mar 7, 2023
2 parents e359ab0 + 01b8e88 commit b15d8ac
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 3 deletions.
46 changes: 46 additions & 0 deletions cmd/generate/cmd.go
Original file line number Diff line number Diff line change
@@ -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")
}
14 changes: 14 additions & 0 deletions cmd/generate/stubs/cmd.stub
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 8 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand All @@ -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)
}
}
49 changes: 49 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

0 comments on commit b15d8ac

Please sign in to comment.