git media [clean | smudge] vs git-media-clean

This commit is contained in:
Rick Olson 2014-05-27 10:27:54 -06:00
parent 4e248600c2
commit 24b1939d45
3 changed files with 80 additions and 37 deletions

50
commands/command_clean.go Normal file

@ -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}
})
}

@ -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}
})
}

@ -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}
})
}