-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrm.go
91 lines (87 loc) · 2.16 KB
/
rm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package ma
import (
"fmt"
"github.com/bzimmer/smugmug"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
)
func rm(c *cli.Context) error {
dryrun := c.Bool("dryrun")
albumKey := c.String("album")
var (
res bool
err error
images map[string]*smugmug.Image
)
for i := 0; i < c.NArg(); i++ {
id := c.Args().Get(i)
if !imageRE.MatchString(id) {
if images == nil {
_, images, err = existing(c, func(img *smugmug.Image) string {
return img.ImageKey
})
if err != nil {
return err
}
}
image, ok := images[id]
if !ok {
return &InvalidVersionError{ImageKey: id}
}
id = fmt.Sprintf("%s-%d", id, image.Serial)
}
if dryrun {
res = false
runtime(c).Metrics.IncrCounter([]string{"rm", c.Command.Name, "dryrun"}, 1)
} else {
runtime(c).Metrics.IncrCounter([]string{"rm", c.Command.Name, "attempt"}, 1)
res, err = runtime(c).Smugmug().Image.Delete(c.Context, albumKey, id)
if err != nil {
runtime(c).Metrics.IncrCounter([]string{"rm", c.Command.Name, "failure"}, 1)
return err
}
runtime(c).Metrics.IncrCounter([]string{"rm", c.Command.Name, "success"}, 1)
}
if err = runtime(c).Encoder.Encode(map[string]any{
"AlbumKey": albumKey,
"ImageKey": id,
"Status": res,
}); err != nil {
return err
}
log.Info().Str("albumKey", albumKey).Str("imageKey", id).Msg("delete")
}
return nil
}
func CommandRemove() *cli.Command {
return &cli.Command{
Name: "rm",
HelpName: "rm",
Usage: "Delete an entity",
Description: "Delete an entity",
Subcommands: []*cli.Command{
{
Name: "image",
HelpName: "image",
Usage: "delete an image from an album",
Description: "delete an image from an album",
ArgsUsage: "<image key> [<image key>, ...]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "album",
Required: true,
Usage: "the album from which the image is to be deleted",
},
&cli.BoolFlag{
Name: "dryrun",
Usage: "prepare to upload but don't actually do it",
Aliases: []string{"n"},
Value: false,
Required: false,
},
},
Action: rm,
},
},
}
}