Skip to content

Commit d45d710

Browse files
authored
Merge pull request #8 from A-Hilaly/aws-sdk-go-version
Add a CLI flag to set the version of aws-sdk-go used by `ack-generate`
2 parents ada6e82 + 92458ab commit d45d710

File tree

5 files changed

+55
-24
lines changed

5 files changed

+55
-24
lines changed

cmd/ack-generate/command/apis.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,9 @@ const (
3535
)
3636

3737
var (
38-
optGenVersion string
39-
optAPIsInputPath string
40-
optAPIsOutputPath string
41-
apisVersionPath string
38+
optGenVersion string
39+
optAPIsInputPath string
40+
apisVersionPath string
4241
)
4342

4443
// apiCmd is the command that generates service API types
@@ -52,9 +51,6 @@ func init() {
5251
apisCmd.PersistentFlags().StringVar(
5352
&optGenVersion, "version", "v1alpha1", "the resource API Version to use when generating API infrastructure and type definitions",
5453
)
55-
apisCmd.PersistentFlags().StringVarP(
56-
&optAPIsOutputPath, "output", "o", "", "path to directory for service controller to create generated files. Defaults to "+optServicesDir+"/$service",
57-
)
5854
rootCmd.AddCommand(apisCmd)
5955
}
6056

@@ -65,8 +61,8 @@ func generateAPIs(cmd *cobra.Command, args []string) error {
6561
return fmt.Errorf("please specify the service alias for the AWS service API to generate")
6662
}
6763
svcAlias := strings.ToLower(args[0])
68-
if optAPIsOutputPath == "" {
69-
optAPIsOutputPath = filepath.Join(optServicesDir, svcAlias)
64+
if optOutputPath == "" {
65+
optOutputPath = filepath.Join(optServicesDir, svcAlias)
7066
}
7167
if err := ensureSDKRepo(optCacheDir); err != nil {
7268
return err
@@ -98,7 +94,7 @@ func generateAPIs(cmd *cobra.Command, args []string) error {
9894
return err
9995
}
10096

101-
apisVersionPath = filepath.Join(optAPIsOutputPath, "apis", optGenVersion)
97+
apisVersionPath = filepath.Join(optOutputPath, "apis", optGenVersion)
10298
for path, contents := range ts.Executed() {
10399
if optDryRun {
104100
fmt.Printf("============================= %s ======================================\n", path)

cmd/ack-generate/command/common.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,33 @@ func cloneSDKRepo(srcPath string) (string, error) {
100100
return clonePath, nil
101101
}
102102

103-
// getSDKVersion parses the go.mod file and returns aws-sdk-go version
103+
// getSDKVersion returns the github.com/aws/aws-sdk-go version to use. It
104+
// first tries to get return version from the --aws-sdk-go-version flag, then
105+
// look for the service controller and local go.mod files.
104106
func getSDKVersion() (string, error) {
105-
b, err := ioutil.ReadFile("./go.mod")
107+
// First try to get the version from --aws-sdk-go-version flag
108+
if optAWSSDKGoVersion != "" {
109+
return optAWSSDKGoVersion, nil
110+
}
111+
112+
// then, try to parse the service controller go.mod file
113+
sdkVersion, err := getSDKVersionFromGoMod(filepath.Join(optOutputPath, "go.mod"))
114+
if err == nil {
115+
return sdkVersion, nil
116+
}
117+
118+
// then try to parse a local go.mod
119+
sdkVersion, err = getSDKVersionFromGoMod("go.mod")
120+
if err != nil {
121+
return "", err
122+
}
123+
return sdkVersion, nil
124+
}
125+
126+
// getSDKVersionFromGoMod parses a given go.mod file and returns
127+
// the aws-sdk-go version in the required modules.
128+
func getSDKVersionFromGoMod(goModPath string) (string, error) {
129+
b, err := ioutil.ReadFile(goModPath)
106130
if err != nil {
107131
return "", err
108132
}

cmd/ack-generate/command/controller.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,9 @@ import (
3232
)
3333

3434
var (
35-
optControllerOutputPath string
36-
cmdControllerPath string
37-
pkgResourcePath string
38-
latestAPIVersion string
35+
cmdControllerPath string
36+
pkgResourcePath string
37+
latestAPIVersion string
3938
)
4039

4140
var controllerCmd = &cobra.Command{
@@ -45,9 +44,6 @@ var controllerCmd = &cobra.Command{
4544
}
4645

4746
func init() {
48-
controllerCmd.PersistentFlags().StringVarP(
49-
&optControllerOutputPath, "output", "o", "", "path to root directory to create generated files. Defaults to "+optServicesDir+"/$service",
50-
)
5147
rootCmd.AddCommand(controllerCmd)
5248
}
5349

@@ -57,8 +53,8 @@ func generateController(cmd *cobra.Command, args []string) error {
5753
return fmt.Errorf("please specify the service alias for the AWS service API to generate")
5854
}
5955
svcAlias := strings.ToLower(args[0])
60-
if optControllerOutputPath == "" {
61-
optControllerOutputPath = filepath.Join(optServicesDir, svcAlias)
56+
if optOutputPath == "" {
57+
optOutputPath = filepath.Join(optServicesDir, svcAlias)
6258
}
6359

6460
if err := ensureSDKRepo(optCacheDir); err != nil {
@@ -101,7 +97,7 @@ func generateController(cmd *cobra.Command, args []string) error {
10197
fmt.Println(strings.TrimSpace(contents.String()))
10298
continue
10399
}
104-
outPath := filepath.Join(optControllerOutputPath, path)
100+
outPath := filepath.Join(optOutputPath, path)
105101
outDir := filepath.Dir(outPath)
106102
if _, err := ensureDir(outDir); err != nil {
107103
return err
@@ -117,7 +113,7 @@ func generateController(cmd *cobra.Command, args []string) error {
117113
// latest Kubernetes API version for CRDs exposed by the generated service
118114
// controller.
119115
func getLatestAPIVersion() (string, error) {
120-
apisPath := filepath.Join(optControllerOutputPath, "apis")
116+
apisPath := filepath.Join(optOutputPath, "apis")
121117
versions := []string{}
122118
subdirs, err := ioutil.ReadDir(apisPath)
123119
if err != nil {

cmd/ack-generate/command/root.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,15 @@ var (
3737
defaultCacheDir string
3838
optCacheDir string
3939
optRefreshCache bool
40+
optAWSSDKGoVersion string
4041
defaultTemplatesDir string
4142
optTemplatesDir string
4243
defaultServicesDir string
4344
optServicesDir string
4445
optDryRun bool
4546
sdkDir string
4647
optGeneratorConfigPath string
48+
optOutputPath string
4749
)
4850

4951
var rootCmd = &cobra.Command{
@@ -113,6 +115,12 @@ func init() {
113115
rootCmd.PersistentFlags().StringVar(
114116
&optGeneratorConfigPath, "generator-config-path", "", "Path to file containing instructions for code generation to use",
115117
)
118+
rootCmd.PersistentFlags().StringVarP(
119+
&optOutputPath, "output", "o", "", "Path to directory to output generated files.",
120+
)
121+
rootCmd.PersistentFlags().StringVar(
122+
&optAWSSDKGoVersion, "aws-sdk-go-version", "", "Version of github.com/aws/aws-sdk-go used to generate apis and controllers files",
123+
)
116124
}
117125

118126
// Execute adds all child commands to the root command and sets flags

scripts/build-controller.sh

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ DEFAULT_ACK_GENERATE_BIN_PATH="$ROOT_DIR/bin/ack-generate"
2626
ACK_GENERATE_BIN_PATH=${ACK_GENERATE_BIN_PATH:-$DEFAULT_ACK_GENERATE_BIN_PATH}
2727
ACK_GENERATE_API_VERSION=${ACK_GENERATE_API_VERSION:-"v1alpha1"}
2828
ACK_GENERATE_CONFIG_PATH=${ACK_GENERATE_CONFIG_PATH:-""}
29+
AWS_SDK_GO_VERSION=${AWS_SDK_GO_VERSION:-""}
2930
DEFAULT_TEMPLATES_DIR="$ROOT_DIR/templates"
3031
TEMPLATES_DIR=${TEMPLATES_DIR:-$DEFAULT_TEMPLATES_DIR}
3132

@@ -37,7 +38,7 @@ Usage:
3738
's3' 'sns' or 'sqs'
3839
3940
Environment variables:
40-
ACK_GENERATE_CACHE_DIR Overrides the directory used for caching AWS API
41+
ACK_GENERATE_CACHE_DIR: Overrides the directory used for caching AWS API
4142
models used by the ack-generate tool.
4243
Default: $ACK_GENERATE_CACHE_DIR
4344
ACK_GENERATE_BIN_PATH: Overrides the path to the the ack-generate binary.
@@ -51,6 +52,8 @@ Environment variables:
5152
ACK_GENERATE_CONFIG_PATH: Specify a path to the generator config YAML file to
5253
instruct the code generator for the service.
5354
Default: services/{SERVICE}/generator.yaml
55+
AWS_SDK_GO_VERSION: Overrides the version of github.com/aws/aws-sdk-go used
56+
by `ack-generate` to fetch the service API Specifications.
5457
TEMPLATES_DIR: Overrides the directory containg ack-generate templates
5558
Default: $TEMPLATES_DIR
5659
K8S_RBAC_ROLE_NAME: Name of the Kubernetes Role to use when generating
@@ -120,6 +123,10 @@ if [ -n "$ACK_GENERATE_CONFIG_PATH" ]; then
120123
apis_args="$apis_args --generator-config-path $ACK_GENERATE_CONFIG_PATH"
121124
fi
122125

126+
if [ -n "$AWS_SDK_GO_VERSION" ]; then
127+
ag_args="$ag_args --aws-sdk-go-version $AWS_SDK_GO_VERSION"
128+
fi
129+
123130
echo "Building Kubernetes API objects for $SERVICE"
124131
$ACK_GENERATE_BIN_PATH $apis_args
125132
if [ $? -ne 0 ]; then

0 commit comments

Comments
 (0)