package main import ( "fmt" "log" "os" "path/filepath" clir "github.com/deranjer/clir" clientcmd "github.com/deranjer/gvc/client/clientcmd" clientconfig "github.com/deranjer/gvc/client/clientconfig" config "github.com/deranjer/gvc/client/clientconfig" "github.com/deranjer/gvc/common" "github.com/deranjer/gvc/common/manager" "github.com/deranjer/store" "github.com/rs/zerolog" ) var version = "0.1.5" var configPath = ".gvc" + string(filepath.Separator) + ".gvcconfig.toml" var rootPath string func main() { // Sloppily inject a global variable //TODO maybe just path the variables manually or create a struct with them all in injectVariables() // Getting the root path var err error rootPath, err = os.Getwd() if err != nil { log.Fatalf("Can't get working dir, permissions issue? %s", err) } // Setting up a blank config to read the .toml file in if one exists var conf clientconfig.Gvcconfig var m *manager.Manager isRepo := validateRepo() if isRepo { // If repo folder exists, treat it like a repo and setup logging and database. If not a repo will not need any of this err := store.Load(configPath, &conf) if err != nil { fmt.Printf("Error loading config file into struct, please fix config, panic! \n%s", err) os.Exit(0) } err = clientconfig.ValidateConfig(&conf, version) if err != nil { fmt.Println("Error validating config, your config file is corrupt! ", err) os.Exit(0) } fmt.Println("Attempting to start logger...") // Checking the .gvc structure dirPaths, err := manager.CheckPaths(rootPath) if err != nil { log.Fatalf("unable to create/verify .gvc folder structure: %s", err) } // Setting up the logger to file output logFile, err := os.OpenFile(".gvc/logs/gvclog.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) if err != nil { log.Fatalf("unable to open log file at: %s, exiting with error: %s", ".gvc/logs/gvclog.log", err) } defer logFile.Close() // Setup non-http logging (using middleware for Echo http logging) logLevel, err := common.SetLogLevel(conf.LogLevel) if err != nil { fmt.Println("invalid log level set in config, setting to info") } clientlog := zerolog.New(logFile) clientlog.WithLevel(logLevel) // Check/Setup the database with logging //db, err := database.OpenOrCreateDB(".gvc/gvc.db", &clientlog) //if err != nil { // clientlog.Fatal().Msgf("Unable to open or create a database in the .gvc folder, fatal") //} informer := make(chan manager.OperatingMessage) m, err = manager.NewManager(rootPath, version, ".gvc/gvc.db", informer, dirPaths, &clientlog) if err != nil { clientlog.Fatal().Msgf("unable to create new manager object... %s", err) } } // 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, m) // Adding the ignore commands ignoreCommands(cli, &conf) // Adding the lock commands lockCommands(cli, &conf) // Adding the test commands infoCommands(cli, &conf) // Adding the refresh command refreshCommand(cli, &conf) // Adding the "remote" commands remoteCommands(cli, &conf) // Adding the "branch" command branchCommand(cli, &conf) // Adding the "switch" command switchCommand(cli, &conf) // Adding the "pull" command pullCommand(cli, &conf) // Adding the "commit" command commitCommand(cli, &conf) err = cli.Run() if err != nil { fmt.Printf("Error occurred: %v\n", err) } err = store.Save(configPath, conf) if err != nil { fmt.Println("Error saving config to file, changes were not writted! Check config file... ", err) } } // injectVariables just injects the global variables into the packages func injectVariables() { clientcmd.ConfigPath = configPath config.ConfigPath = configPath } // validateRepo just ensures that the command is being run against an actual repo (bool yes or no) func validateRepo() bool { if _, err := os.Stat(configPath); os.IsNotExist(err) { return false } 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/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() if !isRepo { fmt.Println("no valid repo found.. please run 'init' to setup a repo first") os.Exit(0) } err := clientcmd.RefreshContent(conf, conf.RepoName) if err != nil { return fmt.Errorf("unable to refresh content: %s", err) } return nil }) } 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 { repoName := clientcmd.InitializeRepo() // creates and checks the paths newConf := clientconfig.Gvcconfig{ RepoName: repoName, Version: version, CurrentBranch: "master", } err := store.Save(configPath, &newConf) if err != nil { log.Fatalf("unable to create new config in .gvc %s", err) } return nil } fmt.Println("appears that this directory is already a gvc repo") return nil }) } func addCommands(cli *clir.Cli, conf *clientconfig.Gvcconfig, m *manager.Manager) { //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 { isRepo := validateRepo() if !isRepo { fmt.Println("no valid repo found.. please run 'init' to setup a repo first") os.Exit(0) } 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", conf.Ignores, m) 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", conf.Ignores, m) 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", conf.Ignores, m) 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) in root dir recursively to repo") addall.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(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", conf.Ignores, m) if err != nil { return err } return nil }) } func ignoreCommands(cli *clir.Cli, conf *clientconfig.Gvcconfig) { //All the ignore commands and subcommands //The ignore subcommand ignoreCmd := cli.NewSubCommand("ignore", "ignores file(s)/folder(s) (recursively for folder) to repo") ignoreCmd.LongDescription("You can 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 := ignoreCmd.StringFlag("file", "adds a file to ignore to the config", &file) fileFlag.FlagShortCut("file", "f") folderFlag := ignoreCmd.StringFlag("folder", "tracks all contents of a folder to ignore", &folder) folderFlag.FlagShortCut("folder", "fd") wildCardFlag := ignoreCmd.StringFlag("wildcard", "treats the input as a wildcard and ignores all files that match the wildcard", &wildcard) wildCardFlag.FlagShortCut("wildcard", "wc") ignoreCmd.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(ignoreCmd.OtherArgs()) > 0 { ignoreCmd.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.IgnoreFiles(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.IgnoreFiles(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.IgnoreFiles(wildcard, "wildcard", conf) if err != nil { return err } return nil } return nil }) // Adding the remove ignore command (remove from the ignore list) removeIgnoreCmd(ignoreCmd, conf) } // removeIgnoreCmd removes files/folders/wildcard from the ignore list func removeIgnoreCmd(ignoreCmd *clir.Command, conf *clientconfig.Gvcconfig) { removeIgnoreCmd := ignoreCmd.NewSubCommand("remove", "remove from ignores file(s)/folder(s) (recursively for folder) to repo") removeIgnoreCmd.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 := removeIgnoreCmd.StringFlag("file", "removes ignored file from config", &file) fileFlag.FlagShortCut("file", "f") folderFlag := removeIgnoreCmd.StringFlag("folder", "removes ignored folder", &folder) folderFlag.FlagShortCut("folder", "fd") wildCardFlag := removeIgnoreCmd.StringFlag("wildcard", "removes wildcard from ignores", &wildcard) wildCardFlag.FlagShortCut("wildcard", "wc") removeIgnoreCmd.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(removeIgnoreCmd.OtherArgs()) > 0 { removeIgnoreCmd.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.RemoveIgnoreFiles(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.RemoveIgnoreFiles(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.RemoveIgnoreFiles(wildcard, "wildcard", conf) if err != nil { return err } return nil } return nil }) } func infoCommands(cli *clir.Cli, conf *clientconfig.Gvcconfig) { //All the info commands infoCmd := cli.NewSubCommand("info", "tests various aspects of the client/files/etc and server") infoCmd.LongDescription("You can get information on remotes, files, etc") remoteInfoCmd := infoCmd.NewSubCommand("remote", "takes the suppled server name and gets information about the server") remoteInfoCmd.Action(func() error { isRepo := validateRepo() if !isRepo { fmt.Println("no valid repo found.. please run 'init' to setup a repo first") os.Exit(0) } var server string if len(remoteInfoCmd.OtherArgs()) == 0 { //Notify that we are using default server if none specified fmt.Println("No server specified, using default...") for _, remote := range conf.Remotes { if remote.Default { server = remote.Name } } } else { // Server name was specified, using that server server = remoteInfoCmd.OtherArgs()[0] } connectionString, err := clientcmd.FindServer(server, "master", conf) if err != nil { return err } err = clientcmd.GetServerInfo(connectionString, conf.RepoName) if err != nil { return err } return nil }) // //File/Folder/Wildcard Ignoring // var file string // var folder string // var wildcard string // fileFlag := ignoreCmd.StringFlag("file", "adds a file to ignore to the config", &file) // fileFlag.FlagShortCut("file", "f") // folderFlag := ignoreCmd.StringFlag("folder", "tracks all contents of a folder to ignore", &folder) // folderFlag.FlagShortCut("folder", "fd") // wildCardFlag := ignoreCmd.StringFlag("wildcard", "treats the input as a wildcard and ignores all files that match the wildcard", &wildcard) // wildCardFlag.FlagShortCut("wildcard", "wc") // ignoreCmd.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(ignoreCmd.OtherArgs()) > 0 { // ignoreCmd.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.IgnoreFiles(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.IgnoreFiles(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.IgnoreFiles(wildcard, "wildcard", conf) // if err != nil { // return err // } // return nil // } // 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 && file == "" && folder == "" && wildcard == "" { // if no input lockCmd.PrintHelp() return fmt.Errorf("please provide a file/folder/ext flag and name to be locked") } if len(lockCmd.OtherArgs()) > 1 { 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 { fmt.Println("error occurred locking file, attempting to roll back changes...") err := clientcmd.RemoveLockFiles(file, "file", conf) if err != nil { return fmt.Errorf("fatal error: unable to roll back lock file changes, issue with config: %s", err) } 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 { err := clientcmd.RemoveLockFiles(folder, "folder", conf) if err != nil { return fmt.Errorf("fatal error: unable to roll back lock folder changes, issue with config: %s", err) } 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 { err := clientcmd.RemoveLockFiles(wildcard, "wildcard", conf) if err != nil { return fmt.Errorf("fatal error: unable to roll back lock wildcard changes, issue with config: %s", err) } 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 }) } func branchCommand(cli *clir.Cli, conf *config.Gvcconfig) { branchCommand := cli.NewSubCommand("branch", "creates a new branch off of the current branch") var branchName string nameFlag := branchCommand.StringFlag("name", "name of the branch to create", &branchName) nameFlag.FlagShortCut("name", "n") nameFlag.FlagRequired("name") branchCommand.Action(func() error { isRepo := validateRepo() if !isRepo { fmt.Println("no valid repo found.. please run 'init' to setup a repo first") os.Exit(0) } err := clientcmd.CreateBranch(conf, branchName) if err != nil { return err } return nil }) } func switchCommand(cli *clir.Cli, conf *config.Gvcconfig) { switchCommand := cli.NewSubCommand("switch", "switches (and can create if needed) a new branch to work on") var createBranch bool createFlag := switchCommand.BoolFlag("create", "creates the branch if it does not exist", &createBranch) createFlag.FlagShortCut("create", "c") switchCommand.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 createBranch { if len(switchCommand.OtherArgs()) < 1 { switchCommand.PrintHelp() fmt.Println("branch name required..") os.Exit(0) } branchName := switchCommand.OtherArgs()[0] fmt.Println("attempting to create branch name: ", branchName) err := clientcmd.CreateBranch(conf, branchName) if err != nil { return fmt.Errorf("error creating branch: %s", err) } return nil } if len(switchCommand.OtherArgs()) == 0 { switchCommand.PrintHelp() fmt.Println("branch name required..") os.Exit(0) } branchName := switchCommand.OtherArgs()[0] err := clientcmd.SwitchBranch(conf, branchName) if err != nil { return fmt.Errorf("unable to switch branch: %s", err) } return nil }) } func pullCommand(cli *clir.Cli, conf *config.Gvcconfig) { pullCommand := cli.NewSubCommand("pull", "pulls the latest commit from the server (default if none specified) on your current branch") var createBranch bool createFlag := pullCommand.BoolFlag("create", "creates the branch if it does not exist", &createBranch) createFlag.FlagShortCut("create", "c") pullCommand.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(pullCommand.OtherArgs()) == 0 { pullCommand.PrintHelp() fmt.Println("branch name required..") os.Exit(0) } branchName := pullCommand.OtherArgs()[0] err := clientcmd.SwitchBranch(conf, branchName) if err != nil { return fmt.Errorf("unable to pull branch: %s", err) } return nil }) } func commitCommand(cli *clir.Cli, conf *config.Gvcconfig) { commitCommand := cli.NewSubCommand("commit", "commits the changes to the repo") var commitMessage string commitMessageFlag := pullCommand.StringFlag("message", "adds a message to a commit", &commitMessage) commitMessageFlag.FlagShortCut("message", "m") commitCommand.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(commitCommand.OtherArgs()) == 0 { commitCommand.PrintHelp() fmt.Println("branch name required..") os.Exit(0) } branchName := commitCommand.OtherArgs()[0] err := clientcmd.SwitchBranch(conf, branchName) if err != nil { return fmt.Errorf("unable to pull branch: %s", err) } return nil }) }