Skip to content

Commit

Permalink
Add var subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
oalders committed Jan 19, 2025
1 parent 44ec9b9 commit 4acd864
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ can get you up and running in a hurry.
is cli version tmux gt 3.2 && echo 🥳 || echo 😢
```

### Is Neovim the Default Editor?

```bash
is var EDITOR set && is var EDITOR eq nvim
```

### Is this the target Operating System?

```bash
Expand Down Expand Up @@ -755,6 +761,17 @@ This is useful for scripts where we want to install via `sudo`, but we don't
want the script to be interactive. That means we can skip installing things
that require `sudo` and handle them in some other place.

### var

Check if environment variables are set, unset or contain a specific value.

```bash
is var EDITOR set && is var EDITOR like vim
```

`set` and `unset` don't require arguments, but the other comparison operators
(eq|ne|gt|gte|in|like|lt|lte|unlike) do.

### known

Prints known information about a resource to `STDOUT`. Returns `0` on success
Expand Down Expand Up @@ -972,6 +989,9 @@ Commands:
user [<sudoer>]
Info about current user. e.g. "is user sudoer"
var <name> <op> [<val>]
Check environment variables. e.g. "is var EDITOR eq nvim"
install-completions
install shell completions. e.g. "is install-completions" and then run the
command which is printed to your terminal to get completion in your current
Expand Down
20 changes: 20 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// package main contains the api for the CLI
package main

import (
"fmt"
)

type AgeCmp struct {
Name string `arg:"" required:"" help:"[name of command or path to command]"`
Op string `arg:"" required:"" enum:"gt,lt" help:"[gt|lt]"`
Expand Down Expand Up @@ -68,6 +72,22 @@ type UserCmd struct {
Sudoer string `arg:"" required:"" default:"sudoer" enum:"sudoer" help:"is current user a passwordless sudoer. e.g. \"is user sudoer\""`
}

// VarCmd type is configuration for environment variable checks.
//
//nolint:lll
type VarCmd struct {
Name string `arg:"" required:""`
Op string `arg:"" required:"" enum:"set,unset,eq,ne,gt,gte,in,lt,lte,like,unlike" help:"[set|unset|eq|ne|gt|gte|in|like|lt|lte|unlike]"`
Val string `arg:"" optional:""`
}

func (r *VarCmd) Validate() error {
if r.Op != "set" && r.Op != "unset" && r.Val == "" {
return fmt.Errorf("missing required argument: val")
}
return nil
}

type KnownCLI struct {
Attr string `arg:"" name:"attribute" required:"" enum:"version"`
Name string `arg:"" required:""`
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func main() {
OS OSCmd `cmd:"" help:"Check OS attributes. e.g. \"is os name eq darwin\""`
There ThereCmd `cmd:"" help:"Check if command exists. e.g. \"is there git\""`
User UserCmd `cmd:"" help:"Info about current user. e.g. \"is user sudoer\""`
Var VarCmd `cmd:"" help:"Check environment variables. e.g. \"is var EDITOR eq nvim\""`

Check failure on line 23 in main.go

View workflow job for this annotation

GitHub Actions / precious

The line is 102 characters long, which exceeds the maximum of 100 characters. (lll)
Version kong.VersionFlag `help:"Print version to screen"`

InstallCompletions kongplete.InstallCompletions `cmd:"" help:"install shell completions. e.g. \"is install-completions\" and then run the command which is printed to your terminal to get completion in your current session. add the command to a .bashrc or similar to get completion across all sessions."` //nolint:lll
Expand Down
56 changes: 56 additions & 0 deletions test/var.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env bats

bats_require_minimum_version 1.5.0

setup() {
export TEST_VAR="nvim"
}

teardown() {
unset TEST_VAR
}

@test "is var TEST_VAR set" {
run ./is var TEST_VAR set
[ "$status" -eq 0 ]
}

@test "is var TEST_VAR unset" {
run ./is var TEST_VAR unset
[ "$status" -eq 1 ]
}

@test "is var TEST_VAR eq nvim" {
run ./is var TEST_VAR eq nvim
[ "$status" -eq 0 ]
}

@test "is var TEST_VAR eq vim" {
run ./is var TEST_VAR eq vim
[ "$status" -eq 1 ]
}

@test "is var TEST_VAR gt 1" {
run ./is var TEST_VAR gt 1
[ "$status" -eq 1 ]
}

@test "is var TEST_VAR like nv.*" {
run ./is var TEST_VAR like "nv.*"
[ "$status" -eq 0 ]
}

@test "is var TEST_VAR unlike nv.*" {
run ./is var TEST_VAR unlike "nv.*"
[ "$status" -eq 1 ]
}

@test "is var NON_EXISTENT_VAR set" {
run ./is var NON_EXISTENT_VAR set
[ "$status" -eq 1 ]
}

@test "is var NON_EXISTENT_VAR unset" {
run ./is var NON_EXISTENT_VAR unset
[ "$status" -eq 0 ]
}
35 changes: 35 additions & 0 deletions var.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// This file handles environment variable parsing
package main

import (
"errors"
"os"

"github.com/oalders/is/compare"
"github.com/oalders/is/types"
)

func (r *VarCmd) Run(ctx *types.Context) error {
ctx.Success = false

switch r.Op {
case "set":
_, exists := os.LookupEnv(r.Name)
if exists {
ctx.Success = true
}
return nil
case "unset":
_, exists := os.LookupEnv(r.Name)
if !exists {
ctx.Success = true
}
return nil
default:
val, exists := os.LookupEnv(r.Name)
if !exists {
return errors.New("environment variable not set")
}
return compare.Optimistic(ctx, r.Op, val, r.Val)
}
}

0 comments on commit 4acd864

Please sign in to comment.