Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add executable option to cargo #7352

Merged
merged 8 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/7352-add-executable-option-for-cargo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- "cargo - add option ``executable``, which allows user to specify path to the cargo binary (https://github.com/ansible-collections/community.general/pull/7352)."
16 changes: 10 additions & 6 deletions plugins/modules/cargo.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
diff_mode:
support: none
options:
executable:
description:
- Path to the C(cargo) installed in the system.
- If not specified, the module will look C(cargo) in E(PATH).
type: path
radek-sprta marked this conversation as resolved.
Show resolved Hide resolved
version_added: 7.5.0
name:
description:
- The name of a Rust package to install.
Expand Down Expand Up @@ -60,7 +66,7 @@
default: present
choices: [ "present", "absent", "latest" ]
requirements:
- cargo installed in bin path (recommended /usr/local/bin)
- cargo installed
"""

EXAMPLES = r"""
Expand Down Expand Up @@ -103,14 +109,13 @@
class Cargo(object):
def __init__(self, module, **kwargs):
self.module = module
self.executable = [kwargs["executable"] or module.get_bin_path("cargo", True)]
self.name = kwargs["name"]
self.path = kwargs["path"]
self.state = kwargs["state"]
self.version = kwargs["version"]
self.locked = kwargs["locked"]

self.executable = [module.get_bin_path("cargo", True)]

@property
def path(self):
return self._path
Expand Down Expand Up @@ -176,6 +181,7 @@ def uninstall(self, packages=None):

def main():
arg_spec = dict(
executable=dict(default=None, type="path"),
name=dict(required=True, type="list", elements="str"),
path=dict(default=None, type="path"),
state=dict(default="present", choices=["present", "absent", "latest"]),
Expand All @@ -185,10 +191,8 @@ def main():
module = AnsibleModule(argument_spec=arg_spec, supports_check_mode=True)

name = module.params["name"]
path = module.params["path"]
state = module.params["state"]
version = module.params["version"]
locked = module.params["locked"]

if not name:
module.fail_json(msg="Package name must be specified")
Expand All @@ -198,7 +202,7 @@ def main():
LANG="C", LC_ALL="C", LC_MESSAGES="C", LC_CTYPE="C"
)

cargo = Cargo(module, name=name, path=path, state=state, version=version, locked=locked)
cargo = Cargo(module, **module.params)
changed, out, err = False, None, None
installed_packages = cargo.get_installed()
if state == "present":
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/targets/cargo/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@
- import_tasks: test_version.yml
environment: "{{ cargo_environment }}"
when: has_cargo | default(false)
- import_tasks: test_rustup_cargo.yml
when: rustup_cargo_bin | default(false)
14 changes: 14 additions & 0 deletions tests/integration/targets/cargo/tasks/setup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,17 @@
has_cargo: true
when:
- ansible_system == 'FreeBSD' and ansible_distribution_version is version('13.0', '>')

- block:
- name: Download rustup
get_url:
url: https://sh.rustup.rs
dest: /tmp/sh.rustup.rs
mode: "0750"
force: true
- name: Install rustup cargo
command: /tmp/sh.rustup.rs -y
- set_fact:
rustup_cargo_bin: "{{ lookup('env', 'HOME') }}/.cargo/bin/cargo"
when:
- ansible_distribution != 'CentOS' or ansible_distribution_version is version('7.0', '>=')
23 changes: 23 additions & 0 deletions tests/integration/targets/cargo/tasks/test_rustup_cargo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
# Copyright (c) Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
#
- name: Install application helloworld
community.general.cargo:
executable: "{{ rustup_cargo_bin }}"
name: helloworld
register: rustup_install_absent_helloworld

- name: Uninstall application helloworld
community.general.cargo:
executable: "{{ rustup_cargo_bin }}"
state: absent
name: helloworld
register: rustup_uninstall_present_helloworld

- name: Check assertions helloworld
assert:
that:
- rustup_install_absent_helloworld is changed
- rustup_uninstall_present_helloworld is changed