apkovl-cli/cmd/general.go

81 lines
1.5 KiB
Go
Raw Normal View History

package cmd
import (
"apkovl-cli/compression"
"embed"
"io"
"os"
log "github.com/s00500/env_logger"
)
//go:embed templates/*
var embeddedFS embed.FS
func prepareWorkdir() {
log.Info("Extracting APKovl ", apkovlFile, " to: ", workdir)
// Extract apkovl
err := os.RemoveAll(workdir)
log.MustFatal(err)
err = os.MkdirAll(workdir, 0755)
log.MustFatal(err)
r, err := os.Open(apkovlFile)
log.MustFatal(err)
err = compression.ExtractTarGz(r, workdir)
log.MustFatal(err)
}
func createOutput() {
log.Info("Compress Workdir (", workdir, "), outputting: ", apkovlFileOutput)
//Compress APKovl
err := compression.Compress(workdir, apkovlFileOutput)
log.MustFatal(err)
}
func copyEmbeddedFile(src, dst string) error {
srcfd, err := embeddedFS.Open(src)
if err != nil {
return err
}
defer srcfd.Close()
dstfd, err := os.Create(dst)
if err != nil {
return err
}
defer dstfd.Close()
if _, err = io.Copy(dstfd, srcfd); err != nil {
return err
}
return nil // Do not change permissions
}
// copyFile copies a single file from src to dst
func copyFile(src, dst string) error {
var err error
var srcfd *os.File
var dstfd *os.File
var srcinfo os.FileInfo
if srcfd, err = os.Open(src); err != nil {
return err
}
defer srcfd.Close()
if dstfd, err = os.Create(dst); err != nil {
return err
}
defer dstfd.Close()
if _, err = io.Copy(dstfd, srcfd); err != nil {
return err
}
if srcinfo, err = os.Stat(src); err != nil {
return err
}
return os.Chmod(dst, srcinfo.Mode())
}