Skip to content

Commit fdbaf5a

Browse files
committed
Add GetPythonRunCommand public method to return a cmd instance
1 parent 75cdf5f commit fdbaf5a

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,24 @@ out, err := python.ExecutePython("script.py")
3636

3737
This will run a python script called `script.py` inside the virtual environment, using the proper command, analyzing files existence.
3838

39+
Alternatively, it is possible to get an instance for `*exec.Cmd` which can be handled independently.
40+
41+
```go
42+
cmd := python.GetPythonRunCommand("script.py")
43+
out, err := cmd.CombinedOutput()
44+
```
45+
or
46+
```go
47+
cmd := python.GetPythonRunCommand("script.py")
48+
err := cmd.Run()
49+
```
50+
or
51+
```go
52+
cmd := python.GetPythonRunCommand("script.py")
53+
err := cmd.Start()
54+
err = cmd.Wait()
55+
```
56+
3957
#### Complete example
4058

4159
```go

main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ func main() {
2323
python.CleanUpVirtualEnv()
2424
python.SetupVirtualEnv()
2525

26-
out, err := python.ExecutePython("script.py")
27-
26+
cmd := python.GetPythonRunCommand("script.py")
27+
out, err := cmd.CombinedOutput()
2828
if err != nil {
2929
log.Error(fmt.Sprintf("Job resulted in error: %s", err.Error()))
3030
}

python/run.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ func getPythonCommand() string {
1414
}
1515
}
1616

17+
// GetPythonRunCommand Returns an *exec.Cmd to be handled with the provided "scriptName.py" using the python binary from virtual environment
18+
func GetPythonRunCommand(scriptName string) *exec.Cmd {
19+
pythonPath := getPythonCommand()
20+
return exec.Command(pythonPath, scriptName)
21+
}
22+
1723
// ExecutePython Executes the provided "scriptName.py" using the python binary from virtual environment
1824
func ExecutePython(scriptName string) ([]byte, error) {
1925
// run python job

0 commit comments

Comments
 (0)