refact: refactored to use islazy and updated deps

This commit is contained in:
evilsocket 2018-10-10 19:00:25 +02:00
parent a2b3ee79fb
commit d070445225
238 changed files with 12662 additions and 1586 deletions

View file

@ -1,29 +1,13 @@
package core
import (
"archive/zip"
"fmt"
"io"
"os"
"os/exec"
"os/user"
"path/filepath"
"sort"
"strings"
"github.com/evilsocket/islazy/str"
)
const (
defaultTrimSet = "\r\n\t "
)
func Trim(s string) string {
return strings.Trim(s, defaultTrimSet)
}
func TrimRight(s string) string {
return strings.TrimRight(s, defaultTrimSet)
}
func UniqueInts(a []int, sorted bool) []int {
tmp := make(map[int]bool)
uniq := make([]int, 0)
@ -43,22 +27,6 @@ func UniqueInts(a []int, sorted bool) []int {
return uniq
}
func SepSplit(sv string, sep string) []string {
filtered := make([]string, 0)
for _, part := range strings.Split(sv, sep) {
part = Trim(part)
if part != "" {
filtered = append(filtered, part)
}
}
return filtered
}
func CommaSplit(csv string) []string {
return SepSplit(csv, ",")
}
func ExecSilent(executable string, args []string) (string, error) {
path, err := exec.LookPath(executable)
if err != nil {
@ -69,7 +37,7 @@ func ExecSilent(executable string, args []string) (string, error) {
if err != nil {
return "", err
} else {
return Trim(string(raw)), nil
return str.Trim(string(raw)), nil
}
}
@ -80,72 +48,3 @@ func Exec(executable string, args []string) (string, error) {
}
return out, err
}
func Exists(path string) bool {
if _, err := os.Stat(path); os.IsNotExist(err) {
return false
}
return true
}
func ExpandPath(path string) (string, error) {
// Check if path is empty
if path != "" {
if strings.HasPrefix(path, "~") {
usr, err := user.Current()
if err != nil {
return "", err
} else {
// Replace only the first occurrence of ~
path = strings.Replace(path, "~", usr.HomeDir, 1)
}
}
return filepath.Abs(path)
}
return "", nil
}
// Unzip will decompress a zip archive, moving all files and folders
// within the zip file (parameter 1) to an output directory (parameter 2).
// Credits to https://golangcode.com/unzip-files-in-go/
func Unzip(src string, dest string) ([]string, error) {
var filenames []string
r, err := zip.OpenReader(src)
if err != nil {
return filenames, err
}
defer r.Close()
for _, f := range r.File {
rc, err := f.Open()
if err != nil {
return filenames, err
}
defer rc.Close()
// Store filename/path for returning and using later on
fpath := filepath.Join(dest, f.Name)
// Check for ZipSlip. More Info: https://snyk.io/research/zip-slip-vulnerability#go
if !strings.HasPrefix(fpath, filepath.Clean(dest)+string(os.PathSeparator)) {
return filenames, fmt.Errorf("%s: illegal file path", fpath)
}
filenames = append(filenames, fpath)
if f.FileInfo().IsDir() {
os.MkdirAll(fpath, os.ModePerm)
} else if err = os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil {
return filenames, err
} else if outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode()); err != nil {
return filenames, err
} else {
defer outFile.Close()
if _, err = io.Copy(outFile, rc); err != nil {
return filenames, err
}
}
}
return filenames, nil
}

View file

@ -1,122 +0,0 @@
package core
import (
"github.com/mattn/go-isatty"
"os"
)
const (
DEBUG = iota
INFO
IMPORTANT
WARNING
ERROR
FATAL
)
// https://misc.flogisoft.com/bash/tip_colors_and_formatting
var (
BOLD = "\033[1m"
DIM = "\033[2m"
RED = "\033[31m"
GREEN = "\033[32m"
BLUE = "\033[34m"
YELLOW = "\033[33m"
FG_BLACK = "\033[30m"
FG_WHITE = "\033[97m"
BG_DGRAY = "\033[100m"
BG_RED = "\033[41m"
BG_GREEN = "\033[42m"
BG_YELLOW = "\033[43m"
BG_LBLUE = "\033[104m"
RESET = "\033[0m"
LogLabels = map[int]string{
DEBUG: "dbg",
INFO: "inf",
IMPORTANT: "imp",
WARNING: "war",
ERROR: "err",
FATAL: "!!!",
}
LogColors = map[int]string{
DEBUG: DIM + FG_BLACK + BG_DGRAY,
INFO: FG_WHITE + BG_GREEN,
IMPORTANT: FG_WHITE + BG_LBLUE,
WARNING: FG_WHITE + BG_YELLOW,
ERROR: FG_WHITE + BG_RED,
FATAL: FG_WHITE + BG_RED + BOLD,
}
HasColors = true
)
func isDumbTerminal() bool {
return os.Getenv("TERM") == "dumb" ||
os.Getenv("TERM") == "" ||
(!isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd()))
}
func InitSwag(disableColors bool) {
if disableColors || isDumbTerminal() {
BOLD = ""
DIM = ""
RED = ""
GREEN = ""
BLUE = ""
YELLOW = ""
FG_BLACK = ""
FG_WHITE = ""
BG_DGRAY = ""
BG_RED = ""
BG_GREEN = ""
BG_YELLOW = ""
BG_LBLUE = ""
RESET = ""
LogColors = map[int]string{
DEBUG: "",
INFO: "",
IMPORTANT: "",
WARNING: "",
ERROR: "",
FATAL: "",
}
HasColors = false
}
}
// W for Wrap
func W(e, s string) string {
return e + s + RESET
}
func Bold(s string) string {
return W(BOLD, s)
}
func Dim(s string) string {
return W(DIM, s)
}
func Red(s string) string {
return W(RED, s)
}
func Green(s string) string {
return W(GREEN, s)
}
func Blue(s string) string {
return W(BLUE, s)
}
func Yellow(s string) string {
return W(YELLOW, s)
}

View file

@ -1,95 +0,0 @@
package core
import (
"os"
"testing"
)
func TestIsDumbTerminal(t *testing.T) {
term := os.Getenv("TERM")
os.Setenv("TERM", "dumb")
if !isDumbTerminal() {
t.Fatal("Expected false when TERM==dumb")
}
os.Setenv("TERM", "")
if !isDumbTerminal() {
t.Fatal("Expected false when TERM empty")
}
os.Setenv("TERM", term)
}
func TestW(t *testing.T) {
exp := "<3\033[0m"
got := W("<", "3")
if got != exp {
t.Fatalf("expected '%s', got '%s'", exp, got)
}
}
func TestBold(t *testing.T) {
exp := "\033[1mgohpers\033[0m"
got := Bold("gohpers")
if got != exp {
t.Fatalf("expected path '%s', got '%s'", exp, got)
}
}
func TestDim(t *testing.T) {
exp := "\033[2mgohpers\033[0m"
got := Dim("gohpers")
if got != exp {
t.Fatalf("expected path '%s', got '%s'", exp, got)
}
}
func TestRed(t *testing.T) {
exp := "\033[31mgohpers\033[0m"
got := Red("gohpers")
if got != exp {
t.Fatalf("expected path '%s', got '%s'", exp, got)
}
}
func TestGreen(t *testing.T) {
exp := "\033[32mgohpers\033[0m"
got := Green("gohpers")
if got != exp {
t.Fatalf("expected path '%s', got '%s'", exp, got)
}
}
func TestBlue(t *testing.T) {
exp := "\033[34mgohpers\033[0m"
got := Blue("gohpers")
if got != exp {
t.Fatalf("expected path '%s', got '%s'", exp, got)
}
}
func TestYellow(t *testing.T) {
exp := "\033[33mgohpers\033[0m"
got := Yellow("gohpers")
if got != exp {
t.Fatalf("expected path '%s', got '%s'", exp, got)
}
}
func TestInitSwag(t *testing.T) {
// Run after other tests to avoid breaking globals
// Test InitSwag unsets globals when set
BOLD = "\033[1m"
InitSwag(true)
if BOLD != "" {
t.Fatal("expected BOLD to be empty string")
}
term := os.Getenv("TERM")
os.Setenv("TERM", "dumb")
BOLD = "\033[1m"
InitSwag(false)
if BOLD != "" {
t.Fatal("expected BOLD to be empty string")
}
os.Setenv("TERM", term)
// Would be good to test BOLD *isn't* unset when we have a TTY
// but less trivial to stub os.File.Fd() without complicating architecture
}

View file

@ -1,107 +0,0 @@
package core
import (
"fmt"
"io"
"regexp"
"strings"
"unicode/utf8"
)
var ansi = regexp.MustCompile("\033\\[(?:[0-9]{1,3}(?:;[0-9]{1,3})*)?[m|K]")
func viewLen(s string) int {
for _, m := range ansi.FindAllString(s, -1) {
s = strings.Replace(s, m, "", -1)
}
return utf8.RuneCountInString(s)
}
func maxLen(strings []string) int {
maxLen := 0
for _, s := range strings {
len := viewLen(s)
if len > maxLen {
maxLen = len
}
}
return maxLen
}
type Alignment int
const (
AlignLeft = Alignment(0)
AlignCenter = Alignment(1)
AlignRight = Alignment(2)
)
func getPads(s string, maxLen int, align Alignment) (lPad int, rPad int) {
len := viewLen(s)
diff := maxLen - len
if align == AlignLeft {
lPad = 0
rPad = diff - lPad + 1
} else if align == AlignCenter {
lPad = diff / 2
rPad = diff - lPad + 1
} /* else {
TODO
} */
return
}
func padded(s string, maxLen int, align Alignment) string {
lPad, rPad := getPads(s, maxLen, align)
return fmt.Sprintf("%s%s%s", strings.Repeat(" ", lPad), s, strings.Repeat(" ", rPad))
}
func AsTable(w io.Writer, columns []string, rows [][]string) {
for i, col := range columns {
columns[i] = fmt.Sprintf(" %s ", col)
}
for i, row := range rows {
for j, cell := range row {
rows[i][j] = fmt.Sprintf(" %s ", cell)
}
}
colPaddings := make([]int, 0)
lineSep := ""
for colIndex, colHeader := range columns {
column := []string{colHeader}
for _, row := range rows {
column = append(column, row[colIndex])
}
mLen := maxLen(column)
colPaddings = append(colPaddings, mLen)
lineSep += fmt.Sprintf("+%s", strings.Repeat("-", mLen+1))
}
lineSep += "+"
table := ""
// header
table += fmt.Sprintf("%s\n", lineSep)
for colIndex, colHeader := range columns {
table += fmt.Sprintf("|%s", padded(colHeader, colPaddings[colIndex], AlignCenter))
}
table += fmt.Sprintf("|\n")
table += fmt.Sprintf("%s\n", lineSep)
// rows
for _, row := range rows {
for colIndex, cell := range row {
table += fmt.Sprintf("|%s", padded(cell, colPaddings[colIndex], AlignLeft))
}
table += fmt.Sprintf("|\n")
}
// footer
table += lineSep
fmt.Fprintf(w, "\n%s\n", table)
}

View file

@ -1,82 +0,0 @@
package core
import (
"bytes"
"regexp"
"testing"
)
func TestViewLen(t *testing.T) {
exp := 2
got := viewLen("<3")
if got != exp {
t.Fatalf("expected '%d', got '%d'", exp, got)
}
}
func TestMaxLen(t *testing.T) {
exp := 7
got := maxLen([]string{"go", "python", "ruby", "crystal"})
if got != exp {
t.Fatalf("expected '%d', got '%d'", exp, got)
}
}
func TestAlignLeft(t *testing.T) {
exp := Alignment(0)
got := AlignLeft
if got != exp {
t.Fatalf("expected '%d', got '%d'", exp, got)
}
}
func TestAlignCenter(t *testing.T) {
exp := Alignment(1)
got := AlignCenter
if got != exp {
t.Fatalf("expected '%d', got '%d'", exp, got)
}
}
func TestAlignRight(t *testing.T) {
exp := Alignment(2)
got := AlignRight
if got != exp {
t.Fatalf("expected '%d', got '%d'", exp, got)
}
}
func TestGetPads(t *testing.T) {
lPadExp := -9
rPadExp := -8
lPadGot, rPadGot := getPads("Pikachu, thunderbolt!", 3, AlignCenter)
if rPadGot != rPadExp {
t.Fatalf("expected '%d', got '%d'", rPadExp, rPadGot)
}
if lPadGot != lPadExp {
t.Fatalf("expected '%d', got '%d'", lPadExp, lPadGot)
}
}
func TestPadded(t *testing.T) {
exp := "<3"
got := padded("<3", 1, AlignLeft)
if got != exp {
t.Fatalf("expected '%s', got '%s'", exp, got)
}
}
func TestAsTable(t *testing.T) {
var b bytes.Buffer
AsTable(&b, []string{"top"}, [][]string{[]string{"bottom"}})
// look for "+-------+" table style for whichever size
match, err := regexp.MatchString(`\++-+\+`, b.String())
if err != nil {
t.Fatalf("unable to perform regex on table output")
}
if !match {
t.Fatalf("expected table in format not found")
}
}