|
1 | 1 | package main
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "flag" |
5 | 4 | "fmt"
|
6 | 5 | "os"
|
7 | 6 |
|
8 | 7 | "github.com/otiai10/copy"
|
| 8 | + "github.com/spf13/cobra" |
9 | 9 | )
|
10 | 10 |
|
11 | 11 | 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 | +} |
17 | 15 |
|
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 | + } |
29 | 33 |
|
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 | + }, |
42 | 45 | }
|
| 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 |
43 | 55 | }
|
0 commit comments