@@ -14,41 +14,52 @@ package commands
14
14
15
15
import (
16
16
"fmt"
17
- . "github.com/akrabat/rodeo/internal"
18
- "github.com/spf13/cobra"
19
17
"os"
20
18
"os/exec"
21
19
"path/filepath"
22
20
"strings"
21
+
22
+ . "github.com/akrabat/rodeo/internal"
23
+ "github.com/spf13/cobra"
23
24
)
24
25
25
26
func init () {
26
27
rootCmd .AddCommand (resizeCmd )
28
+
29
+ // Register command line options
30
+ resizeCmd .Flags ().BoolP ("quiet" , "q" , false , "Just print name of resized file on completion" )
27
31
}
28
32
29
33
// resizeCmd displays info about the image file
30
34
var resizeCmd = & cobra.Command {
31
35
Use : "resize <files>..." ,
32
36
Short : "Resize files for use on the web" ,
33
- Long : "Resize files for use on the web" ,
37
+ Long : "Resize files for use on the web" ,
34
38
Run : func (cmd * cobra.Command , args []string ) {
35
39
36
40
if len (args ) == 0 {
37
41
fmt .Println ("Error: At least one file must be specified." )
38
42
os .Exit (2 )
39
43
}
40
44
45
+ // Read the value of --quiet (if it is missing, the value is false)
46
+ quiet , err := cmd .Flags ().GetBool ("quiet" )
47
+ if err != nil {
48
+ quiet = false
49
+ }
50
+
41
51
for _ , filename := range args {
42
- resize (filename )
52
+ resize (filename , quiet )
43
53
}
44
54
},
45
55
}
46
56
47
57
// Resize image using ImageMagick's convert
48
58
//
49
59
// Example:
50
- // convert foo.jpg -scale 2000x2000 -interpolate catrom -quality 75 foo-web.jpeg
51
- func resize (filename string ) {
60
+ //
61
+ // convert foo.jpg -scale 2000x2000 -interpolate catrom -quality 75 foo-web.jpeg
62
+ func resize (filename string , quiet bool ) {
52
63
53
64
config := GetConfig ()
54
65
convert := config .Cmd .Convert
@@ -74,12 +85,18 @@ func resize(filename string) {
74
85
parameters = append (parameters , quality )
75
86
parameters = append (parameters , newFilename )
76
87
77
- fmt .Printf ("Resizing to %s at %s%% quality\n " , scale , quality )
88
+ if ! quiet {
89
+ fmt .Printf ("Resizing to %s at %s%% quality\n " , scale , quality )
90
+ }
78
91
cmd := exec .Command (convert , parameters ... )
79
92
cmd .Dir = filepath .Dir (filename )
80
93
if err := cmd .Run (); err != nil {
81
94
fmt .Println ("Error: " , err )
82
95
os .Exit (1 )
83
96
}
84
- fmt .Printf (" Saved %v\n " , newFilename )
85
- }
97
+ if quiet {
98
+ fmt .Printf ("%v\n " , newFilename )
99
+ } else {
100
+ fmt .Printf (" Saved %v\n " , newFilename )
101
+ }
102
+ }
0 commit comments