44 "flag"
55 "fmt"
66 "os"
7+ "os/exec"
78 "strings"
89
910 "github.com/sourcegraph/sourcegraph/lib/errors"
@@ -18,7 +19,7 @@ func init() {
1819Note that these commands are intended for use as reference - you may need to adjust the commands for your deployment.
1920
2021USAGE
21- src [-v] snapshot databases <pg_dump|docker|kubectl> [--targets=<docker|k8s|"targets.yaml">]
22+ src [-v] snapshot databases [--targets=<docker|k8s|"targets.yaml">] [--run] <pg_dump|docker|kubectl>
2223
2324TARGETS FILES
2425 Predefined targets are available based on default Sourcegraph configurations ('docker', 'k8s').
@@ -38,6 +39,7 @@ TARGETS FILES
3839`
3940 flagSet := flag .NewFlagSet ("databases" , flag .ExitOnError )
4041 targetsKeyFlag := flagSet .String ("targets" , "auto" , "predefined targets ('docker' or 'k8s'), or a custom targets.yaml file" )
42+ run := flagSet .Bool ("run" , false , "Automatically run the commands" )
4143
4244 snapshotCommands = append (snapshotCommands , & command {
4345 flagSet : flagSet ,
@@ -47,35 +49,13 @@ TARGETS FILES
4749 }
4850 out := output .NewOutput (flagSet .Output (), output.OutputOpts {Verbose : * verbose })
4951
50- var builder string
51- if len (args ) > 0 {
52- builder = args [0 ]
53- }
52+ builder := flagSet .Arg (0 )
5453
55- targetKey := "docker"
56- var commandBuilder pgdump.CommandBuilder
57- switch builder {
58- case "pg_dump" , "" :
59- targetKey = "local"
60- commandBuilder = func (t pgdump.Target ) (string , error ) {
61- cmd := pgdump .Command (t )
62- if t .Target != "" {
63- return fmt .Sprintf ("%s --host=%s" , cmd , t .Target ), nil
64- }
65- return cmd , nil
66- }
67- case "docker" :
68- commandBuilder = func (t pgdump.Target ) (string , error ) {
69- return fmt .Sprintf ("docker exec -it %s sh -c '%s'" , t .Target , pgdump .Command (t )), nil
70- }
71- case "kubectl" :
72- targetKey = "k8s"
73- commandBuilder = func (t pgdump.Target ) (string , error ) {
74- return fmt .Sprintf ("kubectl exec -it %s -- bash -c '%s'" , t .Target , pgdump .Command (t )), nil
75- }
76- default :
54+ commandBuilder , targetKey := pgdump .Builder (builder , pgdump .DumpCommand )
55+ if targetKey == "" {
7756 return errors .Newf ("unknown or invalid template type %q" , builder )
7857 }
58+
7959 if * targetsKeyFlag != "auto" {
8060 targetKey = * targetsKeyFlag
8161 }
@@ -94,19 +74,32 @@ TARGETS FILES
9474 out .WriteLine (output .Emojif (output .EmojiInfo , "Using predefined targets for %s environments" , targetKey ))
9575 }
9676
97- commands , err := pgdump .BuildCommands (srcSnapshotDir , commandBuilder , targets )
77+ commands , err := pgdump .BuildCommands (srcSnapshotDir , commandBuilder , targets , true )
9878 if err != nil {
9979 return errors .Wrap (err , "failed to build commands" )
10080 }
10181
10282 _ = os .MkdirAll (srcSnapshotDir , os .ModePerm )
10383
104- b := out .Block (output .Emoji (output .EmojiSuccess , "Run these commands to generate the required database dumps:" ))
105- b .Write ("\n " + strings .Join (commands , "\n " ))
106- b .Close ()
84+ if * run {
85+ for _ , c := range commands {
86+ out .WriteLine (output .Emojif (output .EmojiInfo , "Running command: %q" , c ))
87+ command := exec .Command ("bash" , "-c" , c )
88+ output , err := command .CombinedOutput ()
89+ out .Write (string (output ))
90+ if err != nil {
91+ return errors .Wrapf (err , "failed to run command: %q" , c )
92+ }
93+ }
10794
108- out .WriteLine (output .Styledf (output .StyleSuggestion , "Note that you may need to do some additional setup, such as authentication, beforehand." ))
95+ out .WriteLine (output .Emoji (output .EmojiSuccess , "Successfully completed dump commands" ))
96+ } else {
97+ b := out .Block (output .Emoji (output .EmojiSuccess , "Run these commands to generate the required database dumps:" ))
98+ b .Write ("\n " + strings .Join (commands , "\n " ))
99+ b .Close ()
109100
101+ out .WriteLine (output .Styledf (output .StyleSuggestion , "Note that you may need to do some additional setup, such as authentication, beforehand." ))
102+ }
110103 return nil
111104 },
112105 usageFunc : func () { fmt .Fprint (flag .CommandLine .Output (), usage ) },
0 commit comments