Skip to content

Add input file and os.Stdin support #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions qrcode/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import (
"strings"

qrcode "github.com/skip2/go-qrcode"
"io/ioutil"
)

func main() {
inFile := flag.String("i", "", "content file, empty for STDIN")
outFile := flag.String("o", "", "out PNG file prefix, empty for stdout")
size := flag.Int("s", 256, "image size (pixel)")
flag.Usage = func() {
Expand Down Expand Up @@ -41,12 +43,25 @@ Usage:
if *size <= 0 {
checkError(fmt.Errorf("Error: value of -s should > 0"))
}
if len(flag.Args()) == 0 {
flag.Usage()
checkError(fmt.Errorf("Error: no content given"))
}

content := strings.Join(flag.Args(), " ")
if *inFile != "" {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest prioritising the -i flag, then command line arguments, then reading from STDIN last, like:

content := ""
argsContent := strings.Join(flag.Args(), " ")

if *inFile != "" {
	b, err := ioutil.ReadFile(*inFile)
	if err != nil {
		checkError(err)
	}
	content = string(b)
} else if argsContent != "" {
	content = argsContent
} else {
	b, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		checkError(err)
	}
	content = string(b)
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey,

I think reading content from STDIN/a file is a nice addition :)

After this pull request there are three ways to specify content:

  • String from input file:

$ qrcode -i input_filename | display

  • String on command line:

$ qrcode hello | display

  • String on STDIN:

$ echo -n hello | qrcode | display

  • No arguments: (same as string on STDIN, only you type into the terminal and then press Ctrl+D)

$ qrcode | display

If you try the "qrcode hello | display" method, it now hangs and waits for something on STDIN.

I suggest taking content from one source only (first the -i flag, then command line arguments, or lastly STDIN), so the usage is more obvious and easier to explain. This would work well.

thanks,

Tom

b, err := ioutil.ReadFile(*inFile)
if err != nil {
checkError(err)
}
content += string(b)
} else {
b, err := ioutil.ReadAll(os.Stdin)
if err != nil {
checkError(err)
}
content += string(b)
}
if content == "" {
flag.Usage()
checkError(fmt.Errorf("Error: no content given?"))
}

var err error
var q *qrcode.QRCode
Expand Down