Skip to content

Commit

Permalink
WATCH: Validate and parse persistent flags a branch hwerein config fi…
Browse files Browse the repository at this point in the history
…le is not provided.
  • Loading branch information
Cian911 committed Dec 13, 2021
1 parent a1fda08 commit aae1c46
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 15 deletions.
50 changes: 36 additions & 14 deletions cmd/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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()
}

},
Expand All @@ -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()
Expand All @@ -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"))
}
}
25 changes: 24 additions & 1 deletion utils/utils.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit aae1c46

Please sign in to comment.