Skip to content

Commit

Permalink
Add unarchiver (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
yahavi authored Apr 18, 2023
1 parent 6b9e00f commit 2bf299d
Show file tree
Hide file tree
Showing 40 changed files with 433 additions and 20 deletions.
2 changes: 1 addition & 1 deletion datastructures/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type Set[T comparable] struct {
container map[T]struct{}
}

//MakeSet initialize the set
// MakeSet initialize the set
func MakeSet[T comparable]() *Set[T] {
return &Set[T]{
container: make(map[T]struct{}),
Expand Down
6 changes: 3 additions & 3 deletions fanout/readall_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"sync"
)

//A reader that emits its read to multiple consumers using a ReadAll(p []byte) ([]interface{}, error) func
// A reader that emits its read to multiple consumers using a ReadAll(p []byte) ([]interface{}, error) func
type ReadAllReader struct {
reader io.Reader
consumers []ReadAllConsumer
Expand Down Expand Up @@ -55,8 +55,8 @@ func NewReadAllReader(reader io.Reader, consumers ...ReadAllConsumer) *ReadAllRe
}

func toWriters(pipeWriters []*io.PipeWriter) (writers []io.Writer) {
//Convert to an array of io.Writers so it can be taken by a variadic func
//See: https://groups.google.com/forum/#!topic/golang-nuts/zU3BqD5mKs8
// Convert to an array of io.Writers so it can be taken by a variadic func
// See: https://groups.google.com/forum/#!topic/golang-nuts/zU3BqD5mKs8
writers = make([]io.Writer, len(pipeWriters))
for i, w := range pipeWriters {
writers[i] = w
Expand Down
2 changes: 1 addition & 1 deletion fanout/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"sync"
)

//A reader that emits its read to multiple consumers using an io.Reader Read(p []byte) (int, error) func
// A reader that emits its read to multiple consumers using an io.Reader Read(p []byte) (int, error) func
type Reader struct {
reader io.Reader
consumers []Consumer
Expand Down
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@ module github.com/jfrog/gofrog
go 1.19

require (
github.com/mholt/archiver/v3 v3.5.1
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.0
)

require (
github.com/andybalholm/brotli v1.0.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 // indirect
github.com/golang/snappy v0.0.2 // indirect
github.com/klauspost/compress v1.11.4 // indirect
github.com/klauspost/pgzip v1.2.5 // indirect
github.com/nwaples/rardecode v1.1.0 // indirect
github.com/pierrec/lz4/v4 v4.1.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/ulikunitz/xz v0.5.9 // indirect
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
26 changes: 26 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
github.com/andybalholm/brotli v1.0.1 h1:KqhlKozYbRtJvsPrrEeXcO+N2l6NYT5A2QAFmSULpEc=
github.com/andybalholm/brotli v1.0.1/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 h1:iFaUwBSo5Svw6L7HYpRu/0lE3e0BaElwnNO1qkNQxBY=
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5/go.mod h1:qssHWj60/X5sZFNxpG4HBPDHVqxNm4DfnCKgrbZOT+s=
github.com/dsnet/golib v0.0.0-20171103203638-1ea166775780/go.mod h1:Lj+Z9rebOhdfkVLjJ8T6VcRQv3SXugXy999NBtR9aFY=
github.com/golang/snappy v0.0.2 h1:aeE13tS0IiQgFjYdoL8qN3K1N2bXXtI6Vi51/y7BpMw=
github.com/golang/snappy v0.0.2/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
github.com/klauspost/compress v1.11.4 h1:kz40R/YWls3iqT9zX9AHN3WoVsrAWVyui5sxuLqiXqU=
github.com/klauspost/compress v1.11.4/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/klauspost/pgzip v1.2.5 h1:qnWYvvKqedOF2ulHpMG72XQol4ILEJ8k2wwRl/Km8oE=
github.com/klauspost/pgzip v1.2.5/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
github.com/mholt/archiver/v3 v3.5.1 h1:rDjOBX9JSF5BvoJGvjqK479aL70qh9DIpZCl+k7Clwo=
github.com/mholt/archiver/v3 v3.5.1/go.mod h1:e3dqJ7H78uzsRSEACH1joayhuSyhnonssnDhppzS1L4=
github.com/nwaples/rardecode v1.1.0 h1:vSxaY8vQhOcVr4mm5e8XllHWTiM4JF507A0Katqw7MQ=
github.com/nwaples/rardecode v1.1.0/go.mod h1:5DzqNKiOdpKKBH87u8VlvAnPZMXcGRhxWkRpHbbfGS0=
github.com/pierrec/lz4/v4 v4.1.2 h1:qvY3YFXRQE/XB8MlLzJH7mSzBs74eA2gg52YTk6jUPM=
github.com/pierrec/lz4/v4 v4.1.2/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -10,6 +30,12 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/ulikunitz/xz v0.5.8/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/ulikunitz/xz v0.5.9 h1:RsKRIA2MO8x56wkkcd3LbtcE/uMszhb6DpRf+3uwa3I=
github.com/ulikunitz/xz v0.5.9/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 h1:nIPpBwaJSVYIxUFsDv3M8ofmx9yWTog9BfvIu0q41lo=
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8/go.mod h1:HUYIGzjTL3rfEspMxjDjgmT5uz5wzYJKVo23qUhYTos=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
38 changes: 23 additions & 15 deletions io/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,27 +79,18 @@ func RunCmdWithOutputParser(config CmdConfig, prompt bool, regExpStruct ...*CmdO
}

cmd := config.GetCmd()
cmdReader, err := cmd.StdoutPipe()
stdoutReader, stderrReader, err := createCommandReaders(cmd)
if err != nil {
return
}
defer cmdReader.Close()
scanner := bufio.NewScanner(cmdReader)
cmdReaderStderr, err := cmd.StderrPipe()
if err != nil {
return
}
defer cmdReaderStderr.Close()
scannerStderr := bufio.NewScanner(cmdReaderStderr)
err = cmd.Start()
if err != nil {
if err = cmd.Start(); err != nil {
return
}
errChan := make(chan error)
wg.Add(1)
go func() {
for scanner.Scan() {
line := scanner.Text()
for stdoutReader.Scan() {
line := stdoutReader.Text()
for _, regExp := range regExpStruct {
matched := regExp.RegExp.Match([]byte(line))
if matched {
Expand All @@ -120,8 +111,8 @@ func RunCmdWithOutputParser(config CmdConfig, prompt bool, regExpStruct ...*CmdO
}()
wg.Add(1)
go func() {
for scannerStderr.Scan() {
line := scannerStderr.Text()
for stderrReader.Scan() {
line := stderrReader.Text()
var scannerError error
for _, regExp := range regExpStruct {
matched := regExp.RegExp.Match([]byte(line))
Expand Down Expand Up @@ -167,6 +158,23 @@ func RunCmdWithOutputParser(config CmdConfig, prompt bool, regExpStruct ...*CmdO
return
}

// Create command stdout and stderr readers.
// The returned readers are automatically closed after the running command exit and shouldn't be closed explicitly.
// cmd - The command to execute
func createCommandReaders(cmd *exec.Cmd) (*bufio.Scanner, *bufio.Scanner, error) {
stdoutReader, err := cmd.StdoutPipe()
if err != nil {
return nil, nil, err
}

stderrReader, err := cmd.StderrPipe()
if err != nil {
return nil, nil, err
}

return bufio.NewScanner(stdoutReader), bufio.NewScanner(stderrReader), nil
}

type CmdConfig interface {
GetCmd() *exec.Cmd
GetEnv() map[string]string
Expand Down
Loading

0 comments on commit 2bf299d

Please sign in to comment.