From ddccd1bb61664f42be95925184cca688e36b58a0 Mon Sep 17 00:00:00 2001 From: Valentin Maerten Date: Sat, 21 Sep 2024 11:17:15 -0400 Subject: [PATCH] feat: add TASK_OFFLINE env and expose it as a special variable (#1716) Co-authored-by: Pete Davison --- cmd/task/task.go | 1 + internal/flags/flags.go | 9 +++++++-- website/docs/reference/environment.mdx | 13 +++++++------ website/docs/reference/templating.mdx | 1 + 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/cmd/task/task.go b/cmd/task/task.go index 439ca9c5a7..b33d688e4e 100644 --- a/cmd/task/task.go +++ b/cmd/task/task.go @@ -198,6 +198,7 @@ func run() error { globals.Set("CLI_FORCE", ast.Var{Value: flags.Force || flags.ForceAll}) globals.Set("CLI_SILENT", ast.Var{Value: flags.Silent}) globals.Set("CLI_VERBOSE", ast.Var{Value: flags.Verbose}) + globals.Set("CLI_OFFLINE", ast.Var{Value: flags.Offline}) e.Taskfile.Vars.Merge(globals, nil) if !flags.Watch { diff --git a/internal/flags/flags.go b/internal/flags/flags.go index 4ae50c2efc..8ce7783504 100644 --- a/internal/flags/flags.go +++ b/internal/flags/flags.go @@ -1,9 +1,11 @@ package flags import ( + "cmp" "errors" "log" "os" + "strconv" "time" "github.com/spf13/pflag" @@ -77,7 +79,10 @@ func init() { log.Print(usage) pflag.PrintDefaults() } - + offline, err := strconv.ParseBool(cmp.Or(os.Getenv("TASK_OFFLINE"), "false")) + if err != nil { + offline = false + } pflag.BoolVar(&Version, "version", false, "Show Task version.") pflag.BoolVarP(&Help, "help", "h", false, "Shows Task usage.") pflag.BoolVarP(&Init, "init", "i", false, "Creates a new Taskfile.yml in the current folder.") @@ -120,7 +125,7 @@ func init() { // Remote Taskfiles experiment will adds the "download" and "offline" flags if experiments.RemoteTaskfiles.Enabled { pflag.BoolVar(&Download, "download", false, "Downloads a cached version of a remote Taskfile.") - pflag.BoolVar(&Offline, "offline", false, "Forces Task to only use local or cached Taskfiles.") + pflag.BoolVar(&Offline, "offline", offline, "Forces Task to only use local or cached Taskfiles.") pflag.DurationVar(&Timeout, "timeout", time.Second*10, "Timeout for downloading remote Taskfiles.") pflag.BoolVar(&ClearCache, "clear-cache", false, "Clear the remote cache.") } diff --git a/website/docs/reference/environment.mdx b/website/docs/reference/environment.mdx index ce9b7334b3..40ccfb6344 100644 --- a/website/docs/reference/environment.mdx +++ b/website/docs/reference/environment.mdx @@ -8,16 +8,17 @@ sidebar_position: 4 Task allows you to configure some behavior using environment variables. This page lists all the environment variables that Task supports. -| ENV | Default | Description | -| ----------------- | --------------- | ------------------------------------------------------------------------------------------------------------------------------------------- | -| `TASK_TEMP_DIR` | `.task` | Location of the temp dir. Can relative to the project like `tmp/task` or absolute like `/tmp/.task` or `~/.task`. | -| `TASK_REMOTE_DIR` | `TASK_TEMP_DIR` | Location of the remote temp dir (used for caching). Can relative to the project like `tmp/task` or absolute like `/tmp/.task` or `~/.task`. | -| `FORCE_COLOR` | | Force color output usage. | +| ENV | Default | Description | +|-------------------|-----------------|----------------------------------------------------------------------------------------------------------------------------------------------------| +| `TASK_TEMP_DIR` | `.task` | Location of the temp dir. Can relative to the project like `tmp/task` or absolute like `/tmp/.task` or `~/.task`. | +| `TASK_REMOTE_DIR` | `TASK_TEMP_DIR` | Location of the remote temp dir (used for caching). Can relative to the project like `tmp/task` or absolute like `/tmp/.task` or `~/.task`. | +| `TASK_OFFLINE` | `false` | Set the `--offline` flag through the environment variable. Only for remote experiment. CLI flag `--offline` takes precedence over the env variable | +| `FORCE_COLOR` | | Force color output usage. | ## Custom Colors | ENV | Default | Description | -| --------------------------- | ------- | ----------------------- | +|-----------------------------|---------|-------------------------| | `TASK_COLOR_RESET` | `0` | Color used for white. | | `TASK_COLOR_RED` | `31` | Color used for red. | | `TASK_COLOR_GREEN` | `32` | Color used for green. | diff --git a/website/docs/reference/templating.mdx b/website/docs/reference/templating.mdx index 33df58e876..6c9f738f4a 100644 --- a/website/docs/reference/templating.mdx +++ b/website/docs/reference/templating.mdx @@ -106,6 +106,7 @@ special variable will be overridden. | `CLI_FORCE` | A boolean containing whether the `--force` or `--force-all` flags were set. | | `CLI_SILENT` | A boolean containing whether the `--silent` flag was set. | | `CLI_VERBOSE` | A boolean containing whether the `--verbose` flag was set. | +| `CLI_OFFLINE` | A boolean containing whether the `--offline` flag was set. | | `TASK` | The name of the current task. | | `ALIAS` | The alias used for the current task, otherwise matches `TASK`. | | `TASK_EXE` | The Task executable name or path. |