Skip to content

Commit

Permalink
create common interface for wapty commands
Browse files Browse the repository at this point in the history
  • Loading branch information
silverweed committed Aug 12, 2017
1 parent 072194e commit 0023e9a
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 41 deletions.
24 changes: 24 additions & 0 deletions common/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package common

import (
"flag"
"fmt"
"os"
)

// Command is used by any package exposing a runnable command to gather information
// about command name, usage and flagset.
type Command struct {
Name string
Run func()
UsageLine string
Short string
Long string
Flag flag.FlagSet
}

func (c *Command) Usage() {
fmt.Fprintf(os.Stderr, "usage: %s\n\n", c.UsageLine)
c.Flag.PrintDefaults()
os.Exit(2)
}
25 changes: 25 additions & 0 deletions decode/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package decode

import "github.com/empijei/wapty/common"

var CmdDecode = common.Command{
Name: "decode",
Run: MainStandalone,
UsageLine: "decode [flags]",
Short: "decode something.",
Long: `decode something in a really clever way:
blah blah blah
`,
}

var flagEncode bool // -encode
var flagCodeclist string // -codec

func init() {
CmdDecode.Flag.BoolVar(&flagEncode, "encode", false, "Sets the decoder to an encoder instead")
CmdDecode.Flag.StringVar(&flagCodeclist, "codec", "smart",
`Sets the decoder/encoder codec. Multiple codecs can be specified and comma separated:
they will be applied one on the output of the previous as in a pipeline.
`)
}
24 changes: 4 additions & 20 deletions decode/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,19 @@ import (
"strings"
)

var FlagEncode *bool
var FlagCodeclist *string

// RegisterFlagStandalone initialiases the flags for the decode package
func RegisterFlagStandalone() {
FlagEncode = flag.Bool("encode", false, "Sets the decoder to an encoder instead")
FlagCodeclist = flag.String("codec", "smart", "Sets the decoder/encoder codec. Multiple codecs can be specified and comma separated, they will be applied one on the output of the previous as in a pipeline.")
}

// MainStandalone parses its own flag and it is the funcion to be run when using
// `wapty decode`. This behaves as a main and expects the "decode" parameter to
// be removed from os.Args.
func MainStandalone() {
if flag.Parsed() == false {
RegisterFlagStandalone()
flag.Usage = func() {
fmt.Fprintln(os.Stderr, "Usage of decoder:")
flag.PrintDefaults()
}
flag.Parse()
}

// FIXME: argument validation should be separated from encoding/decoding
buf := takeInput()
sequence := strings.Split(*FlagCodeclist, ",")
sequence := strings.Split(flagCodeclist, ",")
for i, codec := range sequence {
var c CodecC
var codecNames []string
if codec == "smart" {
if *FlagEncode {
if flagEncode {
fmt.Fprintf(os.Stderr, "Cannot 'smart' encode, please specify a codec")
os.Exit(2)
}
Expand All @@ -54,7 +38,7 @@ func MainStandalone() {
}
}
fmt.Fprintf(os.Stderr, "Codec: %s\n", c.Name())
if *FlagEncode {
if flagEncode {
buf = c.Encode()
} else {
buf = c.Decode()
Expand Down
10 changes: 10 additions & 0 deletions mocksy/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@ package mocksy
import (
"io"
"os"

"github.com/empijei/wapty/common"
)

var outw io.Writer

var CmdMocksy = common.Command{
Name: "mocksy",
Run: Main,
UsageLine: "mocksy",
Short: "mock responses from a server",
Long: "",
}

func init() {
responseHistory = make([]Item, 0)
outw = os.Stderr
Expand Down
58 changes: 37 additions & 21 deletions wapty.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"strings"

"github.com/empijei/wapty/common"
"github.com/empijei/wapty/decode"
"github.com/empijei/wapty/intercept"
"github.com/empijei/wapty/mocksy"
Expand All @@ -20,14 +21,17 @@ var (
Commit string
)

var commands = []struct {
name string
main func()
}{
{"decode", decode.MainStandalone},
{"proxy", proxyMain},
{"mocksy", mocksy.Main},
{"version", func() {
var CmdProxy = common.Command{
Name: "proxy",
Run: proxyMain,
UsageLine: "proxy",
Short: "work as a proxy",
Long: "",
}

var CmdVersion = common.Command{
Name: "version",
Run: func() {
// Setup fallback version and commit in case wapty wasn't "properly" compiled
if len(Version) == 0 {
Version = "Unknown"
Expand All @@ -36,7 +40,17 @@ var commands = []struct {
Commit = "Unknown"
}
fmt.Printf("Version: %s\nCommit: %s\n", Version, Commit)
}},
},
UsageLine: "version",
Short: "print version and exit",
Long: "print version and exit",
}

var commands = []common.Command{
decode.CmdDecode,
CmdProxy,
mocksy.CmdMocksy,
CmdVersion,
}

func init() {
Expand Down Expand Up @@ -65,35 +79,37 @@ func proxyMain() {
}

func invokeMain(s string) {
var toinvoke func()
var success bool
var command common.Command
success := false
for _, cmd := range commands {
if cmd.name == s {
toinvoke = cmd.main
if cmd.Name == s {
command = cmd
success = true
break
}
if strings.HasPrefix(cmd.name, s) {
if toinvoke != nil {
if strings.HasPrefix(cmd.Name, s) {
if command.Run != nil {
fmt.Fprintf(os.Stderr, "Ambiguous command: '%s'. ", s)
success = false
} else {
toinvoke = cmd.main
command = cmd
success = true
}
}
}
if success {
toinvoke()
command.Flag.Usage = command.Usage
command.Flag.Parse(os.Args[1:])
command.Run()
return
}
if toinvoke == nil {
if command.Run == nil {
fmt.Fprintf(os.Stderr, "Command not found: '%s'. ", s)
}

fmt.Fprintln(os.Stderr, "Available commands are: ")
fmt.Fprintln(os.Stderr, "Available commands are:\n")
for _, cmd := range commands {
fmt.Fprintln(os.Stderr, "\t"+cmd.name)
fmt.Fprintln(os.Stderr, "\t"+cmd.Name+"\n\t\t"+cmd.Short)
}
fmt.Fprintln(os.Stderr, "Default command is: proxy")
fmt.Fprintln(os.Stderr, "\nDefault command is: proxy")
}

0 comments on commit 0023e9a

Please sign in to comment.