From 3d54401011aac39974b5d9824fddc75865695249 Mon Sep 17 00:00:00 2001 From: Markus Pesch Date: Mon, 4 Mar 2019 21:19:33 +0100 Subject: [PATCH] feat: list configured loggers --- cmd/logger/list.go | 40 ++++++++++++++++++++++++++++++++++++++++ pkg/cli/cli.go | 15 +++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 cmd/logger/list.go diff --git a/cmd/logger/list.go b/cmd/logger/list.go new file mode 100644 index 0000000..74bb13e --- /dev/null +++ b/cmd/logger/list.go @@ -0,0 +1,40 @@ +package logger + +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" +) + +var quiet bool + +var listLoggerCmd = &cobra.Command{ + Use: "ls", + Short: "List internal Loggers", + Aliases: []string{"list"}, + Example: "flucky logger ls", + Run: func(cmd *cobra.Command, args []string) { + + // read configuration + fc, err := config.Read(cfg) + if err != nil { + log.Fatalln(err) + } + + // print all configured remote addresses on stdout + cli.PrintLoggers(fc, os.Stdout) + + // save new configuration + err = config.Write(fc, cfg) + if err != nil { + log.Fatalln(err) + } + }, +} + +func init() { + loggerCmd.AddCommand(listLoggerCmd) +} diff --git a/pkg/cli/cli.go b/pkg/cli/cli.go index 9096fa5..ae75cae 100644 --- a/pkg/cli/cli.go +++ b/pkg/cli/cli.go @@ -5,6 +5,8 @@ import ( "io" "text/tabwriter" + "git.cryptic.systems/fh-trier/go-flucky/pkg/logger" + "git.cryptic.systems/fh-trier/go-flucky/pkg/internal/temperature" "git.cryptic.systems/fh-trier/go-flucky/pkg/config" @@ -69,6 +71,19 @@ func PrintHumidities(humidities []*types.Humidity, cnf *config.FluckyConfig, w i tw.Flush() } +func PrintLoggers(cnf *config.FluckyConfig, w io.Writer) { + // declare tabwriter + tw := tabwriter.NewWriter(w, 0, 0, 3, ' ', 0) + + fmt.Fprintf(w, "type\tvalue\t\tlocation\n") + + for _, logfile := range cnf.LogFiles[logger.LogTemperature] { + fmt.Fprintf(w, "file\t%v\t%v\n", logger.LogTemperature, logfile) + } + + tw.Flush() +} + // PrintRemotes displays a list with all configured remote addresses func PrintRemotes(cnf *config.FluckyConfig, w io.Writer) error {