mirror of
https://github.com/foomo/posh-providers.git
synced 2025-10-16 12:35:41 +00:00
feat(grafana/k6): add secret support
This commit is contained in:
parent
f506782d34
commit
8e4b563e02
@ -4,15 +4,18 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/foomo/posh-providers/onepassword"
|
||||
"github.com/foomo/posh/pkg/command/tree"
|
||||
"github.com/foomo/posh/pkg/env"
|
||||
"github.com/foomo/posh/pkg/log"
|
||||
"github.com/foomo/posh/pkg/prompt/goprompt"
|
||||
"github.com/foomo/posh/pkg/readline"
|
||||
"github.com/foomo/posh/pkg/shell"
|
||||
"github.com/foomo/posh/pkg/util/files"
|
||||
"github.com/foomo/posh/pkg/util/suggests"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
@ -74,7 +77,7 @@ func NewCommand(l log.Logger, op *onepassword.OnePassword, opts ...CommandOption
|
||||
Name: inst.name,
|
||||
Description: "Run k6",
|
||||
Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error {
|
||||
fs.Default().Bool("debug", false, "show debug output")
|
||||
fs.Default().Bool("verbose", false, "enable verbose logging")
|
||||
return nil
|
||||
},
|
||||
Args: tree.Args{
|
||||
@ -89,11 +92,15 @@ func NewCommand(l log.Logger, op *onepassword.OnePassword, opts ...CommandOption
|
||||
Name: "scenario",
|
||||
Description: "Scenario name",
|
||||
Suggest: func(ctx context.Context, t tree.Root, r *readline.Readline) []goprompt.Suggest {
|
||||
scenarios, err := inst.cfg.Scenarios()
|
||||
root := env.Path(inst.cfg.Path)
|
||||
ret, err := files.Find(ctx, root, "*.k6.js", files.FindWithIsFile(true))
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return suggests.List(scenarios)
|
||||
for i, s := range ret {
|
||||
ret[i] = strings.TrimPrefix(s, root+"/")
|
||||
}
|
||||
return suggests.List(ret)
|
||||
},
|
||||
},
|
||||
},
|
||||
@ -140,8 +147,45 @@ func (c *Command) execute(ctx context.Context, r *readline.Readline) error {
|
||||
envs = append(envs, fmt.Sprintf("%s=%s", strings.ToUpper(k), v))
|
||||
}
|
||||
|
||||
return shell.New(ctx, c.l, "k6", "run", "--out", "web-dashboard", path.Join(c.cfg.Path, scenario)).
|
||||
if c.op != nil {
|
||||
{
|
||||
secret := path.Join(c.cfg.Path, "all.k6.secret")
|
||||
if err := files.Exists(secret + ".tpl"); err == nil {
|
||||
if err := exec.CommandContext(ctx, "op", "inject", "-f", "-i", secret+".tpl", "-o", secret).Run(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
{
|
||||
secret := path.Join(c.cfg.Path, strings.TrimSuffix(scenario, ".js")+".secret")
|
||||
if err := files.Exists(secret + ".tpl"); err == nil {
|
||||
if err := exec.CommandContext(ctx, "op", "inject", "-f", "-i", secret+".tpl", "-o", secret).Run(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var args []string
|
||||
{
|
||||
secret := path.Join(c.cfg.Path, "all.k6.secret")
|
||||
if err := files.Exists(secret); err == nil {
|
||||
args = append(args, "--secret-source=file=name=all,filename="+secret)
|
||||
}
|
||||
}
|
||||
{
|
||||
secret := path.Join(c.cfg.Path, strings.TrimSuffix(scenario, ".js")+".secret")
|
||||
if err := files.Exists(secret); err == nil {
|
||||
args = append(args, "--secret-source=file=name=default,filename="+secret)
|
||||
}
|
||||
}
|
||||
|
||||
return shell.New(ctx, c.l, "k6", "run").
|
||||
Args(args...).
|
||||
Args("--no-usage-report").
|
||||
Args("--out", "web-dashboard").
|
||||
Args(fs.Visited().Args()...).
|
||||
Args(path.Join(c.cfg.Path, scenario)).
|
||||
Args(r.AdditionalArgs()...).
|
||||
Args(r.AdditionalFlags()...).
|
||||
Env(envs...).
|
||||
|
||||
@ -1,11 +1,8 @@
|
||||
package k6
|
||||
|
||||
import (
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/foomo/posh/pkg/env"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
@ -23,17 +20,3 @@ func (c Config) EnvNames() []string {
|
||||
sort.Strings(ret)
|
||||
return ret
|
||||
}
|
||||
|
||||
func (c Config) Scenarios() ([]string, error) {
|
||||
entries, err := os.ReadDir(env.Path(c.Path))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var ret []string
|
||||
for _, e := range entries {
|
||||
if !e.IsDir() && strings.HasSuffix(e.Name(), ".js") {
|
||||
ret = append(ret, e.Name())
|
||||
}
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user