PKGBUILD/cmd/convert/convert.go

56 lines
1.2 KiB
Go

package convert
import (
"github.com/volker-raschek/flucky/pkg/storage"
"github.com/volker-raschek/flucky/pkg/storage/logfile"
"github.com/spf13/cobra"
)
var (
compression bool
round float64
)
// InitCmd initialize all convert subcommands
func InitCmd(cmd *cobra.Command) error {
convertCmd := &cobra.Command{
Use: "convert",
Short: "Convert logfiles into other markup language",
Args: cobra.ExactArgs(2),
Example: "flucky convert /var/log/flucky/logfile.json /var/log/flucky/logfile.csv",
RunE: run,
}
convertCmd.Flags().BoolVar(&compression, "compression", false, "Compress measured values")
convertCmd.Flags().Float64Var(&round, "round", 0, "Round values. The value 0 deactivates the function")
cmd.AddCommand(convertCmd)
return nil
}
func run(cmd *cobra.Command, args []string) error {
logfileInput := logfile.New(args[0])
measuredValues, err := logfileInput.Read()
if err != nil {
return err
}
if round != 0 {
storage.Round(measuredValues, round)
}
if compression {
measuredValues = storage.Compression(measuredValues)
}
logfileOutput := logfile.New(args[1])
err = logfileOutput.Write(measuredValues)
if err != nil {
return err
}
return nil
}