mirror of
https://github.com/foomo/posh-providers.git
synced 2025-10-16 12:35:41 +00:00
Merge pull request #199 from foomo/feature/task
feat(arbitrary/task): add command
This commit is contained in:
commit
911044b37c
1
Makefile
1
Makefile
@ -18,6 +18,7 @@ lint:
|
||||
schema:
|
||||
@jsonschema bundle config.schema.base.json \
|
||||
--resolve ./arbitrary/open/config.schema.json \
|
||||
--resolve ./arbitrary/task/config.schema.json \
|
||||
--resolve ./arbitrary/zip/config.schema.json \
|
||||
--resolve ./azure/az/config.schema.json \
|
||||
--resolve ./cloudflare/cloudflared/config.schema.json \
|
||||
|
||||
56
arbitrary/task/README.md
Normal file
56
arbitrary/task/README.md
Normal file
@ -0,0 +1,56 @@
|
||||
# POSH task provider
|
||||
|
||||
> Run simple tasks definitions.
|
||||
|
||||
## Usage
|
||||
|
||||
### Plugin
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
type Plugin struct {
|
||||
l log.Logger
|
||||
commands command.Commands
|
||||
}
|
||||
|
||||
func New(l log.Logger) (plugin.Plugin, error) {
|
||||
var err error
|
||||
inst := &Plugin{
|
||||
l: l,
|
||||
commands: command.Commands{},
|
||||
}
|
||||
|
||||
// ...
|
||||
|
||||
inst.commands.Add(task.NewCommand(l))
|
||||
|
||||
// ...
|
||||
|
||||
return inst, nil
|
||||
}
|
||||
```
|
||||
|
||||
### Config
|
||||
|
||||
```yaml
|
||||
## Open
|
||||
tasks:
|
||||
init:
|
||||
cmds: ['posh execute bun install', 'posh execute go mod tidy']
|
||||
brew-nss:
|
||||
hidden: true
|
||||
confirm: If you're using Firefox, do you want me to install 'nss'?
|
||||
cmds: ['brew install nss']
|
||||
mkcert-install:
|
||||
hidden: true
|
||||
confirm: Do you need me to install the mkcert root certificate (only required once)?
|
||||
cmds: ['posh execute mkcert install']
|
||||
k3d-up:
|
||||
deps: ['brew-nss', 'mkcert-install']
|
||||
cmds:
|
||||
- posh execute mkcert generate
|
||||
- posh execute k3d up local
|
||||
- posh execute cache clear
|
||||
```
|
||||
|
||||
139
arbitrary/task/command.go
Normal file
139
arbitrary/task/command.go
Normal file
@ -0,0 +1,139 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/foomo/posh/pkg/command/tree"
|
||||
"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/suggests"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/pterm/pterm"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type (
|
||||
Command struct {
|
||||
l log.Logger
|
||||
cfg Config
|
||||
name string
|
||||
configKey string
|
||||
commandTree tree.Root
|
||||
}
|
||||
CommandOption func(*Command)
|
||||
)
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// ~ Options
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
|
||||
func CommandWithName(v string) CommandOption {
|
||||
return func(o *Command) {
|
||||
o.name = v
|
||||
}
|
||||
}
|
||||
|
||||
func WithConfigKey(v string) CommandOption {
|
||||
return func(o *Command) {
|
||||
o.configKey = v
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// ~ Constructor
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
|
||||
func NewCommand(l log.Logger, opts ...CommandOption) (*Command, error) {
|
||||
inst := &Command{
|
||||
l: l.Named("task"),
|
||||
name: "task",
|
||||
configKey: "task",
|
||||
}
|
||||
for _, opt := range opts {
|
||||
if opt != nil {
|
||||
opt(inst)
|
||||
}
|
||||
}
|
||||
if err := viper.UnmarshalKey(inst.configKey, &inst.cfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
inst.commandTree = tree.New(&tree.Node{
|
||||
Name: inst.name,
|
||||
Description: "Run make scripts",
|
||||
Args: tree.Args{
|
||||
{
|
||||
Name: "task",
|
||||
Suggest: func(ctx context.Context, t tree.Root, r *readline.Readline) []goprompt.Suggest {
|
||||
return suggests.List(inst.cfg.Names())
|
||||
},
|
||||
},
|
||||
},
|
||||
Execute: inst.execute,
|
||||
})
|
||||
|
||||
return inst, nil
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// ~ Public methods
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
|
||||
func (c *Command) Name() string {
|
||||
return c.commandTree.Node().Name
|
||||
}
|
||||
|
||||
func (c *Command) Description() string {
|
||||
return c.commandTree.Node().Description
|
||||
}
|
||||
|
||||
func (c *Command) Complete(ctx context.Context, r *readline.Readline) []goprompt.Suggest {
|
||||
return c.commandTree.Complete(ctx, r)
|
||||
}
|
||||
|
||||
func (c *Command) Execute(ctx context.Context, r *readline.Readline) error {
|
||||
return c.commandTree.Execute(ctx, r)
|
||||
}
|
||||
|
||||
func (c *Command) Help(ctx context.Context, r *readline.Readline) string {
|
||||
return c.commandTree.Help(ctx, r)
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// ~ Private methods
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
|
||||
func (c *Command) execute(ctx context.Context, r *readline.Readline) error {
|
||||
return c.executeTask(ctx, r.Args().At(0))
|
||||
}
|
||||
|
||||
func (c *Command) executeTask(ctx context.Context, taskID string) error {
|
||||
task, ok := c.cfg[taskID]
|
||||
if !ok {
|
||||
return errors.Errorf("task not found: %s", taskID)
|
||||
}
|
||||
|
||||
if task.Prompt != "" {
|
||||
if result, err := pterm.DefaultInteractiveConfirm.Show(task.Prompt); err != nil {
|
||||
return err
|
||||
} else if !result {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
for _, dep := range task.Deps {
|
||||
if err := c.executeTask(ctx, dep); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
for _, cmd := range task.Cmds {
|
||||
if err := shell.New(ctx, c.l, cmd).Debug().Run(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
18
arbitrary/task/config.go
Normal file
18
arbitrary/task/config.go
Normal file
@ -0,0 +1,18 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"sort"
|
||||
)
|
||||
|
||||
type Config map[string]Task
|
||||
|
||||
func (c Config) Names() []string {
|
||||
var ret []string
|
||||
for k, v := range c {
|
||||
if !v.Hidden {
|
||||
ret = append(ret, k)
|
||||
}
|
||||
}
|
||||
sort.Strings(ret)
|
||||
return ret
|
||||
}
|
||||
47
arbitrary/task/config.schema.json
Normal file
47
arbitrary/task/config.schema.json
Normal file
@ -0,0 +1,47 @@
|
||||
{
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"$id": "https://github.com/foomo/posh-providers/arbitrary/task/config",
|
||||
"$ref": "#/$defs/Config",
|
||||
"$defs": {
|
||||
"Config": {
|
||||
"additionalProperties": {
|
||||
"$ref": "#/$defs/Task"
|
||||
},
|
||||
"type": "object"
|
||||
},
|
||||
"Task": {
|
||||
"properties": {
|
||||
"prompt": {
|
||||
"type": "string",
|
||||
"description": "Prompt string to confirm execution"
|
||||
},
|
||||
"deps": {
|
||||
"items": {
|
||||
"type": "string"
|
||||
},
|
||||
"type": "array",
|
||||
"description": "Dependencies to run"
|
||||
},
|
||||
"cmds": {
|
||||
"items": {
|
||||
"type": "string"
|
||||
},
|
||||
"type": "array",
|
||||
"description": "Commands to execute"
|
||||
},
|
||||
"hidden": {
|
||||
"type": "boolean",
|
||||
"description": "Don't show in the completion list"
|
||||
}
|
||||
},
|
||||
"additionalProperties": false,
|
||||
"type": "object",
|
||||
"required": [
|
||||
"prompt",
|
||||
"deps",
|
||||
"cmds",
|
||||
"hidden"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
40
arbitrary/task/config_test.go
Normal file
40
arbitrary/task/config_test.go
Normal file
@ -0,0 +1,40 @@
|
||||
package task_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path"
|
||||
"testing"
|
||||
|
||||
testingx "github.com/foomo/go/testing"
|
||||
tagx "github.com/foomo/go/testing/tag"
|
||||
"github.com/foomo/posh-providers/arbitrary/task"
|
||||
"github.com/invopop/jsonschema"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestConfig(t *testing.T) {
|
||||
t.Parallel()
|
||||
testingx.Tags(t, tagx.Short)
|
||||
|
||||
cwd, err := os.Getwd()
|
||||
require.NoError(t, err)
|
||||
|
||||
reflector := new(jsonschema.Reflector)
|
||||
require.NoError(t, reflector.AddGoComments("github.com/foomo/posh-providers/arbitrary/task", "./"))
|
||||
schema := reflector.Reflect(&task.Config{})
|
||||
actual, err := json.MarshalIndent(schema, "", " ")
|
||||
require.NoError(t, err)
|
||||
|
||||
filename := path.Join(cwd, "config.schema.json")
|
||||
expected, err := os.ReadFile(filename)
|
||||
if !errors.Is(err, os.ErrNotExist) {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
if !assert.Equal(t, string(expected), string(actual)) {
|
||||
require.NoError(t, os.WriteFile(filename, actual, 0600))
|
||||
}
|
||||
}
|
||||
12
arbitrary/task/task.go
Normal file
12
arbitrary/task/task.go
Normal file
@ -0,0 +1,12 @@
|
||||
package task
|
||||
|
||||
type Task struct {
|
||||
// Prompt string to confirm execution
|
||||
Prompt string `json:"prompt" yaml:"prompt"`
|
||||
// Dependencies to run
|
||||
Deps []string `json:"deps" yaml:"deps"`
|
||||
// Commands to execute
|
||||
Cmds []string `json:"cmds" yaml:"cmds"`
|
||||
// Don't show in the completion list
|
||||
Hidden bool `json:"hidden" yaml:"hidden"`
|
||||
}
|
||||
@ -111,6 +111,9 @@
|
||||
},
|
||||
"kubeforward": {
|
||||
"$ref": "https://github.com/foomo/posh-providers/kubernets/kubeforward/config"
|
||||
},
|
||||
"task": {
|
||||
"$ref": "https://github.com/foomo/posh-providers/arbitrary/task/config"
|
||||
}
|
||||
},
|
||||
"additionalProperties": false,
|
||||
|
||||
@ -96,6 +96,9 @@
|
||||
"stern": {
|
||||
"$ref": "#/$defs/https%3A~1~1github.com~1foomo~1posh-providers~1stern~1stern~1config"
|
||||
},
|
||||
"task": {
|
||||
"$ref": "#/$defs/https%3A~1~1github.com~1foomo~1posh-providers~1arbitrary~1task~1config"
|
||||
},
|
||||
"teleport": {
|
||||
"$ref": "#/$defs/https%3A~1~1github.com~1foomo~1posh-providers~1gravitational~1teleport~1config"
|
||||
},
|
||||
@ -195,6 +198,47 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"https://github.com/foomo/posh-providers/arbitrary/task/config": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"$ref": "#/$defs/https%3A~1~1github.com~1foomo~1posh-providers~1arbitrary~1task~1config/$defs/Config",
|
||||
"$defs": {
|
||||
"Config": {
|
||||
"type": "object",
|
||||
"additionalProperties": {
|
||||
"$ref": "#/$defs/https%3A~1~1github.com~1foomo~1posh-providers~1arbitrary~1task~1config/$defs/Task"
|
||||
}
|
||||
},
|
||||
"Task": {
|
||||
"type": "object",
|
||||
"required": [ "prompt", "deps", "cmds", "hidden" ],
|
||||
"properties": {
|
||||
"cmds": {
|
||||
"description": "Commands to execute",
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"deps": {
|
||||
"description": "Dependencies to run",
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"hidden": {
|
||||
"description": "Don't show in the completion list",
|
||||
"type": "boolean"
|
||||
},
|
||||
"prompt": {
|
||||
"description": "Prompt string to confirm execution",
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"additionalProperties": false
|
||||
}
|
||||
}
|
||||
},
|
||||
"https://github.com/foomo/posh-providers/arbitrary/zip/config": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"$ref": "#/$defs/https%3A~1~1github.com~1foomo~1posh-providers~1arbitrary~1zip~1config/$defs/Config",
|
||||
|
||||
@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"time"
|
||||
|
||||
gokaziconfig "github.com/foomo/gokazi/pkg/config"
|
||||
"github.com/foomo/gokazi/pkg/gokazi"
|
||||
@ -13,9 +14,11 @@ import (
|
||||
"github.com/foomo/posh/pkg/env"
|
||||
"github.com/foomo/posh/pkg/log"
|
||||
"github.com/foomo/posh/pkg/prompt/goprompt"
|
||||
ptermx "github.com/foomo/posh/pkg/pterm"
|
||||
"github.com/foomo/posh/pkg/readline"
|
||||
"github.com/foomo/posh/pkg/util/suggests"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/pterm/pterm"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
@ -108,6 +111,7 @@ func NewCommand(l log.Logger, gk *gokazi.Gokazi, kubectl *kubectl.Kubectl, opts
|
||||
},
|
||||
Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error {
|
||||
fs.Internal().String("profile", "", "Profile to use")
|
||||
fs.Internal().Bool("debug", false, "Debug mode")
|
||||
return nil
|
||||
},
|
||||
Execute: inst.connect,
|
||||
@ -186,6 +190,11 @@ func (c *Command) connect(ctx context.Context, r *readline.Readline) error {
|
||||
fs := r.FlagSets().Default()
|
||||
ifs := r.FlagSets().Internal()
|
||||
|
||||
debug, err := ifs.GetBool("debug")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
profile, err := ifs.GetString("profile")
|
||||
if err != nil {
|
||||
return err
|
||||
@ -207,11 +216,27 @@ func (c *Command) connect(ctx context.Context, r *readline.Readline) error {
|
||||
cmd.Args = append(cmd.Args, r.AdditionalArgs()...)
|
||||
cmd.Env = append(os.Environ(), c.kubectl.Cluster(pf.Cluster).Env(profile))
|
||||
|
||||
if debug {
|
||||
cmd.Stderr = ptermx.NewWriter(pterm.Error)
|
||||
cmd.Stdout = ptermx.NewWriter(pterm.Info)
|
||||
if err := cmd.Run(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := c.gk.Start(context.WithoutCancel(ctx), "kubeforward."+value, cmd); errors.Is(err, gokazi.ErrAlreadyRunning) {
|
||||
c.l.Warn("Task: kubeforward." + value + " already running")
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
time.Sleep(time.Second)
|
||||
if t, err := c.gk.Find(ctx, "kubeforward."+value); err != nil {
|
||||
return err
|
||||
} else if !t.Running {
|
||||
return errors.Errorf("port forward %s not running", value)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@ -223,6 +223,7 @@ func NewCommand(l log.Logger, cache cache.Cache) *Command {
|
||||
Args: tree.Args{
|
||||
{
|
||||
Name: "package",
|
||||
Optional: true,
|
||||
Description: "Package name",
|
||||
},
|
||||
},
|
||||
|
||||
Loading…
Reference in New Issue
Block a user