1- package main
1+ package commands
22
33import (
44 "fmt"
99 "github.com/urfave/cli"
1010)
1111
12- type DataBackup struct {}
12+ type DataBackup struct {
13+ BaseCommand
14+ }
1315
1416func (cmd * DataBackup ) Commands () cli.Command {
1517 return cli.Command {
@@ -27,43 +29,44 @@ func (cmd *DataBackup) Commands() cli.Command {
2729 Usage : "Specify the local directory to store the backup zip." ,
2830 },
2931 },
32+ Before : cmd .Before ,
3033 Action : cmd .Run ,
3134 }
3235}
3336
3437func (cmd * DataBackup ) Run (c * cli.Context ) error {
35- if ! machine .Exists () {
36- out .Error .Fatalf ("No machine named '%s' exists." , machine .Name )
38+ if ! cmd . machine .Exists () {
39+ cmd . out .Error .Fatalf ("No machine named '%s' exists." , cmd . machine .Name )
3740 }
3841
3942 dataDir := c .String ("data-dir" )
4043 backupDir := c .String ("backup-dir" )
41- backupFile := fmt .Sprintf ("%s%c%s.tgz" , backupDir , os .PathSeparator , machine .Name )
44+ backupFile := fmt .Sprintf ("%s%c%s.tgz" , backupDir , os .PathSeparator , cmd . machine .Name )
4245 if _ , err := os .Stat (backupDir ); err != nil {
43- out .Info .Printf ("Creating backup directory: %s..." , backupDir )
46+ cmd . out .Info .Printf ("Creating backup directory: %s..." , backupDir )
4447 if mkdirErr := exec .Command ("mkdir" , "-p" , backupDir ).Run (); mkdirErr != nil {
45- out .Error .Println (mkdirErr )
46- out .Error .Fatalf ("Could not create backup directory %s" , backupDir )
48+ cmd . out .Error .Println (mkdirErr )
49+ cmd . out .Error .Fatalf ("Could not create backup directory %s" , backupDir )
4750 }
4851 } else if _ , err := os .Stat (backupFile ); err == nil {
4952 // If the backup dir already exists, make sure the backup file does not exist.
50- out .Error .Fatalf ("Backup archive %s already exists." , backupFile )
53+ cmd . out .Error .Fatalf ("Backup archive %s already exists." , backupFile )
5154 }
5255
53- out .Info .Printf ("Backing up %s on '%s' to %s..." , dataDir , machine .Name , backupFile )
56+ cmd . out .Info .Printf ("Backing up %s on '%s' to %s..." , dataDir , cmd . machine .Name , backupFile )
5457
5558 // Stream the archive to stdout and capture it in a local file so we don't waste
5659 // space storing an archive on the VM filesystem. There may not be enough space.
5760 archiveCmd := fmt .Sprintf ("sudo tar czf - -C %s ." , dataDir )
58- backup := exec .Command ("docker-machine" , "ssh" , machine .Name , archiveCmd , ">" , backupFile )
61+ backup := exec .Command ("docker-machine" , "ssh" , cmd . machine .Name , archiveCmd , ">" , backupFile )
5962 backup .Stderr = os .Stderr
6063
6164 color .Set (color .FgCyan )
6265 err := backup .Run ()
6366 color .Unset ()
6467
6568 if err != nil {
66- out .Warning .Println ("There may have been problems. See above for any errors" )
69+ cmd . out .Warning .Println ("There may have been problems. See above for any errors" )
6770 }
6871
6972 return nil
0 commit comments