break up into packages
							parent
							
								
									5efe73b3ca
								
							
						
					
					
						commit
						d48544ff0b
					
				@ -0,0 +1,21 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"os"
 | 
			
		||||
 | 
			
		||||
	"git.xbudex.com/buddy/open-hardware-monitor/lib/app"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	a := app.New()
 | 
			
		||||
	handleErr(a.RunClient(os.Args))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func handleErr(err error) {
 | 
			
		||||
	if err == nil {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	fmt.Fprintf(os.Stderr, "%s\n", err)
 | 
			
		||||
	os.Exit(1)
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,21 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"os"
 | 
			
		||||
 | 
			
		||||
	"git.xbudex.com/buddy/open-hardware-monitor/lib/app"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	a := app.New()
 | 
			
		||||
	handleErr(a.RunMetrics(os.Args))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func handleErr(err error) {
 | 
			
		||||
	if err == nil {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	fmt.Fprintf(os.Stderr, "%s\n", err)
 | 
			
		||||
	os.Exit(1)
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,5 @@
 | 
			
		||||
module git.xbudex.com/buddy/open-hardware-monitor
 | 
			
		||||
 | 
			
		||||
go 1.13
 | 
			
		||||
 | 
			
		||||
require github.com/urfave/cli v1.22.1
 | 
			
		||||
@ -0,0 +1,12 @@
 | 
			
		||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
 | 
			
		||||
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
 | 
			
		||||
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
 | 
			
		||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
 | 
			
		||||
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
 | 
			
		||||
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
 | 
			
		||||
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
 | 
			
		||||
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
 | 
			
		||||
github.com/urfave/cli v1.22.1 h1:+mkCCcOFKPnCmVYVcURKps1Xe+3zP90gSYGNfRkjoIY=
 | 
			
		||||
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
 | 
			
		||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 | 
			
		||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
 | 
			
		||||
@ -0,0 +1,98 @@
 | 
			
		||||
package app
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"io"
 | 
			
		||||
	"net/url"
 | 
			
		||||
	"os"
 | 
			
		||||
 | 
			
		||||
	"git.xbudex.com/buddy/open-hardware-monitor/lib/client"
 | 
			
		||||
	"github.com/urfave/cli"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// App context
 | 
			
		||||
type App struct {
 | 
			
		||||
	cli    *cli.App
 | 
			
		||||
	client *client.Client
 | 
			
		||||
	out    io.Writer
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var (
 | 
			
		||||
	// FlagHost flag for host
 | 
			
		||||
	FlagHost = cli.StringFlag{
 | 
			
		||||
		Name:   "host",
 | 
			
		||||
		EnvVar: "OHWM_HOST",
 | 
			
		||||
		Value:  "127.0.0.1:8085",
 | 
			
		||||
		Usage:  "open hardware monitor host [OSHW_HOST]",
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// FlagScheme flag for host
 | 
			
		||||
	FlagScheme = cli.StringFlag{
 | 
			
		||||
		Name:   "scheme",
 | 
			
		||||
		EnvVar: "OHWM_SCHEME",
 | 
			
		||||
		Value:  "http",
 | 
			
		||||
		Usage:  "open hardware monitor scheme (http/https)",
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// FlagPath flag for path url
 | 
			
		||||
	FlagPath = cli.StringFlag{
 | 
			
		||||
		Name:   "path",
 | 
			
		||||
		EnvVar: "OHWM_PATH",
 | 
			
		||||
		Value:  "/data.json",
 | 
			
		||||
		Usage:  "open hardware monitor path (ie /data.json)",
 | 
			
		||||
	}
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// New returns instance of an app
 | 
			
		||||
func New() *App {
 | 
			
		||||
	cliapp := cli.NewApp()
 | 
			
		||||
	app := &App{
 | 
			
		||||
		out: os.Stdout,
 | 
			
		||||
		cli: cliapp,
 | 
			
		||||
	}
 | 
			
		||||
	app.cli.Flags = []cli.Flag{
 | 
			
		||||
		FlagHost,
 | 
			
		||||
		FlagScheme,
 | 
			
		||||
		FlagPath,
 | 
			
		||||
	}
 | 
			
		||||
	app.cli.Before = app.before
 | 
			
		||||
	return app
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Before sets up app
 | 
			
		||||
func (a *App) before(ctx *cli.Context) error {
 | 
			
		||||
	a.client = &client.Client{
 | 
			
		||||
		URL: url.URL{
 | 
			
		||||
			Scheme: ctx.String("scheme"),
 | 
			
		||||
			Host:   ctx.String("host"),
 | 
			
		||||
			Path:   ctx.String("path"),
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (a *App) clientAction(_ *cli.Context) error {
 | 
			
		||||
	node, err := a.client.Fetch()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	fmt.Fprint(a.out, node.String())
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (a *App) actionMetrics(_ *cli.Context) error {
 | 
			
		||||
	fmt.Fprintln(a.out, "todo")
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// RunClient runs client action
 | 
			
		||||
func (a *App) RunClient(args []string) error {
 | 
			
		||||
	a.cli.Action = a.clientAction
 | 
			
		||||
	return a.cli.Run(args)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// RunMetrics runs metrics server
 | 
			
		||||
func (a *App) RunMetrics(args []string) error {
 | 
			
		||||
	a.cli.Action = a.actionMetrics
 | 
			
		||||
	return a.cli.Run(args)
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,73 @@
 | 
			
		||||
package client
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"io"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"net/url"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Client for open hardware monitor
 | 
			
		||||
type Client struct {
 | 
			
		||||
	Timeout time.Duration
 | 
			
		||||
	URL     url.URL
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Fetch requests
 | 
			
		||||
func (c *Client) Fetch() (*Node, error) {
 | 
			
		||||
	client := http.Client{Timeout: c.Timeout}
 | 
			
		||||
	resp, err := client.Get(c.URL.String())
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	defer resp.Body.Close()
 | 
			
		||||
	return c.Decode(resp.Body)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Decode json
 | 
			
		||||
func (c *Client) Decode(r io.Reader) (*Node, error) {
 | 
			
		||||
	node := &Node{}
 | 
			
		||||
	decoder := json.NewDecoder(r)
 | 
			
		||||
	if err := decoder.Decode(node); err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	return node, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Node from data
 | 
			
		||||
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
 | 
			
		||||
}
 | 
			
		||||
@ -1,103 +0,0 @@
 | 
			
		||||
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
 | 
			
		||||
}
 | 
			
		||||
					Loading…
					
					
				
		Reference in New Issue