-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
backup.go
57 lines (47 loc) · 1.15 KB
/
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package cmd
import (
"errors"
"fmt"
"log"
"os"
"github.com/spf13/cobra"
"github.com/guumaster/hostctl/pkg/host"
)
// backupCmd represents the backup command
var backupCmd = &cobra.Command{
Use: "backup",
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.
`,
PreRunE: func(cmd *cobra.Command, args []string) error {
profile, _ := cmd.Flags().GetString("profile")
if profile != "" {
return errors.New("backup can only be done to whole file. remove profile flag")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
src, _ := cmd.Flags().GetString("host-file")
dst, _ := cmd.Flags().GetString("path")
quiet, _ := cmd.Flags().GetBool("quiet")
backupFile, err := host.BackupFile(src, dst)
if err != nil {
return err
}
if quiet {
return nil
}
fmt.Printf("Backup completed.")
return host.ListProfiles(backupFile, &host.ListOptions{})
},
}
func init() {
rootCmd.AddCommand(backupCmd)
cwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
backupCmd.Flags().String("path", cwd, "A path to save the backup")
}