@@ -19,8 +19,11 @@ import (
19
19
"io/ioutil"
20
20
"os"
21
21
"path/filepath"
22
+ "sort"
22
23
"strings"
23
24
25
+ "gopkg.in/src-d/go-git.v4"
26
+
24
27
ackgenconfig "github.com/aws-controllers-k8s/code-generator/pkg/generate/config"
25
28
"github.com/aws-controllers-k8s/code-generator/pkg/names"
26
29
"github.com/aws-controllers-k8s/code-generator/pkg/util"
@@ -37,13 +40,19 @@ var (
37
40
ErrServiceNotFound = errors .New (
38
41
"no such service" ,
39
42
)
43
+ ErrAPIVersionNotFound = errors .New (
44
+ "no such api version" ,
45
+ )
40
46
)
41
47
42
48
// SDKHelper is a helper struct that helps work with the aws-sdk-go models and
43
49
// API model loader
44
50
type SDKHelper struct {
45
- basePath string
46
- loader * awssdkmodel.Loader
51
+ gitRepository * git.Repository
52
+ basePath string
53
+ loader * awssdkmodel.Loader
54
+ // Default is set by `FirstAPIVersion`
55
+ apiVersion string
47
56
// Default is "services.k8s.aws"
48
57
APIGroupSuffix string
49
58
}
@@ -59,6 +68,29 @@ func NewSDKHelper(basePath string) *SDKHelper {
59
68
}
60
69
}
61
70
71
+ // WithSDKVersion checks out the sdk git repository to the provided version. To use
72
+ // this function h.basePath should point to a git repository.
73
+ func (h * SDKHelper ) WithSDKVersion (version string ) error {
74
+ if h .gitRepository == nil {
75
+ gitRepository , err := util .LoadRepository (h .basePath )
76
+ if err != nil {
77
+ return fmt .Errorf ("error loading repository from %s: %v" , h .basePath , err )
78
+ }
79
+ h .gitRepository = gitRepository
80
+ }
81
+
82
+ err := util .CheckoutRepositoryTag (h .gitRepository , version )
83
+ if err != nil {
84
+ return fmt .Errorf ("cannot checkout tag %s: %v" , version , err )
85
+ }
86
+ return nil
87
+ }
88
+
89
+ // WithAPIVersion sets the `apiVersion` field.
90
+ func (h * SDKHelper ) WithAPIVersion (apiVersion string ) {
91
+ h .apiVersion = apiVersion
92
+ }
93
+
62
94
// API returns the aws-sdk-go API model for a supplied service alias
63
95
func (h * SDKHelper ) API (serviceAlias string ) (* SDKAPI , error ) {
64
96
modelPath , _ , err := h .ModelAndDocsPath (serviceAlias )
@@ -89,40 +121,56 @@ func (h *SDKHelper) API(serviceAlias string) (*SDKAPI, error) {
89
121
func (h * SDKHelper ) ModelAndDocsPath (
90
122
serviceAlias string ,
91
123
) (string , string , error ) {
92
- apiVersion , err := h .APIVersion (serviceAlias )
93
- if err != nil {
94
- return "" , "" , err
124
+ if h .apiVersion == "" {
125
+ apiVersion , err := h .FirstAPIVersion (serviceAlias )
126
+ if err != nil {
127
+ return "" , "" , err
128
+ }
129
+ h .apiVersion = apiVersion
95
130
}
96
131
versionPath := filepath .Join (
97
- h .basePath , "models" , "apis" , serviceAlias , apiVersion ,
132
+ h .basePath , "models" , "apis" , serviceAlias , h . apiVersion ,
98
133
)
99
134
modelPath := filepath .Join (versionPath , "api-2.json" )
100
135
docsPath := filepath .Join (versionPath , "docs-2.json" )
101
136
return modelPath , docsPath , nil
102
137
}
103
138
104
- // APIVersion returns the API version (e.h. "2012-10-03") for a service API
105
- func (h * SDKHelper ) APIVersion (serviceAlias string ) (string , error ) {
139
+ // FirstAPIVersion returns the first found API version for a service API.
140
+ // (e.h. "2012-10-03")
141
+ func (h * SDKHelper ) FirstAPIVersion (serviceAlias string ) (string , error ) {
142
+ versions , err := h .GetAPIVersions (serviceAlias )
143
+ if err != nil {
144
+ return "" , err
145
+ }
146
+ sort .Strings (versions )
147
+ return versions [0 ], nil
148
+ }
149
+
150
+ // GetAPIVersions returns the list of API Versions found in a service directory.
151
+ func (h * SDKHelper ) GetAPIVersions (serviceAlias string ) ([]string , error ) {
106
152
apiPath := filepath .Join (h .basePath , "models" , "apis" , serviceAlias )
107
153
versionDirs , err := ioutil .ReadDir (apiPath )
108
154
if err != nil {
109
- return "" , err
155
+ return nil , err
110
156
}
157
+ versions := []string {}
111
158
for _ , f := range versionDirs {
112
159
version := f .Name ()
113
160
fp := filepath .Join (apiPath , version )
114
161
fi , err := os .Lstat (fp )
115
162
if err != nil {
116
- return "" , err
163
+ return nil , err
117
164
}
118
165
if ! fi .IsDir () {
119
- return " " , ErrInvalidVersionDirectory
166
+ return nil , fmt . Errorf ( "found %s: %v " , version , ErrInvalidVersionDirectory )
120
167
}
121
- // TODO(jaypipes): handle more than one version? doesn't seem like
122
- // there is ever more than one.
123
- return version , nil
168
+ versions = append (versions , version )
169
+ }
170
+ if len (versions ) == 0 {
171
+ return nil , ErrNoValidVersionDirectory
124
172
}
125
- return "" , ErrNoValidVersionDirectory
173
+ return versions , nil
126
174
}
127
175
128
176
// SDKAPI contains an API model for a single AWS service API
0 commit comments