diff --git a/cmd/config/io.go b/cmd/config/io.go index e84dd736..b609e5c8 100644 --- a/cmd/config/io.go +++ b/cmd/config/io.go @@ -38,3 +38,8 @@ func WriteConfigFile(configuration OneBuildConfiguration) error { content := string(yamlData) return ioutil.WriteFile(OneBuildConfigFileName, []byte(content), 0777) } + +// DeleteConfigFile deletes the config file +func DeleteConfigFile() error { + return os.Remove(OneBuildConfigFileName) +} diff --git a/cmd/delete.go b/cmd/delete.go new file mode 100644 index 00000000..668a1ac3 --- /dev/null +++ b/cmd/delete.go @@ -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") +} diff --git a/testing/fixtures/command_delete_fixtures.go b/testing/fixtures/command_delete_fixtures.go new file mode 100644 index 00000000..1bed0900 --- /dev/null +++ b/testing/fixtures/command_delete_fixtures.go @@ -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 +} diff --git a/testing/fixtures/fixures.go b/testing/fixtures/fixures.go index 47333778..749e9383 100644 --- a/testing/fixtures/fixures.go +++ b/testing/fixtures/fixures.go @@ -29,6 +29,7 @@ func GetFixtures() []Test { featureUnsetTestsData(), featureFlagVersionTestData(), + featureDeleteTestData(), } var r1 []Test