adding lock command, generalizing the checkfiletypes function
This commit is contained in:
		
							
								
								
									
										110
									
								
								client/clientcmd/lock.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										110
									
								
								client/clientcmd/lock.go
									
									
									
									
									
										Normal 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")
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user