48 lines
962 B
Go
48 lines
962 B
Go
package exec
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"github.com/creack/pty"
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
"io"
|
|
"os"
|
|
"os/exec"
|
|
)
|
|
|
|
var execCmd = &cobra.Command{
|
|
Use: "exec",
|
|
Short: "Execute command of specific alias",
|
|
Args: func(cmd *cobra.Command, args []string) error {
|
|
if err := cobra.ExactArgs(1)(cmd, args); err != nil {
|
|
return err
|
|
}
|
|
data := viper.GetStringMapString("data")
|
|
|
|
if _, ok := data[args[0]]; !ok {
|
|
return errors.New("such alias was not found")
|
|
}
|
|
|
|
return nil
|
|
},
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
data := viper.GetStringMapString("data")
|
|
command := exec.Command("sh", "-c", data[args[0]])
|
|
ptmx, err := pty.Start(command)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error starting PTY: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
defer func() { _ = ptmx.Close() }()
|
|
|
|
go func() { _, _ = io.Copy(ptmx, os.Stdin) }()
|
|
_, _ = io.Copy(os.Stdout, ptmx)
|
|
},
|
|
}
|
|
|
|
func Cmd() *cobra.Command {
|
|
return execCmd
|
|
}
|