Skip to content

Commit

Permalink
Merge pull request #15 from kaypee90/refactor-version-0.2.0
Browse files Browse the repository at this point in the history
fix: improve displayed text on cli
  • Loading branch information
kaypee90 authored Jul 23, 2024
2 parents 6e655fa + 2bec0f1 commit 81f1f98
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 37 deletions.
4 changes: 2 additions & 2 deletions cmd/depbot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

func main() {
// display introduction text
displayIntroductoryText()
printIntroductoryText()

var updates []Update

Expand Down Expand Up @@ -36,5 +36,5 @@ func main() {

// write configration data to yaml file
yamlData := config.ConvertToYaml()
createDependabotYmlFile(yamlData)
createDependabotYamlFile(yamlData)
}
58 changes: 29 additions & 29 deletions cmd/depbot/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,16 @@ import (

const (
dependabotFileName = "dependabot.yml"
githubDir = ".github"
githubDirectory = ".github"
)

func displayIntroductoryText() {
func printIntroductoryText() {
green := "\033[32m"
reset := "\033[0m"
fmt.Printf("%sDepbot wizard will help you configure dependabot in your project%s\n", green, reset)
fmt.Printf("%sDepbot wizard will assist you to configure dependabot in your project%s\n", green, reset)
}

func createDirIfItDoesNotExit(dir string) (hasCreatedNewDir bool, err error) {
if _, err := os.Stat(dir); os.IsNotExist(err) {
err := os.Mkdir(dir, 0755)

if err != nil {
log.Fatalf("Error creating directory %s: %v", dir, err)
return false, err
}

return true, nil
}

return false, nil
}

func writeDataToFile(fileName string, data []byte) {
func writeBytesToFile(fileName string, data []byte) {
file, err := os.Create(fileName)
if err != nil {
log.Fatalf("Error creating file %s: %v", fileName, err)
Expand All @@ -48,30 +33,45 @@ func writeDataToFile(fileName string, data []byte) {
log.Printf("Dependabot config created successfully!")
}

func createDirectoryIfItDoesNotExist(dir string) (hasCreatedNewDir bool, err error) {
if _, err := os.Stat(dir); os.IsNotExist(err) {
err := os.Mkdir(dir, 0755)

if err != nil {
log.Fatalf("Error creating directory %s: %v", dir, err)
return false, err
}

return true, nil
}

return false, nil
}

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

if !skipCreatingDir {
log.Printf("Creating %s directory..", githubDir)
createDirIfItDoesNotExit(destinationDir)
log.Printf("Creating %s directory..", githubDirectory)
createDirectoryIfItDoesNotExist(destinationDir)
fullFilePath = destinationDir + "/" + fileName
} else {
log.Printf("Skipping directory creation since current directory is %s", githubDir)
log.Printf("Skipping directory creation since %s already exists", githubDirectory)
}

writeDataToFile(fullFilePath, data)
writeBytesToFile(fullFilePath, data)
}

func getCurrentDir() string {
currentDir, err := os.Getwd()
func getWorkingDirectory() string {
currentDirectory, err := os.Getwd()
if err != nil {
panic(err)
}

return filepath.Base(currentDir)
return filepath.Base(currentDirectory)
}

func createDependabotYmlFile(data []byte) {
skipCreatingDir := getCurrentDir() == githubDir
createConfigurationFile(dependabotFileName, githubDir, data, skipCreatingDir)
func createDependabotYamlFile(data []byte) {
skipCreatingDir := getWorkingDirectory() == githubDirectory
createConfigurationFile(dependabotFileName, githubDirectory, data, skipCreatingDir)
}
12 changes: 6 additions & 6 deletions cmd/depbot/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ import (

const testDir = "./test"

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

hasCreatedNewDir, err := createDirIfItDoesNotExit(testDir)
hasCreatedNewDir, err := createDirectoryIfItDoesNotExist(testDir)

assert.NilError(t, err)
assert.Equal(t, hasCreatedNewDir, true)
}

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

_ = os.Mkdir(testDir, 0755)

hasCreatedNewDir, err := createDirIfItDoesNotExit(testDir)
hasCreatedNewDir, err := createDirectoryIfItDoesNotExist(testDir)

assert.NilError(t, err)
assert.Equal(t, hasCreatedNewDir, false)
Expand All @@ -46,8 +46,8 @@ func TestCreateConfigurationFile(t *testing.T) {
assert.NilError(t, err)
}

func TestGetCurrentDir(t *testing.T) {
dirName := getCurrentDir()
func TestGetWorkingDirectory(t *testing.T) {
dirName := getWorkingDirectory()

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

0 comments on commit 81f1f98

Please sign in to comment.