
    Vh"                      J   d dl mZmZmZ eZdZdZdZd dl	Z	d dl
Z
d dlmZ d dlmZmZ e
j                   dk  rd d	lmZ  ed
g d      Z edddg      Zn,d dlmZmZ  ed
defdefdefg      Z eddefdee   fg      Zd ZddZd Zd Zd Zd Z d Z!e"dk(  r e!        yy)    )absolute_importdivisionprint_functiona  
module: homebrew_services
author:
  - "Kit Ham (@kitizz)"
requirements:
  - homebrew must already be installed on the target system
short_description: Services manager for Homebrew
version_added: 9.3.0
description:
  - Manages daemons and services using Homebrew.
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  name:
    description:
      - An installed homebrew package whose service is to be updated.
    aliases: ['formula']
    type: str
    required: true
  path:
    description:
      - A V(:) separated list of paths to search for C(brew) executable. Since a package (I(formula) in homebrew parlance)
        location is prefixed relative to the actual path of C(brew) command, providing an alternative C(brew) path enables
        managing different set of packages in an alternative location in the system.
    default: '/usr/local/bin:/opt/homebrew/bin:/home/linuxbrew/.linuxbrew/bin'
    type: path
  state:
    description:
      - State of the package's service.
    choices: ['present', 'absent', 'restarted']
    default: present
    type: str
a  
- name: Install foo package
  community.general.homebrew:
    name: foo
    state: present

- name: Start the foo service (equivalent to `brew services start foo`)
  community.general.homebrew_services:
    name: foo
    state: present

- name: Restart the foo service (equivalent to `brew services restart foo`)
  community.general.homebrew_services:
    name: foo
    state: restarted

- name: Remove the foo service (equivalent to `brew services stop foo`)
  community.general.homebrew_services:
    name: foo
    state: absent
a   
pid:
  description:
    - If the service is now running, this is the PID of the service, otherwise -1.
  returned: success
  type: int
  sample: 1234
running:
  description:
    - Whether the service is running after running this command.
  returned: success
  type: bool
  sample: true
N)AnsibleModule)HomebrewValidateparse_brew_path)      )
namedtupleHomebrewServiceArgsnamestate	brew_pathHomebrewServiceStaterunningpid)
NamedTupleOptionalr   r   r   c                 8   | j                   dd| j                  dg}|j                  |d      \  }}}	 t        j                  |      d   }t        d	   d
k(  |d         S # t        j
                  $ r$ |j                  dj                  |             Y Lw xY w)Nservicesinfoz--jsonTcheck_rcr   z Failed to parse JSON output:
{0}msgstatusstartedr   )r   r   )	r   r   run_commandjsonloadsJSONDecodeError	fail_jsonformatr   )argsmodulecmdrcstdoutstderrdatas          w/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/homebrew_services.py_brew_service_stater-      s    >>:vtyy(
CC++C$+?BQzz&!!$  X)(CeUU  Q@GGOPQs   A" "4BBc                     t        | |      }|dj                  ||      }|j                  ||j                  |j                  |       y )Nz>Running: {state.running}, Changed: {changed}, PID: {state.pid})r   changed)r   r   r   r/   )r-   r$   	exit_jsonr   r   )r%   r&   r/   messager   s        r,   _exit_with_stater2      sS    f-ELSSW T  	
 eiiPWX    c                 &   | j                   d   }t        j                  |      s!| j                  dj	                  |             | j                   d   }|dvr!| j                  dj	                  |             t        |       }t        |||      S )zIReuse the Homebrew module's validation logic to validate these arguments.r   zInvalid package name: {0}r   r   presentabsent	restartedzInvalid state: {0}r   )paramsr   valid_packager#   r$   r   r   )r&   packager   r   s       r,   validate_and_load_argumentsr<      s     mmF#G))'28??HIMM'"E66188?@'IG5INNr3   c                    t        | |      }|j                  rt        | |dd       |j                  rt        | |dd       | j                  dd| j
                  g}|j                  |d      \  }}}t        | |d	       y
)z>Start the requested brew service if it is not already running.FzService already runningr/   r1   TzService would be startedr   startr   r/   Nr-   r   r2   
check_moder   r   r   )r%   r&   r   	start_cmdr(   r)   r*   s          r,   start_servicerD      s|      f-E}}vu>WXvt=WXWdii@I++I+EBT640r3   c                    t        | |      }|j                  st        | |dd       |j                  rt        | |dd       | j                  dd| j
                  g}|j                  |d      \  }}}t        | |d	       y
)z1Stop the requested brew service if it is running.FzService already stoppedr>   TzService would be stoppedr   stopr   r@   NrA   )r%   r&   r   stop_cmdr(   r)   r*   s          r,   stop_servicerH      s|      f-E==vu>WXvt=WX
FDII>H++Ht+DBT640r3   c                     |j                   rt        | |dd       | j                  dd| j                  g}|j	                  |d      \  }}}t        | |d       y)	zDRestart the requested brew service. This always results in a change.TzService would be restartedr>   r   restartr   r@   N)rB   r2   r   r   r   )r%   r&   restart_cmdr(   r)   r*   s         r,   restart_servicerL      s_     &$0L	
 >>:y$))DK++K$+GBT640r3   c            
      f   t        t        t        dgdd      t        g dd      t        dd	
            d      } t        dddd      | _        t        |       }|j                  dk(  rt        ||        y |j                  dk(  rt        ||        y |j                  dk(  rt        ||        y y )NformulaTstr)aliasesrequiredtyper5   r6   )choicesdefaultz?/usr/local/bin:/opt/homebrew/bin:/home/linuxbrew/.linuxbrew/binpath)rT   rR   )r   r   rU   )argument_specsupports_check_modeC)LANGLC_ALLLC_MESSAGESLC_CTYPEr7   r8   )r   dictrun_command_environ_updater<   r   rD   rH   rL   )r&   service_argss     r,   mainr`      s    "
 :! Y
 !!F& )-#)F%
 /v6L Y&lF+			x	'\6*			{	*f- 
+r3   __main__)FN)#
__future__r   r   r   rR   __metaclass__DOCUMENTATIONEXAMPLESRETURNr    sysansible.module_utils.basicr   Cansible_collections.community.general.plugins.module_utils.homebrewr   r   version_infocollectionsr   r   r   typingr   r   rO   boolintr-   r2   r<   rD   rH   rL   r`   __name__ r3   r,   <module>rq      s    A @%N,
  
 4
 f& %=
 &&<y%>PQ , %~S?QR
 &)T!2UHSM4J K

V	YO 1"1"1!.H zF r3   