Comments (12)
This is how I solve this problem:
package shell
import (
"bytes"
"github.com/bitfield/script"
"os/exec"
)
type PipeWrap struct {
*script.Pipe
}
func (p *PipeWrap) ToPipe(s string) *script.Pipe {
return p.Pipe
}
func (p *PipeWrap) Exec(s string) *script.Pipe {
if p == nil || p.Error() != nil {
return p.Pipe
}
q := NewPipeWrap()
//args := strings.Fields(s)
cmd := exec.Command("bash", "-c", s)
cmd.Stdin = p.Reader
output, err := cmd.CombinedOutput()
if err != nil {
q.SetError(err)
}
return q.WithReader(bytes.NewReader(output))
}
func NewPipeWrap() *PipeWrap {
PipeWrap := new(PipeWrap)
PipeWrap.Pipe = script.NewPipe()
return PipeWrap
}
func Exec(s string) *script.Pipe {
return NewPipeWrap().Exec(s)
}
from script.
The right solution for this problem is to change the API of Exec
to a (string, ...string)
, exactly as the exec.Command
API is, and pass it as is to the exec.Command
call.
from script.
That's actually a little bit of a pain, which is one reason why we wrap exec.Command
to be able to pass it a single string.
from script.
Thanks for the report @shumin1027! PR welcome, from anyone who wants to tackle this.
from script.
That's actually a little bit of a pain, which is one reason why we wrap
exec.Command
to be able to pass it a single string.
I'm not sure I fully understand what is the pain point. Having the input as a single string?
from script.
Yes, exec.Command
takes a string and a slice of strings as the command line. In shell scripts we can simply write this as one string, so script
replicates this.
from script.
from script.
Unless you quote it, of course, which is what this issue is really about.
from script.
Quoting does not help: The input string is passed to strings.Field
, which is passed to the exec.Command
in the next line.
Unfortunately, the strings.Fields does not group into a single field quoted words:
fmt.Println(strings.Join(strings.Fields(`a b "c d" e`), ", "))
// Output: a, b, "c, d", e
Notice that in this example, "c
is one string returned from strings.Fields
and d"
is another, while for the command.Exec
you would want c d
as a single string.
from script.
@shumin1027 commented on Oct 10, 2019, 4:35 AM GMT+3:30:
This is how I solve this problem:
package shell import ( "bytes" "github.com/bitfield/script" "os/exec" ) type PipeWrap struct { *script.Pipe } func (p *PipeWrap) ToPipe(s string) *script.Pipe { return p.Pipe } func (p *PipeWrap) Exec(s string) *script.Pipe { if p == nil || p.Error() != nil { return p.Pipe } q := NewPipeWrap() //args := strings.Fields(s) cmd := exec.Command("bash", "-c", s) cmd.Stdin = p.Reader output, err := cmd.CombinedOutput() if err != nil { q.SetError(err) } return q.WithReader(bytes.NewReader(output)) } func NewPipeWrap() *PipeWrap { PipeWrap := new(PipeWrap) PipeWrap.Pipe = script.NewPipe() return PipeWrap } func Exec(s string) *script.Pipe { return NewPipeWrap().Exec(s) }
I'm new to go, but I guess there is no way to make that custom Exec method work in the middle of a pipe? (Because all functions in the library return script.Pipe
, which we can't add an extension method to.)
from script.
@shumin1027 commented on Oct 10, 2019, 4:35 AM GMT+3:30:
This is how I solve this problem:
package shell import ( "bytes" "github.com/bitfield/script" "os/exec" ) type PipeWrap struct { *script.Pipe } func (p *PipeWrap) ToPipe(s string) *script.Pipe { return p.Pipe } func (p *PipeWrap) Exec(s string) *script.Pipe { if p == nil || p.Error() != nil { return p.Pipe } q := NewPipeWrap() //args := strings.Fields(s) cmd := exec.Command("bash", "-c", s) cmd.Stdin = p.Reader output, err := cmd.CombinedOutput() if err != nil { q.SetError(err) } return q.WithReader(bytes.NewReader(output)) } func NewPipeWrap() *PipeWrap { PipeWrap := new(PipeWrap) PipeWrap.Pipe = script.NewPipe() return PipeWrap } func Exec(s string) *script.Pipe { return NewPipeWrap().Exec(s) }
I'm new to go, but I guess there is no way to make that custom Exec method work in the middle of a pipe? (Because all functions in the library return
script.Pipe
, which we can't add an extension method to.)
This issue seems to have been fixed Fix Exec to handle quotes #42
example : https://github.com/bitfield/script/blob/master/examples/execute/main.go
from script.
That would explain why it's closed...
from script.
Related Issues (20)
- script.Exec - no colours on stdout HOT 6
- Bytes and String methods inconsistent on non-zero exist status from Exec HOT 1
- gitignore entry for Intellij IDEA HOT 4
- Feature Request: Add FilterFunc HOT 2
- Basename introduces a line feed HOT 1
- How can I check the progress of a script.Exec() command? HOT 2
- Interaction with bubbletea HOT 10
- Propose adding a delimeter to Column()
- Add Base64 encode and decode methods HOT 3
- Suggestion: `./go-script -c 'script.Stdin()...'` tool for running these HOT 5
- Should `ExecForEach` sanitize or escape arguments differently? HOT 9
- Parameterize a script.Pipe with a user data struct ? HOT 5
- Execv function HOT 1
- how to get the stdout of the Exec result? HOT 3
- Race condition in Pipe.Exec HOT 1
- Methods for mkdir and touch HOT 2
- Conditinally execute statements. HOT 2
- How to create folder, delete folder and list folder contents ? HOT 4
- Feature Suggestion: Fields HOT 4
- Makefile replacement HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from script.