Skip to content

Instantly share code, notes, and snippets.

@podanypepa
Created Feb 28, 2021
Embed
What would you like to do?
package main
import (
"bufio"
"bytes"
"fmt"
"log"
"net/http"
"os/exec"
"strings"
"github.com/PuerkitoBio/goquery"
)
var (
colorReset = "\033[0m"
colorRed = "\033[31m"
colorGreen = "\033[32m"
colorYellow = "\033[33m"
colorBlue = "\033[34m"
colorPurple = "\033[35m"
colorCyan = "\033[36m"
colorWhite = "\033[37m"
)
const verURL = "https://github.com/neovim/neovim/releases"
func main() {
versionOnMyMac, err := getInstalledNvimVersion()
if err != nil {
log.Fatal(err)
}
latestNightBuild, err := getLatestNightBuild()
if err != nil {
log.Fatal(err)
}
fmt.Println("on my mac: ", versionOnMyMac)
fmt.Println("latest night build: ", latestNightBuild)
if versionOnMyMac == latestNightBuild {
fmt.Println(colorGreen + "OK " + colorReset + " you are up to date")
} else {
fmt.Println(colorYellow +
"Yes! " +
colorBlue +
"on net is newer version" +
colorReset,
)
fmt.Println("please visit: ", verURL)
}
}
func getLatestNightBuild() (version string, err error) {
res, err := http.Get(verURL)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
log.Fatal(err)
}
versions := []string{}
doc.
Find(".release-header .f1").
Each(func(i int, s *goquery.Selection) {
title := s.Find("a").Text()
versions = append(versions, title)
})
return string(versions[0]), nil
}
func getInstalledNvimVersion() (version string, err error) {
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd := exec.Command("nvim", "--version")
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err = cmd.Run()
if err != nil {
return "", err
}
lines, err := StringToLines(stdout.String())
if err != nil {
return "", err
}
return lines[0], nil
}
func StringToLines(s string) (lines []string, err error) {
scanner := bufio.NewScanner(strings.NewReader(s))
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
err = scanner.Err()
return lines, err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment