starting client/server connection

This commit is contained in:
2020-05-31 18:42:38 -04:00
parent 6ae1705ef0
commit 0051b92c47
9 changed files with 439 additions and 11 deletions

View File

@@ -10,6 +10,10 @@ import (
//AddFiles adds files to the repo, inputType specifies file, folder, wildcard or all
func AddFiles(input string, inputType string, ignore clientconfig.Ignore) 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
}
workingDir, err := os.Getwd()
if err != nil {
return err

View File

@@ -2,7 +2,10 @@ package clientcmd
import (
"fmt"
"net"
"os"
"path/filepath"
"strconv"
clientconfig "github.com/deranjer/gvc/client/clientconfig"
)
@@ -10,6 +13,25 @@ import (
// ConfigPath is the global path to the config that is injected from the main client package.
var ConfigPath string
func validateFileType(path string, inputType string) error {
fullPath, err := filepath.Abs(path)
if err != nil {
return fmt.Errorf("cannot stat file, invalid input? %s", err)
}
fileInfo, err := os.Stat(fullPath)
if err != nil {
return fmt.Errorf("unable to read file, corrupted? %s", err)
}
if fileInfo.IsDir() {
if inputType == "folder" {
return nil
} else {
return fmt.Errorf("folder flag was used, but input is not a folder, will not continue")
}
}
return nil
}
func checkIgnores(input string, inputType string, ignores clientconfig.Ignore) error {
switch inputType {
case "file":
@@ -39,3 +61,24 @@ func checkIgnores(input string, inputType string, ignores clientconfig.Ignore) e
}
return nil
}
// ConnectToServer sends a quick hello to the server to make sure it is live and we can connect to it.
func ConnectToServer(serverName string, branchName string, conf *clientconfig.Gvcconfig) (conn *net.Conn, err error) {
if branchName == "" { // If no branch listed select master TODO: in future the 'default' branch will be their current branch
branchName = "master"
}
for _, remote := range conf.Remotes {
if serverName == remote.Name {
fmt.Printf("Server found in config, connecting to: %s, host: %s, port: %d \n", remote.Name, remote.Host, remote.Port)
port := ":" + strconv.Itoa(remote.Port)
connectionString := remote.Host + port
conn, err := net.Dial("tcp", connectionString)
if err != nil {
return nil, fmt.Errorf("error connecting to %s", connectionString)
}
fmt.Println("Connection started on: ", connectionString)
return &conn, nil
}
}
return nil, fmt.Errorf("unable to find server name in config...")
}

View File

@@ -2,13 +2,109 @@ package clientcmd
import (
"fmt"
"os"
clientconfig "github.com/deranjer/gvc/client/clientconfig"
)
//IgnoreFiles ignores file(s)/folder based on name/wildcard, etc
func IgnoreFiles(input string, inputType string, ignore clientconfig.Ignore) error {
fmt.Println("File/folder/wildcard to ignore", os.Args[2])
return nil
func IgnoreFiles(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
}
ignore := conf.Ignores
switch inputType { // TODO: add default case for generic error handling
case "file":
err := checkIgnores(input, "file", ignore)
if err != nil {
return fmt.Errorf("%s already ignored: %s", input, err)
}
fmt.Println("Adding file to ignores: ", input)
conf.Ignores.Files = append(conf.Ignores.Files, input)
return nil
case "folder":
err := checkIgnores(input, "folder", ignore)
if err != nil {
return fmt.Errorf("%s is already ignored: %s", input, err)
}
fmt.Println("Adding folder to ignores: ", input)
conf.Ignores.Folders = append(conf.Ignores.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 := checkIgnores(wildcard, "wildcard", ignore)
if err != nil {
return fmt.Errorf("%s is already ignored: %s", input, err)
}
fmt.Println("Adding wildcard to ignores: ", wildcard)
conf.Ignores.Exts = append(conf.Ignores.Exts, wildcard)
return nil
}
return fmt.Errorf("This... should not have happened, some kind of internal error on IgnoreFiles function call, switch failure")
}
// RemoveIgnoreFiles removes files/folders/wildcards from the ignore list
func RemoveIgnoreFiles(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
}
ignore := conf.Ignores
switch inputType { // TODO: add default case for generic error handling
case "file":
err := checkIgnores(input, "file", ignore)
if err != nil {
fmt.Println("Removing file from ignores: ", input)
for i, fileIgnore := range ignore.Files {
if input == fileIgnore {
conf.Ignores.Files[i] = conf.Ignores.Files[len(conf.Ignores.Files)-1] // Deleting the element
conf.Ignores.Files = conf.Ignores.Files[:len(conf.Ignores.Files)-1] // redoing the slice
fmt.Println("Removing file from ignores: ", input)
return nil
}
}
}
fmt.Println("File not found in ingores, unable to remove: ", input)
return nil
case "folder":
err := checkIgnores(input, "folder", ignore)
if err != nil {
for i, folderIgnore := range ignore.Folders {
if input == folderIgnore {
conf.Ignores.Folders[i] = conf.Ignores.Folders[len(conf.Ignores.Folders)-1]
conf.Ignores.Folders = conf.Ignores.Files[:len(conf.Ignores.Folders)-1]
fmt.Println("Removing folder from ignores: ", 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 := checkIgnores(wildcard, "wildcard", ignore)
if err != nil {
for i, wildcardIgnore := range ignore.Exts {
if input == wildcardIgnore {
conf.Ignores.Exts[i] = conf.Ignores.Exts[len(conf.Ignores.Exts)-1]
conf.Ignores.Exts = conf.Ignores.Exts[:len(conf.Ignores.Exts)-1]
fmt.Println("Removing wildcard from ignores: ", 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 RemoveIgnoreFiles function call, switch failure")
}

27
client/clientcmd/info.go Normal file
View File

@@ -0,0 +1,27 @@
package clientcmd
import (
"bufio"
"fmt"
"net"
"os"
)
func GetServerInfo(conn *net.Conn) error {
for {
fmt.Println("New LOop")
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")
conn.Send(fmt.Fprintf(*conn, "Details\n"))
default:
fmt.Println("Details: ", message)
os.Exit(0)
}
}
}