-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvar.go
More file actions
62 lines (58 loc) · 1.19 KB
/
var.go
File metadata and controls
62 lines (58 loc) · 1.19 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// This file handles environment variable parsing
package main
import (
"fmt"
"os"
"strconv"
"github.com/oalders/is/types"
)
//nolint:cyclop
func (r *VarCmd) Run(ctx *types.Context) error {
ctx.Success = false
val, set := os.LookupEnv(r.Name)
switch r.Op {
case "set":
ctx.Success = set
return nil
case "unset":
ctx.Success = !set
return nil
case "true":
if !set {
return fmt.Errorf("environment variable %s is not set", r.Name)
}
boolVal, err := strconv.ParseBool(val)
if err != nil {
return fmt.Errorf(
"environment variable %s value %q cannot be parsed as boolean: %w",
r.Name,
val,
err,
)
}
ctx.Success = boolVal
return nil
case "false":
if !set {
return fmt.Errorf("environment variable %s is not set", r.Name)
}
boolVal, err := strconv.ParseBool(val)
if err != nil {
return fmt.Errorf(
"environment variable %s value %q cannot be parsed as boolean: %w",
r.Name,
val,
err,
)
}
ctx.Success = !boolVal
return nil
default:
if !set {
return fmt.Errorf("environment variable %s is not set", r.Name)
}
success, err := compareOutput(ctx, r.Compare, r.Op, val, r.Val)
ctx.Success = success
return err
}
}