Skip to content

Commit

Permalink
Support gist reference argument
Browse files Browse the repository at this point in the history
  • Loading branch information
msh5 committed Nov 10, 2019
1 parent 915ac37 commit f3c8bfc
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 10 deletions.
Binary file added boy
Binary file not shown.
51 changes: 44 additions & 7 deletions ifadapter/controller/cmd_controller.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package controller

import (
"errors"
"net/url"
"strings"

"github.com/msh5/boy/app/usecase"
"github.com/msh5/boy/ifadapter/di"
)
Expand All @@ -13,21 +17,54 @@ func NewCommandController(diContainer di.Container) *CommandController {
return &CommandController{diContainer: diContainer}
}

func (c *CommandController) ExecuteSnippet(args []string) error {
func (c *CommandController) ExecuteSnippet(gistRef string, commandArgs []string) error {
gistHandle, err := parseGistReference(gistRef)
if err != nil {
return err
}

params := usecase.SnippetExecParameters{
UserID: args[0],
GistEntryName: args[1],
CommandArgs: args[2:],
UserID: gistHandle.UserID,
GistEntryName: gistHandle.GistName,
CommandArgs: commandArgs,
}

return c.diContainer.GetSnippetExecUsecase().Run(params)
}

func (c *CommandController) ShowSnippet(args []string) error {
func (c *CommandController) ShowSnippet(gistRef string) error {
gistHandle, err := parseGistReference(gistRef)
if err != nil {
return err
}

params := usecase.SnippetShowParameters{
UserID: args[0],
GistEntryName: args[1],
UserID: gistHandle.UserID,
GistEntryName: gistHandle.GistName,
}

return c.diContainer.GetSnippetShowUsecase().Run(params)
}

type gistHandle struct {
UserID string
GistName string
}

func parseGistReference(ref string) (*gistHandle, error) {
url, err := url.Parse("http://" + ref)
if err != nil {
return nil, err
}

segments := strings.Split(url.Path, "/")

switch len(segments) {
case 3:
return &gistHandle{UserID: segments[1], GistName: segments[2]}, nil
case 2:
return &gistHandle{UserID: segments[0], GistName: segments[1]}, nil
}

return nil, errors.New("indicated gist reference is unexpected syntax")
}
2 changes: 1 addition & 1 deletion ifadapter/di/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (b *Builder) RegisterSnippetExecUsecase(usecase usecase.SnippetExecUsecase)

func (b *Builder) RegisterSnippetShowUsecase(usecase usecase.SnippetShowUsecase) {
if err := b.Add(di.Def{
Name: snippetExecUsecaseDIObject,
Name: snippetShowUsecaseDIObject,
Build: func(ctn di.Container) (interface{}, error) {
return usecase, nil
},
Expand Down
5 changes: 4 additions & 1 deletion interface/cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ var execCmd = &cobra.Command{
config := loadCommandConfig()

diContainer := dependency.NewCommandDIContainer(config.toDIContainerBuildParams())
cmdController := controller.NewCommandController(diContainer)

if err := controller.NewCommandController(diContainer).ExecuteSnippet(args); err != nil {
gistRef := args[0]
commentArgs := args[1:]
if err := cmdController.ExecuteSnippet(gistRef, commentArgs); err != nil {
log.Fatal(err)
}
},
Expand Down
4 changes: 3 additions & 1 deletion interface/cmd/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ var showCmd = &cobra.Command{
config := loadCommandConfig()

diContainer := dependency.NewCommandDIContainer(config.toDIContainerBuildParams())
cmdController := controller.NewCommandController(diContainer)

if err := controller.NewCommandController(diContainer).ShowSnippet(args); err != nil {
gistRef := args[0]
if err := cmdController.ShowSnippet(gistRef); err != nil {
log.Fatal(err)
}
},
Expand Down

0 comments on commit f3c8bfc

Please sign in to comment.