mirror of
https://github.com/foomo/posh-providers.git
synced 2025-10-16 12:35:41 +00:00
feat(arbitrary/task): add command
This commit is contained in:
parent
f650b9cf69
commit
2897fcbfeb
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"`
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user