Skip to content

Commit

Permalink
Add output flag images (#20)
Browse files Browse the repository at this point in the history
* add flags for images width and height

* update command name to imageCmd

Signed-off-by: Pedro Tashima <[email protected]>

* add output flag to images command

* make directory command inside run images command

* add error validation for make directory

Signed-off-by: Pedro Tashima <[email protected]>
Signed-off-by: Enrico Candino <[email protected]>
Co-authored-by: Pedro Tashima <[email protected]>
Co-authored-by: Enrico Candino <[email protected]>
  • Loading branch information
3 people authored Oct 25, 2022
1 parent 30d1143 commit 1fcc29f
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion internal/cli/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"net/http"
"os"
"time"

"github.com/enrichman/stegosecrets/pkg/file"
Expand All @@ -13,6 +14,7 @@ import (
var (
width uint16
height uint16
output string
)

func newImagesCmd() *cobra.Command {
Expand All @@ -25,13 +27,20 @@ func newImagesCmd() *cobra.Command {

imagesCmd.Flags().Uint16Var(&width, "width", 900, "width")
imagesCmd.Flags().Uint16Var(&height, "height", 600, "height")
imagesCmd.Flags().StringVarP(&output, "output", "o", "images", "output directory")

return imagesCmd
}

var client = http.Client{Timeout: 30 * time.Second}

func runImagesCmd(cmd *cobra.Command, args []string) error {
// creates the output folder if it doesn't exists
err := os.MkdirAll(output, 0755)
if err != nil {
return err
}

for i := 1; i <= 10; i++ {
resp, err := client.Get(fmt.Sprintf("https://picsum.photos/%d/%d", width, height))
if err != nil {
Expand All @@ -44,7 +53,7 @@ func runImagesCmd(cmd *cobra.Command, args []string) error {
return err
}

err = file.WriteFile(bb, fmt.Sprintf("images/%d.jpg", i))
err = file.WriteFile(bb, fmt.Sprintf("%s/%d.jpg", output, i))
if err != nil {
return err
}
Expand Down

0 comments on commit 1fcc29f

Please sign in to comment.