-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathstate_provider.go
102 lines (88 loc) · 2.62 KB
/
state_provider.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/emicklei/tre"
"github.com/urfave/cli"
)
// StateProvider knowns how to load state.
type StateProvider interface {
// LoadState returns the last applied migration
LoadState() (string, error)
SaveState(filename string) error
Config() Config
}
// FileStateProvider use a local file to store state (last migration applied).
type FileStateProvider struct {
Configuration Config
tempDir string
}
// for testing
var osTempDir = os.TempDir
func NewFileStateProvider(c Config) FileStateProvider {
return FileStateProvider{
Configuration: c,
tempDir: osTempDir(),
}
}
func (l FileStateProvider) stateFilename() string {
return filepath.Join(l.tempDir, l.Configuration.LastMigrationObjectName)
}
// LoadState implements StateProvider
func (l FileStateProvider) LoadState() (string, error) {
if l.Configuration.verbose {
d, _ := os.Getwd()
log.Println("reading local copy", l.stateFilename(), ",cwd=", d)
}
data, err := ioutil.ReadFile(l.stateFilename())
return string(data), tre.New(err, "error reading state", "tempDir", l.tempDir, "lastMigration", l.Configuration.LastMigrationObjectName)
}
// SaveState implements StateProvider
func (l FileStateProvider) SaveState(filename string) error {
if l.Configuration.verbose {
d, _ := os.Getwd()
log.Println("writing local copy", l.stateFilename(), ",cwd=", d)
}
return ioutil.WriteFile(l.stateFilename(), []byte(filename), os.ModePerm)
}
// Config implements StateProvider
func (l FileStateProvider) Config() Config {
return l.Configuration
}
// for testing
var osRemove = os.Remove
// DeleteState implements StateProvider
func (l FileStateProvider) DeleteState() {
if l.Configuration.verbose {
d, _ := os.Getwd()
log.Println("deleting local copy", l.stateFilename(), ",cwd=", d)
}
osRemove(l.stateFilename())
}
// read it once
var currentStateProvider StateProvider
func getStateProvider(c *cli.Context) (StateProvider, error) {
if currentStateProvider != nil {
return currentStateProvider, nil
}
verbose := c.GlobalBool("v")
pathToConfig := c.Args().First()
cfg, err := TryToLoadConfig(pathToConfig)
if verbose && err == nil {
abs, _ := filepath.Abs(cfg.filename)
log.Println("loading configuration from", abs)
}
if err != nil {
workdir, _ := os.Getwd()
abs := "?"
if cfg != nil {
abs, _ = filepath.Abs(cfg.filename)
}
return currentStateProvider, tre.New(err, "error loading configuration (did you init?)", "path", pathToConfig, "workdir", workdir, "location", abs)
}
cfg.verbose = cfg.verbose || verbose
currentStateProvider = NewGCS(*cfg)
return currentStateProvider, nil
}