2014-06-03 15:08:58 +00:00
|
|
|
package commands
|
2014-05-27 16:27:54 +00:00
|
|
|
|
|
|
|
import (
|
2014-06-03 15:08:58 +00:00
|
|
|
"github.com/github/git-media/gitmedia"
|
2014-07-28 19:26:10 +00:00
|
|
|
"github.com/github/git-media/pointer"
|
2014-06-26 20:12:25 +00:00
|
|
|
"github.com/spf13/cobra"
|
2014-05-27 16:27:54 +00:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2014-06-26 20:12:25 +00:00
|
|
|
var (
|
|
|
|
cleanCmd = &cobra.Command{
|
|
|
|
Use: "clean",
|
2014-06-26 21:22:24 +00:00
|
|
|
Short: "Implements the Git clean filter",
|
2014-06-26 20:12:25 +00:00
|
|
|
Run: cleanCommand,
|
|
|
|
}
|
|
|
|
)
|
2014-05-27 16:27:54 +00:00
|
|
|
|
2014-06-26 20:12:25 +00:00
|
|
|
func cleanCommand(cmd *cobra.Command, args []string) {
|
2014-06-05 18:48:23 +00:00
|
|
|
gitmedia.InstallHooks()
|
2014-06-04 19:03:47 +00:00
|
|
|
|
2014-05-27 16:27:54 +00:00
|
|
|
var filename string
|
2014-06-26 20:12:25 +00:00
|
|
|
if len(args) > 0 {
|
|
|
|
filename = args[0]
|
2014-05-27 16:27:54 +00:00
|
|
|
} else {
|
|
|
|
filename = ""
|
|
|
|
}
|
|
|
|
|
2014-07-28 19:26:10 +00:00
|
|
|
cleaned, err := pointer.Clean(os.Stdin)
|
2014-07-28 20:38:09 +00:00
|
|
|
defer cleaned.Close()
|
2014-05-27 16:27:54 +00:00
|
|
|
if err != nil {
|
2014-06-05 18:48:23 +00:00
|
|
|
Panic(err, "Error cleaning asset")
|
2014-05-27 16:27:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tmpfile := cleaned.File.Name()
|
2014-07-24 20:34:19 +00:00
|
|
|
mediafile, err := gitmedia.LocalMediaPath(cleaned.Oid)
|
2014-06-05 19:05:02 +00:00
|
|
|
if err != nil {
|
|
|
|
Panic(err, "Unable to get local media path.")
|
|
|
|
}
|
|
|
|
|
2014-05-27 16:27:54 +00:00
|
|
|
if stat, _ := os.Stat(mediafile); stat != nil {
|
|
|
|
if stat.Size() != cleaned.Size {
|
2014-06-05 18:19:30 +00:00
|
|
|
Exit("Files don't match:\n%s\n%s", mediafile, tmpfile)
|
2014-05-27 16:27:54 +00:00
|
|
|
}
|
2014-06-05 18:19:30 +00:00
|
|
|
Debug("%s exists", mediafile)
|
2014-05-27 16:27:54 +00:00
|
|
|
} else {
|
|
|
|
if err := os.Rename(tmpfile, mediafile); err != nil {
|
2014-06-05 18:48:23 +00:00
|
|
|
Panic(err, "Unable to move %s to %s\n", tmpfile, mediafile)
|
2014-05-27 16:27:54 +00:00
|
|
|
}
|
|
|
|
|
2014-07-24 20:34:19 +00:00
|
|
|
if err = gitmedia.QueueUpload(cleaned.Oid, filename); err != nil {
|
|
|
|
Panic(err, "Unable to add %s to queue", cleaned.Oid)
|
2014-06-05 18:48:23 +00:00
|
|
|
}
|
2014-06-05 18:19:30 +00:00
|
|
|
Debug("Writing %s", mediafile)
|
2014-05-27 16:27:54 +00:00
|
|
|
}
|
|
|
|
|
2014-07-28 19:26:10 +00:00
|
|
|
pointer.Encode(os.Stdout, cleaned.Pointer)
|
2014-05-27 16:27:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2014-06-26 20:12:25 +00:00
|
|
|
RootCmd.AddCommand(cleanCmd)
|
2014-05-27 16:27:54 +00:00
|
|
|
}
|