Skip to content

Commit 65ef3b2

Browse files
authored
make cache flags optional for extractContent functionality (#3586)
Signed-off-by: grokspawn <[email protected]>
1 parent cda4c90 commit 65ef3b2

File tree

1 file changed

+41
-29
lines changed

1 file changed

+41
-29
lines changed

cmd/copy-content/main.go

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,55 @@
11
package main
22

33
import (
4-
"flag"
54
"fmt"
65
"os"
76

87
"github.com/otiai10/copy"
8+
"github.com/spf13/cobra"
99
)
1010

1111
func main() {
12-
catalogSource := flag.String("catalog.from", "", "Path to catalog contents to copy.")
13-
catalogDestination := flag.String("catalog.to", "", "Path to where catalog contents should be copied.")
14-
cacheSource := flag.String("cache.from", "", "Path to cache contents to copy.")
15-
cacheDestination := flag.String("cache.to", "", "Path to where cache contents should be copied.")
16-
flag.Parse()
12+
cmd := newCmd()
13+
cmd.Execute()
14+
}
1715

18-
for flagName, value := range map[string]*string{
19-
"catalog.from": catalogSource,
20-
"catalog.to": catalogDestination,
21-
"cache.from": cacheSource,
22-
"cache.to": cacheDestination,
23-
} {
24-
if value == nil || *value == "" {
25-
fmt.Printf("--%s is required", flagName)
26-
os.Exit(1)
27-
}
28-
}
16+
func newCmd() *cobra.Command {
17+
var (
18+
catalogFrom string
19+
catalogTo string
20+
cacheFrom string
21+
cacheTo string
22+
)
23+
cmd := &cobra.Command{
24+
Use: "copy-content",
25+
Short: "Copy catalog and cache content",
26+
Long: `Copy catalog and cache content`,
27+
Run: func(cmd *cobra.Command, args []string) {
28+
var contentMap = make(map[string]string, 2)
29+
contentMap[catalogFrom] = catalogTo
30+
if cmd.Flags().Changed("cache.from") {
31+
contentMap[cacheFrom] = cacheTo
32+
}
2933

30-
for from, to := range map[string]string{
31-
*catalogSource: *catalogDestination,
32-
*cacheSource: *cacheDestination,
33-
} {
34-
if err := os.RemoveAll(to); err != nil {
35-
fmt.Printf("failed to remove %s: %s", to, err)
36-
os.Exit(1)
37-
}
38-
if err := copy.Copy(from, to); err != nil {
39-
fmt.Printf("failed to copy %s to %s: %s\n", from, to, err)
40-
os.Exit(1)
41-
}
34+
for from, to := range contentMap {
35+
if err := os.RemoveAll(to); err != nil {
36+
fmt.Printf("failed to remove %s: %s", to, err)
37+
os.Exit(1)
38+
}
39+
if err := copy.Copy(from, to); err != nil {
40+
fmt.Printf("failed to copy %s to %s: %s\n", from, to, err)
41+
os.Exit(1)
42+
}
43+
}
44+
},
4245
}
46+
47+
cmd.Flags().StringVar(&catalogFrom, "catalog.from", "", "Path to catalog contents to copy")
48+
cmd.Flags().StringVar(&catalogTo, "catalog.to", "", "Path to where catalog contents should be copied")
49+
cmd.Flags().StringVar(&cacheFrom, "cache.from", "", "Path to cache contents to copy (required if cache.to is set)") // optional
50+
cmd.Flags().StringVar(&cacheTo, "cache.to", "", "Path to where cache contents should be copied (required if cache.from is set)") // optional
51+
cmd.MarkFlagRequired("catalog.from")
52+
cmd.MarkFlagRequired("catalog.to")
53+
cmd.MarkFlagsRequiredTogether("cache.from", "cache.to")
54+
return cmd
4355
}

0 commit comments

Comments
 (0)