Files
gvc/client/clientcmd/add.go

193 lines
6.2 KiB
Go

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, ignore clientconfig.Ignore) error {
err := validateFileType(input, inputType) // Making sure that if the file flag was used a folder was not supplied and vice versa
if err != nil {
return err
}
workingDir, err := os.Getwd()
if err != nil {
return err
}
var trackedFiles []string
switch inputType {
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)
}
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)
}
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)
}
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":
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
}
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":
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) // Print out all the new tracked files
return nil
}
// checkRemotesSlice just ranges through the remotes looking for duplicates
func checkRemotesSlice(name, host string, port int, remotes []clientconfig.Remote) error {
for _, remote := range remotes { // Names of remotes must be unique so make sure they are
if name == remote.Name {
return fmt.Errorf("remote with that name already exits: %s", name)
}
}
for _, remote := range remotes { // make sure we don't have another remote with same host and port
if host == remote.Host && port == remote.Port {
return fmt.Errorf("remote with that host and port config already exists: %s:%d", host, port)
}
}
return nil
}
// AddRemote adds a remote to the config file
func AddRemote(name string, host string, port int, defaultRemote bool, remotes []clientconfig.Remote) (newRemotes []clientconfig.Remote, err error) {
if len(remotes) == 0 { //first remote no need to check for duplicates and will set this remote as default
newRemote := clientconfig.Remote{
Name: name,
Host: host,
Port: port,
Default: true,
}
remotes = append(remotes, newRemote)
fmt.Printf("Remote added: Name: %s Host: %s, Port: %d, Default: %t", name, host, port, defaultRemote)
return remotes, nil
}
err = checkRemotesSlice(name, host, port, remotes)
if err != nil {
return remotes, err
}
if defaultRemote { // If the new repo was flagged as default, find old default and remove it
for i, remote := range remotes {
if remote.Default {
remotes[i].Default = false
}
}
newRemote := clientconfig.Remote{
Name: name,
Host: host,
Port: port,
Default: true,
}
remotes = append(remotes, newRemote)
fmt.Printf("Remote added: Name: %s Host: %s, Port: %d, Default: %t", name, host, port, defaultRemote)
return remotes, nil
}
// If passes all checks and is not default, create it and add it
newRemote := clientconfig.Remote{
Name: name,
Host: host,
Port: port,
Default: false,
}
remotes = append(remotes, newRemote)
fmt.Printf("Remote added: Name: %s Host: %s, Port: %d, Default: %t", name, host, port, defaultRemote)
return remotes, nil
}