From e2bc4d2754b3f72ae9bf2cc84e0dfb6a82ccbd89 Mon Sep 17 00:00:00 2001
From: Tamer Tas <contact@tmrts.com>
Date: Thu, 17 Dec 2015 15:41:56 +0200
Subject: [PATCH] Add varargs to delete command

---
 pkg/cmd/delete.go          | 42 +++++++++++++++++++-------------------
 pkg/cmd/download.go        |  5 ++---
 pkg/cmd/must_validate.go   |  6 ++++++
 pkg/cmd/root.go            |  2 +-
 pkg/cmd/util/validation.go | 14 +++++++++++++
 5 files changed, 44 insertions(+), 25 deletions(-)

diff --git a/pkg/cmd/delete.go b/pkg/cmd/delete.go
index 04ca97c..b2050e9 100644
--- a/pkg/cmd/delete.go
+++ b/pkg/cmd/delete.go
@@ -8,8 +8,8 @@ import (
 	cli "github.com/spf13/cobra"
 
 	"github.com/tmrts/tmplt/pkg/tmplt"
-	"github.com/tmrts/tmplt/pkg/util/exit"
 	"github.com/tmrts/tmplt/pkg/util/osutil"
+	"github.com/tmrts/tmplt/pkg/util/tlog"
 	"github.com/tmrts/tmplt/pkg/util/validate"
 )
 
@@ -17,26 +17,26 @@ var Delete = &cli.Command{
 	Use:   "delete <template-name>",
 	Short: "Delete a project template from the template registry",
 	Run: func(c *cli.Command, args []string) {
-		MustValidateArgs(args, []validate.Argument{
-			{"template-path", validate.Alphanumeric},
-		})
-
-		templateName := args[0]
-
-		targetDir := filepath.Join(tmplt.Configuration.TemplateDirPath, templateName)
-
-		switch exists, err := osutil.DirExists(targetDir); {
-		case err != nil:
-			exit.Error(fmt.Errorf("delete: %s", err))
-		case !exists:
-			exit.Error(fmt.Errorf("Template %v doesn't exist", templateName))
+		MustValidateVarArgs(args, validate.Argument{"template-path", validate.Alphanumeric})
+
+		for _, templateName := range args {
+			targetDir := filepath.Join(tmplt.Configuration.TemplateDirPath, templateName)
+
+			switch exists, err := osutil.DirExists(targetDir); {
+			case err != nil:
+				tlog.Error(fmt.Sprintf("delete: %s", err))
+				continue
+			case !exists:
+				tlog.Error(fmt.Sprintf("Template %v doesn't exist", templateName))
+				continue
+			}
+
+			if err := os.RemoveAll(targetDir); err != nil {
+				tlog.Error(fmt.Sprintf("delete: %v", err))
+				continue
+			}
+
+			tlog.Success(fmt.Sprintf("Successfully deleted the template %v", templateName))
 		}
-
-		// TODO Accept globs and multiple arguments
-		if err := os.RemoveAll(targetDir); err != nil {
-			exit.Error(fmt.Errorf("delete: %v", err))
-		}
-
-		exit.OK("Successfully deleted the template %v", templateName)
 	},
 }
diff --git a/pkg/cmd/download.go b/pkg/cmd/download.go
index 8bbbdd9..abc57d9 100644
--- a/pkg/cmd/download.go
+++ b/pkg/cmd/download.go
@@ -118,9 +118,7 @@ var Download = &cli.Command{
 		case err != nil:
 			exit.Error(fmt.Errorf("download: %s", err))
 		case exists:
-			shouldOverwrite := false //GetBoolFlag(c, "update")
-
-			if !shouldOverwrite {
+			if shouldOverwrite := GetBoolFlag(c, "force"); !shouldOverwrite {
 				exit.OK("Template %v already exists use -u to update the template", templateName)
 			}
 		case !exists:
@@ -137,6 +135,7 @@ var Download = &cli.Command{
 
 		zipURL := host.ZipURL(templateURL)
 
+		// TODO validate template as well
 		if err := downloadZip(zipURL, targetDir); err != nil {
 			// Delete if download transaction fails
 			defer os.RemoveAll(targetDir)
diff --git a/pkg/cmd/must_validate.go b/pkg/cmd/must_validate.go
index b4e2a13..89e2a04 100644
--- a/pkg/cmd/must_validate.go
+++ b/pkg/cmd/must_validate.go
@@ -8,6 +8,12 @@ import (
 	"github.com/tmrts/tmplt/pkg/util/validate"
 )
 
+func MustValidateVarArgs(args []string, v validate.Argument) {
+	if err := util.ValidateVarArgs(args, v); err != nil {
+		exit.Error(err)
+	}
+}
+
 func MustValidateArgs(args []string, validations []validate.Argument) {
 	if err := util.ValidateArgs(args, validations); err != nil {
 		exit.Error(err)
diff --git a/pkg/cmd/root.go b/pkg/cmd/root.go
index befb0d5..8ae49a7 100644
--- a/pkg/cmd/root.go
+++ b/pkg/cmd/root.go
@@ -19,7 +19,7 @@ func Run() {
 	Save.PersistentFlags().BoolP("force", "f", false, "Overwrite existing template with the same name")
 	Root.AddCommand(Save)
 
-	// Update Flag
+	Download.PersistentFlags().BoolP("force", "f", false, "Overwrite existing template with the same name")
 	Root.AddCommand(Download)
 
 	Root.AddCommand(Delete)
diff --git a/pkg/cmd/util/validation.go b/pkg/cmd/util/validation.go
index ba6a894..b49fb28 100644
--- a/pkg/cmd/util/validation.go
+++ b/pkg/cmd/util/validation.go
@@ -34,6 +34,20 @@ func ValidateArgCount(expectedArgNo, argNo int) error {
 	return nil
 }
 
+func ValidateVarArgs(args []string, v validate.Argument) error {
+	if len(args) == 0 {
+		return ErrNotEnoughArgs
+	}
+
+	for _, arg := range args {
+		if ok := v.Validate(arg); !ok {
+			return fmt.Errorf(InvalidArg, v.Name, arg, v.Validate.TypeName())
+		}
+	}
+
+	return nil
+}
+
 func ValidateArgs(args []string, validations []validate.Argument) error {
 	if err := ValidateArgCount(len(validations), len(args)); err != nil {
 		return err
-- 
GitLab