PKGBUILD/cmd/convert/convert.go

60 lines
1.3 KiB
Go

package convert
import (
"log"
"github.com/Masterminds/semver"
"github.com/go-flucky/flucky/pkg/storage"
"github.com/go-flucky/flucky/pkg/storage/logfile"
"github.com/spf13/cobra"
)
var (
compression bool
configFile *string
round float64
version *semver.Version
)
var 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",
Run: func(cmd *cobra.Command, args []string) {
logfileInput := logfile.New(args[0])
measuredValues, err := logfileInput.Read()
if err != nil {
log.Fatalln(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 {
log.Fatalln(err)
}
},
}
// InitCmd ...
func InitCmd(cmd *cobra.Command, cnfFile *string, sversion *semver.Version) {
configFile = cnfFile
version = sversion
cmd.AddCommand(convertCmd)
convertCmd.Flags().BoolVar(&compression, "compression", false, "Compress measured values")
convertCmd.Flags().Float64Var(&round, "round", 0, "Round values. The value 0 deactivates the function")
}