Browse Source

Add config dir to variables

feature/create-install-pkgs
Tovi Jaeschke-Rogers 3 years ago
parent
commit
147ae7daf1
9 changed files with 73 additions and 23 deletions
  1. +8
    -1
      Archive/Archive.go
  2. +11
    -4
      Client/Database/Init.go
  3. +1
    -1
      Client/Filesystem/CommitFiles.go
  4. +13
    -4
      Client/Filesystem/FilesystemDiff.go
  5. +7
    -2
      Client/Filesystem/ManageFileBucket.go
  6. +14
    -9
      Client/Filesystem/PickFiles.go
  7. +5
    -2
      Client/Package/CreatePackage.go
  8. +12
    -0
      Client/main.go
  9. +2
    -0
      Variables/Variables.go

+ 8
- 1
Archive/Archive.go View File

@ -1,6 +1,7 @@
package Archive
import (
"PackageManager/Variables"
"archive/tar"
"compress/gzip"
"io"
@ -72,7 +73,13 @@ func addToArchive(tarWriter *tar.Writer, filename string) error {
// If we don't do this the directory strucuture would
// not be preserved
// https://golang.org/src/archive/tar/common.go?#L626
header.Name = strings.TrimPrefix(filename, "/")
header.Name = strings.TrimPrefix(
strings.TrimPrefix(
filename,
Variables.RootDir,
),
"/",
)
// Write file header to the tar archive
e = tarWriter.WriteHeader(header)


+ 11
- 4
Client/Database/Init.go View File

@ -2,6 +2,7 @@ package Database
import (
"database/sql"
"path/filepath"
"time"
_ "github.com/mattn/go-sqlite3"
@ -18,15 +19,21 @@ var (
func init() {
var e error
// Initialise sqlite3 database for package versioning
DB, e = sql.Open("sqlite3", Variables.DatabaseName)
DB, e = sql.Open(
"sqlite3",
filepath.Join(Variables.ConfigDir, Variables.DatabaseName),
)
if e != nil {
panic(e)
}
// Initialise bolt db for filesystem hashing
FsDB, e = bolt.Open(Variables.FsHashDatabaseName, 0600, &bolt.Options{
Timeout: 5 * time.Second,
})
FsDB, e = bolt.Open(
filepath.Join(Variables.ConfigDir, Variables.FsHashDatabaseName),
0600,
&bolt.Options{
Timeout: 5 * time.Second,
})
if e != nil {
panic(e)
}


+ 1
- 1
Client/Filesystem/CommitFiles.go View File

@ -32,7 +32,7 @@ func CommitFiles() error {
bar = ProgressBar.InitBar("Commiting...", len(pickedFiles))
for _, f = range pickedFiles {
bar.Increment()
e = AddFileToBucket(indexBucket, f)
e = AddFileToBucket(indexBucket, f, f)
if e != nil {
return nil
}


+ 13
- 4
Client/Filesystem/FilesystemDiff.go View File

@ -32,9 +32,10 @@ type FilesystemStatus struct {
func ShowFilesystemDiff(root string) error {
var (
fsStatus FilesystemStatus
//f string
e error
fsStatus FilesystemStatus
picksBucket *bolt.Bucket
pickedFiles []string
e error
)
fsStatus, e = GetFilesystemDiff(root)
@ -42,11 +43,19 @@ func ShowFilesystemDiff(root string) error {
return e
}
e = Database.FsDB.View(func(tx *bolt.Tx) error {
picksBucket = tx.Bucket(Variables.FsHashPicksBucket)
return picksBucket.ForEach(func(key, _ []byte) error {
pickedFiles = append(pickedFiles, string(key))
return nil
})
})
fmt.Println("New files:")
PrintFilesOrLength(fsStatus.NewFiles, Color.Green)
fmt.Println("Added files:")
PrintFilesOrLength(fsStatus.PickedFiles, Color.Green)
PrintFilesOrLength(pickedFiles, Color.Green)
fmt.Println("Modified files:")
PrintFilesOrLength(fsStatus.ModifiedFiles, Color.Warning)


+ 7
- 2
Client/Filesystem/ManageFileBucket.go View File

@ -1,12 +1,13 @@
package Filesystem
import (
"fmt"
"os"
bolt "go.etcd.io/bbolt"
)
func AddFileToBucket(bucket *bolt.Bucket, filePath string) error {
func AddFileToBucket(bucket *bolt.Bucket, key, filePath string) error {
var (
fileObject FileObject
fileObjectBytes []byte
@ -19,11 +20,15 @@ func AddFileToBucket(bucket *bolt.Bucket, filePath string) error {
if e != nil {
return nil
}
fmt.Println(fileObject)
fileObjectBytes, e = fileObject.ToBytes()
if e != nil {
return e
}
return bucket.Put([]byte(filePath), fileObjectBytes)
return bucket.Put([]byte(key), fileObjectBytes)
}
func RemoveFileFromBucket(bucket *bolt.Bucket, filePath string) error {


+ 14
- 9
Client/Filesystem/PickFiles.go View File

@ -2,6 +2,7 @@ package Filesystem
import (
"os"
"path/filepath"
"github.com/vbauerster/mpb"
bolt "go.etcd.io/bbolt"
@ -11,21 +12,22 @@ import (
"PackageManager/Variables"
)
func pickFilesSingle(rootPath string) error {
func pickFilesSingle(fileKey, filePath string) error {
var (
indexBucket *bolt.Bucket
picksBucket *bolt.Bucket
e error
)
e = Database.FsDB.Batch(func(tx *bolt.Tx) error {
indexBucket = tx.Bucket(Variables.FsHashIndexBucket)
picksBucket = tx.Bucket(Variables.FsHashPicksBucket)
e = AddFileToBucket(picksBucket, rootPath)
e = AddFileToBucket(picksBucket, fileKey, filePath)
if e != nil {
return e
}
return RemoveFileFromBucket(indexBucket, rootPath)
return RemoveFileFromBucket(indexBucket, fileKey)
})
return e
}
@ -61,7 +63,7 @@ func pickFilesRecursive(rootPath string) error {
if len(fsStatus.NewFiles) > 0 {
for _, f = range fsStatus.NewFiles {
bar.Increment()
e = AddFileToBucket(picksBucket, f)
e = AddFileToBucket(picksBucket, f, f)
if e != nil {
return e
}
@ -71,7 +73,7 @@ func pickFilesRecursive(rootPath string) error {
if len(fsStatus.ModifiedFiles) > 0 {
for _, f = range fsStatus.ModifiedFiles {
bar.Increment()
e = AddFileToBucket(picksBucket, f)
e = AddFileToBucket(picksBucket, f, f)
if e != nil {
return e
}
@ -100,17 +102,20 @@ func pickFilesRecursive(rootPath string) error {
func PickFiles(rootPath string) error {
var (
rootStat os.FileInfo
e error
realRootPath string
rootStat os.FileInfo
e error
)
rootStat, e = os.Stat(rootPath)
realRootPath = filepath.Join(Variables.RootDir, rootPath)
rootStat, e = os.Stat(realRootPath)
if e != nil {
return e
}
if !rootStat.IsDir() {
return pickFilesSingle(rootPath)
return pickFilesSingle(rootPath, realRootPath)
}
return pickFilesRecursive(rootPath)


+ 5
- 2
Client/Package/CreatePackage.go View File

@ -61,6 +61,7 @@ func CreatePackage() error {
pkgName string
pkgVersion string
pkgNameVersion string
pkg string
index int
e error
)
@ -85,7 +86,9 @@ func CreatePackage() error {
choices = Helper.Input()
if choices == "" {
pkgFiles = pickedFiles
for _, pkg = range pickedFiles {
pkgFiles = append(pkgFiles, filepath.Join(Variables.RootDir, pkg))
}
} else {
choicesSplit = strings.Split(choices, ",")
@ -98,7 +101,7 @@ func CreatePackage() error {
if len(pickedFiles) < index {
return errors.New("Invalid choice")
}
pkgFiles = append(pkgFiles, pickedFiles[index])
pkgFiles = append(pkgFiles, filepath.Join(Variables.RootDir, pickedFiles[index]))
}
}


+ 12
- 0
Client/main.go View File

@ -13,6 +13,18 @@ import (
"PackageManager/Variables"
)
func init() {
var (
e error
)
_, e = os.Stat(Variables.ConfigDir)
if os.IsNotExist(e) {
os.MkdirAll(Variables.ConfigDir, 0644)
} else if e != nil {
panic(e)
}
}
func HelpMsg() {
var helpMsg string
helpMsg = `Usage of %s:


+ 2
- 0
Variables/Variables.go View File

@ -5,6 +5,7 @@ import (
)
const (
ConfigDir string = "/etc/tjpkg"
DatabaseName string = "package_manager.db"
FsHashDatabaseName string = "fs_hash.db"
)
@ -20,6 +21,7 @@ var (
FsHashIndexBucket []byte = []byte("FilesystemIndex")
PruneRegexPaths []string = []string{
ConfigDir,
"^/\\.git$",
"^/dist$",
"^/boot/grub$",


Loading…
Cancel
Save