296 lines
5.8 KiB
Go
296 lines
5.8 KiB
Go
package utils
|
|
|
|
import (
|
|
"archive/tar"
|
|
"compress/gzip"
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
const confirmChar = "Y"
|
|
|
|
func addToArchive(tw *tar.Writer, filename string) error {
|
|
file, err := os.Open(filename)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer file.Close() //nolint: errcheck // defered
|
|
|
|
info, err := file.Stat()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
header, err := tar.FileInfoHeader(info, info.Name())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
header.Name = filepath.Base(file.Name())
|
|
|
|
if err = tw.WriteHeader(header); err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = io.Copy(tw, file)
|
|
return err
|
|
}
|
|
|
|
// CreateArchive creates a gzip tar archive in the `destDir` path including `files`.
|
|
func CreateArchive(destDir, name string, files ...string) (string, error) {
|
|
now := time.Now().UTC()
|
|
archivePath := filepath.Join(
|
|
destDir,
|
|
fmt.Sprintf("%s-%s.tar.gz", name, strings.ReplaceAll(now.Format(time.RFC3339), ":", "-")),
|
|
)
|
|
|
|
file, err := os.Create(archivePath)
|
|
if err != nil {
|
|
return "", fmt.Errorf("unable to create archive=%s, err=%v", archivePath, err)
|
|
}
|
|
defer file.Close() //nolint: errcheck // defered
|
|
|
|
gw := gzip.NewWriter(file)
|
|
defer gw.Close() //nolint: errcheck // defered
|
|
|
|
tw := tar.NewWriter(gw)
|
|
defer tw.Close() //nolint: errcheck // defered
|
|
|
|
for _, f := range files {
|
|
if err := addToArchive(tw, f); err != nil {
|
|
return "", fmt.Errorf(
|
|
"unable to add file=%s to archive=%s, err=%v",
|
|
f,
|
|
archivePath,
|
|
err,
|
|
)
|
|
}
|
|
}
|
|
|
|
return archivePath, nil
|
|
}
|
|
|
|
func Confirm(ctx context.Context, destroy bool) error {
|
|
logMsg := "deploy"
|
|
if destroy {
|
|
logMsg = "undeploy"
|
|
}
|
|
|
|
log.Warn().Msg(fmt.Sprintf("Confirm to %s ? Y to confirm", logMsg))
|
|
|
|
var text string
|
|
if _, err := fmt.Fscanf(os.Stdin, "%s", &text); err != nil {
|
|
if !strings.Contains(err.Error(), "newline") {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if !strings.EqualFold(text, confirmChar) {
|
|
log.Info().Msg("Ok, bye !")
|
|
os.Exit(0)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// CatchAndConvertNumber finds and converts the first consecutives digits in integer.
|
|
func CatchAndConvertNumber(value string) (int, error) {
|
|
buf := []rune{}
|
|
|
|
hasLeftTrimmed := false
|
|
for _, c := range value {
|
|
if c >= '0' && c <= '9' {
|
|
hasLeftTrimmed = true
|
|
buf = append(buf, c)
|
|
continue
|
|
}
|
|
|
|
// early return if no number catch
|
|
if hasLeftTrimmed {
|
|
break
|
|
}
|
|
}
|
|
|
|
return strconv.Atoi(string(buf))
|
|
}
|
|
|
|
type headerColumn struct {
|
|
name string
|
|
length int
|
|
pos int
|
|
}
|
|
|
|
type header struct {
|
|
borderStyle string
|
|
headerMeta map[string]*headerColumn
|
|
columns []headerColumn
|
|
}
|
|
|
|
type TableOption func(*Table)
|
|
|
|
func WithHeaderBorderStyle(style string) TableOption {
|
|
return func(t *Table) {
|
|
t.header.borderStyle = style
|
|
}
|
|
}
|
|
|
|
func WithRowSeparator(separator string) TableOption {
|
|
return func(t *Table) {
|
|
t.rowSeparator = separator
|
|
}
|
|
}
|
|
|
|
func WithColSeparator(separator string) TableOption {
|
|
return func(t *Table) {
|
|
t.colSeparator = separator
|
|
}
|
|
}
|
|
|
|
func WithHeader(name string, length int) TableOption {
|
|
return func(t *Table) {
|
|
if t.header.columns == nil {
|
|
t.header.columns = []headerColumn{}
|
|
}
|
|
pos := len(t.header.columns)
|
|
t.header.columns = append(
|
|
t.header.columns,
|
|
headerColumn{name: name, length: length, pos: pos},
|
|
)
|
|
|
|
if t.header.headerMeta == nil {
|
|
t.header.headerMeta = map[string]*headerColumn{}
|
|
}
|
|
|
|
t.header.headerMeta[strings.ToLower(name)] = &t.header.columns[pos]
|
|
}
|
|
}
|
|
|
|
type Column struct {
|
|
name string
|
|
value string
|
|
}
|
|
|
|
func NewColumn(name, value string) Column {
|
|
return Column{name: name, value: value}
|
|
}
|
|
|
|
type Row struct {
|
|
next *Row // useful for grouping rows
|
|
columns []Column
|
|
}
|
|
|
|
func (r *Row) AddNext(row *Row) {
|
|
r.next = row
|
|
}
|
|
|
|
type Table struct {
|
|
rowSeparator string
|
|
colSeparator string
|
|
rows []*Row
|
|
header header
|
|
cursor int
|
|
}
|
|
|
|
func NewTable(options ...TableOption) Table {
|
|
table := Table{}
|
|
for _, o := range options {
|
|
o(&table)
|
|
}
|
|
|
|
table.rows = []*Row{}
|
|
|
|
return table
|
|
}
|
|
|
|
func (t *Table) AddRow(columns ...Column) (*Row, error) {
|
|
if len(columns) == 0 {
|
|
return &Row{}, nil
|
|
}
|
|
|
|
maxNbCols := len(t.header.columns)
|
|
if len(columns) > maxNbCols {
|
|
return nil, fmt.Errorf("invalid column number, should be %d", maxNbCols)
|
|
}
|
|
|
|
row := &Row{columns: make([]Column, maxNbCols)}
|
|
t.rows = append(t.rows, row)
|
|
|
|
for idx := range columns {
|
|
header, ok := t.header.headerMeta[strings.ToLower(columns[idx].name)]
|
|
if !ok {
|
|
return nil, fmt.Errorf("no corresponding %s column exist", columns[idx].name)
|
|
}
|
|
|
|
value := columns[idx].value
|
|
if len(value) > header.length {
|
|
log.Debug().
|
|
Str("column", columns[idx].name).
|
|
Str("value", value).
|
|
Msg("col value too long, trimming...")
|
|
value = value[:header.length-3] + "..."
|
|
}
|
|
|
|
row.columns[header.pos].value = value
|
|
row.columns[header.pos].name = columns[idx].name
|
|
|
|
}
|
|
t.cursor++
|
|
|
|
return row, nil
|
|
}
|
|
|
|
func (t *Table) Render() {
|
|
table := []string{}
|
|
|
|
// header
|
|
headerParts := []string{}
|
|
for idx := range t.header.columns {
|
|
headerParts = append(
|
|
headerParts,
|
|
fmt.Sprintf("%-*s", t.header.columns[idx].length, t.header.columns[idx].name),
|
|
)
|
|
}
|
|
|
|
header := strings.Join(headerParts, t.colSeparator)
|
|
border := ""
|
|
for i := 0; i < len(header); i++ {
|
|
border += t.header.borderStyle
|
|
}
|
|
|
|
rowHr := ""
|
|
for i := 0; i < len(header); i++ {
|
|
rowHr += t.rowSeparator
|
|
}
|
|
|
|
table = append(table, border, header, border)
|
|
|
|
// data
|
|
for idx := range t.rows {
|
|
lineParts := []string{}
|
|
for idy := range t.rows[idx].columns {
|
|
lineParts = append(lineParts, fmt.Sprintf(
|
|
"%-*s",
|
|
t.header.columns[idy].length,
|
|
t.rows[idx].columns[idy].value),
|
|
)
|
|
}
|
|
|
|
if idx+1 < len(t.rows) {
|
|
if t.rows[idx].next != nil {
|
|
table = append(table, strings.Join(lineParts, t.colSeparator))
|
|
} else {
|
|
table = append(table, strings.Join(lineParts, t.colSeparator), rowHr)
|
|
}
|
|
}
|
|
}
|
|
|
|
fmt.Println(strings.Join(table, "\n"))
|
|
}
|