Last active
July 4, 2020 00:24
-
-
Save rolandvarga/f85a9cad630a7daf572cb5a98359c7ac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"fmt" | |
"os" | |
"path/filepath" | |
) | |
func pathExists(path string) bool { | |
if _, err := os.Stat(path); os.IsNotExist(err) { | |
return false | |
} | |
return true | |
} | |
func main() { | |
fmt.Println("Usage: ./grayscale-cbz --dest=/path/to/dest <FILE>") | |
dest := flag.String("dest", "/tmp/", "Destination directory for grayscale images") | |
flag.Parse() | |
// check if flags valid | |
cbz := flag.Args()[0] | |
if !pathExists(cbz) { | |
fmt.Printf("path to file '%s' doesn't exist\n", cbz) | |
os.Exit(1) | |
} | |
// check if cbz is valid | |
ext := filepath.Ext(cbz) | |
if ext != ".cbz" { | |
fmt.Printf("invalid file extension: '%s'\n", ext) | |
os.Exit(1) | |
} | |
// check if dest exists (if no create or exit with error) | |
if !pathExists(*dest) { | |
fmt.Printf("destination folder '%s' doesn't exist. Creating...", *dest) | |
err := os.MkdirAll(*dest, os.ModePerm) | |
if err != nil { | |
fmt.Printf("error creating directory: %s", err.Error()) | |
os.Exit(1) | |
} | |
fmt.Println("done!") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment