Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-01-25 00:39:14 -06:00
parent e24c2c2eb3
commit 41fe4a4659
20 changed files with 187 additions and 203 deletions

View File

@ -1,32 +1,9 @@
all: all:
# 'gaper' is a simple and smart golang tool that just rebuilds every time you change a file
# go get -u github.com/maxcnunes/gaper
# gaper
# simple sortcut to push all git changes goimports:
push: goimports -w *.go
git pull
git add --all
-git commit -a -s
git push
redomod: redomod:
rm -f go.* rm -f go.*
unset GO111MODULES && go mod init GO111MODULE= go mod init
unset GO111MODULES && go mod tidy GO111MODULE= go mod tidy
# should update every go dependancy (?)
update:
git pull
go get -v -t -u ./...
# sync repo to the github backup
# git remote add github2 git@github.com:wit-go/shell.git
# git branch -M master
github:
git push origin master
git push origin devel
git push origin --tags
git push github master
git push github devel
git push github --tags

View File

@ -8,7 +8,7 @@ non-blocking unbuffered way
# install # install
``` ```
go get -v -t -u go.wit.com/shell go get -v -t -u go.wit.com/lib/gui/shell
``` ```
# License # License

View File

@ -12,10 +12,10 @@ var RUN *log.LogFlag
var SSH *log.LogFlag var SSH *log.LogFlag
func init() { func init() {
full := "go.wit.com/shell" full := "go.wit.com/lib/gui/shell"
short := "shell" short := "shell"
NOW = log.NewFlag("NOW", true, full, short, "temp debugging stuff") NOW = log.NewFlag("NOW", true, full, short, "temp debugging stuff")
INFO = log.NewFlag("INFO", false, full, short, "General Info") INFO = log.NewFlag("INFO", false, full, short, "General Info")
RUN = log.NewFlag("RUN", false, full, short, "Run() info") RUN = log.NewFlag("RUN", false, full, short, "Run() info")
SSH = log.NewFlag("SSH", false, full, short, "ssh() info") SSH = log.NewFlag("SSH", false, full, short, "ssh() info")

View File

@ -1,16 +1,16 @@
package shell package shell
/* /*
perl 'chomp' perl 'chomp'
send it anything, always get back a string send it anything, always get back a string
*/ */
import ( import (
"bytes"
"fmt" "fmt"
"reflect" "reflect"
"strings" "strings"
"bytes"
"go.wit.com/log" "go.wit.com/log"
) )
@ -24,14 +24,12 @@ func chompBytesBuffer(buf *bytes.Buffer) string {
return Chomp(string(bytesSplice)) return Chomp(string(bytesSplice))
} }
//
// TODO: obviously this is stupidly wrong // TODO: obviously this is stupidly wrong
// TODO: fix this to trim fucking everything // TODO: fix this to trim fucking everything
// really world? 8 fucking years of this language // really world? 8 fucking years of this language
// and I'm fucking writing this? jesus. how the // and I'm fucking writing this? jesus. how the
// hell is everyone else doing this? Why isn't // hell is everyone else doing this? Why isn't
// this already in the strings package? // this already in the strings package?
//
func perlChomp(s string) string { 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 // 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!) s = strings.Trim(s, "\x00") // removes NULL (needed!)
@ -42,8 +40,8 @@ func perlChomp(s string) string {
s = strings.TrimSuffix(s, "\r") s = strings.TrimSuffix(s, "\r")
s = strings.TrimSuffix(s, "\n") s = strings.TrimSuffix(s, "\n")
s = strings.TrimSpace(s) // this is like 'chomp' in perl s = strings.TrimSpace(s) // this is like 'chomp' in perl
s = strings.TrimSuffix(s, "\n") // this is like 'chomp' in perl s = strings.TrimSuffix(s, "\n") // this is like 'chomp' in perl
return s return s
} }
@ -51,40 +49,40 @@ func perlChomp(s string) string {
func Chomp(a interface{}) string { func Chomp(a interface{}) string {
// switch reflect.TypeOf(a) { // switch reflect.TypeOf(a) {
switch t := a.(type) { switch t := a.(type) {
case string: case string:
var s string var s string
s = a.(string) s = a.(string)
return perlChomp(s) return perlChomp(s)
case []uint8: case []uint8:
// log.Printf("shell.Chomp() FOUND []uint8") // log.Printf("shell.Chomp() FOUND []uint8")
var tmp []uint8 var tmp []uint8
tmp = a.([]uint8) tmp = a.([]uint8)
s := string(tmp) s := string(tmp)
return perlChomp(s) return perlChomp(s)
case uint64: case uint64:
// log.Printf("shell.Chomp() FOUND []uint64") // log.Printf("shell.Chomp() FOUND []uint64")
s := fmt.Sprintf("%d", a.(uint64)) s := fmt.Sprintf("%d", a.(uint64))
return perlChomp(s) return perlChomp(s)
case int64: case int64:
// log.Printf("shell.Chomp() FOUND []int64") // log.Printf("shell.Chomp() FOUND []int64")
s := fmt.Sprintf("%d", a.(int64)) s := fmt.Sprintf("%d", a.(int64))
return perlChomp(s) return perlChomp(s)
case *bytes.Buffer: case *bytes.Buffer:
// log.Printf("shell.Chomp() FOUND *bytes.Buffer") // log.Printf("shell.Chomp() FOUND *bytes.Buffer")
var tmp *bytes.Buffer var tmp *bytes.Buffer
tmp = a.(*bytes.Buffer) tmp = a.(*bytes.Buffer)
if (tmp == nil) { if tmp == nil {
return "" return ""
} }
var bytesSplice []byte var bytesSplice []byte
bytesSplice = tmp.Bytes() bytesSplice = tmp.Bytes()
return Chomp(string(bytesSplice)) return Chomp(string(bytesSplice))
default: default:
tmp := fmt.Sprint("shell.Chomp() NO HANDLER FOR TYPE: %T", a) tmp := fmt.Sprint("shell.Chomp() NO HANDLER FOR TYPE: %T", a)
handleError(fmt.Errorf(tmp), -1) handleError(fmt.Errorf(tmp), -1)
log.Warn("shell.Chomp() NEED TO MAKE CONVERTER FOR type =", reflect.TypeOf(t)) log.Warn("shell.Chomp() NEED TO MAKE CONVERTER FOR type =", reflect.TypeOf(t))
} }
tmp := "shell.Chomp() THIS SHOULD NEVER HAPPEN" tmp := "shell.Chomp() THIS SHOULD NEVER HAPPEN"
handleError(fmt.Errorf(tmp), -1) handleError(fmt.Errorf(tmp), -1)

View File

@ -1,3 +1,11 @@
all: all:
# go build # go build
GO111MODULE="off" go run main.go GO111MODULE="off" go run main.go
goimports:
goimports -w *.go
redomod:
rm -f go.*
GO111MODULE= go mod init
GO111MODULE= go mod tidy

3
example1/go.mod Normal file
View File

@ -0,0 +1,3 @@
module go.wit.com/lib/gui/shell/example1
go 1.21.4

View File

@ -9,7 +9,7 @@ import "os"
// import "github.com/davecgh/go-spew/spew" // import "github.com/davecgh/go-spew/spew"
import "go.wit.com/shell" import "go.wit.com/lib/gui/shell"
func main() { func main() {
shell.Run("ls /tmp") shell.Run("ls /tmp")

View File

@ -2,7 +2,7 @@ package main
import "log" import "log"
// import "fmt" // import "fmt"
import "go.wit.com/shell" import "go.wit.com/lib/gui/shell"
func main() { func main() {
err := shell.Run("cat /etc/issue") err := shell.Run("cat /etc/issue")

10
go.mod
View File

@ -1,4 +1,4 @@
module go.wit.com/shell module go.wit.com/lib/gui/shell
go 1.21.4 go 1.21.4
@ -6,14 +6,14 @@ require (
github.com/svent/go-nbreader v0.0.0-20150201200112-7cef48da76dc github.com/svent/go-nbreader v0.0.0-20150201200112-7cef48da76dc
github.com/tmc/scp v0.0.0-20170824174625-f7b48647feef github.com/tmc/scp v0.0.0-20170824174625-f7b48647feef
github.com/wercker/journalhook v0.0.0-20230927020745-64542ffa4117 github.com/wercker/journalhook v0.0.0-20230927020745-64542ffa4117
go.wit.com/log v0.4.1 go.wit.com/log v0.5.6
golang.org/x/crypto v0.17.0 golang.org/x/crypto v0.18.0
) )
require ( require (
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf // indirect github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect github.com/sirupsen/logrus v1.9.3 // indirect
go.wit.com/spew v0.0.0-20240101141411-c7b8e91573c9 // indirect go.wit.com/dev/davecgh/spew v1.1.4 // indirect
golang.org/x/sys v0.15.0 // indirect golang.org/x/sys v0.16.0 // indirect
) )

20
go.sum
View File

@ -18,17 +18,17 @@ github.com/tmc/scp v0.0.0-20170824174625-f7b48647feef h1:7D6Nm4D6f0ci9yttWaKjM1T
github.com/tmc/scp v0.0.0-20170824174625-f7b48647feef/go.mod h1:WLFStEdnJXpjK8kd4qKLwQKX/1vrDzp5BcDyiZJBHJM= github.com/tmc/scp v0.0.0-20170824174625-f7b48647feef/go.mod h1:WLFStEdnJXpjK8kd4qKLwQKX/1vrDzp5BcDyiZJBHJM=
github.com/wercker/journalhook v0.0.0-20230927020745-64542ffa4117 h1:67A5tweHp3C7osHjrYsy6pQZ00bYkTTttZ7kiOwwHeA= github.com/wercker/journalhook v0.0.0-20230927020745-64542ffa4117 h1:67A5tweHp3C7osHjrYsy6pQZ00bYkTTttZ7kiOwwHeA=
github.com/wercker/journalhook v0.0.0-20230927020745-64542ffa4117/go.mod h1:XCsSkdKK4gwBMNrOCZWww0pX6AOt+2gYc5Z6jBRrNVg= github.com/wercker/journalhook v0.0.0-20230927020745-64542ffa4117/go.mod h1:XCsSkdKK4gwBMNrOCZWww0pX6AOt+2gYc5Z6jBRrNVg=
go.wit.com/log v0.4.1 h1:x2PXlm1exgFrweGTM7thyj6KJabtnGlXssYkxv56hpc= go.wit.com/dev/davecgh/spew v1.1.4 h1:C9hj/rjlUpdK+E6aroyLjCbS5MFcyNUOuP1ICLWdNek=
go.wit.com/log v0.4.1/go.mod h1:EZLvivLZpMoXl5AUBArH0zsIgr+c+WyNXm14BCF+sdw= go.wit.com/dev/davecgh/spew v1.1.4/go.mod h1:sihvWmnQ/09FWplnEmozt90CCVqBtGuPXM811tgfhFA=
go.wit.com/spew v0.0.0-20240101141411-c7b8e91573c9 h1:UEX2EzLQPzLTfy/kUFQD7OXtvKn8wk/+jpDOkbl4ff4= go.wit.com/log v0.5.6 h1:rDC3ju95zfEads4f1Zm+QMkqjZ39CsYAT/UmQQs7VP4=
go.wit.com/spew v0.0.0-20240101141411-c7b8e91573c9/go.mod h1:qBpgJXThMMT15vym7/E4Ur9y8oOo2nP7t2RP52QHUNw= go.wit.com/log v0.5.6/go.mod h1:BaJBfHFqcJSJLXGQ9RHi3XVhPgsStxSMZRlaRxW4kAo=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k= golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4= golang.org/x/term v0.16.0 h1:m+B6fahuftsE9qjo0VWp2FW0mB3MTJvR0BaMQrq0pmE=
golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0= golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

6
int.go
View File

@ -1,6 +1,6 @@
package shell package shell
/* /*
send it anything, always get back an int send it anything, always get back an int
*/ */
@ -13,7 +13,7 @@ import "strconv"
func Int(s string) int { func Int(s string) int {
s = Chomp(s) s = Chomp(s)
i, err := strconv.Atoi(s) i, err := strconv.Atoi(s)
if (err != nil) { if err != nil {
handleError(err, -1) handleError(err, -1)
return 0 return 0
} }
@ -23,7 +23,7 @@ func Int(s string) int {
func Int64(s string) int64 { func Int64(s string) int64 {
s = Chomp(s) s = Chomp(s)
i, err := strconv.Atoi(s) i, err := strconv.Atoi(s)
if (err != nil) { if err != nil {
handleError(err, -1) handleError(err, -1)
return 0 return 0
} }

View File

@ -1,24 +1,20 @@
//go:build linux && go1.7
// +build linux,go1.7 // +build linux,go1.7
// put stuff in here that you only want compiled under linux // put stuff in here that you only want compiled under linux
package shell package shell
import "log" import (
import "os" "log"
import "os/signal" "os"
import "syscall" "os/signal"
"syscall"
// import "runtime" "github.com/wercker/journalhook"
// import "time" )
// import "reflect"
// import "go.wit.com/shell" var sigChan chan os.Signal
// import "github.com/davecgh/go-spew/spew"
import "github.com/wercker/journalhook"
var sigChan chan os.Signal
func handleSignal(err interface{}, ret int) { func handleSignal(err interface{}, ret int) {
log.Println("handleSignal() only should be compiled on linux") log.Println("handleSignal() only should be compiled on linux")

View File

@ -1,10 +1,12 @@
package shell package shell
import "crypto/md5" import (
import "encoding/hex" "crypto/md5"
import "log" "encoding/hex"
import "io" "io"
import "os" "log"
"os"
)
func hash_file_md5(filePath string) (string, error) { func hash_file_md5(filePath string) (string, error) {
var returnMD5String string var returnMD5String string

52
run.go
View File

@ -1,14 +1,14 @@
package shell package shell
import ( import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"os/exec"
"strings" "strings"
"time" "time"
"os/exec"
"bytes"
"io"
"fmt"
"os"
"bufio"
"github.com/svent/go-nbreader" "github.com/svent/go-nbreader"
@ -31,7 +31,7 @@ func Run(cmdline string) string {
func (cmd *Shell) Run(cmdline string) string { func (cmd *Shell) Run(cmdline string) string {
cmd.InitProcess(cmdline) cmd.InitProcess(cmdline)
if (cmd.Error != nil) { if cmd.Error != nil {
return "" return ""
} }
cmd.Exec(cmdline) cmd.Exec(cmdline)
@ -39,17 +39,17 @@ func (cmd *Shell) Run(cmdline string) string {
} }
func (cmd *Shell) InitProcess(cmdline string) { func (cmd *Shell) InitProcess(cmdline string) {
log.Log(RUN, "shell.InitProcess() START " + cmdline) log.Log(RUN, "shell.InitProcess() START "+cmdline)
cmd.Cmdline = Chomp(cmdline) // this is like 'chomp' in perl cmd.Cmdline = Chomp(cmdline) // this is like 'chomp' in perl
cmdArgs := strings.Fields(cmd.Cmdline) cmdArgs := strings.Fields(cmd.Cmdline)
if (len(cmdArgs) == 0) { if len(cmdArgs) == 0 {
cmd.Error = fmt.Errorf("cmdline == ''") cmd.Error = fmt.Errorf("cmdline == ''")
cmd.Done = true cmd.Done = true
return return
} }
if (cmdArgs[0] == "cd") { if cmdArgs[0] == "cd" {
if (len(cmdArgs) > 1) { if len(cmdArgs) > 1 {
log.Log(RUN, "os.Chdir()", cmd) log.Log(RUN, "os.Chdir()", cmd)
os.Chdir(cmdArgs[1]) os.Chdir(cmdArgs[1])
} }
@ -65,17 +65,17 @@ func (cmd *Shell) FileCreate(out string) {
var newfile File var newfile File
var iof io.ReadCloser var iof io.ReadCloser
if (out == "STDOUT") { if out == "STDOUT" {
iof, _ = cmd.Process.StdoutPipe() iof, _ = cmd.Process.StdoutPipe()
} else { } else {
iof, _ = cmd.Process.StderrPipe() iof, _ = cmd.Process.StderrPipe()
} }
newfile.Fio = iof newfile.Fio = iof
newfile.Fbufio = bufio.NewReader(iof) newfile.Fbufio = bufio.NewReader(iof)
newfile.Fnbreader = nbreader.NewNBReader(newfile.Fbufio, 1024) newfile.Fnbreader = nbreader.NewNBReader(newfile.Fbufio, 1024)
if (out == "STDOUT") { if out == "STDOUT" {
cmd.STDOUT = &newfile cmd.STDOUT = &newfile
} else { } else {
cmd.STDERR = &newfile cmd.STDERR = &newfile
@ -85,10 +85,10 @@ func (cmd *Shell) FileCreate(out string) {
// NOTE: this might cause problems: // NOTE: this might cause problems:
// always remove the newlines at the end ? // always remove the newlines at the end ?
func (cmd *Shell) Exec(cmdline string) { func (cmd *Shell) Exec(cmdline string) {
log.Log(RUN, "shell.Run() START " + cmdline) log.Log(RUN, "shell.Run() START "+cmdline)
cmd.InitProcess(cmdline) cmd.InitProcess(cmdline)
if (cmd.Error != nil) { if cmd.Error != nil {
return return
} }
@ -111,7 +111,7 @@ func (cmd *Shell) Exec(cmdline string) {
// time.Sleep(2 * time.Second) // putting this here doesn't help STDOUT flush() // time.Sleep(2 * time.Second) // putting this here doesn't help STDOUT flush()
if (err != nil) { if err != nil {
cmd.Fail = true cmd.Fail = true
cmd.Error = err cmd.Error = err
log.Log(RUN, "process.Wait() END err =", err.Error()) log.Log(RUN, "process.Wait() END err =", err.Error())
@ -125,10 +125,10 @@ func (cmd *Shell) Exec(cmdline string) {
func (cmd *Shell) Capture(f *File) { func (cmd *Shell) Capture(f *File) {
log.Log(RUN, "nbrREADER() START") log.Log(RUN, "nbrREADER() START")
if (cmd.Buffer == nil) { if cmd.Buffer == nil {
cmd.Buffer = new(bytes.Buffer) cmd.Buffer = new(bytes.Buffer)
} }
if (cmd.Buffer == nil) { if cmd.Buffer == nil {
f.Dead = false f.Dead = false
cmd.Error = fmt.Errorf("could not make buffer") cmd.Error = fmt.Errorf("could not make buffer")
log.Error(cmd.Error, "f.Buffer == nil") log.Error(cmd.Error, "f.Buffer == nil")
@ -139,15 +139,15 @@ func (cmd *Shell) Capture(f *File) {
f.Dead = false f.Dead = false
// loop that keeps trying to read from f // loop that keeps trying to read from f
for (f.Dead == false) { for f.Dead == false {
time.Sleep(time.Duration(msecDelay) * time.Millisecond) // only check the buffer 500 times a second time.Sleep(time.Duration(msecDelay) * time.Millisecond) // only check the buffer 500 times a second
// set to false so it keeps retrying reads // set to false so it keeps retrying reads
f.Empty = false f.Empty = false
// tight loop that reads 1024 bytes at a time until buffer is empty // tight loop that reads 1024 bytes at a time until buffer is empty
// 1024 is set in f.BufferSize // 1024 is set in f.BufferSize
for (f.Empty == false) { for f.Empty == false {
f.Empty = cmd.ReadToBuffer(f) f.Empty = cmd.ReadToBuffer(f)
} }
} }
@ -158,7 +158,7 @@ func (cmd *Shell) ReadToBuffer(f *File) bool {
log.Log(RUN, "ReadToBuffer() START") log.Log(RUN, "ReadToBuffer() START")
nbr := f.Fnbreader nbr := f.Fnbreader
oneByte := make([]byte, 1024) oneByte := make([]byte, 1024)
if (nbr == nil) { if nbr == nil {
// log.Debugln("ReadToBuffer() ERROR nbr is nil") // log.Debugln("ReadToBuffer() ERROR nbr is nil")
f.Dead = true f.Dead = true
return true return true
@ -166,13 +166,13 @@ func (cmd *Shell) ReadToBuffer(f *File) bool {
count, err := nbr.Read(oneByte) count, err := nbr.Read(oneByte)
f.TotalCount += count f.TotalCount += count
if (err != nil) { if err != nil {
// log.Debugln("ReadToBuffer() file has closed with", err) // log.Debugln("ReadToBuffer() file has closed with", err)
// log.Debugln("ReadToBuffer() count = ", count, "err = ", err) // log.Debugln("ReadToBuffer() count = ", count, "err = ", err)
f.Dead = true f.Dead = true
return true return true
} }
if (count == 0) { if count == 0 {
// log.Debugln("ReadToBuffer() START count == 0 return true") // log.Debugln("ReadToBuffer() START count == 0 return true")
return true return true
} }

View File

@ -1,18 +1,17 @@
package shell package shell
import ( import (
"strings"
"time"
"os"
"os/exec"
"bufio" "bufio"
"io/ioutil" "io/ioutil"
"os"
"os/exec"
"strings"
"time"
"go.wit.com/log"
"github.com/svent/go-nbreader" "github.com/svent/go-nbreader"
"go.wit.com/log"
) )
// TODO: look at https://github.com/go-cmd/cmd/issues/20 // TODO: look at https://github.com/go-cmd/cmd/issues/20
// use go-cmd instead here? // use go-cmd instead here?
@ -21,8 +20,9 @@ var callback func(interface{}, int)
var shellStdout *os.File var shellStdout *os.File
var shellStderr *os.File var shellStderr *os.File
var spewOn bool = false var spewOn bool = false
var quiet bool = false var quiet bool = false
// var msecDelay int = 20 // number of milliseconds to delay between reads with no data // var msecDelay int = 20 // number of milliseconds to delay between reads with no data
// var bytesBuffer bytes.Buffer // var bytesBuffer bytes.Buffer
@ -30,13 +30,13 @@ var quiet bool = false
func handleError(c interface{}, ret int) { func handleError(c interface{}, ret int) {
log.Log(INFO, "shell.Run() Returned", ret) log.Log(INFO, "shell.Run() Returned", ret)
if (callback != nil) { if callback != nil {
callback(c, ret) callback(c, ret)
} }
} }
func init() { func init() {
callback = nil callback = nil
} }
func InitCallback(f func(interface{}, int)) { func InitCallback(f func(interface{}, int)) {
@ -112,7 +112,7 @@ func nonBlockingReader(buffReader *bufio.Reader, writeFileHandle *os.File, stdou
// newreader := bufio.NewReader(readFileHandle) // newreader := bufio.NewReader(readFileHandle)
// create a nonblocking GO reader // create a nonblocking GO reader
nbr := nbreader.NewNBReader(buffReader, 1024) nbr := nbreader.NewNBReader(buffReader, 1024)
for { for {
// defer buffReader.Close() // defer buffReader.Close()
@ -122,22 +122,22 @@ func nonBlockingReader(buffReader *bufio.Reader, writeFileHandle *os.File, stdou
for { for {
oneByte := make([]byte, 1024) oneByte := make([]byte, 1024)
count, err := nbr.Read(oneByte) count, err := nbr.Read(oneByte)
if (err != nil) { if err != nil {
log.Log(INFO, "count, err =", count, err) log.Log(INFO, "count, err =", count, err)
handleError(err, -1) handleError(err, -1)
return return
} }
totalCount += count totalCount += count
if (count == 0) { if count == 0 {
time.Sleep(time.Duration(msecDelay) * time.Millisecond) // without this delay this will peg the CPU time.Sleep(time.Duration(msecDelay) * time.Millisecond) // without this delay this will peg the CPU
if (totalCount != 0) { if totalCount != 0 {
log.Log(INFO, "STDERR: totalCount = ", totalCount) log.Log(INFO, "STDERR: totalCount = ", totalCount)
totalCount = 0 totalCount = 0
} }
} else { } else {
log.Log(INFO, "STDERR: count = ", count) log.Log(INFO, "STDERR: count = ", count)
writeFileHandle.Write(oneByte[0:count]) writeFileHandle.Write(oneByte[0:count])
if (quiet == false) { if quiet == false {
stdout.Write(oneByte[0:count]) stdout.Write(oneByte[0:count])
stdout.Flush() stdout.Flush()
} }
@ -150,15 +150,15 @@ func nonBlockingReader(buffReader *bufio.Reader, writeFileHandle *os.File, stdou
// TODO: pass STDOUT, STDERR, STDIN correctly // TODO: pass STDOUT, STDERR, STDIN correctly
// TODO: figure out how to nohup the process and exit // TODO: figure out how to nohup the process and exit
func Exec(cmdline string) { func Exec(cmdline string) {
log.Log(INFO, "shell.Run() START " + cmdline) log.Log(INFO, "shell.Run() START "+cmdline)
cmd := Chomp(cmdline) // this is like 'chomp' in perl cmd := Chomp(cmdline) // this is like 'chomp' in perl
cmdArgs := strings.Fields(cmd) cmdArgs := strings.Fields(cmd)
process := exec.Command(cmdArgs[0], cmdArgs[1:len(cmdArgs)]...) process := exec.Command(cmdArgs[0], cmdArgs[1:len(cmdArgs)]...)
process.Stderr = os.Stderr process.Stderr = os.Stderr
process.Stdin = os.Stdin process.Stdin = os.Stdin
process.Stdout = os.Stdout process.Stdout = os.Stdout
process.Start() process.Start()
err := process.Wait() err := process.Wait()
log.Log(INFO, "shell.Exec() err =", err) log.Log(INFO, "shell.Exec() err =", err)

24
ssh.go
View File

@ -5,23 +5,23 @@ import (
"io/ioutil" "io/ioutil"
"time" "time"
"golang.org/x/crypto/ssh"
"github.com/tmc/scp" "github.com/tmc/scp"
"go.wit.com/log" "go.wit.com/log"
"golang.org/x/crypto/ssh"
) )
var sshHostname string var sshHostname string
var sshPort int var sshPort int
var sshUsername string var sshUsername string
var sshPassword string var sshPassword string
var sshKeyfile string var sshKeyfile string
func SSHclientSet(hostname string, port int, username string, pass string, keyfile string) { func SSHclientSet(hostname string, port int, username string, pass string, keyfile string) {
sshHostname = hostname sshHostname = hostname
sshPort = port sshPort = port
sshUsername = username sshUsername = username
sshPassword = pass sshPassword = pass
sshKeyfile = keyfile sshKeyfile = keyfile
} }
func SSHclientSCP(localfile string, remotefile string) { func SSHclientSCP(localfile string, remotefile string) {
@ -49,7 +49,7 @@ func mySsh(hostname string, port int, username string, pass string, keyfile stri
// log.Log(SSH, "hostkey =", hostKey) // log.Log(SSH, "hostkey =", hostKey)
publicKey, err := PublicKeyFile(keyfile) publicKey, err := PublicKeyFile(keyfile)
if (err != nil) { if err != nil {
log.Log(SSH, "PublicKeyFile() error =", err) log.Log(SSH, "PublicKeyFile() error =", err)
} }
@ -75,7 +75,7 @@ func mySsh(hostname string, port int, username string, pass string, keyfile stri
ssh.KeyAlgoED25519, ssh.KeyAlgoED25519,
}, },
// optional tcp connect timeout // optional tcp connect timeout
Timeout: 5 * time.Second, Timeout: 5 * time.Second,
} }
sport := fmt.Sprintf("%d", port) sport := fmt.Sprintf("%d", port)

View File

@ -1,45 +1,48 @@
package shell package shell
import "io" import (
import "os/exec" "bufio"
import "bufio" "bytes"
import "bytes" "io"
import "github.com/svent/go-nbreader" "os/exec"
var FileMap map[string]*File "github.com/svent/go-nbreader"
)
var FileMap map[string]*File
var readBufferSize int var readBufferSize int
type File struct { type File struct {
Name string Name string
// BufferSize int // BufferSize int
// Buffer *bytes.Buffer // Buffer *bytes.Buffer
// Fbytes []byte // Fbytes []byte
TotalCount int TotalCount int
Empty bool Empty bool
Dead bool Dead bool
Fio io.ReadCloser // := process.StdoutPipe() Fio io.ReadCloser // := process.StdoutPipe()
Fbufio *bufio.Reader // := bufio.NewReader(pOUT) Fbufio *bufio.Reader // := bufio.NewReader(pOUT)
Fnbreader *nbreader.NBReader // := nbreader.NewNBReader(readOUT, 1024) Fnbreader *nbreader.NBReader // := nbreader.NewNBReader(readOUT, 1024)
} }
type Shell struct { type Shell struct {
Cmdline string Cmdline string
Process *exec.Cmd Process *exec.Cmd
Done bool Done bool
Quiet bool Quiet bool
Fail bool Fail bool
Error error Error error
Buffer *bytes.Buffer Buffer *bytes.Buffer
// which names are really better here? // which names are really better here?
// for now I init them both to test out // for now I init them both to test out
// how the code looks and feels // how the code looks and feels
STDOUT *File STDOUT *File
STDERR *File STDERR *File
Stdout *File Stdout *File
Stderr *File Stderr *File
} }
// default values for Shell // default values for Shell

15
wget.go
View File

@ -1,6 +1,6 @@
package shell package shell
/* /*
This simply parses the command line arguments using the default golang This simply parses the command line arguments using the default golang
package called 'flag'. This can be used as a simple template to parse package called 'flag'. This can be used as a simple template to parse
command line arguments in other programs. command line arguments in other programs.
@ -11,17 +11,17 @@ package shell
*/ */
import ( import (
"io"
"os"
"fmt"
"bytes" "bytes"
"strings" "fmt"
"io"
"net/http" "net/http"
"os"
"strings"
"go.wit.com/log" "go.wit.com/log"
) )
func Wget(url string) (*bytes.Buffer) { func Wget(url string) *bytes.Buffer {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
// Get the data // Get the data
@ -33,7 +33,7 @@ func Wget(url string) (*bytes.Buffer) {
defer resp.Body.Close() defer resp.Body.Close()
log.Log(INFO, "res.StatusCode: %d\n", resp.StatusCode) log.Log(INFO, "res.StatusCode: %d\n", resp.StatusCode)
if (resp.StatusCode != 200) { if resp.StatusCode != 200 {
handleError(fmt.Errorf(fmt.Sprint("%d", resp.StatusCode)), -1) handleError(fmt.Errorf(fmt.Sprint("%d", resp.StatusCode)), -1)
return nil return nil
} }
@ -72,7 +72,6 @@ func WgetToFile(filepath string, url string) error {
// package. I will quote the evilwm man page: // package. I will quote the evilwm man page:
// //
// BUGS: The author's idea of friendly may differ to that of many other people. // BUGS: The author's idea of friendly may differ to that of many other people.
//
func Write(filepath string, data string) bool { func Write(filepath string, data string) bool {
// TODO: this isn't working for some reason and is making two '\n' chars // TODO: this isn't working for some reason and is making two '\n' chars
// probably because Chomp() isn't fixed yet // probably because Chomp() isn't fixed yet

View File

@ -1,3 +1,4 @@
//go:build windows
// +build windows // +build windows
// put stuff in here that you only want compiled under windows // put stuff in here that you only want compiled under windows
@ -8,9 +9,6 @@ import (
"go.wit.com/log" "go.wit.com/log"
) )
// import "go.wit.com/shell"
// import "github.com/davecgh/go-spew/spew"
func handleSignal(err interface{}, ret int) { func handleSignal(err interface{}, ret int) {
log.Warn("handleSignal() windows doesn't do signals") log.Warn("handleSignal() windows doesn't do signals")
} }

View File

@ -3,7 +3,7 @@ package shell
import ( import (
"runtime" "runtime"
"strings" "strings"
"go.wit.com/log" "go.wit.com/log"
) )