reworking database, init and add commands

This commit is contained in:
2020-07-02 15:00:30 -04:00
parent 6379c73e38
commit 5af55ed62e
15 changed files with 220 additions and 295 deletions

View File

@@ -11,6 +11,7 @@ import (
clientconfig "github.com/deranjer/gvc/client/clientconfig"
config "github.com/deranjer/gvc/client/clientconfig"
"github.com/deranjer/gvc/common"
"github.com/deranjer/gvc/common/database"
"github.com/deranjer/gvc/common/engine"
"github.com/deranjer/store"
"github.com/rs/zerolog"
@@ -18,6 +19,7 @@ import (
var version = "0.1.5"
var configPath = ".gvc" + string(filepath.Separator) + ".gvcconfig.toml"
var dbPath = ".gvc/gvc.db"
var rootPath string
func main() {
@@ -45,12 +47,6 @@ func main() {
fmt.Println("Error validating config, your config file is corrupt! ", err)
os.Exit(0)
}
fmt.Println("Attempting to start logger...")
// Checking the .gvc structure
dirPaths, err := engine.CheckPaths(rootPath)
if err != nil {
log.Fatalf("unable to create/verify .gvc folder structure: %s", err)
}
// Setting up the logger to file output
logFile, err := os.OpenFile(".gvc/logs/gvclog.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
@@ -62,15 +58,17 @@ func main() {
if err != nil {
fmt.Println("invalid log level set in config, setting to info")
}
fmt.Println("Attempting to start logger...")
clientlog := zerolog.New(logFile)
clientlog.WithLevel(logLevel)
// Check/Setup the database with logging
//db, err := database.OpenOrCreateDB(".gvc/gvc.db", &clientlog)
//if err != nil {
// clientlog.Fatal().Msgf("Unable to open or create a database in the .gvc folder, fatal")
//}
// now checking the .gvc structure, currently will just create if they don't exist
dirPaths, err := engine.CheckPaths(rootPath)
if err != nil {
clientlog.Err(err).Msgf("unable to verify .gvc folder structure, attempting to create missing structure: %s", err)
}
engine.CreatePaths(rootPath) //currently will fatal fail if can't create paths
informer := make(chan engine.OperatingMessage)
m, err = engine.NewManager(rootPath, version, ".gvc/gvc.db", informer, dirPaths, &clientlog)
m, err = engine.NewManager(rootPath, version, dbPath, informer, dirPaths, &clientlog)
if err != nil {
clientlog.Fatal().Msgf("unable to create new manager object... %s", err)
}
@@ -80,7 +78,7 @@ func main() {
cli := clir.NewCli("gvcc", "Version control client for GVC", version)
// Adding the init command
initCommand(cli, &conf)
initCommand(cli, &conf, dbPath)
// Adding all the "add" commands
addCommands(cli, &conf, m)
@@ -133,6 +131,11 @@ func validateRepo() bool {
if _, err := os.Stat(configPath); os.IsNotExist(err) {
return false
}
// See if database exits, if not, not a valid repo
err := database.CheckDB(".gvc/gvc.db")
if err != nil {
return false
}
return true
}
@@ -154,20 +157,23 @@ func refreshCommand(cli *clir.Cli, conf *clientconfig.Gvcconfig) {
})
}
func initCommand(cli *clir.Cli, conf *clientconfig.Gvcconfig) {
func initCommand(cli *clir.Cli, conf *clientconfig.Gvcconfig, dbPath string) {
//The init subcommand
initCmd := cli.NewSubCommand("init", "initializes a new gvc repo")
initCmd.LongDescription("This will create the .gvcconfig.toml (with defaults) file in this directory. You can edit this file directly (unsafe) or with the client(safe)")
initCmd.Action(func() error {
isRepo := validateRepo()
if !isRepo {
repoName := clientcmd.InitializeRepo() // creates and checks the paths
repoName, err := clientcmd.InitializeRepo(dbPath, version, rootPath) // creates and checks the paths
if err != nil {
log.Fatalf("unable to create new repo: %s", err)
}
newConf := clientconfig.Gvcconfig{
RepoName: repoName,
Version: version,
CurrentBranch: "master",
}
err := store.Save(configPath, &newConf)
err = store.Save(configPath, &newConf)
if err != nil {
log.Fatalf("unable to create new config in .gvc %s", err)
}