salt icon indicating copy to clipboard operation
salt copied to clipboard

[master] feat: Improve macOS defaults support

Open cdalvaro opened this issue 2 months ago • 1 comments

What does this PR do?

Improve macOS defaults support by adding typecasting as well as dictionaries and arrays support.

Previous Behavior

Dictionaries and arrays were not supported.

New Behavior

Now you can set dictionaries and arrays (even with multi-level support!). Support for floats and integers values has been improved too.

Additional, multi-level key support has been added. Allowing you to set the value for a complex key such as: Key.SubKey.0.OtherKey by providing the parameter name_separator='.' (for the state) and key_separator='.' (for the module).

Some examples that works now:

Rectangle - Almost Maximize height:
  macdefaults.write:
    - name: almostMaximizeHeight
    - domain: com.knollsoft.Rectangle.plist
    - value: 0.80
    - user: cdalvaro
    - require:
        - pkg: Rectangle

Rectangle - Almost Maximize shortcut:
  macdefaults.write:
    - name: almostMaximize
    - domain: com.knollsoft.Rectangle.plist
    - value:
        keyCode: 36
        modifierFlags: 786432
    - vtype: dict
    - user: cdalvaro
    - require:
        - pkg: Rectangle

Set file-mod-date in com.apple.Dock:
  macdefaults.write:
    - domain: com.apple.Dock
    - name: persistent-others.0.tile-data.file-mod-date
    - name_separator: '.'
    # Also works with POSIX timestamps in floating seconds
    - value: 2024-06-28T11:12:05Z
    - vtype: date
    - user: cdalvaro

The recipe Rectangle - Almost Maximize height didn't work because when comparing the current value with the new one, the new one was a float and the retrieved by macdefaults.read was a string. Thus, the comparison doesn't match.

The recipe Rectangle - Almost Maximize shortcut was not working at all. The write command wasn't formed well. And the output retrieved by macdefaults.read was not parsed into a Python dictionary, so comparison always fails.

The last recipe is completely new. Multi-level keys were not supported before as well as keys.

Other recipes that works now:

Empty dict set:
  macdefaults.write:
    - name: PROPERTY
    - domain: DOMAIN
    - value: {}
    - vtype: dict

Add new elements to existing dict:
  macdefaults.write:
    - name: PROPERTY
    - domain: DOMAIN
    - value:
        existingKey: 1
        newKey: 'Hi'
        nestedDict:
          foo: bar
    - vtype: dict-add

Array set:
  macdefaults.write:
    - name: PROPERTY
    - domain: DOMAIN
    - value:
      - 1
      - 2
      - 3
    - vtype: array

Add an array to existing array:
# Order matters, if the new array is not contained
# in the existing array with the same order it will be appended
  macdefaults.write:
    - name: PROPERTY
    - domain: DOMAIN
    - value:
      - 5
      - 6
    - vtype: array-add

⚠️ Breaking changes

Values are typecasted when possible, so some formulas may need to be adapted to be idempotent.

The type parameter in the macdefaults.write method has been deprecated in favor of vtype. This makes the state and the module coherent and avoids the collision between the parameter and the built-in type() Python function.

The type parameter should be removed in version 3009.

Merge requirements satisfied?

  • [x] Docs
  • [x] Changelog - https://docs.saltproject.io/en/master/topics/development/changelog.html
  • [x] Tests written/updated

Commits signed with GPG?

Yes

Please review Salt's Contributing Guide for best practices, including the PR Guidelines.

See GitHub's page on GPG signing for more information about signing commits with GPG.

cdalvaro avatar May 05 '24 18:05 cdalvaro