diff options
author | Martin Polden <mpolden@mpolden.no> | 2018-03-31 12:44:43 +0200 |
---|---|---|
committer | Martin Polden <mpolden@mpolden.no> | 2018-03-31 12:44:43 +0200 |
commit | 29aa7b710da74d459fb823d8fec4664a0baa58c4 (patch) | |
tree | 11dfc4ce9856cdff1300de57d9911ed6641446ce | |
parent | 86afefea1ee91fd2fe904d3d64d8ef58f4022671 (diff) |
Refactor post processing
-rw-r--r-- | cmd/lftpq/main.go | 9 | ||||
-rw-r--r-- | queue/config.go | 19 | ||||
-rw-r--r-- | queue/config_test.go | 5 | ||||
-rw-r--r-- | queue/queue.go | 13 | ||||
-rw-r--r-- | queue/queue_test.go | 22 |
5 files changed, 25 insertions, 43 deletions
diff --git a/cmd/lftpq/main.go b/cmd/lftpq/main.go index 0cb90d0..3d97191 100644 --- a/cmd/lftpq/main.go +++ b/cmd/lftpq/main.go @@ -120,14 +120,7 @@ func (c *CLI) transfer(q queue.Queue) error { if err := q.Transfer(c.consumer); err != nil { return err } - if q.Site.PostCommand == "" { - return nil - } - cmd, err := q.PostCommand(!c.Quiet) - if err != nil { - return err - } - return cmd.Run() + return q.PostProcess(!c.Quiet) } func main() { diff --git a/queue/config.go b/queue/config.go index 1bc1b1f..0d30ecd 100644 --- a/queue/config.go +++ b/queue/config.go @@ -46,6 +46,7 @@ type Site struct { Priorities []string priorities []*regexp.Regexp PostCommand string + postCommand *exec.Cmd Replacements []Replacement Merge bool Skip bool @@ -92,13 +93,15 @@ func parseTemplate(tmpl string) (*template.Template, error) { return t, nil } -func isExecutable(s string) error { - if s == "" { - return nil +func command(cmd string) (*exec.Cmd, error) { + if cmd == "" { + return nil, nil } - args := strings.Split(s, " ") - _, err := exec.LookPath(args[0]) - return err + argv := strings.Split(cmd, " ") + if _, err := exec.LookPath(argv[0]); err != nil { + return nil, err + } + return exec.Command(argv[0], argv[1:]...), nil } func (c *Config) load() error { @@ -135,9 +138,11 @@ func (c *Config) load() error { return err } - if err := isExecutable(site.PostCommand); err != nil { + cmd, err := command(site.PostCommand) + if err != nil { return err } + site.postCommand = cmd var parserFunc parser.Parser switch site.Parser { diff --git a/queue/config_test.go b/queue/config_test.go index 31c9f69..af4b4d5 100644 --- a/queue/config_test.go +++ b/queue/config_test.go @@ -1,6 +1,7 @@ package queue import ( + "reflect" "strings" "testing" "time" @@ -23,6 +24,7 @@ func TestLoad(t *testing.T) { Pattern: "\\.the\\.", Replacement: ".The.", }}, + PostCommand: "xargs echo", }}, } if err := cfg.load(); err != nil { @@ -51,6 +53,9 @@ func TestLoad(t *testing.T) { if len(site.itemParser.replacements) == 0 { t.Error("Expected non-empty replacements") } + if want := []string{"xargs", "echo"}; !reflect.DeepEqual(want, site.postCommand.Args) { + t.Fatalf("Expected %+v, got %+v", want, site.postCommand.Args) + } } func TestReadConfig(t *testing.T) { diff --git a/queue/queue.go b/queue/queue.go index f6fa14c..80dcd70 100644 --- a/queue/queue.go +++ b/queue/queue.go @@ -8,7 +8,6 @@ import ( "io" "io/ioutil" "os" - "os/exec" "path/filepath" "regexp" "sort" @@ -94,19 +93,21 @@ func (q *Queue) Transfer(consumer Consumer) error { return consumer.Consume(name) } -func (q *Queue) PostCommand(inheritIO bool) (*exec.Cmd, error) { +func (q *Queue) PostProcess(inheritIO bool) error { + if q.postCommand == nil { + return nil + } json, err := json.Marshal(q.Items) if err != nil { - return nil, err + return err } - argv := strings.Split(q.Site.PostCommand, " ") - cmd := exec.Command(argv[0], argv[1:]...) + cmd := q.postCommand cmd.Stdin = bytes.NewReader(json) if inheritIO { cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr } - return cmd, nil + return cmd.Run() } func (q Queue) MarshalText() ([]byte, error) { diff --git a/queue/queue_test.go b/queue/queue_test.go index db6a536..0e4581b 100644 --- a/queue/queue_test.go +++ b/queue/queue_test.go @@ -3,9 +3,7 @@ package queue import ( "encoding" "encoding/json" - "io/ioutil" "os" - "reflect" "regexp" "strings" "testing" @@ -203,26 +201,6 @@ func TestDeduplicateIgnoreSelf(t *testing.T) { } } -func TestPostCommand(t *testing.T) { - s := newTestSite() - s.PostCommand = "xargs echo" - q := newTestQueue(s, []os.FileInfo{file{name: "/remote/foo"}}) - cmd, err := q.PostCommand(false) - if err != nil { - t.Fatal(err) - } - if want := []string{"xargs", "echo"}; !reflect.DeepEqual(want, cmd.Args) { - t.Fatalf("Expected %+v, got %+v", want, cmd.Args) - } - data, err := ioutil.ReadAll(cmd.Stdin) - if err != nil { - t.Fatal(err) - } - if len(data) == 0 { - t.Fatal("Expected stdin to contain data") - } -} - func TestMergePreferringRemoteCopy(t *testing.T) { s := newTestSite() s.Merge = true |