Files
gvc/client/clientcmd/commonlib.go

90 lines
2.9 KiB
Go

package clientcmd
import (
"fmt"
"os"
"path/filepath"
"strconv"
clientconfig "github.com/deranjer/gvc/client/clientconfig"
"github.com/go-resty/resty/v2"
)
// 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
}
// PingServer sends a quick hello to the server to make sure it is live and we can connect to it. //TODO Remove this is just extra shit
func PingServer(serverName string, branchName string, conf *clientconfig.Gvcconfig) 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 := "http://" + remote.Host + port + "/hello" //Create our hello request
fmt.Println("Attempting connection on: ", connectionString)
client := resty.New()
resp, err := client.R().EnableTrace().Get(connectionString)
if err != nil {
return fmt.Errorf("error connecting to server at: %s: error was: %s", connectionString, err)
}
if resp.StatusCode() == 200 {
return nil
}
return fmt.Errorf("response from server was not 200, was instead %d", resp.StatusCode())
}
}
return fmt.Errorf("unable to find server name in config")
}