forked from direnv/direnv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_version.go
35 lines (32 loc) · 868 Bytes
/
cmd_version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package main
import (
"fmt"
"strings"
"golang.org/x/mod/semver"
)
// CmdVersion is `direnv version`
var CmdVersion = &Cmd{
Name: "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 {
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("current version %s is older than the desired version %s", semVersion, atLeast)
}
} else {
fmt.Println(Version)
}
return nil
}),
}