| 1 |
package commands |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"os" |
| 6 |
"os/exec" |
| 7 |
"time" |
| 8 |
|
| 9 |
"git.sr.ht/~rjarry/aerc/app" |
| 10 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 11 |
) |
| 12 |
|
| 13 |
type ExecCmd struct { |
| 14 |
Args []string `opt:"..."` |
| 15 |
} |
| 16 |
|
| 17 |
func init() { |
| 18 |
Register(ExecCmd{}) |
| 19 |
} |
| 20 |
|
| 21 |
func (ExecCmd) Description() string { |
| 22 |
return "Execute an arbitrary command in the background." |
| 23 |
} |
| 24 |
|
| 25 |
func (ExecCmd) Context() CommandContext { |
| 26 |
return GLOBAL |
| 27 |
} |
| 28 |
|
| 29 |
func (ExecCmd) Aliases() []string { |
| 30 |
return []string{"exec"} |
| 31 |
} |
| 32 |
|
| 33 |
func (e ExecCmd) Execute(args []string) error { |
| 34 |
cmd := exec.Command(e.Args[0], e.Args[1:]...) |
| 35 |
env := os.Environ() |
| 36 |
|
| 37 |
switch view := app.SelectedTabContent().(type) { |
| 38 |
case *app.AccountView: |
| 39 |
env = append(env, fmt.Sprintf("account=%s", view.AccountConfig().Name)) |
| 40 |
env = append(env, fmt.Sprintf("folder=%s", view.Directories().Selected())) |
| 41 |
case *app.MessageViewer: |
| 42 |
acct := view.SelectedAccount() |
| 43 |
env = append(env, fmt.Sprintf("account=%s", acct.AccountConfig().Name)) |
| 44 |
env = append(env, fmt.Sprintf("folder=%s", acct.Directories().Selected())) |
| 45 |
} |
| 46 |
|
| 47 |
cmd.Env = env |
| 48 |
|
| 49 |
go func() { |
| 50 |
defer log.PanicHandler() |
| 51 |
|
| 52 |
err := cmd.Run() |
| 53 |
if err != nil { |
| 54 |
app.PushError(err.Error()) |
| 55 |
} else { |
| 56 |
if cmd.ProcessState.ExitCode() != 0 { |
| 57 |
app.PushError(fmt.Sprintf( |
| 58 |
"%s: completed with status %d", args[0], |
| 59 |
cmd.ProcessState.ExitCode())) |
| 60 |
} else { |
| 61 |
app.PushStatus(fmt.Sprintf( |
| 62 |
"%s: completed with status %d", args[0], |
| 63 |
cmd.ProcessState.ExitCode()), 10*time.Second) |
| 64 |
} |
| 65 |
} |
| 66 |
}() |
| 67 |
return nil |
| 68 |
} |