Skip to content

Commit

Permalink
Merge pull request #47 from akrabat/46-filename-only-on-resize
Browse files Browse the repository at this point in the history
Add quiet flag to resize
  • Loading branch information
akrabat committed Aug 14, 2023
2 parents bd47ca1 + 0a95b91 commit 4c43663
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions commands/resize.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,52 @@ package commands

import (
"fmt"
. "github.com/akrabat/rodeo/internal"
"github.com/spf13/cobra"
"os"
"os/exec"
"path/filepath"
"strings"

. "github.com/akrabat/rodeo/internal"
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(resizeCmd)

// Register command line options
resizeCmd.Flags().BoolP("quiet", "q", false, "Just print name of resized file on completion")
}

// resizeCmd displays info about the image file
var resizeCmd = &cobra.Command{
Use: "resize <files>...",
Short: "Resize files for use on the web",
Long: "Resize files for use on the web",
Long: "Resize files for use on the web",
Run: func(cmd *cobra.Command, args []string) {

if len(args) == 0 {
fmt.Println("Error: At least one file must be specified.")
os.Exit(2)
}

// Read the value of --quiet (if it is missing, the value is false)
quiet, err := cmd.Flags().GetBool("quiet")
if err != nil {
quiet = false
}

for _, filename := range args {
resize(filename)
resize(filename, quiet)
}
},
}

// Resize image using ImageMagick's convert
//
// Example:
// convert foo.jpg -scale 2000x2000 -interpolate catrom -quality 75 foo-web.jpeg
func resize(filename string) {
//
// convert foo.jpg -scale 2000x2000 -interpolate catrom -quality 75 foo-web.jpeg
func resize(filename string, quiet bool) {

config := GetConfig()
convert := config.Cmd.Convert
Expand All @@ -74,12 +85,18 @@ func resize(filename string) {
parameters = append(parameters, quality)
parameters = append(parameters, newFilename)

fmt.Printf("Resizing to %s at %s%% quality\n", scale, quality)
if !quiet {
fmt.Printf("Resizing to %s at %s%% quality\n", scale, quality)
}
cmd := exec.Command(convert, parameters...)
cmd.Dir = filepath.Dir(filename)
if err := cmd.Run(); err != nil {
fmt.Println("Error: ", err)
os.Exit(1)
}
fmt.Printf(" Saved %v\n", newFilename)
}
if quiet {
fmt.Printf("%v\n", newFilename)
} else {
fmt.Printf(" Saved %v\n", newFilename)
}
}

0 comments on commit 4c43663

Please sign in to comment.