forked from emicklei/gmig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcs.go
68 lines (60 loc) · 1.7 KB
/
gcs.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
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"log"
"os/exec"
"path/filepath"
"strings"
"github.com/emicklei/tre"
)
// GCS = Google Cloud Storage
type GCS struct {
onDiskAccess FileStateProvider
}
// NewGCS returns a new GCS
func NewGCS(cfg Config) GCS {
return GCS{onDiskAccess: NewFileStateProvider(cfg)}
}
// LoadState implements StateProvider
func (g GCS) LoadState() (string, error) {
defer g.onDiskAccess.DeleteState()
cmdline := []string{"gsutil", "-q", "cp",
"gs://" + filepath.Join(g.Config().Bucket, g.Config().LastMigrationObjectName),
g.onDiskAccess.stateFilename()}
if err := g.gsutil(cmdline); err != nil {
// see if there was no last applied state
if strings.Contains(err.Error(), "No URLs matched") { // lame detection method TODO
if g.Config().verbose {
log.Println("no last applied migration found.")
}
return "", nil
}
return "", err
}
return g.onDiskAccess.LoadState()
}
// SaveState implements StateProvider
func (g GCS) SaveState(filename string) error {
defer g.onDiskAccess.DeleteState()
if err := g.onDiskAccess.SaveState(filename); err != nil {
return err
}
cmdline := []string{"gsutil", "-q", "-h", "Content-Type:text/plain", "cp",
g.onDiskAccess.stateFilename(),
"gs://" + filepath.Join(g.Config().Bucket, g.Config().LastMigrationObjectName)}
return g.gsutil(cmdline)
}
// Config implements StateProvider
func (g GCS) Config() Config {
return g.onDiskAccess.Config()
}
func (g GCS) gsutil(cmdline []string) error {
if g.Config().verbose {
log.Println(strings.Join(cmdline, " "))
}
cmd := exec.Command(cmdline[0], cmdline[1:]...)
stdoutStderr, err := runCommand(cmd)
if err != nil {
return tre.New(err, "gsutil cp failed", "output:", string(stdoutStderr))
}
return nil
}