From a243021c70dec71fdd531a2eff7e2ae9e6ee6db5 Mon Sep 17 00:00:00 2001 From: Markus Pesch Date: Mon, 4 Mar 2019 11:31:01 +0100 Subject: [PATCH] fix: moved print functions into cli pkg --- cmd/remote/list.go | 3 ++- cmd/sensor/list.go | 3 ++- pkg/cli/cli.go | 35 +++++++++++++++++++++++++++++++++++ pkg/config/fluckyconfig.go | 34 ---------------------------------- 4 files changed, 39 insertions(+), 36 deletions(-) diff --git a/cmd/remote/list.go b/cmd/remote/list.go index 6cb477a..9b4ff3c 100644 --- a/cmd/remote/list.go +++ b/cmd/remote/list.go @@ -4,6 +4,7 @@ import ( "log" "os" + "git.cryptic.systems/fh-trier/go-flucky/pkg/cli" "git.cryptic.systems/fh-trier/go-flucky/pkg/config" "github.com/spf13/cobra" ) @@ -24,7 +25,7 @@ var listRemoteCmd = &cobra.Command{ } // print all configured remote addresses on stdout - err = fc.PrintRemotes(os.Stdout) + err = cli.PrintRemotes(fc, os.Stdout) if err != nil { log.Fatalln(err) } diff --git a/cmd/sensor/list.go b/cmd/sensor/list.go index 1d59d35..a362e96 100644 --- a/cmd/sensor/list.go +++ b/cmd/sensor/list.go @@ -4,6 +4,7 @@ import ( "log" "os" + "git.cryptic.systems/fh-trier/go-flucky/pkg/cli" "git.cryptic.systems/fh-trier/go-flucky/pkg/config" "github.com/spf13/cobra" ) @@ -20,7 +21,7 @@ var listSensorCmd = &cobra.Command{ } // print sensors on stdout - err = fc.PrintSensors(os.Stdout) + err = cli.PrintSensors(fc, os.Stdout) if err != nil { log.Fatalln(err) } diff --git a/pkg/cli/cli.go b/pkg/cli/cli.go index a70b0af..1ba138a 100644 --- a/pkg/cli/cli.go +++ b/pkg/cli/cli.go @@ -9,6 +9,7 @@ import ( "git.cryptic.systems/fh-trier/go-flucky/pkg/types" ) +// PrintHumidities displays a list of humidities func PrintHumidities(humidities []*types.Humidity, cnf *config.FluckyConfig, w io.Writer) { // determine all humidity sensors based on the humidiy values sensors := []*types.Sensor{} @@ -66,6 +67,40 @@ func PrintHumidities(humidities []*types.Humidity, cnf *config.FluckyConfig, w i tw.Flush() } +// PrintRemotes displays a list with all configured remote addresses +func PrintRemotes(cnf *config.FluckyConfig, w io.Writer) error { + + tw := tabwriter.NewWriter(w, 0, 0, 3, ' ', 0) + + fmt.Fprint(tw, "name\taddress\tenabled\tregistered\n") + + for _, remote := range cnf.Remotes { + fmt.Fprintf(tw, "%v\t%v\t%v\t%v\n", remote.Name, remote.Address, remote.Enabled, remote.Registered) + } + + tw.Flush() + + return nil +} + +// PrintSensors displays a list with all configured sensors +func PrintSensors(cnf *config.FluckyConfig, w io.Writer) error { + + // declar tabwriter + tw := tabwriter.NewWriter(w, 0, 0, 3, ' ', 0) + + fmt.Fprint(tw, "name\tlocation\ttype\twire-id\tgpio\tenabled\n") + + for _, sensor := range cnf.Sensors { + fmt.Fprintf(tw, "%v\t%v\t%v\t%v\t%v\t%v\n", sensor.SensorName, sensor.SensorLocation, sensor.SensorModel, *sensor.WireID, *sensor.GPIONumber, sensor.SensorEnabled) + } + + tw.Flush() + + return nil +} + +// PrintTemperatures displays a list of temperatures func PrintTemperatures(temperatures []*types.Temperature, cnf *config.FluckyConfig, w io.Writer) { sensors := []*types.Sensor{} diff --git a/pkg/config/fluckyconfig.go b/pkg/config/fluckyconfig.go index d76ef3e..d1b49a4 100644 --- a/pkg/config/fluckyconfig.go +++ b/pkg/config/fluckyconfig.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" "io" - "text/tabwriter" "time" "git.cryptic.systems/fh-trier/go-flucky/pkg/logger" @@ -302,39 +301,6 @@ func (fc *FluckyConfig) JSONWriter(w io.Writer) error { return nil } -// PrintRemotes displays a list with all configured remote addresses -func (fc *FluckyConfig) PrintRemotes(w io.Writer) error { - - tw := tabwriter.NewWriter(w, 0, 0, 3, ' ', 0) - - fmt.Fprint(tw, "name\taddress\tenabled\tregistered\n") - - for _, remote := range fc.Remotes { - fmt.Fprintf(tw, "%v\t%v\t%v\t%v\n", remote.Name, remote.Address, remote.Enabled, remote.Registered) - } - - tw.Flush() - - return nil -} - -// PrintSensors displays a list with all configured sensors -func (fc *FluckyConfig) PrintSensors(w io.Writer) error { - - // declar tabwriter - tw := tabwriter.NewWriter(w, 0, 0, 3, ' ', 0) - - fmt.Fprint(tw, "name\tlocation\ttype\twire-id\tgpio\tenabled\n") - - for _, sensor := range fc.Sensors { - fmt.Fprintf(tw, "%v\t%v\t%v\t%v\t%v\t%v\n", sensor.SensorName, sensor.SensorLocation, sensor.SensorModel, *sensor.WireID, *sensor.GPIONumber, sensor.SensorEnabled) - } - - tw.Flush() - - return nil -} - // RemoveSensor deletes a sensor by its name or its unique UUID func (fc *FluckyConfig) RemoveSensor(nameOrUUID string) error { for i, sensor := range fc.Sensors {