working on adding files to repo

This commit is contained in:
2020-05-28 17:41:13 -04:00
parent 9b017f3128
commit 8c59c4b067
3 changed files with 171 additions and 6 deletions

View File

@@ -3,13 +3,141 @@ package clientcmd
import (
"fmt"
"os"
"path/filepath"
clientconfig "github.com/deranjer/gvc/client/clientconfig"
)
//AddFiles adds files to the repo, inputType specifies file, folder, wildcard or all
func AddFiles(input string, inputType string) error {
fmt.Println("File/folder/wildcard to add", os.Args[2])
func AddFiles(input string, inputType string, ignore clientconfig.Ignore) error {
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
}