From c2e74ce7f4b4aa4018841361d60b2e67490c47ad Mon Sep 17 00:00:00 2001 From: deranjer Date: Mon, 8 Jun 2020 20:55:34 -0400 Subject: [PATCH] adding more server/client communication commands, need to fix echo path params --- client/.gvc/.gvcconfig.toml | 4 +-- client/client.go | 15 ++++++++--- client/clientcmd/commonlib.go | 2 +- client/clientcmd/info.go | 11 +++----- client/clientcmd/refresh.go | 21 +++++++++++++-- common/commonlib.go | 25 +++++++++--------- common/structures.go | 8 ++++++ server/engine/engine.go | 50 +++++++++++++++++++++++++++++++++-- server/server.go | 7 +++-- server/serverConfig.toml | 1 + 10 files changed, 111 insertions(+), 33 deletions(-) diff --git a/client/.gvc/.gvcconfig.toml b/client/.gvc/.gvcconfig.toml index 82451a6..e073777 100644 --- a/client/.gvc/.gvcconfig.toml +++ b/client/.gvc/.gvcconfig.toml @@ -9,7 +9,7 @@ remotebranches = ["master", "test", "test2"] name = "test2" host = "localhost" port = 80 - default = false + default = true [[remote]] name = "test4" @@ -27,7 +27,7 @@ remotebranches = ["master", "test", "test2"] name = "test5" host = "localhost1" port = 9997 - default = true + default = false [[remote]] name = "test6" diff --git a/client/client.go b/client/client.go index e4e9ae7..f0371b9 100644 --- a/client/client.go +++ b/client/client.go @@ -323,11 +323,18 @@ func infoCommands(cli *clir.Cli, conf *clientconfig.Gvcconfig) { 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") + var server string + if len(remoteInfoCmd.OtherArgs()) == 0 { //Notify that we are using default server if none specified + fmt.Println("No server specified, using default...") + for _, remote := range conf.Remotes { + if remote.Default { + server = remote.Name + } + } + } else { // Server name was specified, using that server + server = remoteInfoCmd.OtherArgs()[0] } - server := remoteInfoCmd.OtherArgs()[0] + connectionString, err := clientcmd.FindServer(server, "master", conf) if err != nil { return err diff --git a/client/clientcmd/commonlib.go b/client/clientcmd/commonlib.go index 3489b44..8f8fe32 100644 --- a/client/clientcmd/commonlib.go +++ b/client/clientcmd/commonlib.go @@ -40,7 +40,7 @@ func FindServer(serverName string, branchName string, conf *clientconfig.Gvcconf 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 //Create our connection string + connectionString := "http://" + remote.Host + port + "/" + conf.RepoName //Create our connection string //'http://server:port/:repo' //TODO, what about admin commands for entire server management, not just one repo? fmt.Println("Generated connection string: ", connectionString) return connectionString, nil } diff --git a/client/clientcmd/info.go b/client/clientcmd/info.go index db20ace..dbd93b0 100644 --- a/client/clientcmd/info.go +++ b/client/clientcmd/info.go @@ -8,21 +8,16 @@ import ( // GetServerInfo queries the supplied connection string for server info and uses the provided repoName to get repo specific information func GetServerInfo(connectionString string, repoName string) error { - serverURL := connectionString + "/info/" //creating the full string to get info client := resty.New() - resp, err := client.R(). - SetPathParams(map[string]string{ - "repoName": repoName, - }). - Get(serverURL + "{repoName}") + resp, err := client.R().Get(connectionString + "/info") //creating the full string to get info if err != nil { - return fmt.Errorf("error connecting to server at: %s: error was: %s", serverURL, err) + return fmt.Errorf("error connecting to server at: %s: error was: %s", connectionString, err) } if resp.IsError() { if resp.StatusCode() == 404 { return fmt.Errorf("error: repo was not found on server, 404: %s", resp.Request.URL) } - return fmt.Errorf("reponse not a success: %d: connection URL: %s", resp.StatusCode(), resp.Request.URL) + return fmt.Errorf("response not a success: %d: connection URL: %s", resp.StatusCode(), resp.Request.URL) } fmt.Println(resp) return nil diff --git a/client/clientcmd/refresh.go b/client/clientcmd/refresh.go index 79084d7..22ef970 100644 --- a/client/clientcmd/refresh.go +++ b/client/clientcmd/refresh.go @@ -4,6 +4,7 @@ import ( "fmt" clientconfig "github.com/deranjer/gvc/client/clientconfig" + "github.com/go-resty/resty/v2" ) // RefreshContent gets all the new file locks and updated pulls from the server (like git fetch) @@ -15,8 +16,24 @@ func RefreshContent(conf *clientconfig.Gvcconfig) error { if err != nil { return err } - fmt.Println("Connection String; ", connectionString) //TODO: Remove not needed - //TODO: Now refresh content + client := resty.New() + resp, err := client.R(). + // SetPathParams(map[string]string{ + // "repoName": repoName, + // }). + //Get(connectionString + "{repoName}" + "/info") //creating the full string to get info + Get(connectionString + "/refresh") + if err != nil { + return fmt.Errorf("error connecting to server at: %s: error was: %s", connectionString, err) + } + if resp.IsError() { + if resp.StatusCode() == 404 { + return fmt.Errorf("error: repo was not found on server, 404: %s", resp.Request.URL) + } + return fmt.Errorf("reponse not a success: %d: connection URL: %s", resp.StatusCode(), resp.Request.URL) + } + fmt.Println(resp) + return nil } } return nil diff --git a/common/commonlib.go b/common/commonlib.go index 057b84a..736b9cc 100644 --- a/common/commonlib.go +++ b/common/commonlib.go @@ -6,29 +6,30 @@ import ( ) // CheckFileTypes allows the server and client to do some basic validation on adding/removing file types to the toml file -func CheckFileTypes(input string, inputType string, ignores FileTypes) error { +// Since Ignore, NoCompress, and Locked have the same structure, you can provide the exclude list for any of them and this func will check that list to see if it already exists +func CheckFileTypes(input string, inputType string, excludeList 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 _, suppliedExt := range excludeList.Exts { + if fileExt == suppliedExt { //Check to make sure the file is not already on the list via ext + return fmt.Errorf("file ext is on excludeList, cannot add 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) + for _, suppliedFile := range excludeList.Files { + if input == suppliedFile { + return fmt.Errorf("file name is on excludeList, 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) + for _, suppliedFolder := range excludeList.Folders { + if input == suppliedFolder { + return fmt.Errorf("folder name is on the excludeList, cannot add folder with name %s", input) } } case "wildcard": - for _, ignoredExt := range ignores.Exts { - if input == ignoredExt { + for _, suppliedExt := range excludeList.Exts { + if input == suppliedExt { return fmt.Errorf("cannot add wildcard, since that ext is already added to the ignore config %s", input) } } diff --git a/common/structures.go b/common/structures.go index 0104505..06d9ee2 100644 --- a/common/structures.go +++ b/common/structures.go @@ -6,3 +6,11 @@ type FileTypes struct { Exts []string `toml:"exts"` Folders []string `toml:"folders"` } + +// RepoRefreshRequest returns locks, ignores, and commits/branches. Server marshals into this, client unmarshals +type RepoRefreshRequest struct { + Branches []string // List of known branches on server + Commits string //TODO: This will be pulled from DB and be a different type + Locked FileTypes + Ignores FileTypes +} diff --git a/server/engine/engine.go b/server/engine/engine.go index 7dc1693..6ad0eed 100644 --- a/server/engine/engine.go +++ b/server/engine/engine.go @@ -4,12 +4,15 @@ import ( "fmt" "net/http" + "github.com/deranjer/gvc/common" + serverconfig "github.com/deranjer/gvc/server/serverconfig" "github.com/labstack/echo" ) // GetInfo return the relevant repo specific info to the client func (Server *GVCServer) GetInfo(context echo.Context) error { - repo := context.Param("repoName") + repo := context.Param("repo") + fmt.Println("Asking about repo: ", repo) config := Server.Config var repoInfo RepoInfoRequest // Create an engine struct that contains basic server info as well as all repo info for _, knownRepo := range config.Repos { @@ -31,14 +34,57 @@ func (Server *GVCServer) GetInfo(context echo.Context) error { func (Server *GVCServer) LockFile(context echo.Context) error { fileType := context.Param("type") fileName := context.Param("name") + repoName := context.Param("repo") + var repo serverconfig.RepoConfig + for i, knownRepo := range Server.Config.Repos { + if knownRepo.RepoName == repoName { + repo = Server.Config.Repos[i] + } + } switch fileType { case "file": - //common.CheckFileTypes(fileName, fileType,) + err := common.CheckFileTypes(fileName, fileType, repo.Locked) + if err != nil { + return fmt.Errorf("failed checking file lock: %s", err) + } fmt.Println("Filename: ", fileName) } return nil } +// Refresh sends all updated information to client (like git fetch) +func (Server *GVCServer) Refresh(context echo.Context) error { + repoName := context.Param("repo") + var serverRepo serverconfig.RepoConfig + for i, repo := range Server.Config.Repos { + if repo.RepoName == repoName { + serverRepo = Server.Config.Repos[i] + } + } + // pull the locks and ignores to send back to client + branches := serverRepo.LocalBranches + serverLocks := serverRepo.Locked + serverIgnores := serverRepo.DefaultIgnores + refreshResult := common.RepoRefreshRequest{ + Branches: branches, + Locked: serverLocks, + Ignores: serverIgnores, + } + return context.JSON(http.StatusOK, refreshResult) +} + +// Revert fetches the supplied file (or entire repo) at version X and pushes it to the client // TODO: This should perhaps just be part of PULL? +func (Server *GVCServer) Revert(context echo.Context) error { + repoName := context.Param("repo") + return context.JSON(http.StatusOK, repoName) +} + +// Pull fetches the latest changes from the server to the client. +func (Server *GVCServer) Pull(context echo.Context) error { + helloMsg := "server alive" + return context.JSON(http.StatusOK, helloMsg) +} + // Hello just verifies the server is running //TODO remove this, just extra shit we are sending func (Server *GVCServer) Hello(context echo.Context) error { helloMsg := "server alive" diff --git a/server/server.go b/server/server.go index b13af11..3fe7f9c 100644 --- a/server/server.go +++ b/server/server.go @@ -49,7 +49,10 @@ func main() { server.Echo = e //Start the routes //e.GET("/hello", server.Hello) - e.GET("/info/:repoName", server.GetInfo) - e.GET("/lock/:type/:name", server.LockFile) + e.GET("/:repo/info/", server.GetInfo) + e.GET("/:repo/lock/:type/:name", server.LockFile) + e.GET("/:repo/refresh", server.Refresh) + e.GET("/:repo/revert/:hash", server.Revert) // TODO: Might not need this, just add extra args to pull? + e.GET("/:repo/pull/:branch", server.Pull) e.Logger.Fatal(e.Start(fmt.Sprintf("%s:%d", server.Config.BindIP, server.Config.Port))) } diff --git a/server/serverConfig.toml b/server/serverConfig.toml index 1b63793..d27d040 100644 --- a/server/serverConfig.toml +++ b/server/serverConfig.toml @@ -15,6 +15,7 @@ reporootpath = "F:\\repos" key = "12345" lastcommit = "4343434343434" [repo.locked] + files = ["client1.exe", "client2.exe"] [repo.defaultignore] [repo.nocompress]