working on 'add remote' command and 'refresh' command

This commit is contained in:
2020-05-31 23:16:15 -04:00
parent 4fee4cec7d
commit 1a76a8f2aa
9 changed files with 192 additions and 16 deletions

View File

@@ -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
}

View File

@@ -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")
}

View File

@@ -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')

View File

@@ -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)
}
}
}
}
}