123 lines
2.4 KiB
Go
123 lines
2.4 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))
|
|
}
|