package main import ( "bufio" "fmt" "github.com/schollz/progressbar/v3" "github.com/skip2/go-qrcode" "io/ioutil" "os" "path" "path/filepath" "runtime" "strings" ) var VersionNum = "dev" 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() { fmt.Printf("Bulk QR Code generate tool, Version %s\n\r", VersionNum) // 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 == "\n" || workingDir == "\n\r" { workingDir = cwd } outPath := filepath.Dir(path.Join(workingDir, "qrcodes")) if err := os.MkdirAll(outPath, os.ModePerm); err != nil { handleErr("Could not create output folder: %s", err) } // 2. Get Files glob := workingDir + "/*.txt" if runtime.GOOS == "windows" { glob = workingDir + "\\*.txt" } files, err := filepath.Glob(glob) if err != nil { handleErr("Could not get files: %s", err) } fmt.Printf("Generating %d QR Codes from files in %s...\n\r", len(files), workingDir) bar := progressbar.Default(int64(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(file) 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.Low, 2048, path.Join(outPath, strings.TrimSuffix(path.Base(f.Name()), path.Ext(file)))+".png") if err != nil { handleErr("Could not create qr code for file '%s': %s", f.Name(), err) } _ = bar.Add(1) } // 4. Done fmt.Println("Done!") }