folder-is-empty

Example:

# Create a folder if it doesn't exist yet.
- folder-is-empty:
    path: /tmp/freckles

Description

Ensure a folder exists on the filesystem and is empty.

If a file or folder already exists for that path, it will be deleted before the new folder will be created.

If the owner and/or group variables is/are specified, create those in case they don't exist yet.

If the owner variable is specified, this frecklet will use elevated permissions.

Variables

Name Type Default Description

path

string --

The path to the folder. Required

group

string --

The group of the folder, will be created if necessary.

mode

string --

The permissions of the folder.

owner

string --

The owner of the folder, will be created if necessary.

system_user

boolean False

Whether the user (and group) should be of system user/group type.

Examples

Example 1

Create a folder if it doesn't exist yet.

Code
- folder-is-empty:
    path: /tmp/freckles

Example 2

Create a folder incl. parent folders if necessary.

Code
- folder-is-empty:
    path: /tmp/parent_1/parent_2/freckles

Code

doc:
  short_help: Ensure a folder exists and is empty.
  help: |
    Ensure a folder exists on the filesystem and is empty.

    If a file or folder already exists for that path, it will be deleted before the new folder will be created.

    If the ``owner`` and/or ``group`` variables is/are specified, create those in case they don't exist yet.

    If the ``owner`` variable is specified, this frecklet will use elevated permissions.
  examples:
  - title: Create a folder if it doesn't exist yet.
    vars:
      path: /tmp/freckles
  - title: Create a folder incl. parent folders if necessary.
    vars:
      path: /tmp/parent_1/parent_2/freckles
args:
  path:
    doc:
      short_help: The path to the folder.
    type: string
    required: true
    cli:
      param_type: argument
  owner:
    doc:
      short_help: The owner of the folder, will be created if necessary.
    type: string
    required: false
    cli:
      metavar: USER_NAME
  group:
    doc:
      short_help: The group of the folder, will be created if necessary.
    type: string
    required: false
    cli:
      metavar: GROUP_NAME
  mode:
    doc:
      short_help: The permissions of the folder.
    type: string
    required: false
    cli:
      metavar: MODE
#  become:
#    doc:
#      short_help: Whether to use root privileges to create the folder.
#    type: boolean
#    default: false
#    required: false
#    cli:
#      is_flag: true
  system_user:
    doc:
      short_help: Whether the user (and group) should be of system user/group type.
    type: boolean
    default: false
    required: false
    cli:
      show_default: true
      is_flag: true

meta:
  is_interface: true
  tags:
  - file
  - filesystem
  - folder
  - mkdir
  - featured-frecklecutable

frecklets:
- group-exists:
    group: '{{:: group ::}}'
    system_group: '{{:: system_user ::}}'
    frecklet::skip: "{{:: group | true_if_empty_or('root') ::}}"
- user-exists:
    name: '{{:: owner ::}}'
    system_user: '{{:: system_user ::}}'
    frecklet::skip: "{{:: owner | true_if_empty_or('root') ::}}"
- path-is-absent:
    frecklet::desc:
      long: |
        Delete file or folder '{{:: path ::}}' if it exists (recursively, if folder).
    path: '{{:: path ::}}'
    become: '{{:: owner | true_if_not_empty ::}}'
- folder-exists:
    frecklet::desc:
      long: |
        Create folder '{{:: path ::}}' if it doesn't exist yet. Fail if '{{:: path ::}}' already exists and is not a folder.

        {%:: if mode ::%}Ensure the folders mode is '{{:: mode ::}}' (recursively, incl. children){%:: else ::%}Leave the folders mode as is (or default if folder has to be created){%:: endif ::%}. {%:: if group ::%}Set group (recursively, incl. children) to be '{{:: group ::}}'{%:: else ::%}Use the executing users main group (or leave be if file already exists){%:: endif ::%} and {%:: if owner ::%}set user to be '{{:: owner ::}}' (recursively){%:: else ::%}use the executing users name as the owner (or leave be if file already exists){%:: endif ::%}.
    become: '{{:: owner | true_if_not_empty ::}}'
    path: '{{:: path ::}}'
    owner: '{{:: owner ::}}'
    group: '{{:: group ::}}'
    mode: '{{:: mode ::}}'
frecklecute folder-is-empty --help

Usage: frecklecute folder-is-empty [OPTIONS] PATH

  Ensure a folder exists on the filesystem and is empty.

  If a file or folder already exists for that path, it will be deleted
  before the new folder will be created.

  If the ``owner`` and/or ``group`` variables is/are specified, create those
  in case they don't exist yet.

  If the ``owner`` variable is specified, this frecklet will use elevated
  permissions.

Options:
  --group GROUP_NAME              The group of the folder, will be created if
                                  necessary.
  --mode MODE                     The permissions of the folder.
  --owner USER_NAME               The owner of the folder, will be created if
                                  necessary.
  --system-user / --no-system-user
                                  Whether the user (and group) should be of
                                  system user/group type.
  --help                          Show this message and exit.
# -*- coding: utf-8 -*-


#
# module path: pycklets.folder_is_empty.FolderIsEmpty
#


from dataclasses import dataclass
from pyckles import AutoPycklet
from typing import *    # noqa

@dataclass
class FolderIsEmpty(AutoPycklet):
    """Ensure a folder exists on the filesystem and is empty.

     If a file or folder already exists for that path, it will be deleted before the new folder will be created.

     If the ``owner`` and/or ``group`` variables is/are specified, create those in case they don't exist yet.

     If the ``owner`` variable is specified, this frecklet will use elevated permissions.

       Args:
         group: The group of the folder, will be created if necessary.
         mode: The permissions of the folder.
         owner: The owner of the folder, will be created if necessary.
         path: The path to the folder.
         system_user: Whether the user (and group) should be of system user/group type.

    """

    FRECKLET_ID = "folder-is-empty"

    group: str = None
    mode: str = None
    owner: str = None
    path: str = None
    system_user: bool = None


    def __post_init__(self):
        super(FolderIsEmpty, self).__init__(var_names=["group", "mode", "owner", "path", "system_user"])


frecklet_class = FolderIsEmpty
# -*- coding: utf-8 -*-


#
# module path: pycklets.folder_is_empty.FolderIsEmpty
#


from pyckles import AutoPycklet

class FolderIsEmpty(AutoPycklet):
    """Ensure a folder exists on the filesystem and is empty.

     If a file or folder already exists for that path, it will be deleted before the new folder will be created.

     If the ``owner`` and/or ``group`` variables is/are specified, create those in case they don't exist yet.

     If the ``owner`` variable is specified, this frecklet will use elevated permissions.

       Args:
         group: The group of the folder, will be created if necessary.
         mode: The permissions of the folder.
         owner: The owner of the folder, will be created if necessary.
         path: The path to the folder.
         system_user: Whether the user (and group) should be of system user/group type.

    """

    FRECKLET_ID = "folder-is-empty"

    def __init__(self, group=None, mode=None, owner=None, path=None, system_user=None):

        super(FolderIsEmpty, self).__init__(var_names=["group", "mode", "owner", "path", "system_user"])
        self._group = group
        self._mode = mode
        self._owner = owner
        self._path = path
        self._system_user = system_user

    @property
    def group(self):
        return self._group

    @group.setter
    def group(self, group):
        self._group = group

    @property
    def mode(self):
        return self._mode

    @mode.setter
    def mode(self, mode):
        self._mode = mode

    @property
    def owner(self):
        return self._owner

    @owner.setter
    def owner(self, owner):
        self._owner = owner

    @property
    def path(self):
        return self._path

    @path.setter
    def path(self, path):
        self._path = path

    @property
    def system_user(self):
        return self._system_user

    @system_user.setter
    def system_user(self, system_user):
        self._system_user = system_user



frecklet_class = FolderIsEmpty