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


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

@ -2,6 +2,7 @@ package Database
import ( import (
"database/sql" "database/sql"
"path/filepath"
"time" "time"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
@ -18,15 +19,21 @@ var (
func init() { func init() {
var e error var e error
// Initialise sqlite3 database for package versioning // 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 { if e != nil {
panic(e) panic(e)
} }
// Initialise bolt db for filesystem hashing // 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 { if e != nil {
panic(e) panic(e)
} }


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

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


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

@ -32,9 +32,10 @@ type FilesystemStatus struct {
func ShowFilesystemDiff(root string) error { func ShowFilesystemDiff(root string) error {
var ( var (
fsStatus FilesystemStatus
//f string
e error
fsStatus FilesystemStatus
picksBucket *bolt.Bucket
pickedFiles []string
e error
) )
fsStatus, e = GetFilesystemDiff(root) fsStatus, e = GetFilesystemDiff(root)
@ -42,11 +43,19 @@ func ShowFilesystemDiff(root string) error {
return e 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:") fmt.Println("New files:")
PrintFilesOrLength(fsStatus.NewFiles, Color.Green) PrintFilesOrLength(fsStatus.NewFiles, Color.Green)
fmt.Println("Added files:") fmt.Println("Added files:")
PrintFilesOrLength(fsStatus.PickedFiles, Color.Green)
PrintFilesOrLength(pickedFiles, Color.Green)
fmt.Println("Modified files:") fmt.Println("Modified files:")
PrintFilesOrLength(fsStatus.ModifiedFiles, Color.Warning) PrintFilesOrLength(fsStatus.ModifiedFiles, Color.Warning)


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

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


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

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


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

@ -61,6 +61,7 @@ func CreatePackage() error {
pkgName string pkgName string
pkgVersion string pkgVersion string
pkgNameVersion string pkgNameVersion string
pkg string
index int index int
e error e error
) )
@ -85,7 +86,9 @@ func CreatePackage() error {
choices = Helper.Input() choices = Helper.Input()
if choices == "" { if choices == "" {
pkgFiles = pickedFiles
for _, pkg = range pickedFiles {
pkgFiles = append(pkgFiles, filepath.Join(Variables.RootDir, pkg))
}
} else { } else {
choicesSplit = strings.Split(choices, ",") choicesSplit = strings.Split(choices, ",")
@ -98,7 +101,7 @@ func CreatePackage() error {
if len(pickedFiles) < index { if len(pickedFiles) < index {
return errors.New("Invalid choice") 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" "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() { func HelpMsg() {
var helpMsg string var helpMsg string
helpMsg = `Usage of %s: helpMsg = `Usage of %s:


+ 2
- 0
Variables/Variables.go View File

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


Loading…
Cancel
Save