This repository has been archived by the owner on Mar 2, 2020. It is now read-only.
/
migrate.go
133 lines (116 loc) · 2.61 KB
/
migrate.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// package migrate transforms a repository from a given vendor schema to
// the vendor folder schema.
package migrate
import (
"errors"
"fmt"
"os"
"path/filepath"
"sort"
)
type ErrNoSuchSystem struct {
NotExist string
Has []string
}
func (err ErrNoSuchSystem) Error() string {
return fmt.Sprintf("Migration system for %q doesn't exist. Current systems %q.", err.NotExist, err.Has)
}
// From is the current vendor schema.
type From string
// Migrate from the given system using the current working directory.
func MigrateWD(from From) error {
wd, err := os.Getwd()
if err != nil {
return err
}
return Migrate(from, wd)
}
// SystemList list available migration systems.
func SystemList() []string {
list := make([]string, 0, len(registered))
for key := range registered {
list = append(list, string(key))
}
sort.Strings(list)
return list
}
// Migrate from the given system using the given root.
func Migrate(from From, root string) error {
sys, found := registered[from]
if !found {
return ErrNoSuchSystem{
NotExist: string(from),
Has: SystemList(),
}
}
sys, err := sys.Check(root)
if err != nil {
return err
}
if sys == nil {
return errors.New("Root not found.")
}
return sys.Migrate(root)
}
type system interface {
Check(root string) (system, error)
Migrate(root string) error
}
func register(name From, sys system) {
_, found := registered[name]
if found {
panic("system " + name + " already registered.")
}
registered[name] = sys
}
var registered = make(map[From]system, 10)
var errAutoSystemNotFound = errors.New("Unable to determine vendor system.")
func init() {
register("auto", sysAuto{})
}
type sysAuto struct{}
func (auto sysAuto) Check(root string) (system, error) {
for _, sys := range registered {
if sys == auto {
continue
}
out, err := sys.Check(root)
if err != nil {
return nil, err
}
if out != nil {
return out, nil
}
}
return nil, errAutoSystemNotFound
}
func (sysAuto) Migrate(root string) error {
return errors.New("Auto.Migrate shouldn't be called")
}
func hasDirs(root string, dd ...string) bool {
for _, d := range dd {
fi, err := os.Stat(filepath.Join(root, d))
if err != nil {
return false
}
if fi.IsDir() == false {
return false
}
}
return true
}
func hasFiles(root string, dd ...string) bool {
for _, d := range dd {
fi, err := os.Stat(filepath.Join(root, d))
if err != nil {
return false
}
if fi.IsDir() == true {
return false
}
}
return true
}