Skip to content

Commit 578d785

Browse files
authored
Warm the user when upgrading (#115)
1 parent 529479c commit 578d785

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

python/rpdk/go/codegen.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,23 @@
1212
from rpdk.core.jsonutils.resolver import resolve_models
1313
from rpdk.core.plugin_base import LanguagePlugin
1414

15+
from . import __version__
1516
from .resolver import translate_type
1617
from .utils import safe_reserved, validate_path
18+
from .version import check_version
1719

1820
LOG = logging.getLogger(__name__)
1921

2022
OPERATIONS = ("Create", "Read", "Update", "Delete", "List")
2123
EXECUTABLE = "cfn-cli"
2224

25+
LANGUAGE = "go"
26+
27+
DEFAULT_SETTINGS = {
28+
"protocolVersion": "1.0",
29+
"pluginVersion": __version__,
30+
}
31+
2332

2433
class GoExecutableNotFoundError(SysExitRecommendedError):
2534
pass
@@ -121,6 +130,7 @@ def _init_settings(self, project):
121130
project.runtime = self.RUNTIME
122131
project.entrypoint = self.ENTRY_POINT.format(self.import_path)
123132
project.test_entrypoint = self.TEST_ENTRY_POINT.format(self.import_path)
133+
project.settings.update(DEFAULT_SETTINGS)
124134

125135
def init_handlers(self, project, src):
126136
LOG.debug("Writing stub handlers")
@@ -172,6 +182,23 @@ def generate(self, project):
172182
except (FileNotFoundError, CalledProcessError) as e:
173183
raise DownstreamError("go fmt failed") from e
174184

185+
# Update settings as needed
186+
need_to_write = False
187+
for key, new in DEFAULT_SETTINGS.items():
188+
old = project.settings.get(key)
189+
190+
if project.settings.get(key) != new:
191+
LOG.debug(f"{key} version change from {old} to {new}")
192+
project.settings[key] = new
193+
need_to_write = True
194+
195+
if key == "pluginVersion":
196+
# Display any upgrade messages
197+
print(*check_version(old), sep="\n")
198+
199+
if need_to_write:
200+
project._write_settings(LANGUAGE)
201+
175202
@staticmethod
176203
def pre_package(project):
177204
# zip the Go build output - it's all needed to execute correctly

python/rpdk/go/version.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
The version package contains warning messages that should be displayed
3+
to a Go plugin user when upgrading from an older version of the plugin
4+
"""
5+
6+
import semver
7+
8+
"""
9+
If there are breaking changes that need to be communicated to a user,
10+
add them to this dict. For readability, an opening and closing newline
11+
is recommended for each warning message
12+
"""
13+
WARNINGS = {
14+
# FIXME: Version number to be finalised
15+
semver.VersionInfo(
16+
0, 1, 3
17+
): """
18+
Generated models no longer use the types exported in the encoding package.
19+
Your model's fields have been regenerated using standard pointer types (*string, *int, etc) as used in the AWS Go SDK.
20+
The AWS SDK has helper functions that you can use to get and set your model's values.
21+
22+
Make the following changes to your handler code as needed:
23+
24+
* Replace `encoding.New{Type}` with `aws.{Type}`
25+
* Replace `model.{field}.Value()` with `aws.{Type}Value(model.{field})`
26+
27+
Where {Type} is either String, Bool, Int, or Float64 and {field} is any field within your generated model.
28+
""",
29+
}
30+
31+
32+
def check_version(current_version):
33+
"""
34+
check_version compares the user's current plugin version with each
35+
version in WARNINGS and returns any appropriate messages
36+
"""
37+
38+
if current_version is not None:
39+
current_version = semver.VersionInfo.parse(current_version)
40+
41+
return [
42+
f"Change message for Go plugin v{version}:" + WARNINGS[version]
43+
for version in sorted(WARNINGS.keys())
44+
if current_version is None or current_version < version
45+
]

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def find_version(*file_paths):
3636
# package_data -> use MANIFEST.in instead
3737
include_package_data=True,
3838
zip_safe=True,
39-
install_requires=["cloudformation-cli>=0.1,<0.2"],
39+
install_requires=["cloudformation-cli>=0.1,<0.2", "semver>=2.9.0",],
4040
python_requires=">=3.6",
4141
entry_points={"rpdk.v1.languages": ["go = rpdk.go.codegen:GoLanguagePlugin"]},
4242
license="Apache License 2.0",

0 commit comments

Comments
 (0)