diff --git a/client/.gvc/.gvcconfig.toml b/client/.gvc/.gvcconfig.toml index fde0e65..eafa6ed 100644 --- a/client/.gvc/.gvcconfig.toml +++ b/client/.gvc/.gvcconfig.toml @@ -1,10 +1,36 @@ version = "0.1.5" rootPath = "" +currentBranch = "master" [[remote]] - name = "test" + name = "test2" host = "localhost" port = 9999 + default = false + +[[remote]] + name = "test4" + host = "localhost" + port = 9998 + default = false + +[[remote]] + name = "test3" + host = "localhost" + port = 9997 + default = false + +[[remote]] + name = "test5" + host = "localhost1" + port = 9997 + default = true + +[[remote]] + name = "test6" + host = "localhost1" + port = 9995 + default = false [ignore] files = ["client1.exe", "client2.exe", "client3.exe", "client4.exe", "client5.exe", "client6.exe", "client7.exe", "test1\\client8.exe", "clientcmd\\init.go"] diff --git a/client/.gvc/.gvcconfigNew.toml b/client/.gvc/.gvcconfigNew.toml index c39a6ea..d6e9bfe 100644 --- a/client/.gvc/.gvcconfigNew.toml +++ b/client/.gvc/.gvcconfigNew.toml @@ -5,11 +5,13 @@ rootPath = "client" name = "origin" host = "testorigin.com" port = 8694 + default = true [[remote]] name = "secondOrigin" host = "170.5.95.195" port = 4253 + default = false [ignore] files = ["file1.txt", "file2.txt"] diff --git a/client/client.go b/client/client.go index 70e079b..baae89d 100644 --- a/client/client.go +++ b/client/client.go @@ -58,6 +58,9 @@ func main() { // Adding the test commands infoCommands(cli, &conf) + // Adding the refresh command + refreshCommand(cli, &conf) + err = cli.Run() if err != nil { fmt.Printf("Error occurred: %v\n", err) @@ -82,6 +85,20 @@ func validateRepo() bool { return true } +func refreshCommand(cli *clir.Cli, conf *clientconfig.Gvcconfig) { + refreshCmd := cli.NewSubCommand("refresh", "pulls down all locks unknown to client pushes") + 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) + } + + return nil + }) +} + func initCommand(cli *clir.Cli, conf *clientconfig.Gvcconfig) { //The init subcommand initCmd := cli.NewSubCommand("init", "initializes a new gvc repo") @@ -91,7 +108,8 @@ func initCommand(cli *clir.Cli, conf *clientconfig.Gvcconfig) { if !isRepo { clientcmd.InitializeRepo() // creates and checks the paths newConf := clientconfig.Gvcconfig{ - Version: version, + Version: version, + CurrentBranch: "master", } err := store.Save(configPath, &newConf) if err != nil { @@ -157,8 +175,13 @@ func addCommands(cli *clir.Cli, conf *clientconfig.Gvcconfig) { }) //Add all files recursively to repo - addall := addCmd.NewSubCommand("all", "add all of the file(s)/folders(s) 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") @@ -176,6 +199,7 @@ func addCommands(cli *clir.Cli, conf *clientconfig.Gvcconfig) { 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) @@ -187,7 +211,23 @@ func addCommands(cli *clir.Cli, conf *clientconfig.Gvcconfig) { 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 }) @@ -302,7 +342,7 @@ func removeIgnoreCmd(ignoreCmd *clir.Command, conf *clientconfig.Gvcconfig) { } func infoCommands(cli *clir.Cli, conf *clientconfig.Gvcconfig) { - //All the test commands + //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") diff --git a/client/clientcmd/add.go b/client/clientcmd/add.go index a297ea9..a7efe92 100644 --- a/client/clientcmd/add.go +++ b/client/clientcmd/add.go @@ -131,8 +131,62 @@ func AddFiles(input string, inputType string, ignore clientconfig.Ignore) error return nil } -// AddRemote adds a remote to the config file -func AddRemote(name string, host string, port int, conf *clientconfig.Gvcconfig) error { - fmt.Println("name: ", name, "host: ", host, "port: ", port) +// 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 +} diff --git a/client/clientcmd/commonlib.go b/client/clientcmd/commonlib.go index c465f1e..3763f24 100644 --- a/client/clientcmd/commonlib.go +++ b/client/clientcmd/commonlib.go @@ -80,5 +80,5 @@ func ConnectToServer(serverName string, branchName string, conf *clientconfig.Gv return &conn, nil } } - return nil, fmt.Errorf("unable to find server name in config...") + return nil, fmt.Errorf("unable to find server name in config") } diff --git a/client/clientcmd/info.go b/client/clientcmd/info.go index 96a2dcb..8b0ef59 100644 --- a/client/clientcmd/info.go +++ b/client/clientcmd/info.go @@ -7,6 +7,7 @@ import ( "os" ) +// GetServerInfo queries the supplied server name for information about the server func GetServerInfo(conn *net.Conn) error { for { // Server should send 'Connected\n' to client after connect, then client can ask for server details message, err := bufio.NewReader(*conn).ReadString('\n') diff --git a/client/clientcmd/refresh.go b/client/clientcmd/refresh.go new file mode 100644 index 0000000..dd08986 --- /dev/null +++ b/client/clientcmd/refresh.go @@ -0,0 +1,43 @@ +package clientcmd + +import ( + "bufio" + "fmt" + "os" + + clientconfig "github.com/deranjer/gvc/client/clientconfig" +) + +func RefreshContent(conf *clientconfig.Gvcconfig) error { + remotes := conf.Remotes + for _, remote := range remotes { + if remote.Default { + conn, err := ConnectToServer(remote.Name, conf.CurrentBranch, conf) + if err != nil { + return err + } + for { // Server should send 'Connected\n' to client after connect, then client can ask for server details + message, err := bufio.NewReader(*conn).ReadString('\n') + if err != nil { + return fmt.Errorf("error reading from connection: %s", err) + } + fmt.Printf("Message from server: %s", message) + switch message { + case "Connected\n": + fmt.Println("Server has acknowledged connection, asking for server details") + bytesSent, err := fmt.Fprintf(*conn, "Details\n") + if err != nil { + fmt.Println("Error sending message to server! ", err) + os.Exit(0) + } + if bytesSent > 0 { + fmt.Println("Message Sent") + } + default: + fmt.Println("Details: ", message) + os.Exit(0) + } + } + } + } +} diff --git a/client/clientconfig/structures.go b/client/clientconfig/structures.go index a5a2e4f..03c569c 100644 --- a/client/clientconfig/structures.go +++ b/client/clientconfig/structures.go @@ -2,18 +2,20 @@ package config //Gvcconfig will be the struct that holds the entire client settings type Gvcconfig struct { - Version string `toml:"version"` - RootPath string `toml:"rootPath"` - 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"` + CurrentBranch string `toml:"currentbranch"` + 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 } //Remote will be a slice of remote server information type Remote struct { - Name string `toml:"name"` - Host string `toml:"host"` - Port int `toml:"port"` + Name string `toml:"name"` + Host string `toml:"host"` + Port int `toml:"port"` + Default bool `toml:"default"` //Is this repo the default repo? } //Ignore is for ignoring files to add or ignoring compress @@ -22,3 +24,10 @@ type Ignore struct { Exts []string `toml:"exts"` Folders []string `toml:"folders"` } + +//Locked is a list of locked files/folders/wildcards +type Locked struct { + Files []string `toml:"files"` + Exts []string `toml:"exts"` + Folders []string `toml:"folders"` +} diff --git a/server/server.go b/server/server.go index 521dc6d..c2cc170 100644 --- a/server/server.go +++ b/server/server.go @@ -30,6 +30,7 @@ func main() { }) server.OnClientConnectionClosed(func(c *tcp_server.Client, err error) { // connection with client lost + fmt.Println("Connection with client closed") }) server.Listen()