working on initing a repo, more work on 'add' command

This commit is contained in:
2020-05-27 22:54:02 -04:00
parent 2d743d17c3
commit 9b017f3128
8 changed files with 93 additions and 30 deletions

View File

@@ -2,25 +2,46 @@ package main
import (
"fmt"
"log"
"os"
"path/filepath"
clir "github.com/deranjer/clir"
clientcmd "github.com/deranjer/gvc/client/clientcmd"
clientconfig "github.com/deranjer/gvc/client/clientconfig"
config "github.com/deranjer/gvc/client/clientconfig"
"github.com/deranjer/store"
)
var version = "0.1.5"
var configPath = ".gvc" + string(filepath.Separator) + ".gvcconfig.toml"
var rootPath string
func main() {
var conf clientconfig.Gvcconfig
// Sloppily inject a global variable //TODO maybe just path the variables manually or create a struct with them all in
injectVariables()
err := store.Load(".gvcconfig.toml", &conf)
// Getting the root path
var err error
rootPath, err = os.Getwd()
if err != nil {
fmt.Println("Error loading config file into struct! ", err)
log.Fatalf("Can't get working dir, permissions issue %s", err)
}
clientconfig.ValidateConfig(&conf, version)
// Setting up a blank config to read the .toml file in if one exists
var conf clientconfig.Gvcconfig
isRepo := validateRepo()
if isRepo {
err := store.Load(configPath, &conf)
if err != nil {
fmt.Println("Error loading config file into struct! ", err)
}
err = clientconfig.ValidateConfig(&conf, version)
if err != nil {
fmt.Println("Error validating config, your config file is corrupt! ", err)
os.Exit(0)
}
}
// Initialize our new cli
cli := clir.NewCli("gvcc", "Version control client for GVC", version)
@@ -34,22 +55,17 @@ func main() {
if err != nil {
fmt.Printf("Error occurred: %v\n", err)
}
// After the cli is finished running and writing new values, then validate the input and write it back to the TOML config file
err = clientconfig.ValidateConfig(&conf, version)
if err != nil {
fmt.Println("Error validating config, no changes from the command saved! ", err)
os.Exit(0)
}
err = store.Save(".gvcconfigNew.toml", &conf)
if err != nil {
fmt.Println("Error saving config back to toml file: ", err)
}
}
// injectVariables just injects the global variables into the packages
func injectVariables() {
clientcmd.ConfigPath = configPath
config.ConfigPath = configPath
}
// validateRepo just ensures that the command is being run against an actual repo (bool yes or no)
func validateRepo() bool {
if _, err := os.Stat(".gvcconfig.toml"); os.IsNotExist(err) {
if _, err := os.Stat(configPath); os.IsNotExist(err) {
return false
}
return true
@@ -62,11 +78,13 @@ func initCommand(cli *clir.Cli, conf *clientconfig.Gvcconfig) {
initCmd.Action(func() error {
isRepo := validateRepo()
if !isRepo {
err := clientcmd.InitializeRepo()
clientcmd.InitializeRepo() // creates and checks the paths
newConf := clientconfig.Gvcconfig{
Version: version,
}
err := store.Save(configPath, &newConf)
if err != nil {
err = fmt.Errorf("unable to initialize repo: %s", err)
fmt.Println(err)
return err
log.Fatalf("unable to create new config in .gvc %s", err)
}
return nil
}
@@ -91,6 +109,11 @@ func addCommands(cli *clir.Cli, conf *clientconfig.Gvcconfig) {
wildCardFlag := addCmd.StringFlag("wildcard", "treats the input as a wildcard and tracks all files that match the wildcard", &wildcard)
wildCardFlag.FlagShortCut("wildcard", "wc")
addCmd.Action(func() error {
isRepo := validateRepo()
if !isRepo {
fmt.Println("no valid repo found.. please run 'init' to setup a repo first")
os.Exit(0)
}
if len(addCmd.OtherArgs()) > 0 {
addCmd.PrintHelp()
return fmt.Errorf("incorrect input detected, please fix and retry")
@@ -138,14 +161,23 @@ func addCommands(cli *clir.Cli, conf *clientconfig.Gvcconfig) {
})
//The add remote command
remoteCmd := cli.NewSubCommand("remote", "add/delete/show remotes")
remoteCmd := addCmd.NewSubCommand("remote", "add/delete/show remotes")
var name string
var host string
var port int
remoteAddCmd := remoteCmd.NewSubCommand("add", "add a remote, requires -name -host and -port")
remoteAddCmd.LongDescription("Adds a remote to the .gvcconfig.toml. Requires the -name, -host and -port flags. Example: gvc remote add -name exampleRemote -host examplehost.com -port 8080")
remoteAddCmd.StringFlag("name", "the name you want for your remote server", &name)
remoteAddCmd.StringFlag("host", "the hostname or IP Address of the remote", &host)
remoteAddCmd.IntFlag("port", "the port the remote server is listening on", &port)
nameFlag := remoteAddCmd.StringFlag("name", "the name you want for your remote server", &name)
nameFlag.FlagShortCut("name", "n")
remoteAddCmd.FlagRequired("name")
hostFlag := remoteAddCmd.StringFlag("host", "the hostname or IP Address of the remote", &host)
hostFlag.FlagShortCut("host", "h")
remoteAddCmd.FlagRequired("host")
portFlag := remoteAddCmd.IntFlag("port", "the port the remote server is listening on", &port)
portFlag.FlagShortCut("port", "p")
remoteAddCmd.FlagRequired("port")
remoteAddCmd.Action(func() error {
return nil
})
}