From 9e907a6027e97882ce2ac535ed0f4cc6933937ec Mon Sep 17 00:00:00 2001 From: xzyaoi Date: Sun, 9 Dec 2018 17:55:15 +0800 Subject: [PATCH] add system info query system --- cli/daemon.go | 24 ++++++++++++++++++++++++ cli/handler.go | 13 +------------ cli/repository.go | 12 ++++++++++++ cli/system.go | 30 ++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 12 deletions(-) create mode 100644 cli/system.go diff --git a/cli/daemon.go b/cli/daemon.go index 5539354ee..010c3aced 100644 --- a/cli/daemon.go +++ b/cli/daemon.go @@ -68,6 +68,23 @@ func GetRunningSolversByPackageHandler(c *gin.Context) { c.JSON(http.StatusOK, runningSolversInPackage) } +// Handle Post Repos Request -> Install Package +type addRepoRequest struct { + RepoType string `json:type` + URL string `json:url` +} +func PostReposHandler(c *gin.Context) { + config := readConfig() + var _addRepoRequest addRepoRequest + c.BindJSON(&_addRepoRequest) + if (_addRepoRequest.RepoType == "git") { + InstallFromGit(_addRepoRequest.URL) + c.JSON(http.StatusOK, config.Repositories) + } else { + c.JSON(http.StatusBadRequest, config.Repositories) + } +} + // Handle Get Request -> Get All Repos func GetReposHandler(c *gin.Context) { config := readConfig() @@ -81,6 +98,11 @@ func GetRepoMetaHandler(c *gin.Context) { c.JSON(http.StatusOK, GetMetaInfo(vendor, name)) } +// Handle Get System Information +func GetSystemHandler(c *gin.Context) { + c.JSON(http.StatusOK, getSystemInfo()) +} + // Handle Socket Request func socketHandler(c *gin.Context) { socketServer.On("connection", func(so socketio.Socket) { @@ -144,6 +166,8 @@ func runServer(port string) { "daemon": "running", }) }) + // System Related Handlers + r.GET("/system", GetSystemHandler) // Repo Related Routes r.GET("/repo/meta/:vendor/:name", GetRepoMetaHandler) r.POST("/repo/running", PostRunningRepoHandler) diff --git a/cli/handler.go b/cli/handler.go index 8744b5a78..95726ecb4 100644 --- a/cli/handler.go +++ b/cli/handler.go @@ -57,21 +57,10 @@ func InstallHandler(c *cli.Context) { } else { color.Cyan("Installing to " + localFolder) } - var repoFolder string - var repo Repository // Download Codebase if strings.HasPrefix(remoteURL, "https://github.com") { - repo = CloneFromGit(remoteURL, localFolder) + InstallFromGit(remoteURL) } - repoFolder = repo.LocalFolder - // Install Dependencies - color.Cyan("Installing Dependencies... please wait patiently") - InstallDependencies(repoFolder) - color.Blue("Generating Runners") - GeneratingRunners(repoFolder) - color.Cyan("Adding to Local Configuration") - config.Repositories = addRepo(config.Repositories, repo) - writeConfig(config) } // Handle List diff --git a/cli/repository.go b/cli/repository.go index e56ad92f4..69d9ef8e7 100644 --- a/cli/repository.go +++ b/cli/repository.go @@ -165,3 +165,15 @@ func GetMetaInfo(Vendor string, Name string) RepositoryMetaInfo { } return repositoryMeta } + +// Install Repository from Git +func InstallFromGit(remoteURL string) { + config := readConfig() + var repo Repository + repo = CloneFromGit(remoteURL, config.Local.LocalFolder) + repoFolder := repo.LocalFolder + InstallDependencies(repoFolder) + GeneratingRunners(repoFolder) + config.Repositories = addRepo(config.Repositories, repo) + writeConfig(config) +} diff --git a/cli/system.go b/cli/system.go new file mode 100644 index 000000000..09a9bba31 --- /dev/null +++ b/cli/system.go @@ -0,0 +1,30 @@ +package main + +import ( + "github.com/shirou/gopsutil/cpu" + "github.com/shirou/gopsutil/host" + "github.com/shirou/gopsutil/mem" +) + +type SystemInfo struct { + CpuName string `json:"cpu"` + Memory uint64 `json:"memory"` + Platform string `json:"platform"` + Os string `json:"os"` + PlatformVersion string `json:"platformVersion"` +} + +func getSystemInfo() SystemInfo { + var systemInfo SystemInfo + v, _ := mem.VirtualMemory() + systemInfo.Memory = v.Total + platform, _ := host.Info() + systemInfo.Platform = platform.Platform + systemInfo.PlatformVersion = platform.PlatformVersion + systemInfo.Os = platform.OS + + // Handle CPU + cpuInfo, _ := cpu.Info() + systemInfo.CpuName = cpuInfo[0].ModelName + return systemInfo +}