Skip to content

Commit

Permalink
Gives an error if a directory already exists. Colorized the errors wh…
Browse files Browse the repository at this point in the history
…en creating a new project if occurred
  • Loading branch information
Jamlie committed Nov 11, 2023
1 parent da70da5 commit 0f7a72b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
35 changes: 30 additions & 5 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ var createCmd = &cobra.Command{

Run: func(cmd *cobra.Command, args []string) {
var tprogram *tea.Program
var err error

options := steps.Options{
ProjectName: &textinput.Output{},
Expand All @@ -59,12 +60,17 @@ var createCmd = &cobra.Command{
isInteractive := !utils.HasChangedFlag(cmd.Flags())

flagName := cmd.Flag("name").Value.String()
if flagName != "" && doesDirectoryExistAndIsNotEmpty(flagName) {
err = fmt.Errorf("Directory '%s' already exists and is not empty. Please choose a different name", flagName)
cobra.CheckErr(textinput.CreateErrorInputModel(err).Err())
}
flagFramework := cmd.Flag("framework").Value.String()

if flagFramework != "" {
isValid := isValidProjectType(flagFramework, allowedProjectTypes)
if !isValid {
cobra.CheckErr(fmt.Errorf("Project type '%s' is not valid. Valid types are: %s", flagFramework, strings.Join(allowedProjectTypes, ", ")))
err = fmt.Errorf("Project type '%s' is not valid. Valid types are: %s", flagFramework, strings.Join(allowedProjectTypes, ", "))
cobra.CheckErr(textinput.CreateErrorInputModel(err).Err())
}
}

Expand All @@ -81,7 +87,11 @@ var createCmd = &cobra.Command{
tprogram := tea.NewProgram(textinput.InitialTextInputModel(options.ProjectName, "What is the name of your project?", project))
if _, err := tprogram.Run(); err != nil {
log.Printf("Name of project contains an error: %v", err)
cobra.CheckErr(err)
cobra.CheckErr(textinput.CreateErrorInputModel(err).Err())
}
if doesDirectoryExistAndIsNotEmpty(options.ProjectName.Output) {
err = fmt.Errorf("Directory '%s' already exists and is not empty. Please choose a different name", options.ProjectName.Output)
cobra.CheckErr(textinput.CreateErrorInputModel(err).Err())
}
project.ExitCLI(tprogram)

Expand All @@ -94,7 +104,7 @@ var createCmd = &cobra.Command{
s := &multiInput.Selection{}
tprogram = tea.NewProgram(multiInput.InitialModelMulti(step.Options, s, step.Headers, project))
if _, err := tprogram.Run(); err != nil {
cobra.CheckErr(err)
cobra.CheckErr(textinput.CreateErrorInputModel(err).Err())
}
project.ExitCLI(tprogram)

Expand All @@ -108,7 +118,7 @@ var createCmd = &cobra.Command{
currentWorkingDir, err := os.Getwd()
if err != nil {
log.Printf("could not get current working directory: %v", err)
cobra.CheckErr(err)
cobra.CheckErr(textinput.CreateErrorInputModel(err).Err())
}

project.AbsolutePath = currentWorkingDir
Expand All @@ -117,7 +127,7 @@ var createCmd = &cobra.Command{
err = project.CreateMainFile()
if err != nil {
log.Printf("Problem creating files for project. %v", err)
cobra.CheckErr(err)
cobra.CheckErr(textinput.CreateErrorInputModel(err).Err())
}

fmt.Println(endingMsgStyle.Render("\nNext steps cd into the newly created project with:"))
Expand All @@ -141,3 +151,18 @@ func isValidProjectType(input string, allowedTypes []string) bool {
}
return false
}

// doesDirectoryExistAndIsNotEmpty checks if the directory exists and is not empty
func doesDirectoryExistAndIsNotEmpty(name string) bool {
if _, err := os.Stat(name); err == nil {
dirEntries, err := os.ReadDir(name)
if err != nil {
log.Printf("could not read directory: %v", err)
cobra.CheckErr(textinput.CreateErrorInputModel(err))
}
if len(dirEntries) > 0 {
return true
}
}
return false
}
29 changes: 27 additions & 2 deletions cmd/ui/textinput/textinput.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package textinput

import (
"errors"
"fmt"

"github.com/charmbracelet/bubbles/textinput"
Expand All @@ -11,8 +12,10 @@ import (
"github.com/melkeydev/go-blueprint/cmd/program"
)

var titleStyle = lipgloss.NewStyle().Background(lipgloss.Color("#01FAC6")).Foreground(lipgloss.Color("#030303")).Bold(true).Padding(0, 1, 0)

var (
titleStyle = lipgloss.NewStyle().Background(lipgloss.Color("#01FAC6")).Foreground(lipgloss.Color("#030303")).Bold(true).Padding(0, 1, 0)
errorStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#FF8700")).Bold(true).Padding(0, 0, 0)
)
type (
errMsg error
)
Expand Down Expand Up @@ -55,6 +58,24 @@ func InitialTextInputModel(output *Output, header string, program *program.Proje
}
}

// CreateErrorInputModel creates a textinput step
// with the given error
func CreateErrorInputModel(err error) model {
ti := textinput.New()
ti.Focus()
ti.CharLimit = 156
ti.Width = 20
exit := true

return model{
textInput: ti,
err: errors.New(errorStyle.Render(err.Error())),
output: nil,
header: "",
exit: &exit,
}
}

// Init is called at the beginning of a textinput step
// and sets the cursor to blink
func (m model) Init() tea.Cmd {
Expand Down Expand Up @@ -97,3 +118,7 @@ func (m model) View() string {
m.textInput.View(),
)
}

func (m model) Err() string {
return m.err.Error()
}

0 comments on commit 0f7a72b

Please sign in to comment.