Skip to content

Commit

Permalink
module
Browse files Browse the repository at this point in the history
  • Loading branch information
Lack30 committed Apr 12, 2024
1 parent 04f1783 commit 1fc1342
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
18 changes: 10 additions & 8 deletions module/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ type RunE func(ctx *RunContext, options ...client.ExecOption) ([]byte, error)

type Command struct {
Name string `json:"name" yaml:"name"`
Alias string `json:"alias" yaml:"alias"`
Long string `json:"long" yaml:"long"`
Script string `json:"script" yaml:"script"`
Authors []string `json:"authors" yaml:"authors"`
Expand All @@ -91,6 +92,8 @@ type Command struct {
Params []*Schema `json:"params" yaml:"params"`
Returns []*Schema `json:"returns" yaml:"returns"`
Commands []*Command `json:"commands" yaml:"commands"`
Mutable bool `json:"mutable" yaml:"mutable"`
Hide bool `json:"hide" yaml:"hide"`
Root string `json:"root" yaml:"root"`
cobra *cobra.Command `yaml:"-"`

Expand All @@ -110,9 +113,7 @@ func (c *Command) NewContext(ctx context.Context, lg *zap.Logger, conn client.IC
return rctx
}

func (c *Command) Runnable() bool {
return c.Script != ""
}
func (c *Command) Runnable() bool { return c.Script != "" }

func (c *Command) ParseCmd() *cobra.Command {
if c.cobra != nil {
Expand Down Expand Up @@ -146,9 +147,7 @@ func (c *Command) ParseCmd() *cobra.Command {
return cmd
}

func (c *Command) Flags() *pflag.FlagSet {
return c.cobra.PersistentFlags()
}
func (c *Command) Flags() *pflag.FlagSet { return c.cobra.PersistentFlags() }

var DefaultRunCommand RunE = func(ctx *RunContext, opts ...client.ExecOption) ([]byte, error) {
command := ctx.Cmd
Expand Down Expand Up @@ -212,14 +211,17 @@ var DefaultRunCommand RunE = func(ctx *RunContext, opts ...client.ExecOption) ([
options = append(options, client.ExecWithEnv(key, value))
}

shell := fmt.Sprintf("%s -import %s %s %s", repl, resolve, script, strings.Join(args, " "))
shell := fmt.Sprintf("%s -import %s %s %s",
repl, resolve, script, strings.Join(args, " "))
start := time.Now()
cmd, err := conn.Execute(ctx, repl, options...)
if err != nil {
return nil, err
}
data, err := cmd.CombinedOutput()
lg.Debug("remote execute", zap.String("command", shell), zap.Duration("took", time.Now().Sub(start)))
lg.Debug("remote execute",
zap.String("command", shell),
zap.Duration("took", time.Now().Sub(start)))
if err != nil {
return nil, &CommandErr{Err: err, Stderr: beautify(data)}
}
Expand Down
3 changes: 3 additions & 0 deletions module/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ func (mg *Manager) LoadDir(dir string) error {
if err != nil {
return err
}
if m.Hide {
return nil
}
if hk, ok := hook.Hooks[m.Name]; ok {
forCommandHook(&m.Command, hk)
}
Expand Down
3 changes: 3 additions & 0 deletions module/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ func readCommand(dir string) (*Command, error) {
if err != nil {
return nil, err
}
if c.Alias == "" {
c.Alias = c.Name
}
c.Run = DefaultRunCommand

if err = validateScript(c, dir); err != nil {
Expand Down

0 comments on commit 1fc1342

Please sign in to comment.