Skip to content

Commit

Permalink
add table presenter
Browse files Browse the repository at this point in the history
  • Loading branch information
tupyy committed May 12, 2023
1 parent 9b55262 commit a9f6dc9
Show file tree
Hide file tree
Showing 877 changed files with 318,257 additions and 20 deletions.
17 changes: 17 additions & 0 deletions cmd/common/flags.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package common

import (
"github.com/spf13/cobra"
"github.com/tupyy/airzone/internal/presenter"
)

const (
AllZones = 0
)
Expand All @@ -8,3 +13,15 @@ var (
SystemID int = 1
Host string
)

func OutputPresenter(cmd *cobra.Command) func(v interface{}) (string, error) {
output := cmd.Flag("output")
if output == nil {
return presenter.Json
}

if output.Value.String() == "table" {
return presenter.Table
}
return presenter.Json
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ func init() {
RootCmd.AddCommand(zone.RootCmd)
RootCmd.PersistentFlags().StringVarP(&common.Host, "host", "", "airzone:3000", "airzone host url. Example: 192.168.1.1:3000")
RootCmd.PersistentFlags().IntVarP(&common.SystemID, "system-id", "", 1, "system id")
RootCmd.PersistentFlags().StringP("output", "o", "json", "output style.Acceptes json or table")
}
7 changes: 4 additions & 3 deletions cmd/zone/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package zone

import (
"context"
"encoding/json"
"errors"
"fmt"
"strconv"
Expand Down Expand Up @@ -32,11 +31,13 @@ var RootCmd = &cobra.Command{
if err != nil {
return err
}
j, err := json.Marshal(hvac)

value, err := common.OutputPresenter(cmd)(hvac)
if err != nil {
return err
}
fmt.Printf("%s", string(j))
fmt.Printf("%s", value)

return nil
},
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/zone/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package zone

import (
"context"
"encoding/json"
"errors"
"fmt"
"strconv"
Expand Down Expand Up @@ -46,11 +45,11 @@ var setCmd = &cobra.Command{
return err
}

j, err := json.Marshal(resp)
outputValue, err := common.OutputPresenter(cmd)(resp)
if err != nil {
return err
}
fmt.Printf("%s", string(j))
fmt.Printf("%s", outputValue)

return nil
},
Expand Down
8 changes: 7 additions & 1 deletion cmd/zone/turn.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ var turnCmd = &cobra.Command{
if err != nil {
return err
}
fmt.Printf("%+v", resp)

outputValue, err := common.OutputPresenter(cmd)(resp)
if err != nil {
return err
}
fmt.Printf("%s", outputValue)

return nil
},
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/zones/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package zones

import (
"context"
"encoding/json"
"fmt"

"github.com/spf13/cobra"
Expand All @@ -20,11 +19,12 @@ var RootCmd = &cobra.Command{
if err != nil {
return err
}
j, err := json.Marshal(hvac)

value, err := common.OutputPresenter(c)(hvac)
if err != nil {
return err
}
fmt.Printf("%s", string(j))
fmt.Printf("%s", value)
return nil
},
}
20 changes: 10 additions & 10 deletions cmd/zones/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package zones

import (
"context"
"encoding/json"
"errors"
"fmt"
"strconv"
Expand All @@ -23,15 +22,15 @@ var setCmd = &cobra.Command{
}

parameter := args[0]
value := args[1]
outputValue := args[1]

if parameter != "temperature" && parameter != "mode" {
return fmt.Errorf("parameter to set %q unknown", parameter)
}

if parameter == "mode" {
var m hvac.Mode
switch value {
switch outputValue {
case "cooling":
m = hvac.CoollingMode
case "heating":
Expand All @@ -41,37 +40,38 @@ var setCmd = &cobra.Command{
case "dehumidification":
m = hvac.Dehumidification
default:
return fmt.Errorf("unknown mode: %q", value)
return fmt.Errorf("unknown mode: %q", outputValue)
}

resp, err := hvac.SetMode(context.TODO(), common.Host, common.SystemID, common.AllZones, m)
if err != nil {
return err
}

j, err := json.Marshal(resp)
output, err := common.OutputPresenter(cmd)(resp)
if err != nil {
return err
}
fmt.Printf("%s", string(j))
fmt.Printf("%s", output)

return nil
}

temperature, err := strconv.ParseFloat(value, 64)
temperature, err := strconv.ParseFloat(outputValue, 64)
if err != nil {
return fmt.Errorf("invalid temperature value: %q", value)
return fmt.Errorf("invalid temperature value: %q", outputValue)
}

resp, err := hvac.SetTemperature(context.TODO(), common.Host, common.SystemID, common.AllZones, temperature)
if err != nil {
return err
}

j, err := json.Marshal(resp)
output, err := common.OutputPresenter(cmd)(resp)
if err != nil {
return err
}
fmt.Printf("%s", string(j))
fmt.Printf("%s", output)

return nil
},
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/tupyy/airzone
go 1.18

require (
github.com/jedib0t/go-pretty/v6 v6.4.6
github.com/prometheus/client_golang v1.15.1
github.com/spf13/cobra v1.7.0
go.uber.org/zap v1.24.0
Expand All @@ -13,10 +14,12 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
Expand Down
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jedib0t/go-pretty/v6 v6.4.6 h1:v6aG9h6Uby3IusSSEjHaZNXpHFhzqMmjXcPq1Rjl9Jw=
github.com/jedib0t/go-pretty/v6 v6.4.6/go.mod h1:Ndk3ase2CkQbXLLNf5QDHoYb6J9WtVfmHZu9n8rk2xs=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/profile v1.6.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v1.15.1 h1:8tXpTmJbyH5lydzFPoxSIJ0J46jdh3tylbvM1xCv0LI=
Expand All @@ -29,13 +34,18 @@ github.com/prometheus/common v0.42.0 h1:EKsfXEYo4JpWMHH5cg+KOUWeuJSov1Id8zGR8eeI
github.com/prometheus/common v0.42.0/go.mod h1:xBwqVerjNdUDjgODMpudtOMwlOwf2SaTr1yjz4b7Zbc=
github.com/prometheus/procfs v0.9.0 h1:wzCHvIvM5SxWqYvwgVL7yJY8Lz3PKn49KQtpgMYJfhI=
github.com/prometheus/procfs v0.9.0/go.mod h1:+pB4zwohETzFnmlpe6yd2lSc+0/46IYZRB/chUwxUZY=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.4/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
Expand All @@ -45,6 +55,7 @@ go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9i
go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand All @@ -53,5 +64,6 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
17 changes: 17 additions & 0 deletions internal/hvac/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@ package hvac

type Mode int

func (m Mode) String() string {
switch m {
case StopMode:
return "stop"
case CoollingMode:
return "cooling"
case HeatingMode:
return "heating"
case VentilationMode:
return "ventilation"
case Dehumidification:
return "dehumidification"
default:
return "unknown mode"
}
}

const (
StopMode Mode = 1 + iota
CoollingMode
Expand Down
56 changes: 56 additions & 0 deletions internal/presenter/presenter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package presenter

import (
"encoding/json"
"errors"
"fmt"

"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
"github.com/tupyy/airzone/internal/hvac"
)

func Json(v interface{}) (string, error) {
hvac, ok := v.(hvac.Hvac)
if !ok {
return "", errors.New("invalid model.want hvac.Hvac")
}

j, err := json.MarshalIndent(hvac.Zones, "", "\t")
if err != nil {
return "", err
}
return string(j), nil
}

func Table(v interface{}) (string, error) {
hvac, ok := v.(hvac.Hvac)
if !ok {
return "", errors.New("invalid model.want hvac.Hvac")
}

tw := table.NewWriter()
tw.AppendHeader(table.Row{"ZoneID", "Name", "On", "Mode", "CoolSetPoint °C", "HeatSetPoint °C", "Temperature °C", "Humidity %"})

for _, z := range hvac.Zones {
tw.AppendRow(table.Row{z.ID, z.Name, z.On, z.GetMode().String(), z.CoolSetPoint, z.HeatSetPoint, z.RoomTemp, z.Humidity})
}

tw.SetColumnConfigs([]table.ColumnConfig{
{Number: 3, Align: text.AlignRight, AlignHeader: text.AlignCenter, Transformer: func(val interface{}) string {
v, _ := val.(int)
switch v {
case 0:
return "off"
case 1:
return "on"
default:
return ""
}
}},
{Number: 5, Align: text.AlignRight, AlignHeader: text.AlignCenter, Transformer: func(val interface{}) string { return fmt.Sprintf("%.2f", val) }},
{Number: 6, Align: text.AlignRight, AlignHeader: text.AlignCenter, Transformer: func(val interface{}) string { return fmt.Sprintf("%.2f", val) }},
{Number: 7, Align: text.AlignRight, AlignHeader: text.AlignCenter, Transformer: func(val interface{}) string { return fmt.Sprintf("%.2f", val) }},
})
return tw.Render(), nil
}
20 changes: 20 additions & 0 deletions vendor/github.com/beorn7/perks/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a9f6dc9

Please sign in to comment.