-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor flags handling, add 'ls', 'cat', 'unpack' subcommands
Closes #11
- Loading branch information
Showing
15 changed files
with
381 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
_completion_booster() { | ||
# All arguments except the first one | ||
args=("${COMP_WORDS[@]:1:$COMP_CWORD}") | ||
|
||
# Only split on newlines | ||
local IFS=$'\n' | ||
|
||
# Call completion (note that the first element of COMP_WORDS is | ||
# the executable itself) | ||
COMPREPLY=($(GO_FLAGS_COMPLETION=1 ${COMP_WORDS[0]} "${args[@]}")) | ||
return 0 | ||
} | ||
|
||
complete -F _completion_booster booster |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"os" | ||
) | ||
|
||
type matcher func(seeker io.ReadSeeker) (bool, error) | ||
|
||
var matchers = map[string]matcher{ | ||
"zstd": matchZstd, | ||
"gzip": matchGzip, | ||
"xz": matchXz, | ||
"lz4": matchLz4, | ||
"cpio": matchCpio, | ||
} | ||
|
||
func matchBytes(f io.ReadSeeker, offset int64, marker []byte) (bool, error) { | ||
if _, err := f.Seek(offset, io.SeekStart); err != nil { | ||
return false, err | ||
} | ||
buff := make([]byte, len(marker)) | ||
if _, err := io.ReadFull(f, buff); err != nil { | ||
return false, nil | ||
} | ||
return bytes.Equal(marker, buff), nil | ||
} | ||
|
||
func matchCpio(f io.ReadSeeker) (bool, error) { | ||
return matchBytes(f, 0, []byte{'0', '7', '0', '7', '0', '1'}) // "new" cpio format | ||
} | ||
|
||
func matchLz4(f io.ReadSeeker) (bool, error) { | ||
return matchBytes(f, 0, []byte{0x02, 0x21, 0x4c, 0x18}) // legacy format used by linux loader | ||
} | ||
|
||
func matchXz(f io.ReadSeeker) (bool, error) { | ||
return matchBytes(f, 0, []byte{0xfd, '7', 'z', 'X', 'Z', 0x00}) | ||
} | ||
|
||
func matchGzip(f io.ReadSeeker) (bool, error) { | ||
return matchBytes(f, 0, []byte{0x1f, 0x8b}) | ||
} | ||
|
||
func matchZstd(f io.ReadSeeker) (bool, error) { | ||
return matchBytes(f, 0, []byte{0x28, 0xb5, 0x2f, 0xfd}) | ||
} | ||
|
||
func filetype(r *os.File) (string, error) { | ||
loc, err := r.Seek(0, io.SeekCurrent) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer r.Seek(loc, io.SeekStart) | ||
|
||
for name, match := range matchers { | ||
ok, err := match(r) | ||
if err != nil { | ||
return "", err | ||
} | ||
if ok { | ||
return name, nil | ||
} | ||
} | ||
|
||
return "", nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/cavaliercoder/go-cpio" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFileType(t *testing.T) { | ||
dir := t.TempDir() | ||
check := func(compression, expectedType string) { | ||
fileName := dir + "/" + compression | ||
img, err := NewImage(fileName, compression, false) | ||
require.NoError(t, err) | ||
|
||
require.NoError(t, img.AppendEntry("foo.txt", cpio.ModeRegular, []byte("hello, world!"))) | ||
require.NoError(t, img.Close()) | ||
|
||
f, err := os.Open(fileName) | ||
require.NoError(t, err) | ||
|
||
kind, err := filetype(f) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, expectedType, kind) | ||
} | ||
|
||
check("zstd", "zstd") | ||
check("gzip", "gzip") | ||
check("xz", "xz") | ||
check("lz4", "lz4") | ||
check("none", "cpio") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.