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

@@ -1,7 +1,14 @@
version = "0.1.5"
rootPath = ""
[[remote]]
name = "test"
host = "localhost"
port = 9999
[ignore]
files = ["client1.exe", "client2.exe", "client3.exe", "client4.exe", "client5.exe", "client6.exe", "client7.exe", "test1\\client8.exe", "clientcmd\\init.go"]
exts = [".exe", ".tl"]
[nocompress]

View File

@@ -25,7 +25,7 @@ func main() {
var err error
rootPath, err = os.Getwd()
if err != nil {
log.Fatalf("Can't get working dir, permissions issue %s", err)
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
@@ -34,7 +34,8 @@ func main() {
if isRepo {
err := store.Load(configPath, &conf)
if err != nil {
fmt.Println("Error loading config file into struct! ", err)
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 {
@@ -54,10 +55,17 @@ func main() {
// Adding the ignore commands
ignoreCommands(cli, &conf)
// Adding the test commands
infoCommands(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
@@ -211,16 +219,16 @@ func ignoreCommands(cli *clir.Cli, conf *clientconfig.Gvcconfig) {
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 to tracked files: ", file)
err := clientcmd.IgnoreFiles(file, "file", conf.Ignores)
//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 to tracked files: ", folder)
err := clientcmd.IgnoreFiles(folder, "folder", conf.Ignores)
fmt.Println("Ignoring contents of folder: ", folder)
err := clientcmd.IgnoreFiles(folder, "folder", conf)
if err != nil {
return err
}
@@ -228,7 +236,62 @@ func ignoreCommands(cli *clir.Cli, conf *clientconfig.Gvcconfig) {
}
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.Ignores)
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
}
@@ -237,3 +300,78 @@ func ignoreCommands(cli *clir.Cli, conf *clientconfig.Gvcconfig) {
return nil
})
}
func infoCommands(cli *clir.Cli, conf *clientconfig.Gvcconfig) {
//All the test 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)
}
if len(remoteInfoCmd.OtherArgs()) == 0 {
remoteInfoCmd.PrintHelp()
return fmt.Errorf("Please supply a server name to test a remote")
}
server := remoteInfoCmd.OtherArgs()[0]
conn, err := clientcmd.ConnectToServer(server, "master", conf)
if err != nil {
return err
}
err = clientcmd.GetServerInfo(conn)
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
// })
}

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