2019-07-09 00:19:08 +02:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-08-01 14:25:13 +02:00
|
|
|
"os"
|
2019-07-09 00:19:08 +02:00
|
|
|
"os/exec"
|
2020-05-28 16:32:32 +02:00
|
|
|
"time"
|
2019-07-09 00:19:08 +02:00
|
|
|
|
2022-03-22 09:52:27 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/logging"
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/widgets"
|
2019-07-09 00:19:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type ExecCmd struct{}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
register(ExecCmd{})
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (ExecCmd) Aliases() []string {
|
2019-07-09 00:19:08 +02:00
|
|
|
return []string{"exec"}
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (ExecCmd) Complete(aerc *widgets.Aerc, args []string) []string {
|
2019-07-09 00:19:08 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (ExecCmd) Execute(aerc *widgets.Aerc, args []string) error {
|
2019-07-09 00:19:08 +02:00
|
|
|
if len(args) < 2 {
|
|
|
|
return errors.New("Usage: exec [cmd...]")
|
|
|
|
}
|
2020-08-01 14:25:13 +02:00
|
|
|
|
2019-07-09 00:19:08 +02:00
|
|
|
cmd := exec.Command(args[1], args[2:]...)
|
2020-08-01 14:25:13 +02:00
|
|
|
env := os.Environ()
|
|
|
|
|
2022-07-18 12:54:55 +02:00
|
|
|
switch view := aerc.SelectedTabContent().(type) {
|
2020-08-01 14:25:13 +02:00
|
|
|
case *widgets.AccountView:
|
|
|
|
env = append(env, fmt.Sprintf("account=%s", view.AccountConfig().Name))
|
|
|
|
env = append(env, fmt.Sprintf("folder=%s", view.Directories().Selected()))
|
|
|
|
case *widgets.MessageViewer:
|
|
|
|
acct := view.SelectedAccount()
|
|
|
|
env = append(env, fmt.Sprintf("account=%s", acct.AccountConfig().Name))
|
|
|
|
env = append(env, fmt.Sprintf("folder=%s", acct.Directories().Selected()))
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Env = env
|
|
|
|
|
2019-07-09 00:19:08 +02:00
|
|
|
go func() {
|
2022-03-22 09:52:27 +01:00
|
|
|
defer logging.PanicHandler()
|
|
|
|
|
2019-07-09 00:19:08 +02:00
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
2021-01-30 13:51:32 +01:00
|
|
|
aerc.PushError(err.Error())
|
2019-07-09 00:19:08 +02:00
|
|
|
} else {
|
2019-07-09 00:32:31 +02:00
|
|
|
if cmd.ProcessState.ExitCode() != 0 {
|
2020-07-27 10:03:55 +02:00
|
|
|
aerc.PushError(fmt.Sprintf(
|
|
|
|
"%s: completed with status %d", args[0],
|
|
|
|
cmd.ProcessState.ExitCode()))
|
|
|
|
} else {
|
|
|
|
aerc.PushStatus(fmt.Sprintf(
|
|
|
|
"%s: completed with status %d", args[0],
|
|
|
|
cmd.ProcessState.ExitCode()), 10*time.Second)
|
2019-07-09 00:32:31 +02:00
|
|
|
}
|
2019-07-09 00:19:08 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|