Skip to content

Directory Resource

Synopsis

Manages directories on the local filesystem. Supports creating directories, copying directory contents from a source, and removing directories. Works on Windows, Linux, and macOS.

Type

OpenDsc.FileSystem/Directory

Capabilities

  • Get
  • Set
  • Test
  • Delete

Properties

path

Path to the directory.

Type: string
Required: Yes
Access: Read/Write
Default value: None

sourcePath

Source directory to copy contents from.

Type: string
Required: No
Access: Read/Write
Default value: None

_exist

Whether the directory should exist.

Type: bool
Required: No
Access: Read/Write
Default value: true

_inDesiredState

Whether the directory is in the desired state.

Type: bool
Required: No
Access: Read-Only
Default value: None

Examples

Example 1 — Get a directory

$resourceInput = @'
path: /var/log/myapp
'@

dsc resource get -r OpenDsc.FileSystem/Directory --input $resourceInput
resource_input=$(cat <<'EOF'
path: /var/log/myapp
EOF
)

dsc resource get -r OpenDsc.FileSystem/Directory --input "$resource_input"

Example 2 — Create a directory

$resourceInput = @'
path: /var/log/myapp
'@

dsc resource set -r OpenDsc.FileSystem/Directory --input $resourceInput
resource_input=$(cat <<'EOF'
path: /var/log/myapp
EOF
)

dsc resource set -r OpenDsc.FileSystem/Directory --input "$resource_input"

Example 3 — Copy directory contents from source

$resourceInput = @'
path: /opt/myapp/config
sourcePath: /opt/myapp/config-template
'@

dsc resource set -r OpenDsc.FileSystem/Directory --input $resourceInput
resource_input=$(cat <<'EOF'
path: /opt/myapp/config
sourcePath: /opt/myapp/config-template
EOF
)

dsc resource set -r OpenDsc.FileSystem/Directory --input "$resource_input"

Example 4 — Delete a directory

$resourceInput = @'
path: /tmp/staging
'@

dsc resource delete -r OpenDsc.FileSystem/Directory --input $resourceInput
resource_input=$(cat <<'EOF'
path: /tmp/staging
EOF
)

dsc resource delete -r OpenDsc.FileSystem/Directory --input "$resource_input"

Example 5 — Configuration document

$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
  - name: Application log directory
    type: OpenDsc.FileSystem/Directory
    properties:
      path: /var/log/myapp

  - name: Application configuration
    type: OpenDsc.FileSystem/Directory
    properties:
      path: /opt/myapp/config
      sourcePath: /opt/myapp/config-template

Exit codes

Code Description
0 Success
1 Error
2 Invalid JSON
3 Access denied
4 Invalid argument
5 IO error
6 Access denied