From aae1c4633418f6bbf92ce6e790971879003e5413 Mon Sep 17 00:00:00 2001 From: Cian Gallagher Date: Mon, 13 Dec 2021 22:22:35 +0000 Subject: [PATCH] WATCH: Validate and parse persistent flags a branch hwerein config file is not provided. --- cmd/watch.go | 50 ++++++++++++++++++++++++++++++++++++-------------- utils/utils.go | 25 ++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 15 deletions(-) diff --git a/cmd/watch.go b/cmd/watch.go index 3f84efc..4884d0e 100644 --- a/cmd/watch.go +++ b/cmd/watch.go @@ -4,6 +4,8 @@ import ( "fmt" "log" + "github.com/cian911/switchboard/utils" + "github.com/cian911/switchboard/watcher" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -35,21 +37,23 @@ func Watch() { Short: shortDesc, Long: longDesc, Run: func(cmd *cobra.Command, args []string) { - fmt.Println(ws) - if viper.ConfigFileUsed() != "" && ws.Watchers != nil { - /* var pw watcher.Producer = &watcher.PathWatcher{ */ - /* Path: path, */ - /* } */ - /* */ - /* var pc watcher.Consumer = &watcher.PathConsumer{ */ - /* Path: path, */ - /* Destination: destination, */ - /* Ext: "", */ - /* } */ - /* */ - /* pw.Register(&pc) */ - /* pw.Observe() */ + // Do something with a loop here. + } else { + validateFlags() + + var pw watcher.Producer = &watcher.PathWatcher{ + Path: viper.GetString("path"), + } + + var pc watcher.Consumer = &watcher.PathConsumer{ + Path: viper.GetString("path"), + Destination: viper.GetString("destination"), + Ext: viper.GetString("ext"), + } + + pw.Register(&pc) + pw.Observe() } }, @@ -66,6 +70,10 @@ func initCmd(runCmd cobra.Command) { runCmd.PersistentFlags().StringP("ext", "e", "", "File type you want to watch for.") runCmd.PersistentFlags().StringVar(&configFile, "config", "", "Pass an optional config file containing multipe paths to watch.") + viper.BindPFlag("path", runCmd.PersistentFlags().Lookup("path")) + viper.BindPFlag("destination", runCmd.PersistentFlags().Lookup("destination")) + viper.BindPFlag("ext", runCmd.PersistentFlags().Lookup("ext")) + var rootCmd = &cobra.Command{} rootCmd.AddCommand(&runCmd) rootCmd.Execute() @@ -91,3 +99,17 @@ func initConfig() { } } + +func validateFlags() { + if !utils.ValidatePath(viper.GetString("path")) { + log.Fatalf("Path cannot be found. Does the path exist?: %s", viper.GetString("path")) + } + + if !utils.ValidatePath(viper.GetString("destination")) { + log.Fatalf("Destination cannot be found. Does the path exist?: %s", viper.GetString("destination")) + } + + if !utils.ValidateFileExt(viper.GetString("ext")) { + log.Fatalf("Ext is not valid. A file extention should contain a '.': %s", viper.GetString("ext")) + } +} diff --git a/utils/utils.go b/utils/utils.go index fd05dbc..88c9fa7 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -1,7 +1,30 @@ package utils -import "path/filepath" +import ( + "os" + "path/filepath" +) func ExtractFileExt(path string) string { return filepath.Ext(path) } + +func ValidatePath(path string) bool { + if path == "" { + return false + } + + if _, err := os.Stat(path); os.IsNotExist(err) { + return false + } + + return true +} + +func ValidateFileExt(ext string) bool { + if ext[0:1] != "." { + return false + } + + return true +}