Add modes

This commit is contained in:
kolaente 2020-11-15 15:38:34 +01:00
parent ca5dee2467
commit ccdc004365
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
1 changed files with 81 additions and 10 deletions

91
main.go
View File

@ -10,6 +10,7 @@ import (
"path"
"path/filepath"
"runtime"
"strconv"
"strings"
)
@ -26,6 +27,47 @@ func readText() (text string, err error) {
return
}
// One QR Code for each line of a file
const modeLine int = 1
// One QR Code for each text file
const modeFile int = 2
func getMode() int {
fmt.Println("Availabl Modes:")
fmt.Println("1: Generate one QR-Code for each line of text in each text file")
fmt.Println("2: Generate one QR-Code for each text file")
fmt.Println("What mode should the tool use? [1]")
modeStr, err := readText()
if err != nil {
handleErr("Could not get mode: %s", err)
}
modeStr = strings.Trim(modeStr, "\n")
modeStr = strings.Trim(modeStr, "\r")
switch modeStr {
case "":
fallthrough
case "1":
return modeLine
case "2":
return modeFile
default:
handleErr("Unknown mode '%s'", modeStr)
}
return 0
}
func createQrCode(filenameWithoutSuffix, outPath, content string) {
outfile := path.Join(outPath, filenameWithoutSuffix+".png")
err := qrcode.WriteFile(content, qrcode.Low, 2048, outfile)
if err != nil {
handleErr("Could not create qr code for file '%s.txt': %s", filenameWithoutSuffix, err)
}
}
func main() {
fmt.Printf("Bulk QR Code generate tool, Version %s\n\r", VersionNum)
@ -44,8 +86,12 @@ func main() {
workingDir = cwd
}
outPath := path.Join(workingDir, "qrcodes")
if err := os.MkdirAll(outPath, os.ModePerm); err != nil {
// 1.1 Operating Mode
mode := getMode()
// Out directory
outBasePath := path.Join(workingDir, "qrcodes")
if err := os.MkdirAll(outBasePath, os.ModePerm); err != nil {
handleErr("Could not create output folder: %s", err)
}
@ -55,7 +101,7 @@ func main() {
handleErr("Could not get files: %s", err)
}
fmt.Printf("Generating %d QR Codes from files in %s...\n\r", len(files), workingDir)
fmt.Printf("Generating QR Codes from %d files in %s...\n\r", len(files), workingDir)
bar := progressbar.Default(int64(len(files)))
for _, file := range files {
@ -66,16 +112,41 @@ func main() {
if !strings.HasSuffix(f.Name(), ".txt") {
continue
}
content, err := ioutil.ReadFile(file)
if err != nil {
handleErr("Could not open file '%s': %s", f.Name(), err)
fNameNoSuffix := strings.TrimSuffix(path.Base(filepath.ToSlash(f.Name())), path.Ext(file))
var outPath = outBasePath
if mode == modeLine {
outPath = path.Join(outBasePath, fNameNoSuffix)
if err := os.MkdirAll(outPath, os.ModePerm); err != nil {
handleErr("Could not create output folder: %s", err)
}
}
// 3. Generate QR-Codes and save
outfile := path.Join(outPath, strings.TrimSuffix(path.Base(filepath.ToSlash(f.Name())), path.Ext(file))) + ".png"
err = qrcode.WriteFile(string(content), qrcode.Low, 2048, outfile)
if err != nil {
handleErr("Could not create qr code for file '%s': %s", f.Name(), err)
if mode == modeFile {
content, err := ioutil.ReadFile(file)
if err != nil {
handleErr("Could not open file '%s': %s", f.Name(), err)
}
createQrCode(fNameNoSuffix, outPath, string(content))
}
if mode == modeLine {
scanner := bufio.NewScanner(f)
i := 0
for scanner.Scan() {
createQrCode(fNameNoSuffix+"_"+strconv.Itoa(i), outPath, scanner.Text())
i++
}
if err := scanner.Err(); err != nil {
handleErr("Could not read file '%s': %s", f.Name(), err)
}
}
_ = bar.Add(1)
}