Browse Source

Create packages from filesystem diff

pull/1/head
Tovi Jaeschke-Rogers 3 years ago
parent
commit
feaad6a039
5 changed files with 103 additions and 32 deletions
  1. +2
    -0
      .gitignore
  2. +5
    -19
      Archive/Archive.go
  3. +7
    -2
      Archive/Unarchive.go
  4. +50
    -0
      Client/Filesystem/CopyFile.go
  5. +39
    -11
      Client/Package/CreatePackage.go

+ 2
- 0
.gitignore View File

@ -1 +1,3 @@
*.db *.db
*.tar
*.tar.gz

+ 5
- 19
Archive/Archive.go View File

@ -44,7 +44,6 @@ func Tar(source, target string) error {
var ( var (
tarfile, file *os.File tarfile, file *os.File
tarball *tar.Writer tarball *tar.Writer
info os.FileInfo
header *tar.Header header *tar.Header
e error e error
) )
@ -58,16 +57,6 @@ func Tar(source, target string) error {
tarball = tar.NewWriter(tarfile) tarball = tar.NewWriter(tarfile)
defer tarball.Close() defer tarball.Close()
info, e = os.Stat(source)
if e != nil {
return e
}
var baseDir string
if info.IsDir() {
baseDir = filepath.Base(source)
}
return filepath.Walk(source, return filepath.Walk(source,
func(path string, info os.FileInfo, e error) error { func(path string, info os.FileInfo, e error) error {
if e != nil { if e != nil {
@ -79,14 +68,11 @@ func Tar(source, target string) error {
return e return e
} }
if baseDir != "" {
header.Name = filepath.Join(
baseDir,
strings.TrimPrefix(
strings.Join(strings.Split(path, "/")[1:], "/"),
source,
),
)
// TODO change "/" to work cross platform
header.Name = strings.TrimPrefix(strings.TrimPrefix(path, source), "/")
if header.Name == "" {
return nil
} }
e = tarball.WriteHeader(header) e = tarball.WriteHeader(header)


+ 7
- 2
Archive/Unarchive.go View File

@ -69,7 +69,7 @@ func Untar(tarball, target string) error {
return e return e
} }
path = "/" + strings.Join(strings.Split(header.Name, "/")[1:], "/")
path = filepath.Join(target, header.Name)
info = header.FileInfo() info = header.FileInfo()
if info.IsDir() { if info.IsDir() {
@ -107,5 +107,10 @@ func UntarGzip(source, target string) error {
return e return e
} }
return Untar(tarPath, target)
e = Untar(tarPath, target)
if e != nil {
return e
}
return os.Remove(tarPath)
} }

+ 50
- 0
Client/Filesystem/CopyFile.go View File

@ -0,0 +1,50 @@
package Filesystem
import (
"fmt"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
)
func CopyFile(src, dest string) error {
var (
input []byte
fileInfo fs.FileInfo
srcBasePath string
destBasePath string
e error
)
srcBasePath = filepath.Dir(src)
destBasePath = filepath.Dir(dest)
fileInfo, e = os.Stat(srcBasePath)
if e != nil {
return e
}
e = os.MkdirAll(destBasePath, fileInfo.Mode())
if e != nil {
return e
}
fileInfo, e = os.Stat(src)
if e != nil {
return e
}
input, e = ioutil.ReadFile(src)
if e != nil {
return e
}
e = ioutil.WriteFile(dest, input, fileInfo.Mode())
if e != nil {
fmt.Println(e)
return e
}
return nil
}

+ 39
- 11
Client/Package/CreatePackage.go View File

@ -3,9 +3,13 @@ package Package
import ( import (
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv" "strconv"
"strings" "strings"
"PackageManager/Archive"
"PackageManager/Client/Filesystem" "PackageManager/Client/Filesystem"
"PackageManager/Color" "PackageManager/Color"
"PackageManager/Helper" "PackageManager/Helper"
@ -13,17 +17,19 @@ import (
func CreatePackage() error { func CreatePackage() error {
var ( var (
dirtyFiles map[int]string
newFiles map[int]string
pkgFiles map[int]string = make(map[int]string)
choices string
choicesSplit []string
filePath string
pkgName string
pkgVersion string
index int
ok bool
e error
dirtyFiles map[int]string
newFiles map[int]string
pkgFiles map[int]string = make(map[int]string)
choices string
choicesSplit []string
filePath string
pkgName string
pkgVersion string
pkgNameVersion string
tmpDir string
index int
ok bool
e error
) )
fmt.Println("Initialising package creation...") fmt.Println("Initialising package creation...")
@ -111,5 +117,27 @@ func CreatePackage() error {
return errors.New("User aborted") return errors.New("User aborted")
} }
pkgNameVersion = fmt.Sprintf("%s-%s", pkgName, pkgVersion)
tmpDir, e = ioutil.TempDir("/tmp", pkgNameVersion)
if e != nil {
return e
}
defer os.RemoveAll(tmpDir)
for _, file := range pkgFiles {
Filesystem.CopyFile(file, filepath.Join(tmpDir, file))
}
e = Archive.TarGzip(tmpDir, pkgNameVersion+".tar.gz")
if e != nil {
return e
}
fmt.Printf(
Color.Green("\nSuccessfully created package %s\n"),
pkgNameVersion,
)
return nil return nil
} }

Loading…
Cancel
Save