@@ -29,17 +29,17 @@ var (
29
29
)
30
30
31
31
type Github struct {
32
- client * github.Client
33
- url string
34
-
35
- pathOwner string
36
- pathRepo string
37
- path string
32
+ config * Config
33
+ client * github.Client
38
34
options * github.RepositoryContentGetOptions
39
35
migrations * source.Migrations
40
36
}
41
37
42
38
type Config struct {
39
+ Owner string
40
+ Repo string
41
+ Path string
42
+ Ref string
43
43
}
44
44
45
45
func (g * Github ) Open (url string ) (source.Driver , error ) {
@@ -64,20 +64,21 @@ func (g *Github) Open(url string) (source.Driver, error) {
64
64
65
65
gn := & Github {
66
66
client : github .NewClient (tr .Client ()),
67
- url : url ,
68
67
migrations : source .NewMigrations (),
69
68
options : & github.RepositoryContentGetOptions {Ref : u .Fragment },
70
69
}
71
70
71
+ gn .ensureFields ()
72
+
72
73
// set owner, repo and path in repo
73
- gn .pathOwner = u .Host
74
+ gn .config . Owner = u .Host
74
75
pe := strings .Split (strings .Trim (u .Path , "/" ), "/" )
75
76
if len (pe ) < 1 {
76
77
return nil , ErrInvalidRepo
77
78
}
78
- gn .pathRepo = pe [0 ]
79
+ gn .config . Repo = pe [0 ]
79
80
if len (pe ) > 1 {
80
- gn .path = strings .Join (pe [1 :], "/" )
81
+ gn .config . Path = strings .Join (pe [1 :], "/" )
81
82
}
82
83
83
84
if err := gn .readDirectory (); err != nil {
@@ -90,16 +91,29 @@ func (g *Github) Open(url string) (source.Driver, error) {
90
91
func WithInstance (client * github.Client , config * Config ) (source.Driver , error ) {
91
92
gn := & Github {
92
93
client : client ,
94
+ config : config ,
93
95
migrations : source .NewMigrations (),
96
+ options : & github.RepositoryContentGetOptions {Ref : config .Ref },
94
97
}
98
+
95
99
if err := gn .readDirectory (); err != nil {
96
100
return nil , err
97
101
}
102
+
98
103
return gn , nil
99
104
}
100
105
101
106
func (g * Github ) readDirectory () error {
102
- fileContent , dirContents , _ , err := g .client .Repositories .GetContents (context .Background (), g .pathOwner , g .pathRepo , g .path , g .options )
107
+ g .ensureFields ()
108
+
109
+ fileContent , dirContents , _ , err := g .client .Repositories .GetContents (
110
+ context .Background (),
111
+ g .config .Owner ,
112
+ g .config .Repo ,
113
+ g .config .Path ,
114
+ g .options ,
115
+ )
116
+
103
117
if err != nil {
104
118
return err
105
119
}
@@ -120,37 +134,58 @@ func (g *Github) readDirectory() error {
120
134
return nil
121
135
}
122
136
137
+ func (g * Github ) ensureFields () {
138
+ if g .config == nil {
139
+ g .config = & Config {}
140
+ }
141
+ }
142
+
123
143
func (g * Github ) Close () error {
124
144
return nil
125
145
}
126
146
127
147
func (g * Github ) First () (version uint , er error ) {
148
+ g .ensureFields ()
149
+
128
150
if v , ok := g .migrations .First (); ! ok {
129
- return 0 , & os.PathError {Op : "first" , Path : g .path , Err : os .ErrNotExist }
151
+ return 0 , & os.PathError {Op : "first" , Path : g .config . Path , Err : os .ErrNotExist }
130
152
} else {
131
153
return v , nil
132
154
}
133
155
}
134
156
135
157
func (g * Github ) Prev (version uint ) (prevVersion uint , err error ) {
158
+ g .ensureFields ()
159
+
136
160
if v , ok := g .migrations .Prev (version ); ! ok {
137
- return 0 , & os.PathError {Op : fmt .Sprintf ("prev for version %v" , version ), Path : g .path , Err : os .ErrNotExist }
161
+ return 0 , & os.PathError {Op : fmt .Sprintf ("prev for version %v" , version ), Path : g .config . Path , Err : os .ErrNotExist }
138
162
} else {
139
163
return v , nil
140
164
}
141
165
}
142
166
143
167
func (g * Github ) Next (version uint ) (nextVersion uint , err error ) {
168
+ g .ensureFields ()
169
+
144
170
if v , ok := g .migrations .Next (version ); ! ok {
145
- return 0 , & os.PathError {Op : fmt .Sprintf ("next for version %v" , version ), Path : g .path , Err : os .ErrNotExist }
171
+ return 0 , & os.PathError {Op : fmt .Sprintf ("next for version %v" , version ), Path : g .config . Path , Err : os .ErrNotExist }
146
172
} else {
147
173
return v , nil
148
174
}
149
175
}
150
176
151
177
func (g * Github ) ReadUp (version uint ) (r io.ReadCloser , identifier string , err error ) {
178
+ g .ensureFields ()
179
+
152
180
if m , ok := g .migrations .Up (version ); ok {
153
- file , _ , _ , err := g .client .Repositories .GetContents (context .Background (), g .pathOwner , g .pathRepo , path .Join (g .path , m .Raw ), g .options )
181
+ file , _ , _ , err := g .client .Repositories .GetContents (
182
+ context .Background (),
183
+ g .config .Owner ,
184
+ g .config .Repo ,
185
+ path .Join (g .config .Path , m .Raw ),
186
+ g .options ,
187
+ )
188
+
154
189
if err != nil {
155
190
return nil , "" , err
156
191
}
@@ -162,12 +197,21 @@ func (g *Github) ReadUp(version uint) (r io.ReadCloser, identifier string, err e
162
197
return ioutil .NopCloser (strings .NewReader (r )), m .Identifier , nil
163
198
}
164
199
}
165
- return nil , "" , & os.PathError {Op : fmt .Sprintf ("read version %v" , version ), Path : g .path , Err : os .ErrNotExist }
200
+ return nil , "" , & os.PathError {Op : fmt .Sprintf ("read version %v" , version ), Path : g .config . Path , Err : os .ErrNotExist }
166
201
}
167
202
168
203
func (g * Github ) ReadDown (version uint ) (r io.ReadCloser , identifier string , err error ) {
204
+ g .ensureFields ()
205
+
169
206
if m , ok := g .migrations .Down (version ); ok {
170
- file , _ , _ , err := g .client .Repositories .GetContents (context .Background (), g .pathOwner , g .pathRepo , path .Join (g .path , m .Raw ), g .options )
207
+ file , _ , _ , err := g .client .Repositories .GetContents (
208
+ context .Background (),
209
+ g .config .Owner ,
210
+ g .config .Repo ,
211
+ path .Join (g .config .Path , m .Raw ),
212
+ g .options ,
213
+ )
214
+
171
215
if err != nil {
172
216
return nil , "" , err
173
217
}
@@ -179,5 +223,5 @@ func (g *Github) ReadDown(version uint) (r io.ReadCloser, identifier string, err
179
223
return ioutil .NopCloser (strings .NewReader (r )), m .Identifier , nil
180
224
}
181
225
}
182
- return nil , "" , & os.PathError {Op : fmt .Sprintf ("read version %v" , version ), Path : g .path , Err : os .ErrNotExist }
226
+ return nil , "" , & os.PathError {Op : fmt .Sprintf ("read version %v" , version ), Path : g .config . Path , Err : os .ErrNotExist }
183
227
}
0 commit comments