From 04c972c1d0937e7e0f943794a96ff07583a998e4 Mon Sep 17 00:00:00 2001 From: Nicolas Giard Date: Sat, 18 Aug 2018 02:04:31 -0400 Subject: [PATCH] feat: installer syscheck + prompt (wip) --- dev/installer/main.go | 56 ++++++++++++++++++++++++++++++++++++--- dev/installer/syscheck.go | 54 +++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 dev/installer/syscheck.go diff --git a/dev/installer/main.go b/dev/installer/main.go index f0cfac23..221d4555 100644 --- a/dev/installer/main.go +++ b/dev/installer/main.go @@ -19,29 +19,75 @@ var logo = ` |__/ ` +var finish = ` +>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> +| | +| Open http://localhost:3000/ in your browser | +| to complete the installation! | +| | +<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< +` + func main() { fmt.Println(chalk.Yellow.Color(logo)) fmt.Println(chalk.Bold.TextStyle("Installer for Wiki.js 2.x")) - fmt.Printf("for %s-%s\n\n", runtime.GOOS, runtime.GOARCH) + fmt.Printf("%s-%s\n\n", runtime.GOOS, runtime.GOARCH) + + // Check system requirements + + fmt.Println(chalk.Bold.TextStyle("Verifying system requirements...")) + CheckNodeJs() + CheckRAM() + fmt.Println(chalk.Bold.TextStyle("\nSetup")) // Prompt for build to install - prompt := promptui.Select{ + promptBuild := promptui.Select{ Label: "Select Build to install", Items: []string{"Stable", "Dev"}, + Templates: &promptui.SelectTemplates{ + Help: " ", + Selected: chalk.Green.Color("✔") + " Build: {{ . }}", + }, } - _, result, err := prompt.Run() + _, _, err := promptBuild.Run() if err != nil { fmt.Printf("Prompt failed %v\n", err) return } - fmt.Printf("You choose %q\n", result) + // Choose database driver + + promptDB := promptui.Select{ + Label: "Select database driver", + Items: []string{"MariaDB", "MySQL", "MS SQL Server", "PostgreSQL", "SQLite"}, + Templates: &promptui.SelectTemplates{ + Help: " ", + Selected: chalk.Green.Color("✔") + " Database Driver: {{ . }}", + }, + Size: 10, + } + + _, _, err = promptDB.Run() + + // Port + + promptPort := promptui.Prompt{ + Label: "Port", + Default: "3000", + Templates: &promptui.PromptTemplates{ + Success: chalk.Green.Color("✔") + " Port: {{ . }}", + }, + } + + _, err = promptPort.Run() // Download archives... + fmt.Println(chalk.Bold.TextStyle("\nDownloading packages...")) + uiprogress.Start() bar := uiprogress.AddBar(100) @@ -51,4 +97,6 @@ func main() { for bar.Incr() { time.Sleep(time.Millisecond * 20) } + + fmt.Println("\n" + chalk.Yellow.Color(finish)) } diff --git a/dev/installer/syscheck.go b/dev/installer/syscheck.go new file mode 100644 index 00000000..9b9f3b4b --- /dev/null +++ b/dev/installer/syscheck.go @@ -0,0 +1,54 @@ +package main + +import ( + "fmt" + "log" + "os" + "os/exec" + + "github.com/blang/semver" + "github.com/pbnjay/memory" + "github.com/ttacon/chalk" +) + +const nodejsSemverRange = ">=8.11.3 <10.0.0" +const ramMin = 768 + +// CheckNodeJs checks if Node.js is installed and has minimal supported version +func CheckNodeJs() bool { + cmd := exec.Command("node", "-v") + cmdOutput, err := cmd.CombinedOutput() + if err != nil { + log.Fatal(err) + } + + validRange := semver.MustParseRange(nodejsSemverRange) + nodeVersion, err := semver.ParseTolerant(string(cmdOutput[:])) + if !validRange(nodeVersion) { + fmt.Printf(chalk.Red.Color("Error: Installed Node.js version is not supported! %s"), nodejsSemverRange) + os.Exit(1) + } + + fmt.Printf(chalk.Green.Color("✔")+" Node.js %s: OK\n", nodeVersion.String()) + + return true +} + +// CheckRAM checks if system total RAM meets requirements +func CheckRAM() bool { + var totalRAM = memory.TotalMemory() / 1024 / 1024 + if totalRAM < ramMin { + fmt.Printf(chalk.Red.Color("Error: System does not meet RAM requirements. %s MB minimum."), ramMin) + os.Exit(1) + } + + fmt.Printf(chalk.Green.Color("✔")+" Total System RAM %d MB: OK\n", totalRAM) + + return true +} + +// CheckNetworkAccess checks if download server can be reached +func CheckNetworkAccess() bool { + // TODO + return true +}