-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4b221f6
commit a039389
Showing
4 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package cmd | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/gopinath-langote/1build/cmd/config" | ||
"github.com/gopinath-langote/1build/cmd/utils" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var shouldDelete bool | ||
|
||
var deleteCmd = &cobra.Command{ | ||
Use: "delete", | ||
Short: "Deletes project configuration", | ||
Long: `Deletes project configuration | ||
- To forcibly delete file without asking for consent use -f or --force | ||
For example: | ||
1build delete | ||
1build delete --force`, | ||
PreRun: func(cmd *cobra.Command, args []string) { | ||
if !config.IsConfigFilePresent() { | ||
fmt.Println(utils.Colored("No configuration file found!", utils.RED)) | ||
utils.ExitError() | ||
} | ||
}, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if !shouldDelete { | ||
fmt.Printf("Delete 1build configuration file? (y/N) ") | ||
reader := bufio.NewReader(os.Stdin) | ||
prompt, err := reader.ReadString('\n') | ||
if err == nil && strings.ToLower(strings.TrimSpace(prompt)) == "y" { | ||
shouldDelete = true | ||
} | ||
} | ||
if shouldDelete { | ||
if err := config.DeleteConfigFile(); err != nil { | ||
fmt.Println(utils.Colored("Error deleting configuration file.", utils.RED)) | ||
} | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(deleteCmd) | ||
deleteCmd.Flags().BoolVarP(&shouldDelete, "force", "f", false, "Forcibly delete configuration file") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package fixtures | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/gopinath-langote/1build/testing/def" | ||
"github.com/gopinath-langote/1build/testing/utils" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func featureDeleteTestData() []Test { | ||
feature := "delete" | ||
return []Test{ | ||
shouldDeleteConfigFile(feature), | ||
shouldFailIfFileDoesntExists(feature, ""), | ||
shouldFailIfFileDoesntExists(feature, "--force"), | ||
} | ||
} | ||
|
||
func shouldDeleteConfigFile(feature string) Test { | ||
return Test{ | ||
Feature: feature, | ||
Name: "shouldDeleteConfigFile", | ||
CmdArgs: []string{"delete", "--force"}, | ||
Setup: func(dir string) error { | ||
return utils.CreateConfigFile(dir, "project: Sample Project\ncommands:\n") | ||
}, | ||
Assertion: func(dir string, actualOutput string, t *testing.T) bool { | ||
return assertFileNotExists(t, dir+"/"+def.ConfigFileName) | ||
}, | ||
} | ||
} | ||
|
||
func shouldFailIfFileDoesntExists(feature string, arg string) Test { | ||
expectedOutput := "No configuration file found!" | ||
return Test{ | ||
Feature: feature, | ||
Name: "shouldFailIfFileDoesntExists", | ||
CmdArgs: []string{"delete", arg}, | ||
Assertion: func(dir string, actualOutput string, t *testing.T) bool { | ||
return assert.Contains(t, actualOutput, expectedOutput) | ||
}, | ||
} | ||
} | ||
|
||
func assertFileNotExists(t *testing.T, path string) bool { | ||
_, err := os.Stat(path) | ||
if err == nil { | ||
assert.Fail(t, "Delete command did not delete config file!") | ||
return false | ||
} else if !os.IsNotExist(err) { | ||
assert.Fail(t, "error running os.Stat(%q): %s", path, err) | ||
return false | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters