Use docopt for command arg parsing

This commit is contained in:
Marcin Kulik
2015-03-03 16:13:33 +01:00
parent f38e9bd07a
commit db0a8fd0d6
36 changed files with 4671 additions and 321 deletions

View File

@@ -1,10 +1,8 @@
package commands
import (
"flag"
"fmt"
"github.com/asciinema/asciinema-cli/cli"
"github.com/asciinema/asciinema-cli/util"
)
@@ -13,17 +11,14 @@ type AuthCommand struct {
apiToken string
}
func NewAuthCommand(cfg *util.Config) cli.Command {
func NewAuthCommand(cfg *util.Config) *AuthCommand {
return &AuthCommand{
apiURL: cfg.API.URL,
apiToken: cfg.API.Token,
}
}
func (c *AuthCommand) RegisterFlags(flags *flag.FlagSet) {
}
func (c *AuthCommand) Execute(args []string) error {
func (c *AuthCommand) Execute() error {
fmt.Println("Open the following URL in your browser to register your API token and assign any recorded asciicasts to your profile:")
fmt.Printf("%v/connect/%v\n", c.apiURL, c.apiToken)

View File

@@ -1,35 +1,17 @@
package commands
import (
"errors"
"flag"
"github.com/asciinema/asciinema-cli/asciicast"
"github.com/asciinema/asciinema-cli/cli"
)
import "github.com/asciinema/asciinema-cli/asciicast"
type PlayCommand struct {
Player asciicast.Player
}
func NewPlayCommand() cli.Command {
func NewPlayCommand() *PlayCommand {
return &PlayCommand{
Player: asciicast.NewPlayer(),
}
}
func (c *PlayCommand) RegisterFlags(flags *flag.FlagSet) {
}
func (c *PlayCommand) Execute(args []string) error {
if len(args) == 0 {
return errors.New("filename required. Usage: asciinema play <file>")
}
err := c.Player.Play(args[0])
if err != nil {
return err
}
return nil
func (c *PlayCommand) Execute(filename string) error {
return c.Player.Play(filename)
}

View File

@@ -1,28 +1,22 @@
package commands
import (
"flag"
"fmt"
"io/ioutil"
"os"
"github.com/asciinema/asciinema-cli/api"
"github.com/asciinema/asciinema-cli/asciicast"
"github.com/asciinema/asciinema-cli/cli"
"github.com/asciinema/asciinema-cli/util"
)
type RecordCommand struct {
Cfg *util.Config
API api.API
Recorder asciicast.Recorder
Command string
Title string
NoConfirm bool
MaxWait uint
Cfg *util.Config
API api.API
Recorder asciicast.Recorder
}
func NewRecordCommand(api api.API, cfg *util.Config) cli.Command {
func NewRecordCommand(api api.API, cfg *util.Config) *RecordCommand {
return &RecordCommand{
API: api,
Cfg: cfg,
@@ -30,89 +24,45 @@ func NewRecordCommand(api api.API, cfg *util.Config) cli.Command {
}
}
func (c *RecordCommand) RegisterFlags(flags *flag.FlagSet) {
flags.StringVar(
&c.Command,
"c",
defaultRecCommand(c.Cfg.Record.Command),
"command to record",
)
flags.StringVar(
&c.Title,
"t",
"",
"set asciicast title",
)
flags.BoolVar(
&c.NoConfirm,
"y",
false,
"upload without asking for confirmation",
)
flags.UintVar(
&c.MaxWait,
"max-wait",
0,
"reduce recorded terminal inactivity to maximum of <max-wait> seconds (0 turns off)",
)
}
func (c *RecordCommand) Execute(args []string) error {
var path string
func (c *RecordCommand) Execute(command, title string, assumeYes bool, maxWait uint, filename string) error {
var upload bool
var err error
if len(args) > 0 {
path = args[0]
if filename != "" {
upload = false
} else {
path, err = tmpPath()
filename, err = tmpPath()
if err != nil {
return err
}
upload = true
}
err = c.Recorder.Record(path, c.Command, c.Title, c.MaxWait)
err = c.Recorder.Record(filename, command, title, maxWait)
if err != nil {
return err
}
if upload {
if !c.NoConfirm {
if !assumeYes {
util.Printf("Press <Enter> to upload, <Ctrl-C> to cancel.")
util.ReadLine()
}
url, err := c.API.UploadAsciicast(path)
url, err := c.API.UploadAsciicast(filename)
if err != nil {
util.Warningf("Upload failed, asciicast saved at %v", path)
util.Warningf("Retry later by executing: asciinema upload %v", path)
util.Warningf("Upload failed, asciicast saved at %v", filename)
util.Warningf("Retry later by executing: asciinema upload %v", filename)
return err
}
os.Remove(path)
os.Remove(filename)
fmt.Println(url)
}
return nil
}
func defaultRecCommand(recCommand string) string {
if recCommand == "" {
recCommand = os.Getenv("SHELL")
if recCommand == "" {
recCommand = "/bin/sh"
}
}
return recCommand
}
func tmpPath() (string, error) {
file, err := ioutil.TempFile("", "asciicast-")
if err != nil {

View File

@@ -47,14 +47,11 @@ func TestRecordCommand_Execute(t *testing.T) {
api := &testAPI{err: test.apiError, t: t}
command := &commands.RecordCommand{
Command: "ls",
Title: "listing",
MaxWait: 5,
Recorder: recorder,
API: api,
}
err := command.Execute(nil)
err := command.Execute("ls", "listing", false, 5, "")
if err != test.expectedError {
t.Errorf("expected error %v, got %v", test.expectedError, err)
}

View File

@@ -1,33 +1,23 @@
package commands
import (
"errors"
"flag"
"fmt"
"github.com/asciinema/asciinema-cli/api"
"github.com/asciinema/asciinema-cli/cli"
)
type UploadCommand struct {
API api.API
}
func NewUploadCommand(api api.API) cli.Command {
func NewUploadCommand(api api.API) *UploadCommand {
return &UploadCommand{
API: api,
}
}
func (c *UploadCommand) RegisterFlags(flags *flag.FlagSet) {
}
func (c *UploadCommand) Execute(args []string) error {
if len(args) == 0 {
return errors.New("filename required. Usage: asciinema upload <file>")
}
url, err := c.API.UploadAsciicast(args[0])
func (c *UploadCommand) Execute(filename string) error {
url, err := c.API.UploadAsciicast(filename)
if err != nil {
return err
}

View File

@@ -1,35 +0,0 @@
package commands
import (
"flag"
"fmt"
"github.com/asciinema/asciinema-cli/cli"
)
type VersionCommand struct {
version string
gitCommit string
}
func NewVersionCommand(version, gitCommit string) cli.Command {
return &VersionCommand{
version: version,
gitCommit: gitCommit,
}
}
func (c *VersionCommand) RegisterFlags(flags *flag.FlagSet) {
}
func (c *VersionCommand) Execute(args []string) error {
var commitInfo string
if c.gitCommit != "" {
commitInfo = fmt.Sprintf("-%v", c.gitCommit)
}
fmt.Printf("asciinema %v%v\n", c.version, commitInfo)
return nil
}