-
Notifications
You must be signed in to change notification settings - Fork 38
/
migration_path.go
59 lines (51 loc) · 1.68 KB
/
migration_path.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
package cli
import (
"fmt"
"os"
"strings"
"github.com/pkg/errors"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
const (
// MigrationPathFlag is the migration path flag used for finding files to migrate the DB
MigrationPathFlag string = "migration-path"
)
var (
errMissingMigrationPath = errors.New("missing migration path, expected to be set")
)
type errInvalidMigrationPath struct {
Path string
}
func (e *errInvalidMigrationPath) Error() string {
return fmt.Sprintf("invalid migration path %q", e.Path)
}
// InitMigrationPathFlags initializes the Migration command line flags
func InitMigrationPathFlags(flag *pflag.FlagSet) {
flag.StringP(MigrationPathFlag, "p", "file:///migrations/app/schema", "Path to the migrations folder")
}
// CheckMigrationPath validates migration command line flags
func CheckMigrationPath(v *viper.Viper) error {
// Remove any extra quotes around path
migrationPath := strings.Trim(v.GetString(MigrationPathFlag), "\"")
if len(migrationPath) == 0 {
return errMissingMigrationPath
}
for _, p := range strings.Split(migrationPath, ";") {
if len(p) == 0 {
continue
}
if strings.HasPrefix(p, "file://") {
filesystemPath := p[len("file://"):]
if _, err := os.Stat(filesystemPath); os.IsNotExist(err) {
return errors.Wrapf(&errInvalidMigrationPath{Path: filesystemPath}, "Expected %s to be a path in the filesystem", filesystemPath)
}
} else if !strings.HasPrefix(p, "s3://") {
return errors.Wrapf(&errInvalidMigrationPath{Path: p}, "Expected %s to have prefix file:// or s3://", p)
}
if strings.HasSuffix(p, "/") {
return errors.Wrapf(&errInvalidMigrationPath{Path: p}, "Path %s Cannot end in slash", p)
}
}
return nil
}