civicrm / cv

CiviCRM CLI Utility
28 stars 30 forks source link

Add commands to view/edit settings #135

Closed totten closed 2 years ago

totten commented 2 years ago

Add new helper commands for managing settings.

Command name Alias Description
cv setting:get cv vget Read the values of some settings
cv setting:set cv vset Update the values of some settings
cv setting:revert cv vdel Revert/undo/delete the values of some settings

Reading the settings

The basic command (setting:get aka vget) shows the effective value of all settings:

## Show all
$ cv vget
+--------+-----------------------------------------------+---------------------------------------------+-----------+
| scope  | key                                           | value                                       | layer     |
+--------+-----------------------------------------------+---------------------------------------------+-----------+
| domain | acl_cache_refresh_mode                        | "opportunistic"                             | default   |
| domain | acl_financial_type                            | 0                                           | default   |
| domain | activity_assignee_notification                | "1"                                         | default   |
| domain | activity_assignee_notification_ics            | "0"                                         | default   |

The value is the live/effective value, and the layer indicates the origin of the value. Values may originate in these layers:

The list is long. You can narrow down by exact name or by regex:

## Lookup by name
$ cv vget mailerJobSize
+--------+---------------+-------+---------+
| scope  | key           | value | layer   |
+--------+---------------+-------+---------+
| domain | mailerJobSize | 0     | default |
+--------+---------------+-------+---------+

## Lookup by regex
$ cv vget /mail/
+--------+-----------------------------------+---------------------------------------------+-----------+
| scope  | key                               | value                                       | layer     |
+--------+-----------------------------------+---------------------------------------------+-----------+
| domain | civimail_sync_interval            | 10                                          | default   |
| domain | civimail_workflow                 | "0"                                         | default   |
| domain | mailing_backend                   | {"outBound_option":0,"smtpServer":"local... | mandatory |
| domain | mailing_format                    | "{contact.addressee}\n{contact.street_ad... | default   |

There is a lot of information available for debugging -- e.g. with verbose mode (-v), it will present every configuration layer (default, explicit, mandatory) as well the metadata for the setting.

$ cv vget /debug/ -v
+----------------------+-------------------------------------------------------------------------------------------------------------------------------------+
| key                  | value                                                                                                                               |
+----------------------+-------------------------------------------------------------------------------------------------------------------------------------+
| scope                | domain                                                                                                                              |
| key                  | debug_enabled                                                                                                                       |
| value                | 1                                                                                                                                   |
| default              | 0                                                                                                                                   |
| explicit             | 1                                                                                                                                   |
| mandatory            | 1                                                                                                                                   |
| layer                | mandatory                                                                                                                           |
| meta.group_name      | Developer Preferences                                                                                                               |
| meta.group           | developer                                                                                                                           |
| meta.name            | debug_enabled                                                                                                                       |
| meta.config_key      | debug                                                                                                                               |
| meta.type            | Boolean                                                                                                                             |
| meta.quick_form_type | YesNo                                                                                                                               |
| meta.default         | 0                                                                                                                                   |
| meta.add             | 4.3                                                                                                                                 |
| meta.title           | Enable Debugging                                                                                                                    |
| meta.is_domain       | 1                                                                                                                                   |
| meta.is_contact      | 0                                                                                                                                   |
| meta.description     | Set this value to Yes if you want to use one of CiviCRM's debugging tools. This feature should NOT be enabled for production sites. |
| meta.help_text       | Do not turn this on on production sites                                                                                             |
+----------------------+-------------------------------------------------------------------------------------------------------------------------------------+

Writing the settings

The setting:set (aka vset) command will update the settings. It accepts key-value pairs:

## Pass key=value (basic number)
cv vset track_civimail_replies=1

## Pass key=value (basic string)
cv vset petition_contacts='Petition People'

## Pass key=value (JSON array or JSON object)
cv vset authx_guards='["perm"]'

For precise handling of complex settings, you can supply values as pure JSON:

## Pass key+value as JSON (command line parameter)
cv vset '{"authx_guards": ["perm"]}'

## Pass key+value as JSON (standard input)
echo '{"authx_guards": ["perm"]}'  | cv vset --in=json

The above operations will replace the setting. For complex settings, it may be more convenient to target the change at a specific property, but this requires some additional hints. For example, mailing_backend is an object with nested keys, and you may edit it with the +object/+o helper:

## Modify an object's property
cv vset +object mailing_backend.smtpServer=mail.example.com
cv vset +o mailing_backend.smtpServer=mail.example.com

## Remove an object's property
cv vset +object !mailing_backend.smtpServer
cv vset +o !mailing_backend.smtpServer

Similarly, authx_guards is a list with multiple values. You may add and remove values with the +list/+l helper:

## Add to list
cv vset +list authx_guards+=site_key
cv vset +l authx_guards+=site_key

## Remove from a list
cv vset +list authx_guards-=site_key
cv vset +l authx_guards-=site_key

Details: Scope

The commands manage settings for the default Domain. This can be changed with --scope=domain:123, --scope=domain:*, or --scope=contact:123.

Details: Serialized

For settings that have a serialize=>XYZ option, it will behave like APIv4 "Setting" - e.g. contact_edit_options will look like an array (["1","2","3"...]) rather than ^1^2^3...^. This interpretation applies when displaying any/all configuration layers (ie default, explicit, mandatory).