Execute is the main dispatcher function for shell builtin commands
(ctx context.Context, h *shellCallHandler, args []string, st *ShellState)
| 131 | |
| 132 | // Execute is the main dispatcher function for shell builtin commands |
| 133 | func (c *ShellCommand) Execute(ctx context.Context, h *shellCallHandler, args []string, st *ShellState) error { |
| 134 | switch c.State { |
| 135 | case AnyState: |
| 136 | case RequiredState: |
| 137 | if st == nil { |
| 138 | return fmt.Errorf("command %q must be piped\n\nUsage: %s", c.Name(), c.Use) |
| 139 | } |
| 140 | case NoState: |
| 141 | if st != nil { |
| 142 | return fmt.Errorf("command %q cannot be piped\n\nUsage: %s", c.Name(), c.Use) |
| 143 | } |
| 144 | } |
| 145 | if c.Args != nil { |
| 146 | if err := c.Args(args); err != nil { |
| 147 | return fmt.Errorf("command %q %w\n\nUsage: %s", c.Name(), err, c.Use) |
| 148 | } |
| 149 | } |
| 150 | if !c.NoResolveStateArgs { |
| 151 | // resolve state values in arguments |
| 152 | a, err := h.resolveResults(ctx, args) |
| 153 | if err != nil { |
| 154 | return err |
| 155 | } |
| 156 | args = a |
| 157 | } |
| 158 | if h.Debug() { |
| 159 | shellDebug(ctx, "Command: "+c.Name(), args, st) |
| 160 | } |
| 161 | return c.Run(ctx, c, args, st) |
| 162 | } |
| 163 | |
| 164 | func (h *shellCallHandler) BuiltinCommand(name string) (*ShellCommand, error) { |
| 165 | if name == "." || !strings.HasPrefix(name, ".") || strings.Contains(name, "/") { |