-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
backup.go
43 lines (35 loc) · 853 Bytes
/
backup.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
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"github.com/guumaster/hostctl/pkg/host/file"
)
func newBackupCmd() *cobra.Command {
return &cobra.Command{
Use: "backup [flags]",
Short: "Creates a backup copy of your hosts file",
Long: `
Creates a backup copy of your hosts file with the date in .YYYYMMDD
as extension.
`,
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
src, _ := cmd.Flags().GetString("host-file")
dst, _ := cmd.Flags().GetString("path")
quiet, _ := cmd.Flags().GetBool("quiet")
h, err := file.NewFile(src)
if err != nil {
return err
}
fname, err := h.Backup(dst)
if err != nil {
return err
}
if !quiet {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Backup '%s' created.\n", fname)
}
return nil
},
PostRunE: postRunListOnly,
}
}