Merge pull request #47 from pelotech/timeout-formatting

Shim bare numbers into duration strings
This commit is contained in:
Joachim Hill-Grannec 2019-12-28 09:27:40 -07:00 committed by GitHub
commit edb1fb6e27
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 0 deletions

View file

@ -67,6 +67,7 @@ Any setting (with the exception of `prefix`; [see below](#user-content-using-the
* Booleans can be yaml's `true` and `false` literals or the strings `"true"` and `"false"`.
* Durations are strings formatted with the syntax accepted by [golang's ParseDuration function](https://golang.org/pkg/time/#ParseDuration) (e.g. 5m30s)
* For backward-compatibility with drone-helm, a duration can also be an integer, in which case it will be interpreted to mean seconds.
* List\<string\>s can be a yaml sequence or a comma-separated string.
All of the following are equivalent:

View file

@ -4,8 +4,11 @@ import (
"fmt"
"github.com/kelseyhightower/envconfig"
"io"
"regexp"
)
var justNumbers = regexp.MustCompile(`^\d+$`)
// The Config struct captures the `settings` and `environment` blocks in the application's drone
// config. Configuration in drone's `settings` block arrives as uppercase env vars matching the
// config key, prefixed with `PLUGIN_`. Config from the `environment` block is uppercased, but does
@ -62,6 +65,10 @@ func NewConfig(stdout, stderr io.Writer) (*Config, error) {
}
}
if justNumbers.MatchString(cfg.Timeout) {
cfg.Timeout = fmt.Sprintf("%ss", cfg.Timeout)
}
if cfg.Debug && cfg.Stderr != nil {
cfg.logDebug()
}

View file

@ -106,6 +106,13 @@ func (suite *ConfigTestSuite) TestNewConfigWithConflictingVariables() {
suite.Equal("2m30s", cfg.Timeout)
}
func (suite *ConfigTestSuite) TestNewConfigInfersNumbersAreSeconds() {
suite.setenv("PLUGIN_TIMEOUT", "42")
cfg, err := NewConfig(&strings.Builder{}, &strings.Builder{})
suite.Require().NoError(err)
suite.Equal("42s", cfg.Timeout)
}
func (suite *ConfigTestSuite) TestNewConfigSetsWriters() {
stdout := &strings.Builder{}
stderr := &strings.Builder{}