From 147ae7daf102bbd93cfcbe2eebf340ecab3b3c50 Mon Sep 17 00:00:00 2001 From: Tovi Jaeschke-Rogers Date: Tue, 20 Jul 2021 18:07:01 +0930 Subject: [PATCH] Add config dir to variables --- Archive/Archive.go | 9 ++++++++- Client/Database/Init.go | 15 +++++++++++---- Client/Filesystem/CommitFiles.go | 2 +- Client/Filesystem/FilesystemDiff.go | 17 +++++++++++++---- Client/Filesystem/ManageFileBucket.go | 9 +++++++-- Client/Filesystem/PickFiles.go | 23 ++++++++++++++--------- Client/Package/CreatePackage.go | 7 +++++-- Client/main.go | 12 ++++++++++++ Variables/Variables.go | 2 ++ 9 files changed, 73 insertions(+), 23 deletions(-) diff --git a/Archive/Archive.go b/Archive/Archive.go index be3761a..c32fc13 100644 --- a/Archive/Archive.go +++ b/Archive/Archive.go @@ -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) diff --git a/Client/Database/Init.go b/Client/Database/Init.go index 636f89c..a06937b 100644 --- a/Client/Database/Init.go +++ b/Client/Database/Init.go @@ -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) } diff --git a/Client/Filesystem/CommitFiles.go b/Client/Filesystem/CommitFiles.go index 642a72d..411a94b 100644 --- a/Client/Filesystem/CommitFiles.go +++ b/Client/Filesystem/CommitFiles.go @@ -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 } diff --git a/Client/Filesystem/FilesystemDiff.go b/Client/Filesystem/FilesystemDiff.go index aef648f..9680d24 100644 --- a/Client/Filesystem/FilesystemDiff.go +++ b/Client/Filesystem/FilesystemDiff.go @@ -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) diff --git a/Client/Filesystem/ManageFileBucket.go b/Client/Filesystem/ManageFileBucket.go index 4a0c8b2..fe7d35f 100644 --- a/Client/Filesystem/ManageFileBucket.go +++ b/Client/Filesystem/ManageFileBucket.go @@ -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 { diff --git a/Client/Filesystem/PickFiles.go b/Client/Filesystem/PickFiles.go index 580aac4..1362a83 100644 --- a/Client/Filesystem/PickFiles.go +++ b/Client/Filesystem/PickFiles.go @@ -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) diff --git a/Client/Package/CreatePackage.go b/Client/Package/CreatePackage.go index 8b60db9..687a8a3 100644 --- a/Client/Package/CreatePackage.go +++ b/Client/Package/CreatePackage.go @@ -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])) } } diff --git a/Client/main.go b/Client/main.go index a23bc76..c09c714 100644 --- a/Client/main.go +++ b/Client/main.go @@ -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: diff --git a/Variables/Variables.go b/Variables/Variables.go index 7a0f874..ce4a9d4 100644 --- a/Variables/Variables.go +++ b/Variables/Variables.go @@ -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$",