adding lock command, generalizing the checkfiletypes function

This commit is contained in:
2020-06-04 17:38:26 -04:00
parent 0276a1d776
commit db2221c515
8 changed files with 348 additions and 118 deletions

View File

@@ -41,3 +41,6 @@ remotebranches = ["master", "test", "test2"]
[nocompress]
[locked]
files = ["test1\\client8.exe", "client1.exe"]
exts = [".png"]

View File

@@ -61,6 +61,9 @@ func main() {
// Adding the refresh command
refreshCommand(cli, &conf)
// Adding the "remote" commands
remoteCommands(cli, &conf)
err = cli.Run()
if err != nil {
fmt.Printf("Error occurred: %v\n", err)
@@ -85,8 +88,9 @@ func validateRepo() bool {
return true
}
// refreshCommand contacts the server and pulls down locked files/commits/etc to the client (like git fetch)
func refreshCommand(cli *clir.Cli, conf *clientconfig.Gvcconfig) {
refreshCmd := cli.NewSubCommand("refresh", "pulls down all locks unknown to client pushes")
refreshCmd := cli.NewSubCommand("refresh", "pulls down all locks/branches/commits unknown to client")
refreshCmd.LongDescription("Works similar to git fetch where it shows the number of commits pushed to server/branches that the client doesn't know as well as file/folder locks")
refreshCmd.Action(func() error {
isRepo := validateRepo()
@@ -94,7 +98,10 @@ func refreshCommand(cli *clir.Cli, conf *clientconfig.Gvcconfig) {
fmt.Println("no valid repo found.. please run 'init' to setup a repo first")
os.Exit(0)
}
err := clientcmd.RefreshContent(conf)
if err != nil {
return fmt.Errorf("unable to refresh content: %s", err)
}
return nil
})
}
@@ -193,44 +200,6 @@ func addCommands(cli *clir.Cli, conf *clientconfig.Gvcconfig) {
}
return nil
})
//The add remote command
remoteCmd := addCmd.NewSubCommand("remote", "add/delete/show remotes")
var name string
var host string
var port int
var defaultRemote bool
remoteAddCmd := remoteCmd.NewSubCommand("add", "add a remote, requires -name -host and -port")
remoteAddCmd.LongDescription("Adds a remote to the .gvcconfig.toml. Requires the -name, -host and -port flags. Example: gvc remote add -name exampleRemote -host examplehost.com -port 8080")
nameFlag := remoteAddCmd.StringFlag("name", "the name you want for your remote server", &name)
nameFlag.FlagShortCut("name", "n")
remoteAddCmd.FlagRequired("name")
hostFlag := remoteAddCmd.StringFlag("host", "the hostname or IP Address of the remote", &host)
hostFlag.FlagShortCut("host", "h")
remoteAddCmd.FlagRequired("host")
portFlag := remoteAddCmd.IntFlag("port", "the port the remote server is listening on", &port)
portFlag.FlagShortCut("port", "p")
remoteAddCmd.FlagRequired("port")
defaultFlag := remoteAddCmd.BoolFlag("default", "is used, the repo is set as default. (if first remote automatically set as default)", &defaultRemote)
defaultFlag.FlagShortCut("default", "d")
remoteAddCmd.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 name == "" || host == "" || port == 0 || port == 1 || port > 65535 {
fmt.Println("incorrect input found, exiting, ensure you entered a valid port")
os.Exit(0)
}
newRemotes, err := clientcmd.AddRemote(name, host, port, defaultRemote, conf.Remotes)
conf.Remotes = newRemotes //Overwriting the old Remote list with new list
if err != nil {
return fmt.Errorf("error adding remote: %s", err)
}
return nil
})
}
func ignoreCommands(cli *clir.Cli, conf *clientconfig.Gvcconfig) {
@@ -415,3 +384,150 @@ func infoCommands(cli *clir.Cli, conf *clientconfig.Gvcconfig) {
// return nil
// })
}
func remoteCommands(cli *clir.Cli, conf *config.Gvcconfig) {
//The add remote command
remoteCmd := cli.NewSubCommand("remote", "add/delete/show remotes")
var name string
var host string
var port int
var defaultRemote bool
remoteAddCmd := remoteCmd.NewSubCommand("add", "add a remote, requires -name -host and -port")
remoteAddCmd.LongDescription("Adds a remote to the .gvcconfig.toml. Requires the -name, -host and -port flags. Example: gvc remote add -name exampleRemote -host examplehost.com -port 8080")
nameFlag := remoteAddCmd.StringFlag("name", "the name you want for your remote server", &name)
nameFlag.FlagShortCut("name", "n")
remoteAddCmd.FlagRequired("name")
hostFlag := remoteAddCmd.StringFlag("host", "the hostname or IP Address of the remote", &host)
hostFlag.FlagShortCut("host", "h")
remoteAddCmd.FlagRequired("host")
portFlag := remoteAddCmd.IntFlag("port", "the port the remote server is listening on", &port)
portFlag.FlagShortCut("port", "p")
remoteAddCmd.FlagRequired("port")
defaultFlag := remoteAddCmd.BoolFlag("default", "is used, the repo is set as default. (if first remote automatically set as default)", &defaultRemote)
defaultFlag.FlagShortCut("default", "d")
remoteAddCmd.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 name == "" || host == "" || port == 0 || port == 1 || port > 65535 {
fmt.Println("incorrect input found, exiting, ensure you entered a valid port")
os.Exit(0)
}
newRemotes, err := clientcmd.AddRemote(name, host, port, defaultRemote, conf.Remotes)
conf.Remotes = newRemotes //Overwriting the old Remote list with new list
if err != nil {
return fmt.Errorf("error adding remote: %s", err)
}
return nil
})
}
func lockCommands(cli clir.Cli, conf *clientconfig.Gvcconfig) {
//All the lock commands and subcommands
//The lock subcommand
lockCmd := cli.NewSubCommand("lock", "locks file(s)/folder(s) (recursively for folder) to repo")
lockCmd.LongDescription("You can lock 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 := lockCmd.StringFlag("file", "adds a file to lock to the config", &file)
fileFlag.FlagShortCut("file", "f")
folderFlag := lockCmd.StringFlag("folder", "tracks all contents of a folder to lock", &folder)
folderFlag.FlagShortCut("folder", "fd")
wildCardFlag := lockCmd.StringFlag("wildcard", "treats the input as a wildcard and locks all files that match the wildcard", &wildcard)
wildCardFlag.FlagShortCut("wildcard", "wc")
lockCmd.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(lockCmd.OtherArgs()) > 0 {
lockCmd.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: ", file)
err := clientcmd.LockFiles(file, "file", conf)
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: ", folder)
err := clientcmd.LockFiles(folder, "folder", conf)
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.LockFiles(wildcard, "wildcard", conf)
if err != nil {
return err
}
return nil
}
return nil
})
// Adding the remove lock command (remove from the lock list)
removeLockCmd(lockCmd, conf)
}
// removeLockCmd removes files/folders/wildcard from the ignore list
func removeLockCmd(ignoreCmd *clir.Command, conf *clientconfig.Gvcconfig) {
RemoveLockCmd := ignoreCmd.NewSubCommand("remove", "remove from ignores file(s)/folder(s) (recursively for folder) to repo")
RemoveLockCmd.LongDescription("You can remove from 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 := RemoveLockCmd.StringFlag("file", "removes ignored file from config", &file)
fileFlag.FlagShortCut("file", "f")
folderFlag := RemoveLockCmd.StringFlag("folder", "removes ignored folder", &folder)
folderFlag.FlagShortCut("folder", "fd")
wildCardFlag := RemoveLockCmd.StringFlag("wildcard", "removes wildcard from ignores", &wildcard)
wildCardFlag.FlagShortCut("wildcard", "wc")
RemoveLockCmd.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(RemoveLockCmd.OtherArgs()) > 0 {
RemoveLockCmd.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: ", file)
err := clientcmd.RemoveLockFiles(file, "file", conf)
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: ", folder)
err := clientcmd.RemoveLockFiles(folder, "folder", conf)
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.RemoveLockFiles(wildcard, "wildcard", conf)
if err != nil {
return err
}
return nil
}
return nil
})
}

View File

@@ -9,7 +9,7 @@ import (
)
//AddFiles adds files to the repo, inputType specifies file, folder, wildcard or all
func AddFiles(input string, inputType string, ignore clientconfig.Ignore) error {
func AddFiles(input string, inputType string, ignore clientconfig.FileTypes) 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
@@ -25,7 +25,7 @@ func AddFiles(input string, inputType string, ignore clientconfig.Ignore) error
if err != nil {
return fmt.Errorf("unable to add file %s", err)
}
err = checkIgnores(input, "file", ignore)
err = checkFileTypes(input, "file", ignore)
if err != nil {
return fmt.Errorf("unable to add file as it (or ext) is on the ignores list %s", input)
}
@@ -49,9 +49,9 @@ func AddFiles(input string, inputType string, ignore clientconfig.Ignore) error
return fmt.Errorf("failure accessing path %s", err)
}
if info.IsDir() {
err = checkIgnores(currentFile, "folder", ignore)
err = checkFileTypes(currentFile, "folder", ignore)
} else {
err = checkIgnores(currentFile, "file", ignore)
err = checkFileTypes(currentFile, "file", ignore)
}
if err != nil {
fmt.Printf("Not adding file %s as it is on the ingores list \n", currentFile)
@@ -72,7 +72,7 @@ func AddFiles(input string, inputType string, ignore clientconfig.Ignore) error
} else {
wildcard = input
}
err := checkIgnores(wildcard, "wildcard", ignore)
err := checkFileTypes(wildcard, "wildcard", ignore)
if err != nil {
return err
}
@@ -82,9 +82,9 @@ func AddFiles(input string, inputType string, ignore clientconfig.Ignore) error
return fmt.Errorf("failure accessing path %s", err)
}
if info.IsDir() {
err = checkIgnores(currentFile, "folder", ignore)
err = checkFileTypes(currentFile, "folder", ignore)
} else {
err = checkIgnores(currentFile, "file", ignore)
err = checkFileTypes(currentFile, "file", ignore)
}
if err != nil {
fmt.Printf("Not adding file %s as it is on the ingores list \n", currentFile)
@@ -108,9 +108,9 @@ func AddFiles(input string, inputType string, ignore clientconfig.Ignore) error
return fmt.Errorf("failure accessing path %s", err)
}
if info.IsDir() {
err = checkIgnores(currentFile, "folder", ignore)
err = checkFileTypes(currentFile, "folder", ignore)
} else {
err = checkIgnores(currentFile, "file", ignore)
err = checkFileTypes(currentFile, "file", ignore)
}
if err != nil {
fmt.Printf("Not adding file %s as it is on the ingores list \n", currentFile)
@@ -145,48 +145,3 @@ func checkRemotesSlice(name, host string, port int, remotes []clientconfig.Remot
}
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
}

View File

@@ -34,7 +34,7 @@ func validateFileType(path string, inputType string) error {
return nil
}
func checkIgnores(input string, inputType string, ignores clientconfig.Ignore) error {
func checkFileTypes(input string, inputType string, ignores clientconfig.FileTypes) error {
switch inputType {
case "file":
fileExt := filepath.Ext(input) // TODO more sanity checking on ext

View File

@@ -15,7 +15,7 @@ func IgnoreFiles(input string, inputType string, conf *clientconfig.Gvcconfig) e
ignore := conf.Ignores
switch inputType { // TODO: add default case for generic error handling
case "file":
err := checkIgnores(input, "file", ignore)
err := checkFileTypes(input, "file", ignore)
if err != nil {
return fmt.Errorf("%s already ignored: %s", input, err)
}
@@ -23,7 +23,7 @@ func IgnoreFiles(input string, inputType string, conf *clientconfig.Gvcconfig) e
conf.Ignores.Files = append(conf.Ignores.Files, input)
return nil
case "folder":
err := checkIgnores(input, "folder", ignore)
err := checkFileTypes(input, "folder", ignore)
if err != nil {
return fmt.Errorf("%s is already ignored: %s", input, err)
}
@@ -37,7 +37,7 @@ func IgnoreFiles(input string, inputType string, conf *clientconfig.Gvcconfig) e
} else {
wildcard = input
}
err := checkIgnores(wildcard, "wildcard", ignore)
err := checkFileTypes(wildcard, "wildcard", ignore)
if err != nil {
return fmt.Errorf("%s is already ignored: %s", input, err)
}
@@ -57,7 +57,7 @@ func RemoveIgnoreFiles(input string, inputType string, conf *clientconfig.Gvccon
ignore := conf.Ignores
switch inputType { // TODO: add default case for generic error handling
case "file":
err := checkIgnores(input, "file", ignore)
err := checkFileTypes(input, "file", ignore)
if err != nil {
fmt.Println("Removing file from ignores: ", input)
for i, fileIgnore := range ignore.Files {
@@ -72,7 +72,7 @@ func RemoveIgnoreFiles(input string, inputType string, conf *clientconfig.Gvccon
fmt.Println("File not found in ingores, unable to remove: ", input)
return nil
case "folder":
err := checkIgnores(input, "folder", ignore)
err := checkFileTypes(input, "folder", ignore)
if err != nil {
for i, folderIgnore := range ignore.Folders {
if input == folderIgnore {
@@ -92,7 +92,7 @@ func RemoveIgnoreFiles(input string, inputType string, conf *clientconfig.Gvccon
} else {
wildcard = input
}
err := checkIgnores(wildcard, "wildcard", ignore)
err := checkFileTypes(wildcard, "wildcard", ignore)
if err != nil {
for i, wildcardIgnore := range ignore.Exts {
if input == wildcardIgnore {

110
client/clientcmd/lock.go Normal file
View File

@@ -0,0 +1,110 @@
package clientcmd
import (
"fmt"
clientconfig "github.com/deranjer/gvc/client/clientconfig"
)
//LockFiles locks file(s)/folder based on name/wildcard, etc
func LockFiles(input string, inputType string, conf *clientconfig.Gvcconfig) 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
}
locked := conf.Locked
switch inputType { // TODO: add default case for generic error handling
case "file":
err := checkFileTypes(input, "file", locked)
if err != nil {
return fmt.Errorf("%s already locked: %s", input, err)
}
fmt.Println("Adding file to locked: ", input)
conf.Locked.Files = append(conf.Locked.Files, input)
return nil
case "folder":
err := checkFileTypes(input, "folder", locked)
if err != nil {
return fmt.Errorf("%s is already locked: %s", input, err)
}
fmt.Println("Adding folder to locked: ", input)
conf.Locked.Folders = append(conf.Locked.Folders, input)
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 := checkFileTypes(wildcard, "wildcard", locked)
if err != nil {
return fmt.Errorf("%s is already locked: %s", input, err)
}
fmt.Println("Adding wildcard to locked: ", wildcard)
conf.Locked.Exts = append(conf.Locked.Exts, wildcard)
return nil
}
return fmt.Errorf("This... should not have happened, some kind of internal error on LockFiles function call, switch failure")
}
// RemoveLockFiles removes files/folders/wildcards from the locked list
func RemoveLockFiles(input string, inputType string, conf *clientconfig.Gvcconfig) 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
}
locked := conf.Locked
switch inputType { // TODO: add default case for generic error handling
case "file":
err := checkFileTypes(input, "file", locked)
if err != nil {
fmt.Println("Removing file from locked: ", input)
for i, fileLock := range locked.Files {
if input == fileLock {
conf.Locked.Files[i] = conf.Locked.Files[len(conf.Locked.Files)-1] // Deleting the element
conf.Locked.Files = conf.Locked.Files[:len(conf.Locked.Files)-1] // redoing the slice
fmt.Println("Removing file from locked: ", input)
return nil
}
}
}
fmt.Println("File not found in ingores, unable to remove: ", input)
return nil
case "folder":
err := checkFileTypes(input, "folder", locked)
if err != nil {
for i, folderLock := range locked.Folders {
if input == folderLock {
conf.Locked.Folders[i] = conf.Locked.Folders[len(conf.Locked.Folders)-1]
conf.Locked.Folders = conf.Locked.Files[:len(conf.Locked.Folders)-1]
fmt.Println("Removing folder from locked: ", input)
return nil
}
}
}
fmt.Println("Folder not found in ingores, unable to remove: ", input)
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 := checkFileTypes(wildcard, "wildcard", locked)
if err != nil {
for i, wildcardLock := range locked.Exts {
if input == wildcardLock {
conf.Locked.Exts[i] = conf.Locked.Exts[len(conf.Locked.Exts)-1]
conf.Locked.Exts = conf.Locked.Exts[:len(conf.Locked.Exts)-1]
fmt.Println("Removing wildcard from locked: ", wildcard)
return nil
}
}
}
fmt.Println("Wildcard not found in ingores, unable to remove: ", wildcard)
return nil
}
return fmt.Errorf("This... should not have happened, some kind of internal error on RemoveLockFiles function call, switch failure")
}

View File

@@ -0,0 +1,52 @@
package clientcmd
import (
"fmt"
clientconfig "github.com/deranjer/gvc/client/clientconfig"
)
// 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
}

View File

@@ -2,14 +2,15 @@ package config
//Gvcconfig will be the struct that holds the entire client settings
type Gvcconfig struct {
Version string `toml:"version"`
RootPath string `toml:"rootPath"`
CurrentBranch string `toml:"currentbranch"`
LocalBranches []string `toml:"localbranches"` // LocalBranches constains a string list of branches on the local client. Names must be unique. \\TODO: someday add folders like git for branches
RemoteBranches []string `toml:"remotebranches"` // RemoteBranches constains a string list of branches on the server. Names must be unique. \\TODO: someday add folders like git for branches
Remotes []Remote `toml:"remote"` //The remote servers for the repo
Ignores Ignore `toml:"ignore"` //These files will be ignored for all add functions
NoCompress Ignore `toml:"nocompress"` //For binary compression some files should be ignored because the performance hit isn't worth the size savings
Version string `toml:"version"`
RootPath string `toml:"rootPath"`
Remotes []Remote `toml:"remote"` //The remote servers for the repo
CurrentBranch string `toml:"currentbranch"`
LocalBranches []string `toml:"localbranches"` // LocalBranches constains a string list of branches on the local client. Names must be unique. \\TODO: someday add folders like git for branches
RemoteBranches []string `toml:"remotebranches"` // RemoteBranches constains a string list of branches on the server. Names must be unique. \\TODO: someday add folders like git for branches
Locked FileTypes `toml:"locked"`
Ignores FileTypes `toml:"ignore"` //These files will be ignored for all add functions
NoCompress FileTypes `toml:"nocompress"` //For binary compression some files should be ignored because the performance hit isn't worth the size savings
}
//Remote will be a slice of remote server information
@@ -20,15 +21,8 @@ type Remote struct {
Default bool `toml:"default"` //Is this repo the default repo?
}
//Ignore is for ignoring files to add or ignoring compress
type Ignore struct {
Files []string `toml:"files"`
Exts []string `toml:"exts"`
Folders []string `toml:"folders"`
}
//Locked is a list of locked files/folders/wildcards
type Locked struct {
//FileTypes is for ignoring files to add or ignoring compress, or for locked files, all use the same type of struct (files, folders and exts)
type FileTypes struct {
Files []string `toml:"files"`
Exts []string `toml:"exts"`
Folders []string `toml:"folders"`