Skip to content

Commit

Permalink
Added delete config file command
Browse files Browse the repository at this point in the history
  • Loading branch information
diptag authored and gopinath-langote committed Oct 11, 2019
1 parent 4b221f6 commit a039389
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cmd/config/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
53 changes: 53 additions & 0 deletions cmd/delete.go
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")
}
57 changes: 57 additions & 0 deletions testing/fixtures/command_delete_fixtures.go
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
}
1 change: 1 addition & 0 deletions testing/fixtures/fixures.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func GetFixtures() []Test {
featureUnsetTestsData(),

featureFlagVersionTestData(),
featureDeleteTestData(),
}

var r1 []Test
Expand Down

0 comments on commit a039389

Please sign in to comment.