diff --git a/.gitignore b/.gitignore index 7e330d6..0b3e5b9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ - +# Binaries build +gemget +gemget-* # Created by https://www.gitignore.io/api/go,linux,code # Edit at https://www.gitignore.io/?templates=go,linux,code diff --git a/README.md b/README.md index c96917d..cffbed9 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,9 @@ Usage of gemget: -e, --add-extension Add .gmi extensions to gemini files that don't have it, like directories. -d, --directory string The directory where downloads go (default ".") -i, --insecure Skip checking the cert - -o, --output string Output file, for when there is only one URL. + -o, --output string Output path, for when there is only one URL. '-' means stdout and implies --quiet. + It overrides --directory. -q, --quiet No output except for errors. -r, --redirects uint How many redirects to follow before erroring out. (default 5) -s, --skip Move to the next URL when one fails. diff --git a/gemget.go b/gemget.go index 55a3e5f..eab5ca7 100644 --- a/gemget.go +++ b/gemget.go @@ -20,7 +20,7 @@ import ( var insecure = flag.BoolP("insecure", "i", false, "Skip checking the cert") var dir = flag.StringP("directory", "d", ".", "The directory where downloads go") -var output = flag.StringP("output", "o", "", "Output file, for when there is only one URL.\n'-' means stdout and implies --quiet.") +var output = flag.StringP("output", "o", "", "Output path, for when there is only one URL.\n'-' means stdout and implies --quiet.\nIt overrides --directory.") var errorSkip = flag.BoolP("skip", "s", false, "Move to the next URL when one fails.") var exts = flag.BoolP("add-extension", "e", false, "Add .gmi extensions to gemini files that don't have it, like directories.") var quiet bool // Set in main, so that it can be changed later if needed @@ -45,10 +45,10 @@ func urlError(format string, a ...interface{}) { func saveFile(resp *gemini.Response, u *url.URL) { var name string - if *output != "" { - name = *output - } else { - name = path.Base(u.Path) // Filename from URL + var savePath string + + if *output == "" { + name := path.Base(u.Path) // Filename from URL if name == "/" || name == "." { // Domain is being downloaded, so there's no path/file name = u.Hostname() @@ -57,9 +57,13 @@ func saveFile(resp *gemini.Response, u *url.URL) { // It's a gemini file, but it doesn't have that extension - and the user wants them added name += ".gmi" } + savePath = filepath.Join(*dir, name) + } else { + // There is an output path + savePath = *output } - f, err := os.OpenFile(filepath.Join(*dir, name), os.O_CREATE|os.O_WRONLY, 0644) + f, err := os.OpenFile(savePath, os.O_CREATE|os.O_WRONLY, 0644) if err != nil { fatal("Error saving file %s", name) }