From 6ae1705ef09e67aef1dec36a035d503771357beb Mon Sep 17 00:00:00 2001 From: deranjer Date: Thu, 28 May 2020 21:29:50 -0400 Subject: [PATCH] some optimization for add files --- client/client.go | 56 ++++++++++++++++++++++++++++++++++++++ client/clientcmd/add.go | 26 ++++-------------- client/clientcmd/ignore.go | 4 ++- 3 files changed, 65 insertions(+), 21 deletions(-) diff --git a/client/client.go b/client/client.go index e7ab5de..451969e 100644 --- a/client/client.go +++ b/client/client.go @@ -51,6 +51,9 @@ func main() { // Adding all the "add" commands addCommands(cli, &conf) + // Adding the ignore commands + ignoreCommands(cli, &conf) + err = cli.Run() if err != nil { fmt.Printf("Error occurred: %v\n", err) @@ -181,3 +184,56 @@ func addCommands(cli *clir.Cli, conf *clientconfig.Gvcconfig) { }) } + +func ignoreCommands(cli *clir.Cli, conf *clientconfig.Gvcconfig) { + //All the ignore commands and subcommands + //The ignore subcommand + ignoreCmd := cli.NewSubCommand("ignore", "ignores file(s)/folder(s) (recursively for folder) to repo") + ignoreCmd.LongDescription("You can ignore all: all, a -file (-f): file.txt, or a -folder (-fd): folder, or a -wildcard (-wc): *.txt") + //File/Folder/Wildcard Ignoring + var file string + var folder string + var wildcard string + fileFlag := ignoreCmd.StringFlag("file", "adds a file to ignore to the config", &file) + fileFlag.FlagShortCut("file", "f") + folderFlag := ignoreCmd.StringFlag("folder", "tracks all contents of a folder to ignore", &folder) + folderFlag.FlagShortCut("folder", "fd") + wildCardFlag := ignoreCmd.StringFlag("wildcard", "treats the input as a wildcard and ignores all files that match the wildcard", &wildcard) + wildCardFlag.FlagShortCut("wildcard", "wc") + ignoreCmd.Action(func() error { + isRepo := validateRepo() + if !isRepo { + fmt.Println("no valid repo found.. please run 'init' to setup a repo first") + os.Exit(0) + } + if len(ignoreCmd.OtherArgs()) > 0 { + ignoreCmd.PrintHelp() + return fmt.Errorf("incorrect input detected, please fix and retry") + } + if file != "" { // if the file flag was used it won't be empty + fmt.Println("Ignoring file to tracked files: ", file) + err := clientcmd.IgnoreFiles(file, "file", conf.Ignores) + if err != nil { + return err + } + return nil + } + if folder != "" { // if the folder flag was used it won't be empty + fmt.Println("Ignoring contents of folder to tracked files: ", folder) + err := clientcmd.IgnoreFiles(folder, "folder", conf.Ignores) + if err != nil { + return err + } + return nil + } + if wildcard != "" { // if the wildcard flag was used it won't be empty + fmt.Println("Ignoring files with wildcard filter: ", wildcard) + err := clientcmd.IgnoreFiles(wildcard, "wildcard", conf.Ignores) + if err != nil { + return err + } + return nil + } + return nil + }) +} diff --git a/client/clientcmd/add.go b/client/clientcmd/add.go index 747a317..7097b4d 100644 --- a/client/clientcmd/add.go +++ b/client/clientcmd/add.go @@ -10,9 +10,13 @@ import ( //AddFiles adds files to the repo, inputType specifies file, folder, wildcard or all func AddFiles(input string, inputType string, ignore clientconfig.Ignore) error { + workingDir, err := os.Getwd() + if err != nil { + return err + } var trackedFiles []string switch inputType { - case "file": + case "file": // If the -file flag was used, then make sure it is a file, make sure not on ignore list, then add it to tracked files _, err := os.Stat(input) if err != nil { return fmt.Errorf("unable to add file %s", err) @@ -21,10 +25,6 @@ func AddFiles(input string, inputType string, ignore clientconfig.Ignore) error if err != nil { return fmt.Errorf("unable to add file as it (or ext) is on the ignores list %s", input) } - workingDir, err := os.Getwd() - if err != nil { - return err - } relativePath, err := filepath.Rel(workingDir, input) if err != nil { return fmt.Errorf("unable to create relative path for file: %s", err) @@ -39,10 +39,6 @@ func AddFiles(input string, inputType string, ignore clientconfig.Ignore) error if !folder.IsDir() { return fmt.Errorf("file exists, but is not a folder %s", err) } - workingDir, err := os.Getwd() - if err != nil { - return err - } filepath.Walk(input, func(path string, info os.FileInfo, err error) error { currentFile := filepath.Base(path) if err != nil { @@ -66,7 +62,6 @@ func AddFiles(input string, inputType string, ignore clientconfig.Ignore) error return nil }) case "wildcard": - fmt.Println("First char, ", input[:1]) var wildcard string if input[:1] == "*" { // Removing the wildcard char since we don't store that or test with that char wildcard = input[1:] @@ -77,10 +72,6 @@ func AddFiles(input string, inputType string, ignore clientconfig.Ignore) error if err != nil { return err } - workingDir, err := os.Getwd() - if err != nil { - return err - } filepath.Walk(workingDir, func(path string, info os.FileInfo, err error) error { currentFile := filepath.Base(path) // Stripping all the pathing so we just get the filename if err != nil { @@ -107,10 +98,6 @@ func AddFiles(input string, inputType string, ignore clientconfig.Ignore) error return nil }) case "all": - workingDir, err := os.Getwd() - if err != nil { - return err - } filepath.Walk(workingDir, func(path string, info os.FileInfo, err error) error { currentFile := filepath.Base(path) if err != nil { @@ -136,8 +123,7 @@ func AddFiles(input string, inputType string, ignore clientconfig.Ignore) error return nil }) } - - fmt.Println("Added tracked files", trackedFiles) + fmt.Println("Added tracked files", trackedFiles) // Print out all the new tracked files return nil } diff --git a/client/clientcmd/ignore.go b/client/clientcmd/ignore.go index cc8e4a4..fa4fa79 100644 --- a/client/clientcmd/ignore.go +++ b/client/clientcmd/ignore.go @@ -3,10 +3,12 @@ package clientcmd import ( "fmt" "os" + + clientconfig "github.com/deranjer/gvc/client/clientconfig" ) //IgnoreFiles ignores file(s)/folder based on name/wildcard, etc -func IgnoreFiles() error { +func IgnoreFiles(input string, inputType string, ignore clientconfig.Ignore) error { fmt.Println("File/folder/wildcard to ignore", os.Args[2]) return nil }