WIP: need to develop website further

This commit is contained in:
Nils Stinnesbeck 2020-05-11 01:19:55 +02:00
parent 4294636641
commit 2e237a0518
Signed by: nils
GPG Key ID: 86D4882C6C6CA48B
4 changed files with 177 additions and 26 deletions

4
.gitignore vendored
View File

@ -3,3 +3,7 @@ images
# go checksums
go.sum
# executable
ffrs-fw-dl
ffrs-fw-dl.exe

View File

@ -17,29 +17,65 @@ manufacturers:
prettyname: Aerohive
- name: alfa-network
prettyname: Alfa
- name: allnet
prettyname: ALLNET
- name: aruba
prettyname: Aruba
- name: asus
prettyname: ASUS
- name: avm
prettyname: AVM
- name: buffalo
prettyname: Buffalo
- name: d-link
prettyname: D-Link
- name: devolo
prettyname: Devolo
- name: enterasys
prettyname: Enterasys Networks
- name: gl-inet
prettyname: GL-Inet
prettyname: GL Technologies
- name: gl.inet
prettyname: GL Technologies
- name: gl
prettyname: GL Technologies
- name: lemaker
prettyname: LeMaker
- name: linksys
prettyname: Linksys
- name: meraki
prettyname: Meraki
- name: netgear
prettyname: Netgear
- name: nexx
prettyname: Nexx
- name: ocedo
prettyname: Ocedo
- name: onion
prettyname: Onion Corporation
- name: openmesh
prettyname: Openmesh
- name: raspberrypi
prettyname: Raspberry Pi
- name: raspberry
prettyname: Raspberry Pi
- name: tp-link
prettyname: TP-Link
- name: ubiquiti
prettyname: Ubiquiti
- name: ubnt
prettyname: Ubiquiti
- name: vocore
prettyname: VoCore
- name: vocore2
prettyname: VoCore
- name: wd
prettyname: Western Digital
- name: x86
prettyname: x86
- name: xiaomi
prettyname: Xiaomi
- name: zbt
prettyname: ZBT
- name: zyxel
prettyname: Zyxel

127
main.go
View File

@ -1,21 +1,55 @@
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"text/template"
"git.nils.zone/nils/prettify"
"github.com/sergi/go-diff/diffmatchpatch"
"gopkg.in/yaml.v2"
)
// constants
const (
//root = "/srv/fwuploads/"
root = "./images/"
)
// structs
type branch struct {
Name string
}
type method struct {
Name string
branch []branch
}
type version struct {
Name string
method []method
}
type model struct {
Name string
Version []version
}
type vendor struct {
Name string
Models []model
}
type firmware struct {
Community string
domain string
Hersteller string
Modell string
Version string
@ -43,15 +77,26 @@ type assignments struct {
Manufacturers []manufacturer `json:"manufacturers"`
}
type data struct {
Name string
Title string
Body string
Skills skills
}
type skills []struct {
Name string
}
// global variables
var branches = []string{"stable", "beta", "experimental"}
var installMethod = []string{"sysupgrade", "factory"}
var community string
var domains []string
var prefix = ""
var assign assignments
var currentBranch string // holds info of the current branch
var currentMethod string // holds info of the current install method
var firmwares []firmware
func stringInSlice(a string, list []string) bool {
for _, b := range list {
@ -69,7 +114,8 @@ func getFirmwareImages(folder string) {
// check if files exist
//fmt.Println(files)
if files != nil {
getImagePrefix(folder)
prefix := getImagePrefix(folder)
//fmt.Println(folder)
// loop through all files
for _, file := range files {
@ -77,15 +123,7 @@ func getFirmwareImages(folder string) {
if !file.IsDir() {
// check if file starts with gluon
if strings.HasPrefix(file.Name(), "gluon") {
//fmt.Println(file.Name())
/*
path, err := filepath.Abs(file.Name())
if err != nil {
panic(err)
}
*/
//getFirmwareData(path, prefix)
// get Firmware Data for file
getFirmwareData(file.Name(), prefix)
}
}
@ -96,8 +134,9 @@ func getFirmwareImages(folder string) {
func getFirmwareData(image string, prefix string) {
// create firmware variable
var fw firmware
//fmt.Println(prefix)
//fmt.Println(">" + prefix + "<")
//fmt.Println(image)
//fmt.Println(currentBranch, currentMethod)
// set the branch of firmware
fw.Entwicklungsstadium = currentBranch
@ -109,8 +148,19 @@ func getFirmwareData(image string, prefix string) {
fw.Modell = strings.Split(image, prefix)[1]
// cut away the extension
//fw.Modell = strings.TrimSuffix(fw.Modell, filepath.Ext(fw.Modell)) // this has issues with img.gz use below until we have an image with a "." in it
fw.Modell = strings.Split(fw.Modell, ".")[0]
fw.Modell = strings.TrimSuffix(fw.Modell, filepath.Ext(fw.Modell)) // this has issues with img.gz use below until we have an image with a "." in it
// quickfix for .img.gz
if filepath.Ext(fw.Modell) == ".img" {
// remove .img as well
fw.Modell = strings.TrimSuffix(fw.Modell, filepath.Ext(fw.Modell))
}
// remove sysupgrade suffix
if strings.HasSuffix(fw.Modell, "-sysupgrade") {
fw.Modell = strings.TrimSuffix(fw.Modell, "-sysupgrade")
}
//fmt.Println(fw.Modell)
//fw.Modell = strings.Split(fw.Modell, ".")[0]
for _, manufacturer := range assign.Manufacturers {
// check if image is from manufacturer
@ -134,9 +184,12 @@ func getFirmwareData(image string, prefix string) {
out = true
}
// assemble new image name (cut away manufacturer)
fw.Modell = strings.Split(fw.Modell, fw.Hersteller+"-")[1]
//fmt.Println(fw.Modell)
if strings.Contains(fw.Modell, "-") {
// assemble new image name (cut away manufacturer)
fw.Modell = strings.Split(fw.Modell, fw.Hersteller+"-")[1]
} else {
fw.Modell = strings.Split(fw.Modell, fw.Hersteller)[1]
}
// compile regex for hw revisions
r, err := regexp.Compile("(v[0-9]{1,2}$)|([-]*rev-\\w*$)|(-[a-zA-Z][0-9]{0,1}$)")
@ -185,6 +238,8 @@ func getFirmwareData(image string, prefix string) {
log.Println(image)
prettify.Print(fw)
}
firmwares = append(firmwares, fw)
}
func getSubdirectory(folder string) {
@ -196,7 +251,7 @@ func getSubdirectory(folder string) {
// check if entry is a directory
if f.IsDir() {
// check if we are at root
if folder == "./images" {
if folder == root {
community = f.Name()
//fmt.Println(community)
}
@ -246,11 +301,18 @@ func getFilesOrFolders(folder string) []os.FileInfo {
func getImagePrefix(folder string) string {
files := getFilesOrFolders(folder)
if prefix == "" {
prefix = files[0].Name()
}
//fmt.Printf("there are %v files in folder %v\n", len(files), folder)
var prefix string
for _, file := range files {
prefix = diffStrings(file.Name(), prefix)
// check if file starts with gluon
if strings.HasPrefix(file.Name(), "gluon") {
// set prefix as file name first
if prefix == "" {
prefix = file.Name()
}
// diff the prefix until it is correct
prefix = diffStrings(file.Name(), prefix)
}
}
return prefix
}
@ -280,11 +342,26 @@ func getAssignments() assignments {
return a
}
func handler(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles("tmpl/index.html")
//t.Execute(w, assign)
t.Execute(w, firmwares)
}
func startServer() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
func main() {
// get all assignments from file
assign = getAssignments()
// start script in images folder
getSubdirectory("./images")
fmt.Println(branches)
getSubdirectory(root)
//fmt.Println(branches)
//prettify.Print(assign)
startServer()
}

34
tmpl/index.html Normal file
View File

@ -0,0 +1,34 @@
<html>
<body>
<h1>Test</h1>
<h2>Firmwares</h2>
{{ range . }}
{{ .Hersteller }}
{{ end }}
<h1>Test</h1>
<h2>Cities range</h2>
{{ range .Cities }}
<h3>{{ .Name }}</h3>
{{ range .Images }}
&emsp;<b>Name:</b> {{ .Name }}<br>
&emsp;<b>Prettyname:</b> {{ .Prettyname }}<br>
{{ end }}
</p>
{{ end }}
<form>
<fieldset>
<legend>Select Manufacturer</legend>
<p>
<label>Select list</label>
<select id = "manufacturers">
{{ range .Manufacturers }}
<option value = "{{ .Name }}">{{ .Name }}</option>
{{ end }}
</select>
</p>
</fieldset>
</form>
</body>
</html>