Shim bare numbers into duration strings [#39]

Helm2's --timeout took a number of seconds, rather than the
ParseDuration-compatible string that helm3 uses. For backward-
compatibility, update a bare number into a duration string.
This commit is contained in:
Erin Call 2019-12-27 16:18:10 -08:00
parent 3b78f01b45
commit d5a59590a1
No known key found for this signature in database
GPG key ID: 4071FF6C15B8DAD1
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"`. * 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) * 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. * List\<string\>s can be a yaml sequence or a comma-separated string.
All of the following are equivalent: All of the following are equivalent:

View file

@ -4,8 +4,11 @@ import (
"fmt" "fmt"
"github.com/kelseyhightower/envconfig" "github.com/kelseyhightower/envconfig"
"io" "io"
"regexp"
) )
var justNumbers = regexp.MustCompile(`^\d+$`)
// The Config struct captures the `settings` and `environment` blocks in the application's drone // 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. 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 // 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 { if cfg.Debug && cfg.Stderr != nil {
cfg.logDebug() cfg.logDebug()
} }

View file

@ -106,6 +106,13 @@ func (suite *ConfigTestSuite) TestNewConfigWithConflictingVariables() {
suite.Equal("2m30s", cfg.Timeout) 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() { func (suite *ConfigTestSuite) TestNewConfigSetsWriters() {
stdout := &strings.Builder{} stdout := &strings.Builder{}
stderr := &strings.Builder{} stderr := &strings.Builder{}