package main import ( "errors" "flag" "fmt" "log" "opslag.de/schobers/tins2020/img" ) func run() error { flag.Parse() args := flag.Args() if len(args) < 1 { return errors.New("no command specified") } switch args[0] { case "setalpha": flags := flag.NewFlagSet("setalpha", flag.ContinueOnError) var alpha int flags.IntVar(&alpha, "alpha", 127, "sets the target alpha") flags.Parse(args[1:]) for _, path := range flags.Args() { err := setAlpha(path, alpha) if err != nil { return fmt.Errorf("couldn't set alpha of '%s'; error: %v", path, err) } } case "gray": flags := flag.NewFlagSet("gray", flag.ContinueOnError) flags.Parse(args[1:]) for _, path := range flags.Args() { err := convertToGray(path) if err != nil { return fmt.Errorf("couldn't convert to grayscale of '%s'; error: %v", path, err) } } case "crop": flags := flag.NewFlagSet("crop", flag.ContinueOnError) var rect string var resize string var dir string flags.StringVar(&rect, "crop", "", "sets the crop rectangle \"x1,y1,y1,y2\"") flags.StringVar(&resize, "resize", "", "sets the target size, \"width,height\", if not specified the crop rectangle size is used") flags.StringVar(&dir, "dir", "", "crops all images in the specified directory (recursively)") flags.Parse(args[1:]) cropRect, err := parseRect(rect) if err != nil { return fmt.Errorf("crop rectangle is invalid: error: %v", err) } var size *point if len(resize) != 0 { p, err := parsePoint(resize) if err != nil { return fmt.Errorf("resize parameter is invalid: error: %v", err) } size = &p } var files []string if len(dir) == 0 { files = flags.Args() } else { files, err = scanDir(dir) if err != nil { return fmt.Errorf("couldn't find files to crop; error: %v", err) } } for _, path := range files { err := crop(path, cropRect, size) if err != nil { return fmt.Errorf("couldn't crop '%s'; error: %v", path, err) } } case "color": flags := flag.NewFlagSet("color", flag.ContinueOnError) var col string flags.StringVar(&col, "color", "#ff0000", "target color") flags.Parse(args[1:]) for _, path := range flags.Args() { err := colorImage(path, img.MustHexColor(col)) if err != nil { return fmt.Errorf("couldn't convert to grayscale of '%s'; error: %v", path, err) } } } return nil } func main() { if err := run(); err != nil { log.Fatal(err) } }