Skip to content

Commit

Permalink
Merge pull request #21 from amirfakhrullah/feat/refactor-cli
Browse files Browse the repository at this point in the history
cli.ConfirmProceed enhancement
  • Loading branch information
amirfakhrullah authored Dec 3, 2022
2 parents 6d57549 + e004c6e commit c93d857
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
8 changes: 3 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ import (
"github.com/amirfakhrullah/license-gen/pkg/licenses"
)

var fileName = "LICENSE"

func main() {
existedLicenseList, fileErr := helpers.IsLicenseExist()
helpers.HandlePanic(&fileErr)

// get confirmation to proceed if there's existing LICENSE
if len(existedLicenseList) > 0 {
toProceed := cli.ConfirmProceed()
toProceed := cli.ConfirmProceed(&existedLicenseList)
if !toProceed {
return
}
Expand All @@ -32,11 +30,11 @@ func main() {
licContent := licenses.Fill_License(&name, &year)

// execute file deletion process for files in existedLicenseList with extensions (.txt, .md, ...)
delErr := helpers.DeleteExistingLicenseFiles(&fileName, &existedLicenseList)
delErr := helpers.DeleteExistingLicenseFiles(&existedLicenseList)
helpers.HandlePanic(&delErr)

// file writes
writeErr := helpers.CreateAndWriteLicense(&fileName, licContent)
writeErr := helpers.CreateAndWriteLicense(licContent)
helpers.HandlePanic(&writeErr)

fmt.Printf("✅ Successfully added %v\n", lic[i].Name)
Expand Down
6 changes: 4 additions & 2 deletions pkg/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
"errors"
"fmt"
"strings"
"time"

"github.com/amirfakhrullah/license-gen/pkg/helpers"
Expand Down Expand Up @@ -79,9 +80,10 @@ func GetYear() string {
return year
}

func ConfirmProceed() bool {
func ConfirmProceed(files *[]string) bool {
fmt.Printf("⚠️ LICENSE file(s) found in your directory: %v\n", strings.Join(*files, ", "))
prompt := promptui.Select{
Label: "LICENSE file found in your directory. If you proceed, this will replace the current license",
Label: "Continuing this will erase/replace the file(s) above. Do you wish to proceed?",
Items: []string{"Proceed", "Cancel"},
}

Expand Down
10 changes: 6 additions & 4 deletions pkg/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"time"
)

var fileName = "LICENSE"

func HandlePanic(e *error) {
if *e != nil {
panic(*e)
Expand All @@ -31,9 +33,9 @@ func GetDefaultYear() string {
return strconv.Itoa(year)
}

func DeleteExistingLicenseFiles(skipName *string, existingFiles *[]string) error {
func DeleteExistingLicenseFiles(existingFiles *[]string) error {
for _, lic := range *existingFiles {
if lic == *skipName {
if lic == fileName {
continue
}
err := os.Remove(lic)
Expand All @@ -44,8 +46,8 @@ func DeleteExistingLicenseFiles(skipName *string, existingFiles *[]string) error
return nil
}

func CreateAndWriteLicense(fileName *string, content *string) error {
f, osErr := os.Create(*fileName)
func CreateAndWriteLicense(content *string) error {
f, osErr := os.Create(fileName)
if osErr != nil {
return osErr
}
Expand Down

0 comments on commit c93d857

Please sign in to comment.