Initial script

This commit is contained in:
kolaente 2020-09-15 18:02:56 +02:00
parent e314f330aa
commit 1620428d5c
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
1 changed files with 76 additions and 0 deletions

76
main.go Normal file
View File

@ -0,0 +1,76 @@
package main
import (
"bufio"
"fmt"
"github.com/skip2/go-qrcode"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
)
func handleErr(format string, args ...interface{}) {
fmt.Printf(format, args)
os.Exit(1)
}
func readText() (text string, err error) {
cr := bufio.NewReader(os.Stdin)
text, err = cr.ReadString('\n')
return
}
func main() {
// 1. Get directory
cwd, err := os.Getwd()
if err != nil {
handleErr("Could not get current working dir: %s", err)
}
fmt.Printf("Enter the directory path with text files [%s]\n\r", cwd)
workingDir, err := readText()
if err != nil {
handleErr("Could not get current dir: %s", err)
}
if workingDir == "" {
workingDir = cwd
}
outPath := path.Join(workingDir, "qrcodes")
if err := os.MkdirAll(outPath, 0755); err != nil {
handleErr("Could not create output folder: %s", err)
}
// 2. Get Files
files, err := filepath.Glob(path.Join(workingDir, "*.txt"))
if err != nil {
handleErr("Could not get files: %s", err)
}
fmt.Printf("Generating %d QR Codes...\n\r", len(files))
for _, file := range files {
f, err := os.Open(file)
if err != nil {
handleErr("Could not open file '%s': %s", file, err)
}
if !strings.HasSuffix(f.Name(), ".txt") {
continue
}
content, err := ioutil.ReadFile(path.Join(workingDir, f.Name()))
if err != nil {
handleErr("Could not open file '%s': %s", f.Name(), err)
}
// 3. Generate QR-Codes and save
err = qrcode.WriteFile(string(content), qrcode.High, 1024, path.Join(outPath, strings.TrimSuffix(f.Name(), path.Ext(file))))
if err != nil {
handleErr("Could not create qr code for file '%s': %s", f.Name(), err)
}
fmt.Printf("Generated QR Code for %s\n\r", f.Name())
}
// 4. Done
fmt.Println("Done!")
}