refac(cmd): mergeing, ordering and outsourcing cmd subcommands

This commit is contained in:
2020-01-13 22:26:41 +01:00
parent 0261203395
commit 0765bd29d1
34 changed files with 1209 additions and 1302 deletions

View File

@ -1,8 +1,6 @@
package compression
import (
"log"
"github.com/go-flucky/flucky/pkg/storage"
"github.com/go-flucky/flucky/pkg/storage/logfile"
@ -10,40 +8,43 @@ import (
)
var (
configFile *string
round float64
round float64
)
var compressionCmd = &cobra.Command{
Use: "compression",
Short: "Compress a logfile",
Args: cobra.ExactArgs(1),
Example: "flucky compression /var/log/flucky/logfile.csv",
Run: func(cmd *cobra.Command, args []string) {
// InitCmd initialize all compression subcommands
func InitCmd(cmd *cobra.Command) error {
logfileInput := logfile.New(args[0])
measuredValues, err := logfileInput.Read()
if err != nil {
log.Fatalln(err)
}
compressionCmd := &cobra.Command{
Use: "compression",
Short: "Compress a logfile",
Args: cobra.ExactArgs(1),
Example: "flucky compression /var/log/flucky/logfile.csv",
RunE: run,
}
if round != 0 {
storage.Round(measuredValues, round)
}
storage.Compression(measuredValues)
err = logfileInput.Write(measuredValues)
if err != nil {
log.Fatalln(err)
}
},
}
func InitCmd(cmd *cobra.Command, cnfFile *string) {
configFile = cnfFile
cmd.AddCommand(compressionCmd)
compressionCmd.Flags().Float64Var(&round, "round", 0, "Round values. The value 0 deactivates the function")
cmd.AddCommand(compressionCmd)
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)
}
storage.Compression(measuredValues)
err = logfileInput.Write(measuredValues)
if err != nil {
return err
}
return nil
}