From 24b1939d45cc59dd1cd3ad7cb4657289a60ad595 Mon Sep 17 00:00:00 2001 From: Rick Olson Date: Tue, 27 May 2014 10:27:54 -0600 Subject: [PATCH] `git media [clean | smudge]` vs `git-media-clean` --- commands/command_clean.go | 50 ++++++++++++++++++++++++++++++++++++++ commands/command_media.go | 37 ---------------------------- commands/command_smudge.go | 30 +++++++++++++++++++++++ 3 files changed, 80 insertions(+), 37 deletions(-) create mode 100644 commands/command_clean.go delete mode 100644 commands/command_media.go create mode 100644 commands/command_smudge.go diff --git a/commands/command_clean.go b/commands/command_clean.go new file mode 100644 index 00000000..d485f897 --- /dev/null +++ b/commands/command_clean.go @@ -0,0 +1,50 @@ +package gitmedia + +import ( + ".." + "../filters" + "os" +) + +type CleanCommand struct { + *Command +} + +func (c *CleanCommand) Run() { + var filename string + if len(c.Args) > 1 { + filename = c.Args[1] + } else { + filename = "" + } + + cleaned, err := gitmediafilters.Clean(os.Stdin) + if err != nil { + gitmedia.Panic(err, "Error cleaning asset") + } + defer cleaned.Close() + + tmpfile := cleaned.File.Name() + mediafile := gitmedia.LocalMediaPath(cleaned.Sha) + if stat, _ := os.Stat(mediafile); stat != nil { + if stat.Size() != cleaned.Size { + gitmedia.Exit("Files don't match:\n%s\n%s", mediafile, tmpfile) + } + gitmedia.Debug("%s exists", mediafile) + } else { + if err := os.Rename(tmpfile, mediafile); err != nil { + gitmedia.Panic(err, "Unable to move %s to %s\n", tmpfile, mediafile) + } + + gitmedia.QueueUpload(cleaned.Sha, filename) + gitmedia.Debug("Writing %s", mediafile) + } + + gitmedia.Encode(os.Stdout, cleaned.Sha) +} + +func init() { + registerCommand("clean", func(c *Command) RunnableCommand { + return &CleanCommand{Command: c} + }) +} diff --git a/commands/command_media.go b/commands/command_media.go deleted file mode 100644 index a688b193..00000000 --- a/commands/command_media.go +++ /dev/null @@ -1,37 +0,0 @@ -package gitmedia - -import ( - ".." -) - -type CleanCommand struct { - *Command -} - -type SmudgeCommand struct { - *Command -} - -func (c *CleanCommand) Run() { - err := PipeMediaCommand("git-media-clean") - if err != nil { - gitmedia.Panic(err, "Error running 'git media clean'") - } -} - -func (c *SmudgeCommand) Run() { - err := PipeMediaCommand("git-media-smudge") - if err != nil { - gitmedia.Panic(err, "Error running 'git media smudge'") - } -} - -func init() { - registerCommand("clean", func(c *Command) RunnableCommand { - return &CleanCommand{Command: c} - }) - - registerCommand("smudge", func(c *Command) RunnableCommand { - return &SmudgeCommand{Command: c} - }) -} diff --git a/commands/command_smudge.go b/commands/command_smudge.go new file mode 100644 index 00000000..978cea58 --- /dev/null +++ b/commands/command_smudge.go @@ -0,0 +1,30 @@ +package gitmedia + +import ( + ".." + "../filters" + "os" +) + +type SmudgeCommand struct { + *Command +} + +func (c *SmudgeCommand) Run() { + sha, err := gitmedia.Decode(os.Stdin) + if err != nil { + gitmedia.Panic(err, "Error reading git-media meta data from stdin:") + } + + err = gitmediafilters.Smudge(os.Stdout, sha) + if err != nil { + smudgerr := err.(*gitmediafilters.SmudgeError) + gitmedia.Panic(err, "Error reading file from local media dir: %s", smudgerr.Filename) + } +} + +func init() { + registerCommand("smudge", func(c *Command) RunnableCommand { + return &SmudgeCommand{Command: c} + }) +}