Skip to content

Commit

Permalink
Merge pull request #20 from kaypee90/add-config-override-check
Browse files Browse the repository at this point in the history
feat: add prompt before overriding existing configuration
  • Loading branch information
kaypee90 authored Jul 30, 2024
2 parents dd96dbd + 9cf01f9 commit ca27142
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 11 deletions.
8 changes: 7 additions & 1 deletion cmd/depbot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os"
)

const version = "0.2.3"
const version = "0.3.0"

func displayAppVersion() {
fmt.Printf("Depbot %s\n", version)
Expand All @@ -21,6 +21,12 @@ func launchApplicaton() {

// get dependabot configuration details
for {
if dependabotFileExists() {
if getConfigurationOverrideConfirmation() == NO {
os.Exit(0)
}
}

packageEcosystem := getPackageEcosystem()
directory := getDirectory()
interval := getInterval()
Expand Down
15 changes: 15 additions & 0 deletions cmd/depbot/prompts.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ import (
const YES = "yes"
const NO = "no"

func getConfigurationOverrideConfirmation() string {
prompt := promptui.Select{
Label: "Dependabot is currently configured for this project. Would you like to override the current configuration?",
Items: []string{YES, NO},
}

_, result, err := prompt.Run()

if err != nil {
panic(err)
}

return result
}

func getPackageEcosystem() string {
prompt := promptui.Select{
Label: "Select Package Ecosystem",
Expand Down
40 changes: 34 additions & 6 deletions cmd/depbot/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
const (
dependabotFileName = "dependabot.yml"
githubDirectory = ".github"
pathSeperator = "/"
)

func printIntroductoryText() {
Expand All @@ -33,6 +34,16 @@ func writeBytesToFile(fileName string, data []byte) {
log.Printf("Dependabot config created successfully!")
}

func fileExists(filename string) bool {
_, err := os.Stat(filename)
if err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}

func createDirectoryIfItDoesNotExist(dir string) (hasCreatedNewDir bool, err error) {
if _, err := os.Stat(dir); os.IsNotExist(err) {
err := os.Mkdir(dir, 0755)
Expand All @@ -48,12 +59,9 @@ func createDirectoryIfItDoesNotExist(dir string) (hasCreatedNewDir bool, err err
return false, nil
}

func createConfigurationFile(fileName string, destinationDir string, data []byte, skipCreatingDir bool) {
fullFilePath := fileName

func createConfigurationFile(fullFilePath string, destinationDir string, data []byte, skipCreatingDir bool) {
if !skipCreatingDir {
createDirectoryIfItDoesNotExist(destinationDir)
fullFilePath = destinationDir + "/" + fileName
}

writeBytesToFile(fullFilePath, data)
Expand All @@ -68,7 +76,27 @@ func getWorkingDirectory() string {
return filepath.Base(currentDirectory)
}

func workingDirectoryIsGithub() bool {
return getWorkingDirectory() == githubDirectory
}

func getDependabotYamlFilePath(fileName string, destinationDir string) string {
fullFilePath := fileName

if !workingDirectoryIsGithub() {
fullFilePath = destinationDir + pathSeperator + fileName
}

return fullFilePath
}

func createDependabotYamlFile(data []byte) {
skipCreatingDir := getWorkingDirectory() == githubDirectory
createConfigurationFile(dependabotFileName, githubDirectory, data, skipCreatingDir)
skipCreatingDir := workingDirectoryIsGithub()
fullFilePath := getDependabotYamlFilePath(dependabotFileName, githubDirectory)
createConfigurationFile(fullFilePath, githubDirectory, data, skipCreatingDir)
}

func dependabotFileExists() bool {
dependabotYmlFilePath := getDependabotYamlFilePath(dependabotFileName, githubDirectory)
return fileExists(dependabotYmlFilePath)
}
34 changes: 30 additions & 4 deletions cmd/depbot/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,18 @@ func TestCreateDirectoryIfItDoesNotExistIfDirectoryAlreadyExist(t *testing.T) {

func TestCreateConfigurationFile(t *testing.T) {
const fileName = "dependabot.yml"
const destinationDir = ".github"

defer os.RemoveAll(testDir)
defer os.RemoveAll(destinationDir)

_ = os.Mkdir(testDir, 0755)
_ = os.Mkdir(destinationDir, 0755)

data := []byte("version: 2\nupdates:\n - package-ecosystem: \"npm\"")
filePath := getDependabotYamlFilePath(fileName, destinationDir)

createConfigurationFile(fileName, testDir, data, false)
createConfigurationFile(filePath, destinationDir, data, false)

_, err := os.Stat(testDir + "/" + fileName)
_, err := os.Stat(destinationDir + "/" + fileName)

// Exists
assert.NilError(t, err)
Expand All @@ -51,3 +53,27 @@ func TestGetWorkingDirectory(t *testing.T) {

assert.Equal(t, dirName, "depbot")
}

func TestFileExistsWhenFileIsNotCreated(t *testing.T) {
defer os.RemoveAll(testDir)

hasFile := fileExists(testDir + "/dependabot.yml")

assert.Equal(t, hasFile, false)
}

func TestFileExistsWhenFileAlrieadyExists(t *testing.T) {
filePath := testDir + "/dependabot.yml"

// Create the file
file, err := os.Create(filePath)
if err != nil {
return
}
defer file.Close()
defer os.RemoveAll(testDir)

hasFile := fileExists(filePath)

assert.Equal(t, hasFile, true)
}

0 comments on commit ca27142

Please sign in to comment.