Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ not allow to init the project in a directory that is not cleaned (v3+ only) #1738

Merged
merged 1 commit into from
Oct 23, 2020
Merged
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
29 changes: 29 additions & 0 deletions pkg/plugin/v3/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v3

import (
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -111,6 +112,11 @@ func (p *initPlugin) Validate() error {
}
}

// Check if the current directory has not files or directories which does not allow to init the project
if err := checkDir(); err != nil {
return err
}

// Check if the project name is a valid k8s namespace (DNS 1123 label).
if p.config.ProjectName == "" {
dir, err := os.Getwd()
Expand Down Expand Up @@ -167,3 +173,26 @@ func (p *initPlugin) PostScaffold() error {
fmt.Printf("Next: define a resource with:\n$ %s create api\n", p.commandName)
return nil
}

// checkDir will return error if the current directory has files which are
// not the go.mod and/or starts with the prefix (.) such as .gitignore.
// Note that, it is expected that the directory to scaffold the project is cleaned.
// Otherwise, it might face issues to do the scaffold. The go.mod is allowed because user might run
// go mod init before use the plugin it for not be required inform
// the go module via the repo --flag.
func checkDir() error {
err := filepath.Walk(".",
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.Name() != "go.mod" && !strings.HasPrefix(info.Name(), ".") {
return errors.New("only the go.mod and files with the prefix \"(.)\" are allowed before the init")
}
return nil
})
if err != nil {
return err
}
return nil
}