forked from kevin2li/PDF-Guru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_output_win.go
118 lines (107 loc) · 2.71 KB
/
cmd_output_win.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//go:build windows
package main
import (
"encoding/json"
"os"
"os/exec"
"path/filepath"
"syscall"
"github.com/pkg/errors"
)
type CmdOutput struct {
Status string `json:"status"`
Message string `json:"message"`
}
func GetCmdStatusAndMessage(cmd *exec.Cmd, cmdType string) error {
defer func() {
if err := recover(); err != nil {
logger.Fatalln(err)
}
}()
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
out, err := cmd.CombinedOutput()
if err != nil {
// err = errors.Wrap(err, "get cmd output error! \n args: "+strings.Join(cmd.Args, " ")+"\n stderr: "+string(err.(*exec.ExitError).Stderr))
err = errors.Wrap(err, "命令执行失败!"+string(out))
logger.Errorln("Error:", err)
return err
}
logger.Println(string(out))
if cmdType == "pdf" {
ret_path := filepath.Join(logdir, "cmd_output.json")
var ret CmdOutput
data, err := os.ReadFile(ret_path)
if err != nil {
err = errors.Wrap(err, "read cmd output file error")
return err
}
err = json.Unmarshal(data, &ret)
if err != nil {
err = errors.Wrap(err, "json umarshal error")
return err
}
if ret.Status != "success" {
logger.Errorf("Error: %v\n", ret.Message)
return errors.New(ret.Message)
}
}
return nil
}
func (a *App) cmdRunner(args []string, cmdType string) error {
config, err := a.LoadConfig()
if err != nil {
err = errors.Wrap(err, "")
return err
}
var cmd *exec.Cmd
if cmdType == "pdf" {
cmd = exec.Command(config.PdfPath, args...)
} else if cmdType == "python" {
err = a.CheckFileExists(config.PythonPath)
if err != nil {
err = errors.Wrap(err, "python not found!")
return err
}
cmd = exec.Command(config.PythonPath, args...)
} else if cmdType == "pandoc" {
path := config.PandocPath
err = a.CheckFileExists(config.PandocPath)
if err != nil {
pandoc_path, err := exec.LookPath("pandoc.exe")
if err != nil {
err = errors.Wrap(err, "pandoc not found!")
return err
}
path = pandoc_path
}
cmd = exec.Command(path, args...)
} else if cmdType == "tesseract" {
path := config.TesseractPath
err = a.CheckFileExists(config.TesseractPath)
if err != nil {
tesseract_path, err := exec.LookPath("tesseract.exe")
if err != nil {
err = errors.Wrap(err, "tesseract not found!")
return err
}
path = tesseract_path
}
cmd = exec.Command(path, args...)
} else if cmdType == "hashcat" {
err = a.CheckFileExists(config.HashcatPath)
if err != nil {
err = errors.Wrap(err, "hashcat not found!")
return err
}
cmd = exec.Command(config.HashcatPath, args...)
} else {
err = errors.Wrap(err, "unsupport cmd type!")
return err
}
err = GetCmdStatusAndMessage(cmd, cmdType)
if err != nil {
err = errors.Wrap(err, "")
return err
}
return nil
}