|
33 | 33 | # Define the module's virtual name |
34 | 34 | __virtualname__ = "pkg" |
35 | 35 |
|
| 36 | +_TRUST_TYPES = ("tap", "formula", "cask", "command") |
| 37 | + |
36 | 38 |
|
37 | 39 | def __virtual__(): |
38 | 40 | """ |
@@ -895,3 +897,156 @@ def unhold(name=None, pkgs=None, sources=None, **kwargs): # pylint: disable=W06 |
895 | 897 |
|
896 | 898 |
|
897 | 899 | unpin = unhold |
| 900 | + |
| 901 | + |
| 902 | +def list_trusted(type=None): |
| 903 | + """ |
| 904 | + List trusted taps, formulae, casks and commands. |
| 905 | +
|
| 906 | + .. versionadded:: 3008.2 |
| 907 | +
|
| 908 | + type |
| 909 | + Filter by type. Valid values: ``tap``, ``formula``, ``cask``, ``command``. |
| 910 | + When ``None`` (default), returns a dict with all trusted items grouped by |
| 911 | + type (keys: ``taps``, ``formulae``, ``casks``, ``commands``). When a type |
| 912 | + is specified, returns a list of trusted items of that type. |
| 913 | +
|
| 914 | + CLI Example: |
| 915 | +
|
| 916 | + .. code-block:: bash |
| 917 | +
|
| 918 | + salt '*' pkg.list_trusted |
| 919 | + salt '*' pkg.list_trusted type=tap |
| 920 | + """ |
| 921 | + if type is not None and type not in _TRUST_TYPES: |
| 922 | + raise SaltInvocationError( |
| 923 | + f"Invalid type '{type}'. Valid values: {', '.join(_TRUST_TYPES)}" |
| 924 | + ) |
| 925 | + |
| 926 | + cmd = ["trust", "--json=v1"] |
| 927 | + if type is not None: |
| 928 | + cmd.append(f"--{type}") |
| 929 | + |
| 930 | + result = _call_brew(*cmd) |
| 931 | + try: |
| 932 | + return salt.utils.json.loads(result["stdout"]) |
| 933 | + except ValueError as err: |
| 934 | + msg = f'Unable to interpret output from "brew trust": {err}' |
| 935 | + log.error(msg) |
| 936 | + raise CommandExecutionError(msg) |
| 937 | + |
| 938 | + |
| 939 | +def trust(name, type=None): |
| 940 | + """ |
| 941 | + Trust a tap, formula, cask or command so Homebrew may load it when |
| 942 | + ``$HOMEBREW_REQUIRE_TAP_TRUST`` is set. |
| 943 | +
|
| 944 | + .. versionadded:: 3008.2 |
| 945 | +
|
| 946 | + name |
| 947 | + The name of the tap, formula, cask or command to trust. Can also be a |
| 948 | + remote URL for taps. |
| 949 | +
|
| 950 | + type |
| 951 | + The type of the item. Valid values: ``tap``, ``formula``, ``cask``, |
| 952 | + ``command``. If not specified, Homebrew will auto-detect the type. |
| 953 | +
|
| 954 | + Returns ``True`` on success (including when the item was already trusted), |
| 955 | + ``False`` on failure. |
| 956 | +
|
| 957 | + CLI Example: |
| 958 | +
|
| 959 | + .. code-block:: bash |
| 960 | +
|
| 961 | + salt '*' pkg.trust cdalvaro/tap |
| 962 | + salt '*' pkg.trust cdalvaro/tap type=tap |
| 963 | + salt '*' pkg.trust cdalvaro/tap/salt type=formula |
| 964 | + """ |
| 965 | + if type is not None and type not in _TRUST_TYPES: |
| 966 | + raise SaltInvocationError( |
| 967 | + f"Invalid type '{type}'. Valid values: {', '.join(_TRUST_TYPES)}" |
| 968 | + ) |
| 969 | + |
| 970 | + cmd = ["trust"] |
| 971 | + if type is not None: |
| 972 | + cmd.append(f"--{type}") |
| 973 | + cmd.append(name) |
| 974 | + |
| 975 | + try: |
| 976 | + _call_brew(*cmd) |
| 977 | + except CommandExecutionError: |
| 978 | + log.error('Failed to trust "%s"', name) |
| 979 | + return False |
| 980 | + |
| 981 | + return True |
| 982 | + |
| 983 | + |
| 984 | +def untrust(name, type=None): |
| 985 | + """ |
| 986 | + Stop trusting a tap, formula, cask or command. |
| 987 | +
|
| 988 | + .. versionadded:: 3008.2 |
| 989 | +
|
| 990 | + name |
| 991 | + The name of the tap, formula, cask or command to untrust. |
| 992 | +
|
| 993 | + type |
| 994 | + The type of the item. Valid values: ``tap``, ``formula``, ``cask``, |
| 995 | + ``command``. If not specified, Homebrew will auto-detect the type. |
| 996 | +
|
| 997 | + Returns ``True`` on success (including when the item was not trusted), |
| 998 | + ``False`` on failure. |
| 999 | +
|
| 1000 | + CLI Example: |
| 1001 | +
|
| 1002 | + .. code-block:: bash |
| 1003 | +
|
| 1004 | + salt '*' pkg.untrust cdalvaro/tap |
| 1005 | + salt '*' pkg.untrust cdalvaro/tap type=tap |
| 1006 | + salt '*' pkg.untrust cdalvaro/tap/salt type=formula |
| 1007 | + """ |
| 1008 | + if type is not None and type not in _TRUST_TYPES: |
| 1009 | + raise SaltInvocationError( |
| 1010 | + f"Invalid type '{type}'. Valid values: {', '.join(_TRUST_TYPES)}" |
| 1011 | + ) |
| 1012 | + |
| 1013 | + cmd = ["untrust"] |
| 1014 | + if type is not None: |
| 1015 | + cmd.append(f"--{type}") |
| 1016 | + cmd.append(name) |
| 1017 | + |
| 1018 | + try: |
| 1019 | + _call_brew(*cmd) |
| 1020 | + except CommandExecutionError: |
| 1021 | + log.error('Failed to untrust "%s"', name) |
| 1022 | + return False |
| 1023 | + |
| 1024 | + return True |
| 1025 | + |
| 1026 | + |
| 1027 | +def is_trusted(name, type=None): |
| 1028 | + """ |
| 1029 | + Check whether a tap, formula, cask or command is trusted. |
| 1030 | +
|
| 1031 | + .. versionadded:: 3008.2 |
| 1032 | +
|
| 1033 | + name |
| 1034 | + The name of the tap, formula, cask or command to check. |
| 1035 | +
|
| 1036 | + type |
| 1037 | + The type of the item. Valid values: ``tap``, ``formula``, ``cask``, |
| 1038 | + ``command``. If not specified, checks across all types. |
| 1039 | +
|
| 1040 | + Returns ``True`` if the item is trusted, ``False`` otherwise. |
| 1041 | +
|
| 1042 | + CLI Example: |
| 1043 | +
|
| 1044 | + .. code-block:: bash |
| 1045 | +
|
| 1046 | + salt '*' pkg.is_trusted cdalvaro/tap |
| 1047 | + salt '*' pkg.is_trusted cdalvaro/tap type=tap |
| 1048 | + """ |
| 1049 | + trusted = list_trusted(type=type) |
| 1050 | + if isinstance(trusted, list): |
| 1051 | + return name in trusted |
| 1052 | + return any(name in items for items in trusted.values()) |
0 commit comments