Skip to content

Commit 804ccde

Browse files
committed
Merge branch 'master' of github.com:Shipu/artifact
2 parents e359ab0 + 0bbd539 commit 804ccde

File tree

6 files changed

+118
-5
lines changed

6 files changed

+118
-5
lines changed

app.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func Run() {
6262
port = 8080
6363
}
6464

65-
fmt.Sprintf("Server is running on port :%d", port)
65+
fmt.Println("Server is running on port", fmt.Sprintf(":%d", port))
6666

6767
Router.Run(fmt.Sprintf(":%d", port))
6868
}

cmd/generate/cmd.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package generate
2+
3+
import (
4+
"github.com/spf13/afero"
5+
"github.com/spf13/cobra"
6+
"log"
7+
)
8+
9+
var MakeCommand = &cobra.Command{
10+
Use: "make:command",
11+
Args: cobra.MinimumNArgs(2),
12+
RunE: makeCommand,
13+
}
14+
15+
func makeCommand(cmd *cobra.Command, args []string) error {
16+
PackageName = args[0]
17+
name := args[1]
18+
19+
PackageRoot = "art"
20+
21+
hasDir, _ := afero.DirExists(afero.NewOsFs(), PackageRoot)
22+
23+
if !hasDir {
24+
afero.NewOsFs().Mkdir(PackageRoot, 0755)
25+
}
26+
27+
fs := afero.NewBasePathFs(afero.NewOsFs(), PackageRoot)
28+
29+
createCMDFolders(fs, name)
30+
createCmdFiles(fs, name)
31+
32+
log.Println("Successfully generated Command file for " + Title(name))
33+
34+
return nil
35+
}
36+
37+
func createCMDFolders(fs afero.Fs, name string) {
38+
fs.Mkdir(name, 0755)
39+
fs.Mkdir(name+"/commands", 0755)
40+
}
41+
42+
func createCmdFiles(fs afero.Fs, name string) {
43+
createFile(fs, name, "stubs/cmd.stub", name+"/commands/"+name+"Command.go")
44+
}

cmd/generate/stubs/cmd.stub

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package commands
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
)
6+
7+
var {{TitleName}}Cmd = &cobra.Command{
8+
Use: "{{PackageName}}",
9+
RunE: {{TitleName}}CommandHandle,
10+
}
11+
12+
func {{TitleName}}CommandHandle(cmd *cobra.Command, args []string) error {
13+
return nil
14+
}

cmd/main.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"log"
77
)
88

9-
var rootCmd = &cobra.Command{
9+
var RootCmd = &cobra.Command{
1010
Use: "art",
1111
Short: "A simple artifact command",
1212
Long: `A simple artifact command`,
@@ -17,12 +17,18 @@ func hello(cmd *cobra.Command, args []string) {
1717
log.Println("art command")
1818
}
1919

20+
// AddCommand add custom command function if necessary
21+
func AddCommand(cmd *cobra.Command) {
22+
RootCmd.AddCommand(cmd)
23+
}
24+
2025
func init() {
21-
rootCmd.AddCommand(generate.CrudCmd)
26+
RootCmd.AddCommand(generate.CrudCmd)
27+
RootCmd.AddCommand(generate.MakeCommand)
2228
}
2329

2430
func Execute() {
25-
if err := rootCmd.Execute(); err != nil {
31+
if err := RootCmd.Execute(); err != nil {
2632
log.Fatal(err)
2733
}
2834
}

readme.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,53 @@ var TodoCollection artifact.MongoCollection = artifact.Mongo.Collection("todos")
256256
TodoCollection.Find(bson.M{})
257257
```
258258

259+
## Custom Command
260+
261+
```go
262+
263+
// Define the struct
264+
var TestCmd = &cobra.Command{
265+
Use: "test",
266+
RunE: testCommand,
267+
}
268+
269+
// Write the command function
270+
func testCommand(cmd *cobra.Command, args []string) error {
271+
log.Print("test command")
272+
return nil
273+
}
274+
275+
func main() {
276+
cmd.AddCommand(TestCmd) // Add the command
277+
278+
cmd.Execute()
279+
}
280+
281+
282+
```
283+
284+
You can run command to generate Custom Command file
285+
286+
```go
287+
go run ./art make:command module_name command_name
288+
289+
```
290+
And register in ./art/main.go
291+
```go
292+
package main
293+
294+
import (
295+
"github.com/shipu/artifact/cmd"
296+
"github.com/tenminschool/enrolment-service/art/test/commands"
297+
)
298+
299+
func init() {
300+
cmd.AddCommand(commands.TestCmd) // Register the Custom command
301+
}
302+
303+
func main() {
304+
cmd.Execute()
305+
}
306+
```
307+
259308
All [Go Mongo Driver](https://docs.mongodb.com/drivers/go/current/) Support.

relation_db.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type Database struct {
1212
}
1313

1414
func NewDatabase() *Database {
15-
dsn := Config.GetString(Config.RelationDBConfig+"DB.Username") + ":" + Config.GetString(Config.RelationDBConfig+".Password") + "@tcp(" + Config.GetString(Config.RelationDBConfig+".Host") + ":" + Config.GetString(Config.RelationDBConfig+".Port") + ")/" + Config.GetString(Config.RelationDBConfig+".Database") + "?charset=utf8mb4&parseTime=True&loc=Local"
15+
dsn := Config.GetString(Config.RelationDBConfig+".Username") + ":" + Config.GetString(Config.RelationDBConfig+".Password") + "@tcp(" + Config.GetString(Config.RelationDBConfig+".Host") + ":" + Config.GetString(Config.RelationDBConfig+".Port") + ")/" + Config.GetString(Config.RelationDBConfig+".Database") + "?charset=utf8mb4&parseTime=True&loc=Local"
1616
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
1717

1818
if err != nil {

0 commit comments

Comments
 (0)