-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: go-use plugin to handle go workspaces (#864)
Signed-off-by: Sergio Schvezov <[email protected]> Co-authored-by: Tiago Nobrega <[email protected]>
- Loading branch information
1 parent
9d01009
commit 57cf26f
Showing
15 changed files
with
592 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*- | ||
# | ||
# Copyright 2020-2021,2024 Canonical Ltd. | ||
# | ||
# This program is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU Lesser General Public | ||
# License version 3 as published by the Free Software Foundation. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
# Lesser General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Lesser General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
"""The Go Use plugin.""" | ||
|
||
import logging | ||
import subprocess | ||
from typing import Literal | ||
|
||
from overrides import override | ||
|
||
from craft_parts import errors | ||
|
||
from .base import Plugin | ||
from .go_plugin import GoPluginEnvironmentValidator | ||
from .properties import PluginProperties | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class GoUsePluginProperties(PluginProperties, frozen=True): | ||
"""The part properties used by the Go Use plugin.""" | ||
|
||
plugin: Literal["go-use"] = "go-use" | ||
|
||
# part properties required by the plugin | ||
source: str # pyright: ignore[reportGeneralTypeIssues] | ||
|
||
|
||
class GoUsePlugin(Plugin): | ||
"""A plugin to setup the source into a go workspace. | ||
The go plugin requires a go compiler installed on your system. This can | ||
be achieved by adding the appropriate golang package to ``build-packages``, | ||
or to have it installed or built in a different part. In this case, the | ||
name of the part supplying the go compiler must be "go". | ||
""" | ||
|
||
properties_class = GoUsePluginProperties | ||
validator_class = GoPluginEnvironmentValidator | ||
|
||
@classmethod | ||
def get_out_of_source_build(cls) -> bool: | ||
"""Return whether the plugin performs out-of-source-tree builds.""" | ||
return True | ||
|
||
@override | ||
def get_build_snaps(self) -> set[str]: | ||
"""Return a set of required snaps to install in the build environment.""" | ||
return set() | ||
|
||
@override | ||
def get_build_packages(self) -> set[str]: | ||
"""Return a set of required packages to install in the build environment.""" | ||
return set() | ||
|
||
@override | ||
def get_build_environment(self) -> dict[str, str]: | ||
"""Return a dictionary with the environment to use in the build step.""" | ||
return {} | ||
|
||
@override | ||
def get_build_commands(self) -> list[str]: | ||
"""Return a list of commands to run during the build step.""" | ||
# Set the go workspace directory to live at the root of all parts. | ||
workspace_dir = self._part_info.project_info.dirs.parts_dir | ||
workspace = workspace_dir / "go.work" | ||
|
||
# We do not want this implementation detail exposed in the run script | ||
if not workspace.exists(): | ||
logger.debug(f"Init go workspace at {workspace}") | ||
try: | ||
subprocess.run( | ||
["go", "work", "init"], | ||
capture_output=True, | ||
check=True, | ||
cwd=workspace_dir, | ||
) | ||
except subprocess.CalledProcessError as call_error: | ||
logger.debug( | ||
f"Workspace init failed {call_error!r} " | ||
f"stdout: {call_error.stdout!r} " | ||
f"stderr: {call_error.stderr}" | ||
) | ||
raise errors.PluginBuildError( | ||
part_name=self._part_info.part_name, plugin_name="go-use" | ||
) | ||
|
||
return [ | ||
f"go work use {self._part_info.part_src_dir}", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
docs/common/craft-parts/reference/plugins/go_use_plugin.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
.. _craft_parts_go_use_plugin: | ||
|
||
Go Use plugin | ||
============= | ||
|
||
The Go Use plugin allows for setting up a `go workspace`_ for `Go`_ modules. It is | ||
a companion plugin meant to be used with the :ref:`Go plugin <craft_parts_go_plugin>`. | ||
Use of this plugin sets up ``go.work`` and affects all parts. | ||
|
||
Keywords | ||
-------- | ||
|
||
There are no additional keywords to the the common :ref:`plugin <part-properties-plugin>` | ||
and :ref:`sources <part-properties-sources>` keywords. | ||
|
||
.. _go-use-details-begin: | ||
|
||
Dependencies | ||
------------ | ||
|
||
The Go plugin needs the ``go`` executable to build Go programs but does not | ||
provision it by itself, to allow flexibility in the choice of compiler version. | ||
|
||
Common means of providing ``go`` are: | ||
|
||
* The ``golang`` Ubuntu package, declared as a ``build-package``. | ||
* The ``go`` snap, declared as a ``build-snap`` from the desired channel. | ||
|
||
Another alternative is to define another part with the name ``go-deps``, and | ||
declare that the part using the ``go`` plugin comes :ref:`after <after>` the | ||
``go-deps`` part. In this case, the plugin will assume that this new part will | ||
stage the ``go`` executable to be used in the build step. This can be useful, | ||
for example, in cases where a specific, unreleased version of ``go`` is desired | ||
but unavailable as a snap or an Ubuntu package. | ||
|
||
.. _go-use-details-end: | ||
|
||
How it works | ||
------------ | ||
|
||
During the build step the plugin performs the following actions: | ||
|
||
* Setup a `go workspace`_ if ``go.work`` has not been setup; | ||
* Call ``go work use <source-dir>`` to add the source for the part to the workspace; | ||
|
||
Examples | ||
-------- | ||
|
||
The following snippet declares a parts named ``go-flags`` using the ``go-use`` plugin and | ||
a ``hello`` part that declares this ``go-flags``` in its ``go.mod`` using the ``go`` plugin. | ||
Correct ordering is achieved with the use of the ``after`` keyword in the ``hello`` part. | ||
|
||
.. code-block:: yaml | ||
parts: | ||
go-flags: | ||
source: https://github.com/jessevdk/go-flags.git | ||
plugin: go-use | ||
hello: | ||
build-snaps: | ||
- go/1.22/stable | ||
plugin: go | ||
source: . | ||
after: | ||
- go-flags | ||
.. _Build tags: https://pkg.go.dev/cmd/go#hdr-Build_constraints | ||
.. _Go: https://go.dev/ | ||
.. _go generate: https://go.dev/blog/generate | ||
.. _go workspace: https://go.dev/blog/get-familiar-with-workspaces |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.