add perl 'chomp'. too lazy to make it correct with regex
Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
parent
d7a665031c
commit
f59e1b1f84
|
@ -0,0 +1,73 @@
|
||||||
|
package shell
|
||||||
|
|
||||||
|
/*
|
||||||
|
perl 'chomp'
|
||||||
|
|
||||||
|
send it anything, always get back a string
|
||||||
|
*/
|
||||||
|
|
||||||
|
import "log"
|
||||||
|
import "reflect"
|
||||||
|
import "strings"
|
||||||
|
import "bytes"
|
||||||
|
|
||||||
|
// import "github.com/davecgh/go-spew/spew"
|
||||||
|
|
||||||
|
func chompBytesBuffer(buf *bytes.Buffer) string {
|
||||||
|
var bytesSplice []byte
|
||||||
|
bytesSplice = buf.Bytes()
|
||||||
|
|
||||||
|
return Chomp(string(bytesSplice))
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// TODO: obviously this is stupidly wrong
|
||||||
|
// TODO: fix this to trim fucking everything
|
||||||
|
// really world? 8 fucking years of this language
|
||||||
|
// and I'm fucking writing this? jesus. how the
|
||||||
|
// hell is everyone else doing this? Why isn't
|
||||||
|
// this already in the strings package?
|
||||||
|
//
|
||||||
|
func perlChomp(s string) string {
|
||||||
|
// lots of stuff in go moves around the whole block of whatever it is so lots of things are padded with NULL values
|
||||||
|
s = strings.Trim(s, "\x00") // removes NULL (needed!)
|
||||||
|
|
||||||
|
// TODO: christ. make some fucking regex that takes out every NULL, \t, ' ", \n, and \r
|
||||||
|
s = strings.Trim(s, "\n")
|
||||||
|
s = strings.Trim(s, "\n")
|
||||||
|
s = strings.TrimSuffix(s, "\r")
|
||||||
|
s = strings.TrimSuffix(s, "\n")
|
||||||
|
|
||||||
|
s = strings.TrimSpace(s) // this is like 'chomp' in perl
|
||||||
|
s = strings.TrimSuffix(s, "\n") // this is like 'chomp' in perl
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: fix this to chomp \n \r NULL \t and ' '
|
||||||
|
func Chomp(a interface{}) string {
|
||||||
|
// switch reflect.TypeOf(a) {
|
||||||
|
switch t := a.(type) {
|
||||||
|
case string:
|
||||||
|
var s string
|
||||||
|
s = a.(string)
|
||||||
|
return perlChomp(s)
|
||||||
|
case []uint8:
|
||||||
|
log.Printf("shell.Chomp() FOUND []uint8")
|
||||||
|
var u8s []uint8
|
||||||
|
u8s = a.([]uint8)
|
||||||
|
|
||||||
|
s := string(u8s)
|
||||||
|
return perlChomp(s)
|
||||||
|
case *bytes.Buffer:
|
||||||
|
log.Printf("shell.Chomp() FOUND *bytes.Buffer")
|
||||||
|
var tmp *bytes.Buffer
|
||||||
|
tmp = a.(*bytes.Buffer)
|
||||||
|
|
||||||
|
var bytesSplice []byte
|
||||||
|
bytesSplice = tmp.Bytes()
|
||||||
|
return Chomp(string(bytesSplice))
|
||||||
|
default:
|
||||||
|
log.Printf("shell.Chomp() NEED TO MAKE CONVERTER FOR type =", reflect.TypeOf(t))
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
6
shell.go
6
shell.go
|
@ -44,7 +44,7 @@ func Script(cmds string) int {
|
||||||
lines := strings.Split(strings.Replace(cmds, "\r\n", "\n", -1), "\n")
|
lines := strings.Split(strings.Replace(cmds, "\r\n", "\n", -1), "\n")
|
||||||
|
|
||||||
for _, line := range lines {
|
for _, line := range lines {
|
||||||
line = strings.TrimSpace(line) // this is like 'chomp' in perl
|
line = Chomp(line) // this is like 'chomp' in perl
|
||||||
log.Println("LINE:", line)
|
log.Println("LINE:", line)
|
||||||
time.Sleep(1)
|
time.Sleep(1)
|
||||||
Run(line)
|
Run(line)
|
||||||
|
@ -79,7 +79,7 @@ func SetStderr(newerr *os.File) {
|
||||||
func Run(cmdline string) string {
|
func Run(cmdline string) string {
|
||||||
log.Println("shell.Run() START " + cmdline)
|
log.Println("shell.Run() START " + cmdline)
|
||||||
|
|
||||||
cmd := strings.TrimSpace(cmdline) // this is like 'chomp' in perl
|
cmd := Chomp(cmdline) // this is like 'chomp' in perl
|
||||||
cmdArgs := strings.Fields(cmd)
|
cmdArgs := strings.Fields(cmd)
|
||||||
if (len(cmdArgs) == 0) {
|
if (len(cmdArgs) == 0) {
|
||||||
callback(fmt.Errorf("cmdline == ''"), 0)
|
callback(fmt.Errorf("cmdline == ''"), 0)
|
||||||
|
@ -191,7 +191,7 @@ func Run(cmdline string) string {
|
||||||
log.Println("shell.Run() END ", cmdline)
|
log.Println("shell.Run() END ", cmdline)
|
||||||
log.Println("shell.Run() calling callback() :")
|
log.Println("shell.Run() calling callback() :")
|
||||||
callback(fmt.Errorf("no error"), 0)
|
callback(fmt.Errorf("no error"), 0)
|
||||||
return string(b)
|
return Chomp(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Daemon(cmdline string, timeout time.Duration) int {
|
func Daemon(cmdline string, timeout time.Duration) int {
|
||||||
|
|
Loading…
Reference in New Issue