Skip to content

Commit

Permalink
Merge branch 'master' into linting-corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
ocean authored Dec 27, 2024
2 parents a45e051 + fb5031c commit 3c32c33
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
docs/_build
# Ignore the ahoy binary when it's built
ahoy

.idea
builds/*

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Download the [latest release from GitHub](https://github.com/ahoy-cli/ahoy/relea

Example:
```
os=$(uname -s | tr [:upper:] [:lower:]) && architecture=$(case $(uname -m) in x86_64 | amd64) echo "amd64" ;; aarch64 | arm64 | armv8) echo "arm64" ;; *) echo "amd64" ;; esac) && sudo wget -q https://github.com/ahoy-cli/ahoy/releases/latest/download/ahoy-bin-$os-$architecture -O /usr/local/bin/ahoy && sudo chown $USER /usr/local/bin/ahoy && chmod +x /usr/local/bin/ahoy
os=$(uname -s | tr '[:upper:]' '[:lower:]') && architecture=$(case $(uname -m) in x86_64 | amd64) echo "amd64" ;; aarch64 | arm64 | armv8) echo "arm64" ;; *) echo "amd64" ;; esac) && sudo wget -q https://github.com/ahoy-cli/ahoy/releases/latest/download/ahoy-bin-$os-$architecture -O /usr/local/bin/ahoy && sudo chown $USER /usr/local/bin/ahoy && chmod +x /usr/local/bin/ahoy
```

### Windows
Expand Down
41 changes: 41 additions & 0 deletions ahoy.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Config struct {
AhoyAPI string
Commands map[string]Command
Entrypoint []string
Env string
}

// Command is an ahoy command detailed in ahoy.yml files. Multiple
Expand All @@ -32,6 +33,7 @@ type Command struct {
Description string
Usage string
Cmd string
Env string
Hide bool
Optional bool
Imports []string
Expand Down Expand Up @@ -172,8 +174,38 @@ func getSubCommands(includes []string) []cli.Command {
return subCommands
}

// Given a filepath, return a string array of environment variables.
func getEnvironmentVars(envFile string) []string {
var envVars []string

env, err := os.ReadFile(envFile)
if err != nil {
logger("fatal", "Invalid env file: "+envFile)
return nil
}

lines := strings.Split(string(env), "\n")
for _, line := range lines {
line = strings.TrimSpace(line)

// Ignore empty lines and comments (lines starting with '#').
if line == "" || strings.HasPrefix(line, "#") {
continue
}
envVars = append(envVars, line)
}
return envVars
}

func getCommands(config Config) []cli.Command {
exportCmds := []cli.Command{}
envVars := []string{}

// Get environment variables from the 'global' environment variable file, if it is defined.
if config.Env != "" {
globalEnvFile := filepath.Join(AhoyConf.srcDir, config.Env)
envVars = append(envVars, getEnvironmentVars(globalEnvFile)...)
}

var keys []string
for k := range config.Commands {
Expand Down Expand Up @@ -239,6 +271,14 @@ func getCommands(config Config) []cli.Command {
}
cmdItems = append(cmdEntrypoint, cmdArgs...)

// If defined, included specified command-level environment variables.
// Note that this will intentionally override any conflicting variables
// defined in the 'global' env file.
if cmd.Env != "" {
cmdEnvFile := filepath.Join(AhoyConf.srcDir, cmd.Env)
envVars = append(envVars, getEnvironmentVars(cmdEnvFile)...)
}

if verbose {
log.Println("===> AHOY", name, "from", sourcefile, ":", cmdItems)
}
Expand All @@ -247,6 +287,7 @@ func getCommands(config Config) []cli.Command {
command.Stdout = os.Stdout
command.Stdin = os.Stdin
command.Stderr = os.Stderr
command.Env = append(command.Env, envVars...)
if err := command.Run(); err != nil {
fmt.Fprintln(os.Stderr)
os.Exit(1)
Expand Down
8 changes: 5 additions & 3 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ sudo wget -q https://github.com/ahoy-cli/ahoy/releases/download/1.1.0/ahoy-`unam
sudo wget -q https://github.com/ahoy-cli/ahoy/releases/download/2.0.0/ahoy-bin-`uname -s`-amd64 -O /usr/local/bin/ahoy && sudo chown $USER /usr/local/bin/ahoy && chmod +x /usr/local/bin/ahoy
```

### Bash / Zsh Completion
For Zsh, Just add this to your ~/.zshrc, and your completions will be relative to the directory you're in.
## Autocompletions

`complete -F "ahoy --generate-bash-completion" ahoy`
### Zsh
For Zsh completions, we have a standalone plugin available at [ahoy-cli/zsh-ahoy](https://github.com/ahoy-cli/zsh-ahoy).

### Bash

For Bash, you'll need to make sure you have bash-completion installed and setup. On OSX with homebrew it looks like this:

Expand Down
6 changes: 6 additions & 0 deletions testdata/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Variables defined in this file should be accessible to all Ahoy commands,
# since it is to be included in the 'global' environment variable.
GLOBAL=global

# This variable is to be overridden by the command-level env file, '.env.cmd'.
TO_BE_OVERRIDDEN=before
2 changes: 2 additions & 0 deletions testdata/.env.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
COMMAND=123456789
TO_BE_OVERRIDDEN=after
17 changes: 17 additions & 0 deletions testdata/env.ahoy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
ahoyapi: v2
env: ./.env
commands:
test-global:
cmd: echo $GLOBAL

test-cmd:
cmd: echo $COMMAND
env: .env.cmd

test-override:
cmd: echo $TO_BE_OVERRIDDEN
env: .env.cmd

test-invalid-env:
cmd: echo "This should not print!"
env: .env.thisfiledoesntexist
21 changes: 21 additions & 0 deletions tests/environment-variables.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bats

@test "Command-level variables can be defined and used" {
run ./ahoy -f testdata/env.ahoy.yml test-cmd
[[ "$output" == "123456789" ]]
}

@test "Environment variables can be overriden" {
run ./ahoy -f testdata/env.ahoy.yml test-override
[[ "$output" = "after" ]]
}

@test "Global variables can be defined and used" {
run ./ahoy -f testdata/env.ahoy.yml test-global
[[ "$output" = "global" ]]
}

@test "Fail when attempting to load invalid env files" {
run ./ahoy -f testdata/env.ahoy.yml test-invalid-env
[ $status -eq 1 ]
}

0 comments on commit 3c32c33

Please sign in to comment.