Files
gvc/server/engine/engine.go
2020-06-08 22:52:40 -04:00

107 lines
3.6 KiB
Go

package engine
import (
"fmt"
"log"
"net/http"
"github.com/deranjer/gvc/common"
serverconfig "github.com/deranjer/gvc/server/serverconfig"
"github.com/deranjer/store"
"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("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 {
if knownRepo.RepoName == repo {
repoInfo.BindIP = config.BindIP
repoInfo.Port = config.Port
repoInfo.RawPort = config.RawPort
repoInfo.Version = config.Version
repoInfo.Repo = knownRepo
for i := range repoInfo.Repo.KnownClients { // Blank out the client keys
repoInfo.Repo.KnownClients[i].Key = "REDACTED"
}
}
}
return context.JSONPretty(http.StatusAccepted, repoInfo, " ")
}
// LockFile just locks the file/folder/wildcard
func (Server *GVCServer) LockFile(context echo.Context) error {
fileType := context.Param("type")
fileName := context.Param("name")
repoName := context.Param("repo")
fmt.Printf("Lockfile: %s %s %s", repoName, fileType, fileName)
var locked common.FileTypes
for i, repo := range Server.Config.Repos {
if repo.RepoName == repoName {
locked = Server.Config.Repos[i].Locked
}
}
err := common.CheckFileTypes(fileName, fileType, locked) // making sure fi/f/wc is not already locked or cannot be locked.
if err != nil {
return fmt.Errorf("failed checking file lock: %s", err)
}
switch fileType {
case "file":
fmt.Println("Filename: ", fileName)
locked.Files = append(locked.Files, fileName)
case "folder":
fmt.Println("Folder: ", fileName)
locked.Folders = append(locked.Folders, fileName)
case "wildcard":
fmt.Println("Wildcard: ", fileName)
locked.Exts = append(locked.Exts, fileName)
}
fmt.Println("Locked: ", locked) // TODO!!!!!!!!!!!! Write this to conf
err = store.Save(serverconfig.DefaultConfigPath, Server.Config) // Save our new default config back to TOML so it can be read in
if err != nil {
log.Fatalf("unable to save config to toml file: %s", err)
}
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"
return context.JSON(http.StatusOK, helloMsg)
}