-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from reverbdotcom/hs/set-dur
add configurable duration; add env command to see config
- Loading branch information
Showing
10 changed files
with
177 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package env | ||
|
||
import ( | ||
"os" | ||
"fmt" | ||
"time" | ||
"github.com/joho/godotenv" | ||
"github.com/reverbdotcom/sbx/errr" | ||
) | ||
|
||
const format = "%-20s%-20s%-120s\n" | ||
const durationTooLong = 8 * time.Hour | ||
|
||
const DURATION = "DURATION" | ||
var allowlist = map[string]string{ | ||
DURATION: "how long sandboxes live for", | ||
} | ||
|
||
var warning = errr.Warning | ||
|
||
func Run() (string, error) { | ||
output := fmt.Sprintf(format, "KEY", "VALUE", "DESCRIPTION") | ||
output += fmt.Sprint("\n") | ||
|
||
for key, description := range allowlist { | ||
val := Getenv(key) | ||
if val == "" { | ||
val = "n/a" | ||
} | ||
|
||
output += fmt.Sprintf(format, key, val, description) | ||
} | ||
|
||
return output, nil | ||
} | ||
|
||
func Verify() (error) { | ||
fmtErr := func(msg string) error { | ||
return fmt.Errorf("invalid env, please fix the following issue: %s", msg) | ||
} | ||
|
||
dur := Getenv(DURATION) | ||
if dur != "" { | ||
parsed, err := time.ParseDuration(dur) | ||
if err != nil { | ||
return fmtErr(fmt.Sprintf("%s is an invalid duration", dur)) | ||
} | ||
|
||
if parsed > durationTooLong { | ||
warning(fmt.Sprintf("%s is a long duration! this is okay on occassion, but consider lowering it", dur)) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
var Getenv = _getenv | ||
func _getenv(key string) string { | ||
return readenv()[key] | ||
} | ||
|
||
func readenv() map[string]string { | ||
filepath := fmt.Sprintf("%s/.sbx", os.Getenv("HOME")) | ||
|
||
var err error | ||
env, err := godotenv.Read(filepath) | ||
if err != nil { | ||
env = make(map[string]string) | ||
} | ||
|
||
return env | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package env | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestVerify(t *testing.T) { | ||
t.Run("it errs if duration is invalid", func(t *testing.T) { | ||
Getenv = func(key string) string { | ||
if (key == DURATION) { | ||
return "invalid" | ||
} | ||
|
||
return "" | ||
} | ||
|
||
err := Verify() | ||
if err == nil { | ||
t.Errorf("got nil, want error") | ||
} | ||
}) | ||
|
||
t.Run("it warns when duration is too long", func(t *testing.T) { | ||
Getenv = func(key string) string { | ||
if (key == DURATION) { | ||
return "10h" | ||
} | ||
|
||
return "" | ||
} | ||
|
||
warned := false | ||
warning = func(message string) { | ||
warned = true | ||
} | ||
|
||
_ = Verify() | ||
if !warned { | ||
t.Error("got no warning, want warning about duration being too long") | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters