Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion cmd/project.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package cmd

import (
"bufio"
"fmt"
"os"
"strings"
"text/template"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -36,7 +38,15 @@ func (p *Project) Create() error {
}

// create main.go
mainFile, err := os.Create(fmt.Sprintf("%s/main.go", p.AbsolutePath))
mainPath := fmt.Sprintf("%s/main.go", p.AbsolutePath)
ok, err := confirmMainOverwrite(mainPath)
if err != nil {
return err
}
if !ok {
return fmt.Errorf("aborted: %s already exists", mainPath)
}
mainFile, err := os.Create(mainPath)
if err != nil {
return err
}
Expand Down Expand Up @@ -68,6 +78,24 @@ func (p *Project) Create() error {
return p.createLicenseFile()
}

func confirmMainOverwrite(path string) (bool, error) {
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
return true, nil
}
return false, err
}

fmt.Printf("%s already exists. Overwrite? [y/N]: ", path)
reader := bufio.NewReader(os.Stdin)
response, err := reader.ReadString('\n')
if err != nil {
return false, err
}
response = strings.TrimSpace(strings.ToLower(response))
return response == "y" || response == "yes", nil
}

func (p *Project) createLicenseFile() error {
data := map[string]interface{}{
"copyright": copyrightLine(),
Expand Down