working on adding files to repo
This commit is contained in:
		@@ -120,7 +120,7 @@ func addCommands(cli *clir.Cli, conf *clientconfig.Gvcconfig) {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
		if file != "" { // if the file flag was used it won't be empty
 | 
							if file != "" { // if the file flag was used it won't be empty
 | 
				
			||||||
			fmt.Println("adding file to tracked files: ", file)
 | 
								fmt.Println("adding file to tracked files: ", file)
 | 
				
			||||||
			err := clientcmd.AddFiles(file, "file")
 | 
								err := clientcmd.AddFiles(file, "file", conf.Ignores)
 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				return err
 | 
									return err
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
@@ -128,7 +128,7 @@ func addCommands(cli *clir.Cli, conf *clientconfig.Gvcconfig) {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
		if folder != "" { // if the folder flag was used it won't be empty
 | 
							if folder != "" { // if the folder flag was used it won't be empty
 | 
				
			||||||
			fmt.Println("adding contents of folder to tracked files: ", folder)
 | 
								fmt.Println("adding contents of folder to tracked files: ", folder)
 | 
				
			||||||
			err := clientcmd.AddFiles(folder, "folder")
 | 
								err := clientcmd.AddFiles(folder, "folder", conf.Ignores)
 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				return err
 | 
									return err
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
@@ -136,7 +136,7 @@ func addCommands(cli *clir.Cli, conf *clientconfig.Gvcconfig) {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
		if wildcard != "" { // if the wildcard flag was used it won't be empty
 | 
							if wildcard != "" { // if the wildcard flag was used it won't be empty
 | 
				
			||||||
			fmt.Println("adding files with wildcard filter: ", wildcard)
 | 
								fmt.Println("adding files with wildcard filter: ", wildcard)
 | 
				
			||||||
			err := clientcmd.AddFiles(wildcard, "wildcard")
 | 
								err := clientcmd.AddFiles(wildcard, "wildcard", conf.Ignores)
 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				return err
 | 
									return err
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
@@ -153,7 +153,7 @@ func addCommands(cli *clir.Cli, conf *clientconfig.Gvcconfig) {
 | 
				
			|||||||
			return fmt.Errorf("the 'all' subcommand does not accept additional arguments")
 | 
								return fmt.Errorf("the 'all' subcommand does not accept additional arguments")
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		fmt.Println("adding all files recursively in directory to tracked files")
 | 
							fmt.Println("adding all files recursively in directory to tracked files")
 | 
				
			||||||
		err := clientcmd.AddFiles("", "all")
 | 
							err := clientcmd.AddFiles("", "all", conf.Ignores)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return err
 | 
								return err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,13 +3,141 @@ package clientcmd
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
 | 
						"path/filepath"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	clientconfig "github.com/deranjer/gvc/client/clientconfig"
 | 
						clientconfig "github.com/deranjer/gvc/client/clientconfig"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//AddFiles adds files to the repo, inputType specifies file, folder, wildcard or all
 | 
					//AddFiles adds files to the repo, inputType specifies file, folder, wildcard or all
 | 
				
			||||||
func AddFiles(input string, inputType string) error {
 | 
					func AddFiles(input string, inputType string, ignore clientconfig.Ignore) error {
 | 
				
			||||||
	fmt.Println("File/folder/wildcard to add", os.Args[2])
 | 
						var trackedFiles []string
 | 
				
			||||||
 | 
						switch inputType {
 | 
				
			||||||
 | 
						case "file":
 | 
				
			||||||
 | 
							_, err := os.Stat(input)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return fmt.Errorf("unable to add file %s", err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							err = checkIgnores(input, "file", ignore)
 | 
				
			||||||
 | 
							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)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							trackedFiles = append(trackedFiles, relativePath)
 | 
				
			||||||
 | 
							fmt.Println("adding file: ", relativePath)
 | 
				
			||||||
 | 
						case "folder":
 | 
				
			||||||
 | 
							folder, err := os.Stat(input)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return fmt.Errorf("unable to add folder %s", err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							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 {
 | 
				
			||||||
 | 
									return fmt.Errorf("failure accessing path %s", err)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								if info.IsDir() {
 | 
				
			||||||
 | 
									err = checkIgnores(currentFile, "folder", ignore)
 | 
				
			||||||
 | 
								} else {
 | 
				
			||||||
 | 
									err = checkIgnores(currentFile, "file", ignore)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									fmt.Printf("Not adding file %s as it is on the ingores list \n", currentFile)
 | 
				
			||||||
 | 
									return nil
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								fmt.Println("adding file: ", path)
 | 
				
			||||||
 | 
								relativePath, err := filepath.Rel(workingDir, path)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									return fmt.Errorf("unable to create relative path for file: %s", err)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								trackedFiles = append(trackedFiles, relativePath)
 | 
				
			||||||
 | 
								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:]
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								wildcard = input
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							err := checkIgnores(wildcard, "wildcard", ignore)
 | 
				
			||||||
 | 
							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 {
 | 
				
			||||||
 | 
									return fmt.Errorf("failure accessing path %s", err)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								if info.IsDir() {
 | 
				
			||||||
 | 
									err = checkIgnores(currentFile, "folder", ignore)
 | 
				
			||||||
 | 
								} else {
 | 
				
			||||||
 | 
									err = checkIgnores(currentFile, "file", ignore)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									fmt.Printf("Not adding file %s as it is on the ingores list \n", currentFile)
 | 
				
			||||||
 | 
									return nil
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								fileExt := filepath.Ext(path)
 | 
				
			||||||
 | 
								if fileExt == wildcard { // seeing if the file ext matches the wildcard
 | 
				
			||||||
 | 
									fmt.Println("Adding file that matched wildcard: ", currentFile)
 | 
				
			||||||
 | 
									relativePath, err := filepath.Rel(workingDir, path)
 | 
				
			||||||
 | 
									if err != nil {
 | 
				
			||||||
 | 
										return fmt.Errorf("unable to create relative path for file: %s", err)
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									trackedFiles = append(trackedFiles, relativePath)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								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 {
 | 
				
			||||||
 | 
									return fmt.Errorf("failure accessing path %s", err)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								if info.IsDir() {
 | 
				
			||||||
 | 
									err = checkIgnores(currentFile, "folder", ignore)
 | 
				
			||||||
 | 
								} else {
 | 
				
			||||||
 | 
									err = checkIgnores(currentFile, "file", ignore)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									fmt.Printf("Not adding file %s as it is on the ingores list \n", currentFile)
 | 
				
			||||||
 | 
									return nil
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								relativePath, err := filepath.Rel(workingDir, path)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									return fmt.Errorf("unable to create relative path for file: %s", err)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								if relativePath == "." { //Ignoring current directory
 | 
				
			||||||
 | 
									return nil
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								trackedFiles = append(trackedFiles, relativePath)
 | 
				
			||||||
 | 
								return nil
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						fmt.Println("Added tracked files", trackedFiles)
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,4 +1,41 @@
 | 
				
			|||||||
package clientcmd
 | 
					package clientcmd
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"path/filepath"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						clientconfig "github.com/deranjer/gvc/client/clientconfig"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// ConfigPath is the global path to the config that is injected from the main client package.
 | 
					// ConfigPath is the global path to the config that is injected from the main client package.
 | 
				
			||||||
var ConfigPath string
 | 
					var ConfigPath string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func checkIgnores(input string, inputType string, ignores clientconfig.Ignore) error {
 | 
				
			||||||
 | 
						switch inputType {
 | 
				
			||||||
 | 
						case "file":
 | 
				
			||||||
 | 
							fileExt := filepath.Ext(input) // TODO more sanity checking on ext
 | 
				
			||||||
 | 
							for _, ignoredExt := range ignores.Exts {
 | 
				
			||||||
 | 
								if fileExt == ignoredExt {
 | 
				
			||||||
 | 
									return fmt.Errorf("file ext is on ignored list, cannot add file with file ext %s", fileExt)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							for _, ignoredFile := range ignores.Files {
 | 
				
			||||||
 | 
								if input == ignoredFile {
 | 
				
			||||||
 | 
									return fmt.Errorf("file name is on ignored list, cannot add file with name %s", input)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						case "folder":
 | 
				
			||||||
 | 
							for _, ignoredFolder := range ignores.Folders {
 | 
				
			||||||
 | 
								if input == ignoredFolder {
 | 
				
			||||||
 | 
									return fmt.Errorf("folder name is on the ignored list, cannot add folder with name %s", input)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						case "wildcard":
 | 
				
			||||||
 | 
							for _, ignoredExt := range ignores.Exts {
 | 
				
			||||||
 | 
								if input == ignoredExt {
 | 
				
			||||||
 | 
									return fmt.Errorf("cannot add wildcard, since that ext is already added to the ignore config %s", input)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user