2023-03-29 15:26:21 +02:00
|
|
|
package dirtoexcel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2023-04-05 10:24:32 +02:00
|
|
|
"github.com/lmittmann/tint"
|
2023-03-29 15:26:21 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/xuri/excelize/v2"
|
2023-04-05 10:24:32 +02:00
|
|
|
"golang.org/x/exp/slog"
|
2023-03-29 15:26:21 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const re = `^(?P<company>[a-zA-Z0-9-]+)(?:_[a-zA-Z]+)?_(?P<value>\d+\.\d{1,2})`
|
|
|
|
|
|
|
|
func init() {
|
2023-04-05 10:24:32 +02:00
|
|
|
slog.SetDefault(slog.New(tint.Options{
|
|
|
|
Level: slog.LevelDebug,
|
|
|
|
TimeFormat: time.Kitchen,
|
|
|
|
}.NewHandler(os.Stderr)))
|
2023-03-29 15:26:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func Run(cmd *cobra.Command, args []string) {
|
|
|
|
outfile, err := cmd.Flags().GetString("out")
|
|
|
|
if err != nil {
|
2023-04-05 10:24:32 +02:00
|
|
|
slog.Error("failed to get outfile string", "error", err)
|
|
|
|
os.Exit(1)
|
2023-03-29 15:26:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if outfile == "" {
|
|
|
|
outfile = fmt.Sprintf("%d.xlsx", time.Now().Unix())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := Create(outfile, args[0]); err != nil {
|
2023-04-05 10:24:32 +02:00
|
|
|
slog.Error("failed to create excel", "error", err)
|
|
|
|
os.Exit(1)
|
2023-03-29 15:26:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Create(out, dir string) error {
|
2023-04-05 10:24:32 +02:00
|
|
|
log := slog.With("dir", dir, "out", out)
|
2023-03-29 15:26:21 +02:00
|
|
|
|
|
|
|
f := excelize.NewFile()
|
|
|
|
defer func() {
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
log.Error("failed to close excel file")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
files, err := os.ReadDir(dir)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to read dir: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := regexp.Compile(re)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to compile regex: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, j := range files {
|
|
|
|
if j.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("found", "file", j.Name())
|
|
|
|
|
|
|
|
res := r.FindAllStringSubmatch(j.Name(), -1)
|
|
|
|
if res == nil {
|
|
|
|
return fmt.Errorf("nothing found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(res) != 1 || len(res[0]) != 3 {
|
|
|
|
return fmt.Errorf("strange numbers of matches")
|
|
|
|
}
|
|
|
|
|
2023-04-05 11:29:15 +02:00
|
|
|
company := res[0][1]
|
|
|
|
|
|
|
|
log.Debug("extracted", slog.String("company", company))
|
|
|
|
|
|
|
|
if err := f.SetCellStr("Sheet1", fmt.Sprintf("A%d", i+1), company); err != nil {
|
2023-03-29 15:26:21 +02:00
|
|
|
return fmt.Errorf("failed to set cell: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
value, err := strconv.ParseFloat(res[0][2], 32)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse value: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-04-05 11:29:15 +02:00
|
|
|
rValue := math.Round(value*100) / 100
|
|
|
|
|
|
|
|
log.Debug("extracted", slog.Float64("value", rValue))
|
|
|
|
|
|
|
|
if err := f.SetCellValue("Sheet1", fmt.Sprintf("B%d", i+1), rValue); err != nil {
|
2023-03-29 15:26:21 +02:00
|
|
|
return fmt.Errorf("failed to set cell: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := f.SaveAs(out); err != nil {
|
|
|
|
return fmt.Errorf("failed to save excel: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("created excel")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|