Skip to content

Commit

Permalink
Update hw command
Browse files Browse the repository at this point in the history
  • Loading branch information
djthorpe committed Dec 17, 2020
1 parent 6c2d9d7 commit cab7c26
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 99 deletions.
49 changes: 6 additions & 43 deletions cmd/hw/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type header struct {
}

func (h header) Format() (string, table.Alignment, table.Color) {
return "[" + h.string + "]", table.Auto, table.White | table.Inverse
return h.string, table.Auto, table.Bold
}

////////////////////////////////////////////////////////////////////////////////
Expand All @@ -49,26 +49,14 @@ func (this *app) Define(cfg gopi.Config) error {
this.timeout = cfg.FlagDuration("timeout", time.Second, "Discovery timeout", "mdns")

// Define commands
cfg.Command("version", "Return information about the command", func(context.Context) error {
if err := this.PrintVersion(cfg); err != nil {
return err
} else {
return gopi.ErrHelp
}
})
cfg.Command("lirc", "IR sending and receiving control", func(ctx context.Context) error {
return this.RunLIRC(ctx, cfg)
})
cfg.Command("lirc print", "Print LIRC Parameters", func(ctx context.Context) error {
return nil
cfg.Command("info", "Hardware information", this.RunInfo)
cfg.Command("version", "Version information", func(context.Context) error {
return this.PrintVersion(cfg)
})
cfg.Command("mdns", "mDNS Service Discovery", this.RunDiscovery)

/*
// Define mDNS command
cfg.Command("mdns", "Service discovery", this.RunDiscovery)
// Define other commands
cfg.Command("hw", "Return hardware platform information", this.RunHardware)
// TODO Define other commands
cfg.Command("i2c", "Return I2C interface parameters", this.RunI2C)
cfg.Command("gpio", "Control GPIO interface", this.RunGPIO)
cfg.Command("fonts", "Return Font faces", this.RunFonts) // Not yet implemented
Expand Down Expand Up @@ -96,31 +84,6 @@ func (this *app) Run(ctx context.Context) error {
return this.Command.Run(ctx)
}

func (this *app) RunHardware(context.Context) error {
// Display platform information
table := table.New(table.WithHeader(false), table.WithMergeCells())

table.Append(header{"Product"}, this.Platform.Product(), fmt.Sprint(this.Platform.Type()))
table.Append("Serial Number", "", this.Platform.SerialNumber())
table.Append("Uptime", "", this.Platform.Uptime().Truncate(time.Second).String())
if l1, l5, l15 := this.Platform.LoadAverages(); l1 != 0 && l5 != 0 && l15 != 0 {
table.Append("Load Averages", "1m", fmt.Sprintf("%.2f", l1))
table.Append("Load Averages", "5m", fmt.Sprintf("%.2f", l5))
table.Append("Load Averages", "15m", fmt.Sprintf("%.2f", l15))
}
if zones := this.Platform.TemperatureZones(); len(zones) > 0 {
for k, v := range zones {
table.Append("Temperature Zones", k, fmt.Sprintf("%.2fC", v))
}
}
table.Append("Number of Displays", "", fmt.Sprint(this.Platform.NumberOfDisplays()))
table.Append("Attached Displays", "", fmt.Sprint(this.Platform.AttachedDisplays()))
table.Render(os.Stdout)

// Return success
return nil
}

func (this *app) RunFonts(context.Context) error {
if this.fontdir == nil || *this.fontdir == "" {
return gopi.ErrBadParameter.WithPrefix("Missing -fontdir flag")
Expand Down
49 changes: 49 additions & 0 deletions cmd/hw/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

import (
"context"
"fmt"
"os"
"time"

"github.com/djthorpe/gopi/v3"
"github.com/djthorpe/gopi/v3/pkg/table"
)

////////////////////////////////////////////////////////////////////////////////

func (this *app) RunInfo(ctx context.Context) error {
args := this.Command.Args()
ctx, cancel := context.WithTimeout(ctx, *this.timeout)
defer cancel()

if this.Platform == nil {
return gopi.ErrInternalAppError.WithPrefix("Platform")
}

if len(args) != 0 {
return gopi.ErrHelp
}
// Display platform information
table := table.New(table.WithHeader(false), table.WithMergeCells())

table.Append(header{"Product"}, this.Platform.Product(), fmt.Sprint(this.Platform.Type()))
table.Append("Serial Number", "", this.Platform.SerialNumber())
table.Append("Uptime", "", this.Platform.Uptime().Truncate(time.Second).String())
if l1, l5, l15 := this.Platform.LoadAverages(); l1 != 0 && l5 != 0 && l15 != 0 {
table.Append("Load Averages", "1m", fmt.Sprintf("%.2f", l1))
table.Append("Load Averages", "5m", fmt.Sprintf("%.2f", l5))
table.Append("Load Averages", "15m", fmt.Sprintf("%.2f", l15))
}
if zones := this.Platform.TemperatureZones(); len(zones) > 0 {
for k, v := range zones {
table.Append("Temperature Zones", k, fmt.Sprintf("%.2fC", v))
}
}
table.Append("Number of Displays", "", fmt.Sprint(this.Platform.NumberOfDisplays()))
table.Append("Attached Displays", "", fmt.Sprint(this.Platform.AttachedDisplays()))
table.Render(os.Stdout)

// Return success
return nil
}
89 changes: 66 additions & 23 deletions cmd/hw/mdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,94 @@ package main

import (
"context"
"fmt"
"os"

"github.com/djthorpe/gopi/v3"
"github.com/olekukonko/tablewriter"
"github.com/djthorpe/gopi/v3/pkg/table"
)

////////////////////////////////////////////////////////////////////////////////

type hosts struct {
gopi.ServiceRecord
}

type addrs struct {
gopi.ServiceRecord
}

type txt struct {
gopi.ServiceRecord
}

func (h hosts) Format() (string, table.Alignment, table.Color) {
str := ""
for i, h := range h.HostPort() {
if i > 0 {
str += " "
}
str += h
}
return str, table.Auto, table.None
}

func (a addrs) Format() (string, table.Alignment, table.Color) {
str := ""
for i, a := range a.Addrs() {
if i > 0 {
str += " "
}
str += a.String()
}
return str, table.Auto, table.None
}

func (t txt) Format() (string, table.Alignment, table.Color) {
str := ""
for i, t := range t.Txt() {
if i > 0 {
str += " "
}
str += t
}
return str, table.Auto, table.None
}

////////////////////////////////////////////////////////////////////////////////

func (this *app) RunDiscovery(ctx context.Context) error {
args := this.Command.Args()
ctx, cancel := context.WithTimeout(ctx, *this.timeout)
defer cancel()

args := this.Command.Args()
if this.ServiceDiscovery == nil {
return gopi.ErrInternalAppError.WithPrefix("ServiceDiscovery")
}

if len(args) == 0 {
return this.RunDiscoveryServices(ctx)
return this.RunDiscoveryEnumerate(ctx)
} else if len(args) == 1 {
return this.RunDiscoveryLookup(ctx, args[0])
} else {
return gopi.ErrHelp
}
}

func (this *app) RunDiscoveryServices(ctx context.Context) error {
func (this *app) RunDiscoveryEnumerate(ctx context.Context) error {

// Enumerate services
services, err := this.ServiceDiscovery.EnumerateServices(ctx)
if err != nil {
return err
}

// Display platform information
table := tablewriter.NewWriter(os.Stdout)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetAutoMergeCells(true)
table.SetHeader([]string{"Service"})
table := table.New()
table.SetHeader(header{"Service"})
for _, service := range services {
table.Append([]string{
service,
})
table.Append(service)
}
table.Render()
table.Render(os.Stdout)

// Return success
return nil
Expand All @@ -54,18 +103,12 @@ func (this *app) RunDiscoveryLookup(ctx context.Context, name string) error {
}

// Display platform information
table := tablewriter.NewWriter(os.Stdout)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetAutoMergeCells(true)
table.SetHeader([]string{"Service", "Name", "Record"})
table := table.New()
table.SetHeader(header{"Service"}, header{"Name"}, header{"Host"}, header{"Addr"}, header{"Txt"})
for _, record := range records {
table.Append([]string{
record.Service(),
record.Name(),
fmt.Sprint(record),
})
table.Append(record.Service(), record.Name(), hosts{record}, addrs{record}, txt{record})
}
table.Render()
table.Render(os.Stdout)

// Return success
return nil
Expand Down
32 changes: 10 additions & 22 deletions cmd/hw/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,29 @@ import (
"time"

"github.com/djthorpe/gopi/v3"
"github.com/olekukonko/tablewriter"
"github.com/djthorpe/gopi/v3/pkg/table"
)

func (this *app) PrintVersion(cfg gopi.Config) error {
version := cfg.Version()
table := tablewriter.NewWriter(os.Stdout)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.Append([]string{
"Name", version.Name(),
})
table := table.New(table.WithHeader(false))
tag, branch, hash := version.Version()

table.Append(header{"Name"}, version.Name())
if tag != "" {
table.Append([]string{
"Tag", tag,
})
table.Append(header{"Tag"}, tag)
}
if branch != "" {
table.Append([]string{
"Branch", branch,
})
table.Append(header{"Branch"}, branch)
}
if hash != "" {
table.Append([]string{
"Hash", hash,
})
table.Append(header{"Hash"}, hash)
}
table.Append([]string{
"Go version", version.GoVersion(),
})
table.Append(header{"GoVersion"}, version.GoVersion())
if t := version.BuildTime(); t.IsZero() == false {
table.Append([]string{
"Build time", t.Format(time.RFC3339),
})
table.Append(header{"BuildTime"}, t.Format(time.RFC3339))
}
table.Render()
table.Render(os.Stdout)

// Return success
return nil
Expand Down
6 changes: 6 additions & 0 deletions pkg/table/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ func WithHeader(value bool) Option {
}
}

func WithFooter(value bool) Option {
return func(t *Table) {
t.footer = value
}
}

func WithOffsetLimit(offset, limit uint) Option {
return func(t *Table) {
t.offset, t.limit = offset, limit
Expand Down
26 changes: 15 additions & 11 deletions pkg/table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import (

// Table represents a data table with a header & footer
type Table struct {
cols []cell
types []Types
fields map[string]int
rows [][]interface{}
header bool
offset, limit uint
merge bool
cols []cell
types []Types
fields map[string]int
rows [][]interface{}
header, footer bool
offset, limit uint
merge bool
}

// Formatter interface converts internal format to
Expand Down Expand Up @@ -194,11 +194,15 @@ func (t *Table) Render(w io.Writer, opts ...Option) {
}

// Set footer
foot := []string{}
for _, col := range t.types {
foot = append(foot, fmt.Sprint(col.Kind()))
if t.footer {
foot := []string{}
for _, col := range t.types {
foot = append(foot, fmt.Sprint(col.Kind()))
}
table.SetFooter(foot)
}
table.SetFooter(foot)

// Render table
table.Render()
}

Expand Down

0 comments on commit cab7c26

Please sign in to comment.