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.

99 lines
1.8 KiB
Go

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)
}