cli: support int64 slice

This commit is contained in:
Alexander Wang 2023-03-17 17:37:01 -07:00
parent 7ba463c690
commit 060f5e9e05
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
4 changed files with 25 additions and 3 deletions

View file

@ -8,6 +8,7 @@ import (
"fmt" "fmt"
"os/exec" "os/exec"
"strconv" "strconv"
"strings"
"time" "time"
"oss.terrastruct.com/util-go/xdefer" "oss.terrastruct.com/util-go/xdefer"
@ -83,6 +84,12 @@ func (p *execPlugin) HydrateOpts(opts []byte) error {
allString[k] = vt allString[k] = vt
case int64: case int64:
allString[k] = strconv.Itoa(int(vt)) allString[k] = strconv.Itoa(int(vt))
case []int64:
str := make([]string, len(vt))
for i, v := range vt {
str[i] = strconv.Itoa(int(v))
}
allString[k] = strings.Join(str, ",")
case float64: case float64:
allString[k] = strconv.Itoa(int(vt)) allString[k] = strconv.Itoa(int(vt))
} }

View file

@ -44,6 +44,18 @@ func (f *PluginSpecificFlag) AddToOpts(opts *xmain.Opts) {
val = int64(defaultType) val = int64(defaultType)
} }
opts.Int64("", f.Name, "", val, f.Usage) opts.Int64("", f.Name, "", val, f.Usage)
case "[]int64":
var slice []int64
for _, v := range f.Default.([]interface{}) {
switch defaultType := v.(type) {
case int64:
slice = append(slice, defaultType)
case float64:
// json unmarshals numbers to float64
slice = append(slice, int64(defaultType))
}
}
opts.Int64Slice("", f.Name, "", slice, f.Usage)
} }
} }
@ -178,6 +190,9 @@ func HydratePluginOpts(ctx context.Context, ms *xmain.State, plugin Plugin) erro
case "int64": case "int64":
val, _ := ms.Opts.Flags.GetInt64(f.Name) val, _ := ms.Opts.Flags.GetInt64(f.Name)
opts[f.Tag] = val opts[f.Tag] = val
case "[]int64":
val, _ := ms.Opts.Flags.GetInt64Slice(f.Name)
opts[f.Tag] = val
} }
} }

2
go.mod generated
View file

@ -25,7 +25,7 @@ require (
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2
gonum.org/v1/plot v0.12.0 gonum.org/v1/plot v0.12.0
nhooyr.io/websocket v1.8.7 nhooyr.io/websocket v1.8.7
oss.terrastruct.com/util-go v0.0.0-20230308201337-8598db541d34 oss.terrastruct.com/util-go v0.0.0-20230318001724-cdc880b9efd6
) )
require ( require (

4
go.sum generated
View file

@ -327,6 +327,6 @@ honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
nhooyr.io/websocket v1.8.7 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g= nhooyr.io/websocket v1.8.7 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g=
nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0= nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0=
oss.terrastruct.com/util-go v0.0.0-20230308201337-8598db541d34 h1:ujjAH9q0hrfGnyFm9gQqxFtCjMBTqkakzOVQ1aaCztc= oss.terrastruct.com/util-go v0.0.0-20230318001724-cdc880b9efd6 h1:ao1swW83sc5jgczkdsY8U0ZgAD7XnHPB0o3GZzmquKA=
oss.terrastruct.com/util-go v0.0.0-20230308201337-8598db541d34/go.mod h1:eMWv0sOtD9T2RUl90DLWfuShZCYp4NrsqNpI8eqO6U4= oss.terrastruct.com/util-go v0.0.0-20230318001724-cdc880b9efd6/go.mod h1:eMWv0sOtD9T2RUl90DLWfuShZCYp4NrsqNpI8eqO6U4=
rsc.io/pdf v0.1.1 h1:k1MczvYDUvJBe93bYd7wrZLLUEcLZAuF824/I4e5Xr4= rsc.io/pdf v0.1.1 h1:k1MczvYDUvJBe93bYd7wrZLLUEcLZAuF824/I4e5Xr4=