added code to write langues and their versions to a json file
This commit is contained in:
parent
bccd8c265f
commit
eb576d9b66
79
api/main.go
79
api/main.go
|
@ -5,8 +5,11 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
@ -27,15 +30,26 @@ type outbound struct {
|
|||
Output string `json:"output"`
|
||||
}
|
||||
|
||||
type Language struct {
|
||||
Name string `json:"name,omitempty"`
|
||||
Version string `json:"version,omitempty"`
|
||||
}
|
||||
|
||||
type Languages struct {
|
||||
Languages []Language `json:"languages"`
|
||||
}
|
||||
|
||||
var instance int
|
||||
var versionRegex = regexp.MustCompile("([0-9]+\\.[0-9]+\\.[0-9]+)")
|
||||
|
||||
func main() {
|
||||
port := 2000
|
||||
port := "2000"
|
||||
|
||||
updateVersions()
|
||||
|
||||
fmt.Println("starting api on port", port)
|
||||
|
||||
http.HandleFunc("/execute", Execute)
|
||||
http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", port), nil)
|
||||
http.ListenAndServe(":"+port, nil)
|
||||
}
|
||||
|
||||
func Execute(res http.ResponseWriter, req *http.Request) {
|
||||
|
@ -119,3 +133,62 @@ func launch(request inbound, res http.ResponseWriter) {
|
|||
|
||||
res.Write(response)
|
||||
}
|
||||
|
||||
func updateVersions() {
|
||||
f, err := os.Create("versions.json")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
langs, err := getVersions()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
res, err := json.Marshal(langs)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
f.Write(res)
|
||||
}
|
||||
|
||||
// get all the language and their current version
|
||||
func getVersions() ([]Language, error) {
|
||||
var languages []Language
|
||||
res, err := execVersionScript()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
languageInfo := strings.Split(res, "---")
|
||||
for _, v := range languageInfo {
|
||||
if len(v) < 2 {
|
||||
continue
|
||||
}
|
||||
name, version := getVersion(v)
|
||||
languages = append(languages, Language{
|
||||
Name: name,
|
||||
Version: version,
|
||||
})
|
||||
}
|
||||
return languages, nil
|
||||
}
|
||||
|
||||
// run the script that retrieves all the language versions
|
||||
func execVersionScript() (string, error) {
|
||||
fmt.Println("running script")
|
||||
output := bytes.Buffer{}
|
||||
cmd := exec.Command("../lcx/versions")
|
||||
cmd.Stdout = &output
|
||||
err := cmd.Run()
|
||||
return strings.ToLower(output.String()), err
|
||||
}
|
||||
|
||||
// return the language and its version
|
||||
// most of the time it is easy to get the name and version
|
||||
// but for some languages helper functions are used
|
||||
func getVersion(s string) (string, string) {
|
||||
lines := strings.Split(s, "\n")
|
||||
return lines[1], versionRegex.FindString(lines[2])
|
||||
}
|
||||
|
|
|
@ -0,0 +1,82 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Language struct {
|
||||
Name string `json:"name,omitempty"`
|
||||
Version string `json:"version,omitempty"`
|
||||
}
|
||||
|
||||
type Languages struct {
|
||||
Languages []Language `json:"languages"`
|
||||
}
|
||||
|
||||
var versionRegex = regexp.MustCompile("([0-9]+\\.[0-9]+\\.[0-9]+)")
|
||||
|
||||
func updateVersions(){
|
||||
f, err := os.Create("versions.json")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
langs, err := getVersions()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
res, err := json.Marshal(langs)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
f.Write(res)
|
||||
}
|
||||
|
||||
// get all the language and their current version
|
||||
func getVersions() ([]Language, error) {
|
||||
var languages []Language
|
||||
res, err := execVersionScript()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
languageInfo := strings.Split(res, "---")
|
||||
for _, v := range languageInfo {
|
||||
if len(v) < 2 {
|
||||
continue
|
||||
}
|
||||
name, version := getVersion(v)
|
||||
languages = append(languages, Language{
|
||||
Name: name,
|
||||
Version: version,
|
||||
})
|
||||
}
|
||||
return languages, nil
|
||||
}
|
||||
|
||||
// run the script that retrieves all the language versions
|
||||
func execVersionScript() (string, error) {
|
||||
fmt.Println("running script")
|
||||
output := bytes.Buffer{}
|
||||
cmd := exec.Command("../lcx/versions")
|
||||
cmd.Stdout = &output
|
||||
err := cmd.Run()
|
||||
return strings.ToLower(output.String()), err
|
||||
}
|
||||
|
||||
// return the language and its version
|
||||
// most of the time it is easy to get the name and version
|
||||
// but for some languages helper functions are used
|
||||
func getVersion(s string) (string, string) {
|
||||
lines := strings.Split(s, "\n")
|
||||
return lines[1], versionRegex.FindString(lines[2])
|
||||
}
|
Loading…
Reference in New Issue