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:
# '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
push:
git pull
git add --all
-git commit -a -s
git push
goimports:
goimports -w *.go
redomod:
rm -f go.*
unset GO111MODULES && go mod init
unset GO111MODULES && 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
GO111MODULE= go mod init
GO111MODULE= go mod tidy

View File

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

View File

@ -12,7 +12,7 @@ var RUN *log.LogFlag
var SSH *log.LogFlag
func init() {
full := "go.wit.com/shell"
full := "go.wit.com/lib/gui/shell"
short := "shell"
NOW = log.NewFlag("NOW", true, full, short, "temp debugging stuff")

View File

@ -7,10 +7,10 @@ package shell
*/
import (
"bytes"
"fmt"
"reflect"
"strings"
"bytes"
"go.wit.com/log"
)
@ -24,14 +24,12 @@ func chompBytesBuffer(buf *bytes.Buffer) string {
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!)
@ -74,7 +72,7 @@ func Chomp(a interface{}) string {
// log.Printf("shell.Chomp() FOUND *bytes.Buffer")
var tmp *bytes.Buffer
tmp = a.(*bytes.Buffer)
if (tmp == nil) {
if tmp == nil {
return ""
}

View File

@ -1,3 +1,11 @@
all:
# go build
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 "go.wit.com/shell"
import "go.wit.com/lib/gui/shell"
func main() {
shell.Run("ls /tmp")

View File

@ -2,7 +2,7 @@ package main
import "log"
// import "fmt"
import "go.wit.com/shell"
import "go.wit.com/lib/gui/shell"
func main() {
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
@ -6,14 +6,14 @@ require (
github.com/svent/go-nbreader v0.0.0-20150201200112-7cef48da76dc
github.com/tmc/scp v0.0.0-20170824174625-f7b48647feef
github.com/wercker/journalhook v0.0.0-20230927020745-64542ffa4117
go.wit.com/log v0.4.1
golang.org/x/crypto v0.17.0
go.wit.com/log v0.5.6
golang.org/x/crypto v0.18.0
)
require (
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
go.wit.com/spew v0.0.0-20240101141411-c7b8e91573c9 // indirect
golang.org/x/sys v0.15.0 // indirect
go.wit.com/dev/davecgh/spew v1.1.4 // 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/wercker/journalhook v0.0.0-20230927020745-64542ffa4117 h1:67A5tweHp3C7osHjrYsy6pQZ00bYkTTttZ7kiOwwHeA=
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/log v0.4.1/go.mod h1:EZLvivLZpMoXl5AUBArH0zsIgr+c+WyNXm14BCF+sdw=
go.wit.com/spew v0.0.0-20240101141411-c7b8e91573c9 h1:UEX2EzLQPzLTfy/kUFQD7OXtvKn8wk/+jpDOkbl4ff4=
go.wit.com/spew v0.0.0-20240101141411-c7b8e91573c9/go.mod h1:qBpgJXThMMT15vym7/E4Ur9y8oOo2nP7t2RP52QHUNw=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
go.wit.com/dev/davecgh/spew v1.1.4 h1:C9hj/rjlUpdK+E6aroyLjCbS5MFcyNUOuP1ICLWdNek=
go.wit.com/dev/davecgh/spew v1.1.4/go.mod h1:sihvWmnQ/09FWplnEmozt90CCVqBtGuPXM811tgfhFA=
go.wit.com/log v0.5.6 h1:rDC3ju95zfEads4f1Zm+QMkqjZ39CsYAT/UmQQs7VP4=
go.wit.com/log v0.5.6/go.mod h1:BaJBfHFqcJSJLXGQ9RHi3XVhPgsStxSMZRlaRxW4kAo=
golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc=
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.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.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.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.16.0 h1:m+B6fahuftsE9qjo0VWp2FW0mB3MTJvR0BaMQrq0pmE=
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/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=

4
int.go
View File

@ -13,7 +13,7 @@ import "strconv"
func Int(s string) int {
s = Chomp(s)
i, err := strconv.Atoi(s)
if (err != nil) {
if err != nil {
handleError(err, -1)
return 0
}
@ -23,7 +23,7 @@ func Int(s string) int {
func Int64(s string) int64 {
s = Chomp(s)
i, err := strconv.Atoi(s)
if (err != nil) {
if err != nil {
handleError(err, -1)
return 0
}

View File

@ -1,22 +1,18 @@
//go:build linux && go1.7
// +build linux,go1.7
// put stuff in here that you only want compiled under linux
package shell
import "log"
import "os"
import "os/signal"
import "syscall"
import (
"log"
"os"
"os/signal"
"syscall"
// import "runtime"
// import "time"
// import "reflect"
// import "go.wit.com/shell"
// import "github.com/davecgh/go-spew/spew"
import "github.com/wercker/journalhook"
"github.com/wercker/journalhook"
)
var sigChan chan os.Signal

View File

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

46
run.go
View File

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

View File

@ -1,18 +1,17 @@
package shell
import (
"strings"
"time"
"os"
"os/exec"
"bufio"
"io/ioutil"
"os"
"os/exec"
"strings"
"time"
"go.wit.com/log"
"github.com/svent/go-nbreader"
"go.wit.com/log"
)
// TODO: look at https://github.com/go-cmd/cmd/issues/20
// use go-cmd instead here?
@ -23,6 +22,7 @@ var shellStderr *os.File
var spewOn bool = false
var quiet bool = false
// var msecDelay int = 20 // number of milliseconds to delay between reads with no data
// var bytesBuffer bytes.Buffer
@ -30,7 +30,7 @@ var quiet bool = false
func handleError(c interface{}, ret int) {
log.Log(INFO, "shell.Run() Returned", ret)
if (callback != nil) {
if callback != nil {
callback(c, ret)
}
}
@ -122,22 +122,22 @@ func nonBlockingReader(buffReader *bufio.Reader, writeFileHandle *os.File, stdou
for {
oneByte := make([]byte, 1024)
count, err := nbr.Read(oneByte)
if (err != nil) {
if err != nil {
log.Log(INFO, "count, err =", count, err)
handleError(err, -1)
return
}
totalCount += count
if (count == 0) {
if count == 0 {
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)
totalCount = 0
}
} else {
log.Log(INFO, "STDERR: count = ", count)
writeFileHandle.Write(oneByte[0:count])
if (quiet == false) {
if quiet == false {
stdout.Write(oneByte[0:count])
stdout.Flush()
}
@ -150,7 +150,7 @@ func nonBlockingReader(buffReader *bufio.Reader, writeFileHandle *os.File, stdou
// TODO: pass STDOUT, STDERR, STDIN correctly
// TODO: figure out how to nohup the process and exit
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
cmdArgs := strings.Fields(cmd)

4
ssh.go
View File

@ -5,9 +5,9 @@ import (
"io/ioutil"
"time"
"golang.org/x/crypto/ssh"
"github.com/tmc/scp"
"go.wit.com/log"
"golang.org/x/crypto/ssh"
)
var sshHostname string
@ -49,7 +49,7 @@ func mySsh(hostname string, port int, username string, pass string, keyfile stri
// log.Log(SSH, "hostkey =", hostKey)
publicKey, err := PublicKeyFile(keyfile)
if (err != nil) {
if err != nil {
log.Log(SSH, "PublicKeyFile() error =", err)
}

View File

@ -1,10 +1,13 @@
package shell
import "io"
import "os/exec"
import "bufio"
import "bytes"
import "github.com/svent/go-nbreader"
import (
"bufio"
"bytes"
"io"
"os/exec"
"github.com/svent/go-nbreader"
)
var FileMap map[string]*File

13
wget.go
View File

@ -11,17 +11,17 @@ package shell
*/
import (
"io"
"os"
"fmt"
"bytes"
"strings"
"fmt"
"io"
"net/http"
"os"
"strings"
"go.wit.com/log"
)
func Wget(url string) (*bytes.Buffer) {
func Wget(url string) *bytes.Buffer {
buf := new(bytes.Buffer)
// Get the data
@ -33,7 +33,7 @@ func Wget(url string) (*bytes.Buffer) {
defer resp.Body.Close()
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)
return nil
}
@ -72,7 +72,6 @@ func WgetToFile(filepath string, url string) error {
// package. I will quote the evilwm man page:
//
// BUGS: The author's idea of friendly may differ to that of many other people.
//
func Write(filepath string, data string) bool {
// TODO: this isn't working for some reason and is making two '\n' chars
// probably because Chomp() isn't fixed yet

View File

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