You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
104 lines
1.7 KiB
Go
104 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
func main() {
|
|
if err := run(); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
const (
|
|
hostDefault = "127.0.0.1:8085"
|
|
hostDesc = "open hardware monitor host [OSHW_HOST]"
|
|
)
|
|
|
|
func run() error {
|
|
conifg := getConfig()
|
|
node, err := fetchNode(conifg.host)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Printf("%s", node)
|
|
return nil
|
|
}
|
|
|
|
type config struct {
|
|
host string
|
|
}
|
|
|
|
func getConfig() *config {
|
|
c := config{}
|
|
host := os.Getenv("OHWM_HOST")
|
|
hostFlag := flag.String("host", hostDefault, hostDesc)
|
|
flag.Parse()
|
|
if *hostFlag != hostDefault {
|
|
host = *hostFlag
|
|
}
|
|
if host == "" {
|
|
host = hostDefault
|
|
}
|
|
c.host = host
|
|
return &c
|
|
}
|
|
|
|
func fetchNode(host string) (*node, error) {
|
|
client := http.Client{Timeout: time.Second}
|
|
u := url.URL{Scheme: "http", Host: host, Path: "/data.json"}
|
|
resp, err := client.Get(u.String())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
node := &node{}
|
|
decoder := json.NewDecoder(resp.Body)
|
|
if err := decoder.Decode(node); err != nil {
|
|
return nil, err
|
|
}
|
|
return node, nil
|
|
}
|
|
|
|
type node struct {
|
|
ID int `json:"id"`
|
|
ImageURL string
|
|
Max string
|
|
Min string
|
|
Text string
|
|
Value string
|
|
Children []node
|
|
}
|
|
|
|
func (n *node) String() string {
|
|
return n.stringify(0)
|
|
}
|
|
|
|
func (n *node) stringify(indent int) string {
|
|
prefix := ""
|
|
for i := 0; i < indent; i++ {
|
|
prefix += " "
|
|
}
|
|
|
|
ret := prefix + n.Text
|
|
if n.Value != "" {
|
|
ret += ": " + n.Value
|
|
}
|
|
if n.Max != "" && n.Min != "" && n.Max != "-" && n.Min != "-" {
|
|
ret += fmt.Sprintf(" (%s - %s)", n.Min, n.Max)
|
|
}
|
|
ret += "\n"
|
|
for _, child := range n.Children {
|
|
ret += child.stringify(indent + 1)
|
|
}
|
|
|
|
return ret
|
|
}
|