Skip to content

Commit 92458ab

Browse files
committed
Add a CLI flag to set the version of aws-sdk-go used by ack-generate
1 parent 7512e51 commit 92458ab

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

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/root.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ var (
3737
defaultCacheDir string
3838
optCacheDir string
3939
optRefreshCache bool
40+
optAWSSDKGoVersion string
4041
defaultTemplatesDir string
4142
optTemplatesDir string
4243
defaultServicesDir string
@@ -117,6 +118,9 @@ func init() {
117118
rootCmd.PersistentFlags().StringVarP(
118119
&optOutputPath, "output", "o", "", "Path to directory to output generated files.",
119120
)
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+
)
120124
}
121125

122126
// 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)