diff --git a/client/.gvc/.gvcconfig.toml b/client/.gvc/.gvcconfig.toml new file mode 100644 index 0000000..44de743 --- /dev/null +++ b/client/.gvc/.gvcconfig.toml @@ -0,0 +1,7 @@ +version = "0.1.5" +rootPath = "" + +[ignore] + +[nocompress] + diff --git a/client/.gvcconfig.toml b/client/.gvc/.gvcconfig.toml.old similarity index 100% rename from client/.gvcconfig.toml rename to client/.gvc/.gvcconfig.toml.old diff --git a/client/.gvcconfigNew.toml b/client/.gvc/.gvcconfigNew.toml similarity index 100% rename from client/.gvcconfigNew.toml rename to client/.gvc/.gvcconfigNew.toml diff --git a/client/client.go b/client/client.go index 5190d1b..d4d6af4 100644 --- a/client/client.go +++ b/client/client.go @@ -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 + }) } diff --git a/client/clientcmd/add.go b/client/clientcmd/add.go index 4f56b46..010395c 100644 --- a/client/clientcmd/add.go +++ b/client/clientcmd/add.go @@ -3,6 +3,8 @@ package clientcmd import ( "fmt" "os" + + clientconfig "github.com/deranjer/gvc/client/clientconfig" ) //AddFiles adds files to the repo, inputType specifies file, folder, wildcard or all @@ -10,3 +12,9 @@ func AddFiles(input string, inputType string) error { fmt.Println("File/folder/wildcard to add", os.Args[2]) return nil } + +// AddRemote adds a remote to the config file +func AddRemote(name string, host string, port int, conf *clientconfig.Gvcconfig) error { + fmt.Println("name: ", name, "host: ", host, "port: ", port) + return nil +} diff --git a/client/clientcmd/commonlib.go b/client/clientcmd/commonlib.go index a55e450..dcc2c79 100644 --- a/client/clientcmd/commonlib.go +++ b/client/clientcmd/commonlib.go @@ -1 +1,4 @@ package clientcmd + +// ConfigPath is the global path to the config that is injected from the main client package. +var ConfigPath string diff --git a/client/clientcmd/init.go b/client/clientcmd/init.go index 6f747c5..514aacc 100644 --- a/client/clientcmd/init.go +++ b/client/clientcmd/init.go @@ -2,15 +2,19 @@ package clientcmd import ( "fmt" + "log" "os" ) -//AddFiles adds files to the repo -func InitializeRepo() error { +// InitializeRepo creates the repo directory and a new config file +func InitializeRepo() { cwd, err := os.Getwd() if err != nil { - return err + log.Fatal("unable to get current working directory.. permissions issue?") } fmt.Println("Initializing repo in dir: ", cwd) - return nil + err = os.Mkdir(".gvc", 0644) + if err != nil { + fmt.Println(".gvc directory already exists, but no config file... continuing") + } } diff --git a/client/clientconfig/config.go b/client/clientconfig/config.go index 22e4837..8fb942d 100644 --- a/client/clientconfig/config.go +++ b/client/clientconfig/config.go @@ -3,12 +3,17 @@ package config import ( "fmt" "os" + + "github.com/deranjer/store" ) +// ConfigPath is the global path to the config that is injected from the main client package. +var ConfigPath string + // ValidateConfig will go through the entire config and do basic sanity checks func ValidateConfig(conf *Gvcconfig, version string) error { if conf.Version == "" { // No version found, should we update it? - fmt.Printf("No version found, inputing current client version: %s", version) + fmt.Printf("No version found, inputing current client version: %s\n", version) conf.Version = version } if conf.RootPath == "" { @@ -16,7 +21,7 @@ func ValidateConfig(conf *Gvcconfig, version string) error { if err != nil { return fmt.Errorf("unable to get current working directory, and rootpath of repo is not set: %s", err) } - fmt.Printf("No root path found, inputting current working directory: %s", path) + fmt.Printf("No root path found, inputting current working directory: %s\n", path) } err := validateRemotes(conf) if err != nil { @@ -37,9 +42,9 @@ func validateCompress(conf *Gvcconfig) error { if file == "" { fmt.Println("empty file in compress files, removing... ") compress.Files[i] = compress.Files[len(compress.Files)-1] + continue } // TODO: write more validation - return nil } for i, folder := range compress.Folders { file, err := os.Stat(folder) // TODO: check to see if it returns ABS or not, might need to convert @@ -64,6 +69,10 @@ func validateCompress(conf *Gvcconfig) error { } // TODO: validate there is a "." in front of the ext, if not add it? } + err := store.Save(ConfigPath, &conf) + if err != nil { + fmt.Println("Error saving config back to toml file: ", err) + } return nil }