Skip to content

Commit

Permalink
Supporting arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Dec 30, 2015
1 parent 0b52b2e commit e83a5fd
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 11 deletions.
26 changes: 20 additions & 6 deletions appspot/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"

"github.com/moul/showcase"
Expand All @@ -18,16 +19,29 @@ func init() {
func actionHandler(w http.ResponseWriter, r *http.Request) {
path := strings.TrimLeft(r.URL.Path, "/")
if fn, found := moulshowcase.Actions()[path]; found {
args := []string{}
ret, err := fn(args)
// parse CLI arguments
u, err := url.Parse(r.URL.String())
if err != nil {
http.Error(w, fmt.Sprintf("failed to parse url %q: %v", r.URL.String(), err), 500)
}

// call action
ret, err := fn(u.RawQuery)

// render result
if err != nil {
http.Error(w, fmt.Sprintf("service error: %v\n", err), 500)
} else {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
enc := json.NewEncoder(w)
if err := enc.Encode(&ret); err != nil {
http.Error(w, fmt.Sprintf("json encode error: %v\n", err), 500)
switch ret.ContentType {
case "application/json":
w.Header().Set("Content-Type", "application/json; charset=utf-8")
enc := json.NewEncoder(w)
if err := enc.Encode(&(ret.Body)); err != nil {
http.Error(w, fmt.Sprintf("json encode error: %v\n", err), 500)
}
case "text/plain":
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
fmt.Fprintf(w, "%s", ret.Body)
}
}
} else {
Expand Down
26 changes: 24 additions & 2 deletions cmd/moul-showcase/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package main
import (
"encoding/json"
"fmt"
"net/url"
"os"
"strings"

"github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
Expand Down Expand Up @@ -36,11 +38,26 @@ func main() {

func CliActionCallback(c *cli.Context) {
action := c.Command.Name
ret, err := moulshowcase.Actions()[action](c.Args())

// parse CLI arguments
args := []string{}
for _, arg := range c.Args() {
if arg[:2] == "--" {
args = append(args, arg[2:])

} else {
args = append(args, fmt.Sprintf("arg=%s", arg))
}
}
qs := strings.Join(args, "&")

// call action
ret, err := moulshowcase.Actions()[action](qs)
if err != nil {
logrus.Fatalf("Failed to execute %q: %v", action, err)
}

// render result
switch ret.ContentType {
case "application/json":
out, err := json.MarshalIndent(ret.Body, "", " ")
Expand Down Expand Up @@ -69,7 +86,12 @@ func Daemon(c *cli.Context) {
})
for action, fn := range moulshowcase.Actions() {
r.GET(fmt.Sprintf("/%s", action), func(c *gin.Context) {
ret, err := fn(nil)
u, err := url.Parse(c.Request.URL.String())
if err != nil {
c.String(500, fmt.Sprintf("failed to parse url %q: %v", c.Request.URL.String(), err))
}

ret, err := fn(u.RawQuery)
if err != nil {
c.JSON(500, gin.H{
"err": err,
Expand Down
61 changes: 59 additions & 2 deletions sapin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,72 @@ package moulshowcase

import (
"fmt"
"net/url"

"github.com/gorilla/schema"
"github.com/moul/sapin"
)

func init() {
RegisterAction("sapin", SapinAction)
}

func SapinAction(args []string) (*ActionResponse, error) {
sapin := sapin.NewSapin(5)
func SapinAction(qs string) (*ActionResponse, error) {
// Define arguments
var opts struct {
Size int `schema:"size"`
Balls int `schema:"balls"`
Garlands int `schema:"garlands"`
Star bool `schema:"star"`
Emoji bool `schema:"emoji"`
Color bool `schema:"color"`
Presents bool `schema:"presents"`
}
// FIXME: handle --help

// Set defaults
opts.Size = 5
opts.Color = true
opts.Balls = 4
opts.Star = true
opts.Emoji = false
opts.Presents = true
opts.Garlands = 5

// Parse query
m, err := url.ParseQuery(qs)
if err != nil {
return nil, err
}
if len(m) > 0 {
// FIXME: if in web mode -> redirect to have options demo in the URL
decoder := schema.NewDecoder()
if err := decoder.Decode(&opts, m); err != nil {
return nil, err
}
}

// FIXME: validate input (max size etc)

// Create sapin
sapin := sapin.NewSapin(opts.Size)

// Apply options
if opts.Star {
sapin.AddStar()
}
sapin.AddBalls(opts.Balls)
sapin.AddGarlands(opts.Garlands)
if opts.Emoji {
sapin.Emojize()
}
if opts.Color {
// FIXME: handle HTML - see old sapin for example
sapin.Colorize()
}
if opts.Presents {
sapin.AddPresents()
}

return PlainResponse(fmt.Sprintf("%s", sapin)), nil
}
2 changes: 1 addition & 1 deletion showcase.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package moulshowcase

var ActionsMap map[string]Action

type Action func([]string) (*ActionResponse, error)
type Action func(string) (*ActionResponse, error)

func RegisterAction(name string, action Action) {
if ActionsMap == nil {
Expand Down

0 comments on commit e83a5fd

Please sign in to comment.