Skip to content

Commit 1868040

Browse files
committed
feat: add TASK_OFFLINE env and expose it as a special variable
1 parent cdafc67 commit 1868040

File tree

5 files changed

+23
-8
lines changed

5 files changed

+23
-8
lines changed

cmd/task/task.go

+1
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ func run() error {
189189
globals.Set("CLI_FORCE", ast.Var{Value: flags.Force || flags.ForceAll})
190190
globals.Set("CLI_SILENT", ast.Var{Value: flags.Silent})
191191
globals.Set("CLI_VERBOSE", ast.Var{Value: flags.Verbose})
192+
globals.Set("CLI_OFFLINE", ast.Var{Value: flags.Offline})
192193
e.Taskfile.Vars.Merge(globals, nil)
193194

194195
if !flags.Watch {

internal/env/env.go

+7
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,10 @@ func isTypeAllowed(v any) bool {
3535
return false
3636
}
3737
}
38+
39+
func GetEnvOrDefault(key, fallback string) string {
40+
if value, ok := os.LookupEnv(key); ok {
41+
return value
42+
}
43+
return fallback
44+
}

internal/flags/flags.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import (
44
"errors"
55
"log"
66
"os"
7+
"strconv"
78
"time"
89

910
"github.com/spf13/pflag"
1011

12+
"github.com/go-task/task/v3/internal/env"
1113
"github.com/go-task/task/v3/internal/experiments"
1214
"github.com/go-task/task/v3/taskfile/ast"
1315
)
@@ -76,7 +78,10 @@ func init() {
7678
log.Print(usage)
7779
pflag.PrintDefaults()
7880
}
79-
81+
offline, err := strconv.ParseBool(env.GetEnvOrDefault("TASK_OFFLINE", "false"))
82+
if err != nil {
83+
offline = false
84+
}
8085
pflag.BoolVar(&Version, "version", false, "Show Task version.")
8186
pflag.BoolVarP(&Help, "help", "h", false, "Shows Task usage.")
8287
pflag.BoolVarP(&Init, "init", "i", false, "Creates a new Taskfile.yml in the current folder.")
@@ -118,7 +123,7 @@ func init() {
118123
// Remote Taskfiles experiment will adds the "download" and "offline" flags
119124
if experiments.RemoteTaskfiles.Enabled {
120125
pflag.BoolVar(&Download, "download", false, "Downloads a cached version of a remote Taskfile.")
121-
pflag.BoolVar(&Offline, "offline", false, "Forces Task to only use local or cached Taskfiles.")
126+
pflag.BoolVar(&Offline, "offline", offline, "Forces Task to only use local or cached Taskfiles.")
122127
pflag.DurationVar(&Timeout, "timeout", time.Second*10, "Timeout for downloading remote Taskfiles.")
123128
pflag.BoolVar(&ClearCache, "clear-cache", false, "Clear the remote cache.")
124129
}

website/docs/reference/environment.mdx

+7-6
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@ sidebar_position: 4
88
Task allows you to configure some behavior using environment variables. This
99
page lists all the environment variables that Task supports.
1010

11-
| ENV | Default | Description |
12-
| ----------------- | --------------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
13-
| `TASK_TEMP_DIR` | `.task` | Location of the temp dir. Can relative to the project like `tmp/task` or absolute like `/tmp/.task` or `~/.task`. |
14-
| `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`. |
15-
| `FORCE_COLOR` | | Force color output usage. |
11+
| ENV | Default | Description |
12+
|-------------------|-----------------|----------------------------------------------------------------------------------------------------------------------------------------------------|
13+
| `TASK_TEMP_DIR` | `.task` | Location of the temp dir. Can relative to the project like `tmp/task` or absolute like `/tmp/.task` or `~/.task`. |
14+
| `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`. |
15+
| `TASK_OFFLINE` | `false` | Set the `--offline` flag through the environment variable. Only for remote experiment. CLI flag `--offline` takes precedence over the env variable |
16+
| `FORCE_COLOR` | | Force color output usage. |
1617

1718
## Custom Colors
1819

1920
| ENV | Default | Description |
20-
| --------------------------- | ------- | ----------------------- |
21+
|-----------------------------|---------|-------------------------|
2122
| `TASK_COLOR_RESET` | `0` | Color used for white. |
2223
| `TASK_COLOR_RED` | `31` | Color used for red. |
2324
| `TASK_COLOR_GREEN` | `32` | Color used for green. |

website/docs/reference/templating.mdx

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ special variable will be overridden.
106106
| `CLI_FORCE` | A boolean containing whether the `--force` or `--force-all` flags were set. |
107107
| `CLI_SILENT` | A boolean containing whether the `--silent` flag was set. |
108108
| `CLI_VERBOSE` | A boolean containing whether the `--verbose` flag was set. |
109+
| `CLI_OFFLINE` | A boolean containing whether the `--offline` flag was set. |
109110
| `TASK` | The name of the current task. |
110111
| `TASK_EXE` | The Task executable name or path. |
111112
| `ROOT_TASKFILE` | The absolute path of the root Taskfile. |

0 commit comments

Comments
 (0)