###############################################################################
# Detect the OS
###############################################################################
# Full path to the helper that exists only on Windows
locals {
windows_helper = "${abspath(path.module)}\\printf.cmd"
}
# If the helper file exists, we’re on Windows; otherwise assume Linux
data "external" "detect_os" {
program = fileexists(local.windows_helper) ? [local.windows_helper, "{\"os\":\"Windows\"}"] : ["printf", "{\"os\":\"Linux\"}"]
}
locals {
os = data.external.detect_os.result.os
is_windows = lower(local.os) == "windows"
is_linux = lower(local.os) == "linux"
}
# Linux branch
data "external" "generate_linux_timestamp" {
count = local.is_linux ? 1 : 0
working_dir = var.working_dir == null ? path.module : var.working_dir
program = [
"bash",
"-c",
"DATE=$(date '${var.linux_timestamp_format}'); printf '{\"id\":\"%s\",\"timestamp\":\"%s\"}' \"$DATE\" \"$DATE\""
]
}
# Windows branch
data "external" "generate_windows_timestamp" {
count = local.is_windows ? 1 : 0
working_dir = var.working_dir == null ? path.module : var.working_dir
program = [
"powershell",
"-Command",
"$date = Get-Date -Format '${var.windows_timestamp_format}'; $json = @{ id = $date; timestamp = $date } | ConvertTo-Json -Compress; Write-Output $json"
]
}
###############################################################################
# Normalise the result
###############################################################################
locals {
timestamp = local.is_linux ? lower(data.external.generate_linux_timestamp[0].result.timestamp) : lower(data.external.generate_windows_timestamp[0].result.timestamp)
}
No requirements.
Name | Version |
---|---|
external | n/a |
No modules.
Name | Type |
---|---|
external_external.detect_os | data source |
external_external.generate_linux_timestamp | data source |
external_external.generate_windows_timestamp | data source |
Name | Description | Type | Default | Required |
---|---|---|---|---|
linux_timestamp_format | The format of the timestamp to generate on Linux | string |
"+%d-%m-%Y:%H:%M" |
no |
windows_timestamp_format | The format of the timestamp to generate on Windows | string |
"dd-MM-yyyy:HH:mm" |
no |
working_dir | The working directory for the module | string |
null |
no |
Name | Description |
---|---|
is_linux | True if the OS is Linux |
is_windows | True if the OS is Windows |
os | The OS that is running the commands |
timestamp | A random string generated by the external data source |