-
Notifications
You must be signed in to change notification settings - Fork 38
/
migration_gen_path.go
44 lines (36 loc) · 1.2 KB
/
migration_gen_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
package cli
import (
"fmt"
"os"
"github.com/pkg/errors"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
const (
// MigrationGenPathFlag is the migration path flag used for generating new migrations
MigrationGenPathFlag string = "migration-gen-path"
)
var (
errMissingMigrationGenPath = errors.New("missing migration path, expected to be set")
)
type errInvalidMigrationGenPath struct {
Path string
}
func (e *errInvalidMigrationGenPath) Error() string {
return fmt.Sprintf("invalid migration file path %q", e.Path)
}
// InitMigrationGenPathFlags initializes the Migration command line flags
func InitMigrationGenPathFlags(flag *pflag.FlagSet) {
flag.StringP(MigrationGenPathFlag, "p", "./migrations/app/schema", "Path to the migrations folder")
}
// CheckMigrationGenPath validates migration command line flags
func CheckMigrationGenPath(v *viper.Viper) error {
migrationGenPath := v.GetString(MigrationGenPathFlag)
if len(migrationGenPath) == 0 {
return errMissingMigrationGenPath
}
if _, err := os.Stat(migrationGenPath); os.IsNotExist(err) {
return errors.Wrapf(&errInvalidMigrationGenPath{Path: migrationGenPath}, "Expected %s to be a path in the filesystem", migrationGenPath)
}
return nil
}