excel_converter tool created
This commit is contained in:
55
cmd/excel_converter/main.go
Normal file
55
cmd/excel_converter/main.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"git.ecogood.org/andreas.schroepfer/excelConverter/pkg/loader"
|
||||
)
|
||||
|
||||
func main() {
|
||||
inFile, outFile := parseArgs()
|
||||
xf, err := os.Open(inFile)
|
||||
if err != nil {
|
||||
log.Fatal("cannot open file:", err)
|
||||
}
|
||||
defer xf.Close()
|
||||
eBalance, err := loader.XLSX(xf, nil)
|
||||
if err != nil {
|
||||
fmt.Println("errors when loading from excel:")
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
out, err := os.OpenFile(outFile, os.O_CREATE, 0666)
|
||||
fmt.Println("createFileErr:", err)
|
||||
eBalance.EncodeJSON(out)
|
||||
out.Close()
|
||||
}
|
||||
|
||||
func parseArgs() (inFile, outFile string) {
|
||||
flag.Parse()
|
||||
args := flag.Args()
|
||||
switch len(args) {
|
||||
case 0:
|
||||
fmt.Println("you need to provide an input-file")
|
||||
fmt.Println("the output is optional")
|
||||
fmt.Println("exel_converter [input] [[output]]")
|
||||
os.Exit(1)
|
||||
case 1:
|
||||
inFile, outFile = args[0], fmt.Sprintf("%s.json", args[0])
|
||||
case 2:
|
||||
inFile, outFile = args[0], args[1]
|
||||
default:
|
||||
fmt.Println("warning: just the first 2 arguments are used!")
|
||||
fmt.Println("exel_converter [input] [[output]]")
|
||||
}
|
||||
if !strings.EqualFold(".xlsx", filepath.Ext(inFile)) {
|
||||
fmt.Println("input needs to be an xlsx-file!")
|
||||
os.Exit(2)
|
||||
}
|
||||
return inFile, outFile
|
||||
}
|
||||
Reference in New Issue
Block a user