Darwin.setup mas cli.yml

#!/usr/bin/env ansible-playbook
#
# Copyright © 2025 Florent Claerhout.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

- name: "Program is set up: {{ program_name }}"
  hosts: "all"

  vars:
    ##
    ## NOTICE!
    ## variables defined here take precedence over inventory variables;
    ## keep user-defined/able variables commented out.
    ## Use set_fact + | default if a default value is needed.
    ##
    user_downloads_rdir: "{{ artifacts_rdir | default('{{ ansible_env.HOME }}/Downloads') }}"
    system_programs_rdir: "/usr/local/bin"
    program_version: "2.3.0"
    program_pkg_url: "https://github.com/mas-cli/mas/releases/download/v{{ program_version }}/mas-{{ program_version }}.pkg"
    program_pkg_rfile: "{{ user_downloads_rdir }}/mas-{{ program_version }}.pkg"
    program_name: "mas"
    program_rfile: "{{ system_programs_rdir }}/mas"

  handlers: []

  tasks:

    - when: ansible_system != "Darwin"
      fail:
        msg: "not macOS"

    ### PROGRAM ################################################################

    - name: "User downloads directory is present"
      file:
        path: "{{ user_downloads_rdir }}"
        state: "directory"

    - name: "User package is present"
      get_url:
        url: "{{ program_pkg_url }}"
        dest: "{{ program_pkg_rfile }}"

    - block:

        - name: "Current version is assessed"
          changed_when: false
          register: cmd
          shell: |
            set -euo pipefail
            if [[ -e "{{ program_rfile }}" ]]
            then
              case $({{ program_rfile }} version) in
                *"{{ program_version }}"*)
                  echo "expected version is installed"
                  ;;
                *)
                  echo "unexpected version is installed"
              esac
            else
              echo "no version is installed"
            fi

        - name: "Current version is uninstalled"
          when: ("unexpected version is installed" in cmd.stdout)
          shell:
            removes: "{{ program_rfile }}"
            cmd: |
              set -euo pipefail
              exit 1
              # FIXME -- uninstall pkg non-interactively
              # cd /
              # pkgutil --only-files --files io.github.mas-cli | tr '\n' '\0' | xargs -n 1 -0 sudo rm -i
              # pkgutil --only-dirs --files io.github.mas-cli | tail -r | tr '\n' '\0' | xargs -n 1 -0 sudo rmdir
              # sudo pkgutil --forget io.github.mas-cli

    - name: "User program is present"
      become: true
      shell:
        creates: "{{ program_rfile }}"
        cmd: |
          set -euo pipefail
          softwareupdate --install-rosetta --agree-to-license
          installer -pkg '{{ program_pkg_rfile }}' -target /

    ### CONFIGURATION ##########################################################

    #
    # N/A
    #