mirror of
https://github.com/foomo/posh.git
synced 2025-10-16 12:45:38 +00:00
132 lines
3.3 KiB
Plaintext
132 lines
3.3 KiB
Plaintext
package internal
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"strings"
|
|
|
|
icommand "github.com/posh-test-demo/posh/internal/command"
|
|
ownbrewconfig "github.com/foomo/ownbrew/pkg/config"
|
|
"github.com/foomo/ownbrew/pkg/ownbrew"
|
|
"github.com/foomo/posh/pkg/command"
|
|
"github.com/foomo/posh/pkg/config"
|
|
"github.com/foomo/posh/pkg/log"
|
|
"github.com/foomo/posh/pkg/plugin"
|
|
"github.com/foomo/posh/pkg/prompt"
|
|
"github.com/foomo/posh/pkg/prompt/check"
|
|
"github.com/foomo/posh/pkg/prompt/history"
|
|
"github.com/foomo/posh/pkg/readline"
|
|
"github.com/foomo/posh/pkg/require"
|
|
)
|
|
|
|
type Plugin struct {
|
|
l log.Logger
|
|
commands command.Commands
|
|
}
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
// ~ Constructor
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
func New(l log.Logger) (plugin.Plugin, error) { //nolint: ireturn
|
|
inst := &Plugin{
|
|
l: l,
|
|
commands: command.Commands{},
|
|
}
|
|
|
|
// add commands
|
|
inst.commands.Add(command.NewExit(l))
|
|
inst.commands.Add(command.NewHelp(l, inst.commands))
|
|
|
|
// Welcome
|
|
inst.commands.MustAdd(
|
|
icommand.NewWelcome(l,
|
|
icommand.WelcomeWithConfigKey("welcome"),
|
|
),
|
|
)
|
|
|
|
return inst, nil
|
|
}
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
// ~ Public methods
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
func (p *Plugin) Brew(ctx context.Context, cfg ownbrewconfig.Config, tags []string, dry bool) error {
|
|
brew, err := ownbrew.New(slog.New(p.l.SlogHandler()),
|
|
ownbrew.WithDry(dry),
|
|
ownbrew.WithBinDir(cfg.BinDir),
|
|
ownbrew.WithTapDir(cfg.TapDir),
|
|
ownbrew.WithTempDir(cfg.TempDir),
|
|
ownbrew.WithCellarDir(cfg.CellarDir),
|
|
ownbrew.WithPackages(cfg.Packages...),
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return brew.Install(ctx, tags...)
|
|
}
|
|
|
|
func (p *Plugin) Require(ctx context.Context, cfg config.Require) error {
|
|
return require.First(ctx, p.l,
|
|
require.Envs(p.l, cfg.Envs),
|
|
require.Packages(p.l, cfg.Packages),
|
|
require.Scripts(p.l, cfg.Scripts),
|
|
)
|
|
}
|
|
|
|
func (p *Plugin) Execute(ctx context.Context, args []string) error {
|
|
r, err := readline.New(p.l)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := r.Parse(strings.Join(args, " ")); err != nil {
|
|
return err
|
|
}
|
|
|
|
if cmd := p.commands.Get(r.Cmd()); cmd == nil {
|
|
return fmt.Errorf("invalid [cmd] argument: %s", r.Cmd())
|
|
} else {
|
|
if value, ok := cmd.(command.Validator); ok {
|
|
if err := value.Validate(ctx, r); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if err := cmd.Execute(ctx, r); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (p *Plugin) Prompt(ctx context.Context, cfg config.Prompt) error {
|
|
sh, err := prompt.New(p.l,
|
|
prompt.WithContext(ctx),
|
|
prompt.WithTitle(cfg.Title),
|
|
prompt.WithPrefix(cfg.Prefix),
|
|
prompt.WithAliases(cfg.Aliases),
|
|
prompt.WithCommands(p.commands),
|
|
prompt.WithCheckers(
|
|
func(ctx context.Context, l log.Logger) check.Info {
|
|
return check.Info{
|
|
Name: "example",
|
|
Note: "all good",
|
|
Status: check.StatusSuccess,
|
|
}
|
|
},
|
|
),
|
|
prompt.WithFileHistory(
|
|
history.FileWithLimit(cfg.History.Limit),
|
|
history.FileWithFilename(cfg.History.Filename),
|
|
history.FileWithLockFilename(cfg.History.LockFilename),
|
|
),
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return sh.Run()
|
|
}
|