update paths command

This commit is contained in:
Rick Olson 2014-06-26 14:52:16 -06:00
parent 05561b68d7
commit 9abcbde0b1

@ -4,37 +4,46 @@ import (
"bufio" "bufio"
"fmt" "fmt"
"github.com/github/git-media/gitmedia" "github.com/github/git-media/gitmedia"
"github.com/spf13/cobra"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
) )
type PathCommand struct { var (
*Command pathCmd = &cobra.Command{
} Use: "path",
Short: "Manipulate .gitattributes",
Run: pathCommand,
}
pathsCmd = &cobra.Command{
Use: "paths",
Short: pathCmd.Short,
Run: pathCommand,
}
)
func (c *PathCommand) Run() { func pathCommand(cmd *cobra.Command, args []string) {
gitmedia.InstallHooks() gitmedia.InstallHooks()
var sub string var sub string
if len(c.SubCommands) > 0 { if len(args) > 0 {
sub = c.SubCommands[0] sub = args[0]
} }
switch sub { switch sub {
case "add": case "add":
c.addPath() addPath(args)
case "remove": case "remove":
c.removePath() removePath(args)
default: default:
c.listPaths() listPaths()
} }
} }
func (c *PathCommand) addPath() { func addPath(args []string) {
if len(c.SubCommands) < 2 { if len(args) < 2 {
fmt.Println("git media path add <path> [path]*") fmt.Println("git media path add <path> [path]*")
return return
} }
@ -46,7 +55,7 @@ func (c *PathCommand) addPath() {
return return
} }
for _, t := range c.SubCommands[1:] { for _, t := range args[1:] {
isKnownPath := false isKnownPath := false
for _, k := range knownPaths { for _, k := range knownPaths {
if t == k.Path { if t == k.Path {
@ -70,8 +79,8 @@ func (c *PathCommand) addPath() {
attributesFile.Close() attributesFile.Close()
} }
func (c *PathCommand) removePath() { func removePath(args []string) {
if len(c.SubCommands) < 2 { if len(args) < 2 {
fmt.Println("git meda path remove <path> [path]*") fmt.Println("git meda path remove <path> [path]*")
return return
} }
@ -95,7 +104,7 @@ func (c *PathCommand) removePath() {
if strings.Contains(line, "filter=media") { if strings.Contains(line, "filter=media") {
fields := strings.Fields(line) fields := strings.Fields(line)
removeThisPath := false removeThisPath := false
for _, t := range c.SubCommands[1:] { for _, t := range args[1:] {
if t == fields[0] { if t == fields[0] {
removeThisPath = true removeThisPath = true
} }
@ -112,7 +121,7 @@ func (c *PathCommand) removePath() {
attributesFile.Close() attributesFile.Close()
} }
func (c *PathCommand) listPaths() { func listPaths() {
fmt.Println("Listing paths") fmt.Println("Listing paths")
knownPaths := findPaths() knownPaths := findPaths()
for _, t := range knownPaths { for _, t := range knownPaths {
@ -176,10 +185,6 @@ func findPaths() []mediaPath {
} }
func init() { func init() {
registerCommand("path", func(c *Command) RunnableCommand { RootCmd.AddCommand(pathCmd)
return &PathCommand{Command: c} RootCmd.AddCommand(pathsCmd)
})
registerCommand("paths", func(c *Command) RunnableCommand {
return &PathCommand{Command: c}
})
} }