Files
gvc/client/client.go
2020-05-27 20:55:33 -04:00

152 lines
4.9 KiB
Go

package main
import (
"fmt"
"os"
clir "github.com/deranjer/clir"
clientcmd "github.com/deranjer/gvc/client/clientcmd"
clientconfig "github.com/deranjer/gvc/client/clientconfig"
"github.com/deranjer/store"
)
var version = "0.1.5"
func main() {
var conf clientconfig.Gvcconfig
err := store.Load(".gvcconfig.toml", &conf)
if err != nil {
fmt.Println("Error loading config file into struct! ", err)
}
clientconfig.ValidateConfig(&conf, version)
// Initialize our new cli
cli := clir.NewCli("gvcc", "Version control client for GVC", version)
// Adding the init command
initCommand(cli, &conf)
// Adding all the "add" commands
addCommands(cli, &conf)
err = cli.Run()
if err != nil {
fmt.Printf("Error occurred: %v\n", err)
}
// After the cli is finished running and writing new values, then validate the input and write it back to the TOML config file
err = clientconfig.ValidateConfig(&conf, version)
if err != nil {
fmt.Println("Error validating config, no changes from the command saved! ", err)
os.Exit(0)
}
err = store.Save(".gvcconfigNew.toml", &conf)
if err != nil {
fmt.Println("Error saving config back to toml file: ", err)
}
}
// validateRepo just ensures that the command is being run against an actual repo (bool yes or no)
func validateRepo() bool {
if _, err := os.Stat(".gvcconfig.toml"); os.IsNotExist(err) {
return false
}
return true
}
func initCommand(cli *clir.Cli, conf *clientconfig.Gvcconfig) {
//The init subcommand
initCmd := cli.NewSubCommand("init", "initializes a new gvc repo")
initCmd.LongDescription("This will create the .gvcconfig.toml (with defaults) file in this directory. You can edit this file directly (unsafe) or with the client(safe)")
initCmd.Action(func() error {
isRepo := validateRepo()
if !isRepo {
err := clientcmd.InitializeRepo()
if err != nil {
err = fmt.Errorf("unable to initialize repo: %s", err)
fmt.Println(err)
return err
}
return nil
}
fmt.Println("appears that this directory is already a gvc repo")
return nil
})
}
func addCommands(cli *clir.Cli, conf *clientconfig.Gvcconfig) {
//All the add commands and subcommands
//The add subcommand
addCmd := cli.NewSubCommand("add", "adds file(s)/folder(s) (recursively for folder) to repo")
addCmd.LongDescription("You can add all: all, a -file (-f): file.txt, or a -folder (-fd): folder, or a -wildcard (-wc): *.txt")
//File/Folder/Wildcard adding
var file string
var folder string
var wildcard string
fileFlag := addCmd.StringFlag("file", "tracks a file to add to the repo", &file)
fileFlag.FlagShortCut("file", "f")
folderFlag := addCmd.StringFlag("folder", "tracks all contents of a folder to add to the repo", &folder)
folderFlag.FlagShortCut("folder", "fd")
wildCardFlag := addCmd.StringFlag("wildcard", "treats the input as a wildcard and tracks all files that match the wildcard", &wildcard)
wildCardFlag.FlagShortCut("wildcard", "wc")
addCmd.Action(func() error {
if len(addCmd.OtherArgs()) > 0 {
addCmd.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("adding file to tracked files: ", file)
err := clientcmd.AddFiles(file, "file")
if err != nil {
return err
}
return nil
}
if folder != "" { // if the folder flag was used it won't be empty
fmt.Println("adding contents of folder to tracked files: ", folder)
err := clientcmd.AddFiles(folder, "folder")
if err != nil {
return err
}
return nil
}
if wildcard != "" { // if the wildcard flag was used it won't be empty
fmt.Println("adding files with wildcard filter: ", wildcard)
err := clientcmd.AddFiles(wildcard, "wildcard")
if err != nil {
return err
}
return nil
}
return nil
})
//Add all files recursively to repo
addall := addCmd.NewSubCommand("all", "add all of the file(s)/folders(s) recursively to repo")
addall.Action(func() error {
if len(addall.OtherArgs()) > 0 {
addCmd.PrintHelp()
return fmt.Errorf("the 'all' subcommand does not accept additional arguments")
}
fmt.Println("adding all files recursively in directory to tracked files")
err := clientcmd.AddFiles("", "all")
if err != nil {
return err
}
return nil
})
//The add remote command
remoteCmd := cli.NewSubCommand("remote", "add/delete/show remotes")
var name string
var host string
var port int
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")
remoteAddCmd.StringFlag("name", "the name you want for your remote server", &name)
remoteAddCmd.StringFlag("host", "the hostname or IP Address of the remote", &host)
remoteAddCmd.IntFlag("port", "the port the remote server is listening on", &port)
}