Properly handle command-line flags

This commit is contained in:
heyarne 2022-02-09 21:06:10 +01:00
commit ef328697b8
2 changed files with 62 additions and 29 deletions

6
.gitignore vendored
View file

@ -1,3 +1,9 @@
/inkpot-cli
*.epd
*.jpg
*.jpeg
*.png
*.gif
# Created by https://www.toptal.com/developers/gitignore/api/go # Created by https://www.toptal.com/developers/gitignore/api/go
# Edit at https://www.toptal.com/developers/gitignore?templates=go # Edit at https://www.toptal.com/developers/gitignore?templates=go

View file

@ -7,8 +7,10 @@ package cmd
import ( import (
"image" "image"
"image/color" "image/color"
"image/jpeg" _ "image/gif"
_ "image/jpeg"
_ "image/png" _ "image/png"
"io"
"log" "log"
"os" "os"
@ -16,29 +18,65 @@ import (
"golang.org/x/image/draw" "golang.org/x/image/draw"
) )
var width int
var height int
var outfile string
func Foobar () (io.Writer, error) {
return os.Stdout, nil
}
// convertCmd represents the convert command // convertCmd represents the convert command
var convertCmd = &cobra.Command{ var convertCmd = &cobra.Command{
Use: "convert", Use: "convert <file|->",
Short: "A brief description of your command", Short: "Convert a single file to a 4-bit, 16-color grayscale image",
Long: `A longer description that spans multiple lines and likely contains examples Long: `Convert a single file to a 4-bit, 16-color grayscale image.
and usage of using your command. For example: Supports jpeg, png and gif files.
Cobra is a CLI library for Go that empowers applications. Pass "-" as the filename to read from stdin.`,
This application is a tool to generate the needed files Args: cobra.ExactArgs(1),
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
width, _ := cmd.Flags().GetInt("width") var err error
height, _ := cmd.Flags().GetInt("height")
convertImage(width, height) // read from given file or stdin
var input io.Reader
if args[0] == "-" {
input = os.Stdin
} else {
input, err := os.Open(args[0])
if err != nil {
log.Fatalf("Error opening file: %v", err)
}
defer input.Close()
}
// write to given outfile, default to stdout
var output io.Writer
if outfile == "" {
output = os.Stdout
} else {
file, err := os.OpenFile(outfile, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
log.Fatalf("Error writing output file: %v", err)
}
defer file.Close()
output = file
}
result, err := convertImage(input, width, height)
if err != nil {
log.Fatalf("Could not convert image: %v", err)
}
output.Write(result)
}, },
} }
func init() { func init() {
rootCmd.AddCommand(convertCmd) rootCmd.AddCommand(convertCmd)
convertCmd.Flags().Int("width", 540, "Target width") convertCmd.Flags().IntVarP(&width, "width", "x", 540, "target width")
convertCmd.Flags().Int("height", 960, "Target height") convertCmd.Flags().IntVarP(&height, "height", "y", 960, "target height")
convertCmd.Flags().StringVarP(&outfile, "output", "o", "", "file to write the result to (default stdout)")
} }
// Returns a new Rectangle that is resized and centered in `dst` // Returns a new Rectangle that is resized and centered in `dst`
@ -67,22 +105,11 @@ func fitRectInto(src *image.Rectangle, dst *image.Rectangle) image.Rectangle {
return image.Rect(targetX, targetY, targetWidth+targetX, targetHeight+targetY) return image.Rect(targetX, targetY, targetWidth+targetX, targetHeight+targetY)
} }
func convertImage(width int, height int) { func convertImage(input io.Reader, width int, height int) ([]byte, error) {
// for now, we're reading a predefined input file src, _, err := image.Decode(input)
input, err := os.Open("cat.jpg")
if err != nil { if err != nil {
log.Fatalf("Error opening file: %v", err) return nil, err
} }
defer input.Close()
// … and writing to a predefined output file
output, err := os.OpenFile("cat_resized.epd", os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
log.Fatalf("Error writing output file: %v", err)
}
defer output.Close()
src, _ := jpeg.Decode(input)
dst := image.NewGray(image.Rect(0, 0, width, height)) dst := image.NewGray(image.Rect(0, 0, width, height))
srcBounds := src.Bounds() srcBounds := src.Bounds()
@ -104,5 +131,5 @@ func convertImage(width int, height int) {
} }
} }
output.Write(result) return result, nil
} }