Skip to content

Commit

Permalink
chore: refactor img logic out of root cmd function
Browse files Browse the repository at this point in the history
  • Loading branch information
scottmckendry committed May 30, 2024
1 parent 67d183e commit 8267745
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 34 deletions.
27 changes: 3 additions & 24 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cmd

import (
"fmt"
"image"
"os"

"github.com/spf13/cobra"
Expand All @@ -23,22 +21,8 @@ var cmd = &cobra.Command{

Run: func(cmd *cobra.Command, args []string) {
path := args[0]
url := img.IsUrl(path)

if !url && !PathExists(path) {
cobra.CheckErr(fmt.Errorf("could not find file: %s", path))
}

image := image.Image(nil)
err := error(nil)
if !url {
image, err = img.Decode(path)
} else {
image, err = img.DecodeFromUrl(path)
}
if err != nil {
cobra.CheckErr(err)
}
iamge, err := img.Decode(path)
cobra.CheckErr(err)

columns, _ := cmd.Flags().GetInt("columns")
rows, _ := cmd.Flags().GetInt("rows")
Expand All @@ -48,7 +32,7 @@ var cmd = &cobra.Command{
columns = 0
}

ansi.PrintImage(image, columns, rows)
ansi.PrintImage(iamge, columns, rows)
},
}

Expand All @@ -63,8 +47,3 @@ func init() {
cmd.Flags().IntP("columns", "c", 100, "Number of columns to use for the image")
cmd.Flags().IntP("rows", "r", 0, "Number of rows to use for the image")
}

func PathExists(path string) bool {
_, err := os.Stat(path)
return !os.IsNotExist(err)
}
41 changes: 31 additions & 10 deletions img/img.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,15 @@ import (
// Decode reads an image from a file and returns the image.Image.
// If the file cannot be read, an error is returned.
func Decode(filepath string) (image.Image, error) {
file, err := os.Open(filepath)
if err != nil {
return nil, err
if isUrl(filepath) {
return decodeFromUrl(filepath)
}
defer file.Close()

img, _, err := image.Decode(file)
if err != nil {
return nil, err
if !pathExists(filepath) {
return nil, os.ErrNotExist
}

return img, nil
return decodeFromFile(filepath)
}

// Resizes an image to the specified width and height.
Expand All @@ -51,7 +48,8 @@ func Resize(image image.Image, width, height int) image.Image {
return image
}

func DecodeFromUrl(url string) (image.Image, error) {
// execute a GET request to the provided URL and decode the image
func decodeFromUrl(url string) (image.Image, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
Expand All @@ -66,6 +64,29 @@ func DecodeFromUrl(url string) (image.Image, error) {
return img, nil
}

func IsUrl(path string) bool {
// open a file and decode the image
func decodeFromFile(filepath string) (image.Image, error) {
file, err := os.Open(filepath)
if err != nil {
return nil, err
}
defer file.Close()

img, _, err := image.Decode(file)
if err != nil {
return nil, err
}

return img, nil
}

// check if the path is a URL
func isUrl(path string) bool {
return path[:4] == "http"
}

// check if a file exists at the provided path
func pathExists(path string) bool {
_, err := os.Stat(path)
return !os.IsNotExist(err)
}

0 comments on commit 8267745

Please sign in to comment.