Skip to content

Commit

Permalink
direnv version <version_at_least>
Browse files Browse the repository at this point in the history
Add a stdlib function to check that direnv is at least as old as the
desired version.
  • Loading branch information
zimbatm committed Jan 25, 2020
1 parent d374661 commit fc2454f
Show file tree
Hide file tree
Showing 10 changed files with 494 additions and 2 deletions.
23 changes: 21 additions & 2 deletions cmd_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,34 @@ package main

import (
"fmt"
"strings"

"golang.org/x/mod/semver"
)

// CmdVersion is `direnv version`
var CmdVersion = &Cmd{
Name: "version",
Desc: "prints the version (" + Version + ")",
Desc: "prints the version (" + Version + ") or checks that direnv is older than VERSION_AT_LEAST.",
Args: []string{"[VERSION_AT_LEAST]"},
Aliases: []string{"--version"},
Action: actionSimple(func(env Env, args []string) error {
fmt.Println(Version)
semVersion := "v" + Version
if len(args) > 1 {
atLeast := args[1]
if !strings.HasPrefix(atLeast, "v") {
atLeast = "v" + atLeast
}
if !semver.IsValid(atLeast) {
return fmt.Errorf("%s is not a valid semver version", atLeast)
}
cmp := semver.Compare(semVersion, atLeast)
if cmp < 0 {
return fmt.Errorf("%s it older than the desired %s", semVersion, atLeast)
}
} else {
fmt.Println(Version)
}
return nil
}),
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ go 1.12
require (
github.com/BurntSushi/toml v0.3.1
github.com/direnv/go-dotenv v0.0.0-20181227095604-4cce6d1a66f7
golang.org/x/mod v0.2.0
)
13 changes: 13 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,16 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/direnv/go-dotenv v0.0.0-20181227095604-4cce6d1a66f7 h1:JsyIu/zmj8ZuCbyTRYczeOaxLsnSDCAmRwHa0jl4YqY=
github.com/direnv/go-dotenv v0.0.0-20181227095604-4cce6d1a66f7/go.mod h1:T/OO6ZHPlA9WCvpwKhgyBiWiMcLwbP/rsGgxkxVHk6g=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.2.0 h1:KU7oHjnv3XNWfa5COkzUifxZmxp1TyI7ImMXqFxLwvQ=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
6 changes: 6 additions & 0 deletions man/direnv-stdlib.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,12 @@ Example (.envrc):

watch_file Gemfile

### `direnv_version <version_at_least>`

Checks that the direnv version is at least old as `version_at_least`. This can
be useful when sharing an `.envrc` and to make sure that the users are up to
date.

COPYRIGHT
---------

Expand Down
7 changes: 7 additions & 0 deletions stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,13 @@ const StdLib = "#!/usr/bin/env bash\n" +
" eval \"$(guix environment \"$@\" --search-paths)\"\n" +
"}\n" +
"\n" +
"# Usage: direnv_version <version_at_least>\n" +
"#\n" +
"# Checks that the direnv version is at least old as <version_at_least>.\n" +
"direnv_version() {\n" +
" \"$direnv\" version \"$@\"\n" +
"}\n" +
"\n" +
"# Usage: __main__ <cmd> [...<args>]\n" +
"#\n" +
"# Used by rc.go\n" +
Expand Down
7 changes: 7 additions & 0 deletions stdlib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,13 @@ use_guix() {
eval "$(guix environment "$@" --search-paths)"
}

# Usage: direnv_version <version_at_least>
#
# Checks that the direnv version is at least old as <version_at_least>.
direnv_version() {
"$direnv" version "$@"
}

# Usage: __main__ <cmd> [...<args>]
#
# Used by rc.go
Expand Down
27 changes: 27 additions & 0 deletions vendor/golang.org/x/mod/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions vendor/golang.org/x/mod/PATENTS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit fc2454f

Please sign in to comment.