PKGBUILD/cmd/temperature/read.go

57 lines
1.3 KiB
Go

package temperature
import (
"fmt"
"log"
"os"
"git.cryptic.systems/fh-trier/go-flucky/pkg/cli"
"git.cryptic.systems/fh-trier/go-flucky/pkg/config"
"git.cryptic.systems/fh-trier/go-flucky/pkg/sensor"
"github.com/spf13/cobra"
)
var logs bool
var readTemperatureCmd = &cobra.Command{
Use: "read",
Short: "read temperature from sensor",
Example: fmt.Sprintf("flucky temperature read\nflucky temperature read outdoor"),
Run: func(cmd *cobra.Command, args []string) {
// read configuration
fc, err := config.Read(cfg)
if err != nil {
log.Fatalln(err)
}
// fetch all temperature sensors
temperatureSensors, err := fc.GetTemperatureSensors(args)
if err != nil {
log.Fatalln(err)
}
// read temperature from sensors
temperatures, err := sensor.ReadTemperatures(temperatureSensors)
if err != nil {
log.Fatalln(err)
}
// print temperatures on stdout
cli.PrintTemperatures(temperatures, fc, os.Stdout)
if logs {
err = fc.FileLogger.LogTemperatures(temperatures)
if err != nil {
log.Fatalln(err)
}
}
},
}
func init() {
temperatureCmd.AddCommand(readTemperatureCmd)
// readTemperatureCmd.Flags().BoolVarP(&follow, "follow", "f", false, "Follow output")
readTemperatureCmd.Flags().BoolVarP(&logs, "logs", "l", true, "Log temperature")
}