106 lines
3.3 KiB
Go
106 lines
3.3 KiB
Go
package clientcmd
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
|
|
clientconfig "github.com/deranjer/gvc/client/clientconfig"
|
|
messages "github.com/deranjer/gvc/messages"
|
|
)
|
|
|
|
// 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 checkFileTypes(input string, inputType string, ignores clientconfig.FileTypes) error {
|
|
switch inputType {
|
|
case "file":
|
|
fileExt := filepath.Ext(input) // TODO more sanity checking on ext
|
|
for _, ignoredExt := range ignores.Exts {
|
|
if fileExt == ignoredExt {
|
|
return fmt.Errorf("file ext is on ignored list, cannot add file with file ext %s", fileExt)
|
|
}
|
|
}
|
|
for _, ignoredFile := range ignores.Files {
|
|
if input == ignoredFile {
|
|
return fmt.Errorf("file name is on ignored list, cannot add file with name %s", input)
|
|
}
|
|
}
|
|
case "folder":
|
|
for _, ignoredFolder := range ignores.Folders {
|
|
if input == ignoredFolder {
|
|
return fmt.Errorf("folder name is on the ignored list, cannot add folder with name %s", input)
|
|
}
|
|
}
|
|
case "wildcard":
|
|
for _, ignoredExt := range ignores.Exts {
|
|
if input == ignoredExt {
|
|
return fmt.Errorf("cannot add wildcard, since that ext is already added to the ignore config %s", input)
|
|
}
|
|
}
|
|
}
|
|
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)
|
|
defer conn.Close()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error connecting to %s", connectionString)
|
|
}
|
|
fmt.Println("Attempting connection on: ", connectionString)
|
|
decoder := json.NewDecoder(conn)
|
|
var newMessage messages.Command
|
|
for { // Doing our 'handshake'
|
|
decoder.Decode(&newMessage)
|
|
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error reading from connection: %s", err)
|
|
}
|
|
fmt.Println("Message from server", newMessage)
|
|
switch newMessage.CmdID {
|
|
case messages.CONNECTED:
|
|
fmt.Println("Server recognized client")
|
|
return &conn, nil
|
|
default:
|
|
fmt.Println("message: ", newMessage.CmdID)
|
|
return nil, fmt.Errorf("Unexpected message from server")
|
|
//return nil, fmt.Errorf("unexpected response from server: ", newMessage)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return nil, fmt.Errorf("unable to find server name in config")
|
|
}
|