From 99f9fcc0c42fffcfc3ce3c472c4bd26913df57a3 Mon Sep 17 00:00:00 2001 From: Spencer Date: Sun, 22 Jul 2018 19:14:26 -0400 Subject: [PATCH] ## Documentation * Moved package comment to doc.go --- doc.go | 7 +++++++ document.go | 22 +++++++++++++++------- ps.go | 6 ------ ps_test.go | 2 +- runner/runner.go | 2 +- 5 files changed, 24 insertions(+), 15 deletions(-) create mode 100644 doc.go diff --git a/doc.go b/doc.go new file mode 100644 index 0000000..7296bcd --- /dev/null +++ b/doc.go @@ -0,0 +1,7 @@ +// Package ps is a rudimentary API between Adobe Photoshop CS5 and Golang. +// The interaction between the two is implemented using Javascript/VBScript. +// +// Use it to control Photoshop, edit documents, and perform batch operations. +// +// Currently only supports Photoshop CS5 Windows x86_64. +package ps diff --git a/document.go b/document.go index 41e38c6..d48cc5d 100644 --- a/document.go +++ b/document.go @@ -56,7 +56,7 @@ func (d *Document) UnmarshalJSON(b []byte) error { } // Name returns the document's title. -// This fufills the Group interface. +// This fulfills the Group interface. func (d *Document) Name() string { return d.name } @@ -116,7 +116,7 @@ func (d *Document) LayerSet(name string) *LayerSet { // ActiveDocument returns document currently focused in Photoshop. // -// TODO(sbrow): Reduce cylcomatic complexity of ActiveDocument(). +// TODO(sbrow): Reduce cyclomatic complexity of ActiveDocument(). func ActiveDocument() (*Document, error) { log.Println("Loading ActiveDoucment") d := &Document{} @@ -127,7 +127,7 @@ func ActiveDocument() (*Document, error) { } d.name = strings.TrimRight(string(byt), "\r\n") if Mode != Safe { - err = d.Restore() + err = d.Restore(d.Filename()) switch { case os.IsNotExist(err): log.Println("Previous version not found.") @@ -163,8 +163,11 @@ func ActiveDocument() (*Document, error) { } // Restore loads document data from a JSON file. -func (d *Document) Restore() error { - byt, err := ioutil.ReadFile(d.Filename()) +func (d *Document) Restore(path string) error { + if path == "" { + path = d.Filename() + } + byt, err := ioutil.ReadFile(path) if err == nil { log.Println("Previous version found, loading") err = json.Unmarshal(byt, &d) @@ -188,8 +191,13 @@ func (d *Document) Filename() string { if !ok { log.Panic("No caller information") } - return filepath.Join(filepath.Dir(dir), "data", - strings.TrimRight(d.name, "\r\n")+".txt") + err := os.Mkdir(filepath.Join(filepath.Dir(dir), "data"), 0700) + if err != nil { + log.Println(err) + } + name := strings.TrimRight(d.name, "\r\n") + name = strings.TrimSuffix(name, ".psd") + return filepath.Join(filepath.Dir(dir), "data", name+".json") } // Dump saves the document to disk in JSON format. diff --git a/ps.go b/ps.go index 6fe2c5b..ab3d2b5 100644 --- a/ps.go +++ b/ps.go @@ -1,11 +1,5 @@ //go:generate sh -c "godoc2md -template ./.doc.template github.com/sbrow/ps > README.md" -// Package ps is a rudimentary API between Adobe Photoshop CS5 and Golang. -// The interaction between the two is implemented using Javascript/VBScript. -// -// Use it to control Photoshop, edit documents, and perform batch operations. -// -// Currently only supports Photoshop CS5 Windows x86_64. package ps import ( diff --git a/ps_test.go b/ps_test.go index df85904..d81020e 100644 --- a/ps_test.go +++ b/ps_test.go @@ -160,7 +160,7 @@ func TestActiveDocument(t *testing.T) { } Open(testDoc) d, err := ActiveDocument() - defer d.Dump() + defer func(){d.Dump()}() if err != nil { t.Fatal(err) } diff --git a/runner/runner.go b/runner/runner.go index dfb2b8a..daefdc2 100644 --- a/runner/runner.go +++ b/runner/runner.go @@ -51,7 +51,7 @@ func Run(name string, args ...string) ([]byte, error) { cmd := exec.Command(std.Cmd, parseArgs(name, args...)...) cmd.Stdout, cmd.Stderr = &out, &errs if err := cmd.Run(); err != nil || len(errs.Bytes()) != 0 { - return out.Bytes(), fmt.Errorf("err: \"%s\"\nargs: \"%s\"\nout: \"%s\"", errs.String(), args, out.String()) + return out.Bytes(), fmt.Errorf("err: \"%s %s\"\nargs: \"%s\"\nout: \"%s\"", err, errs.String(), args, out.String()) } return out.Bytes(), nil }