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

added method to check if parent project exists for module #80

Merged
merged 3 commits into from
May 30, 2021
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
23 changes: 23 additions & 0 deletions core/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
var (
ErrProjectNotFound = errors.New("project not found")
ErrProjectAlreadyExists = errors.New("project already exists")
ErrParentlessModule = errors.New("no parent project for module exists, please create parent first")
)

type Project struct {
Expand Down Expand Up @@ -69,6 +70,13 @@ func (t *Timetrace) ListProjects() ([]*Project, error) {
// SaveProject persists the given project. Returns ErrProjectAlreadyExists if
// the project already exists and saving isn't forced.
func (t *Timetrace) SaveProject(project Project, force bool) error {
if project.IsModule() {
err := t.assertParent(project)
if err != nil {
return err
}
}

path := t.fs.ProjectFilepath(project.Key)

if _, err := os.Stat(path); os.IsExist(err) && !force {
Expand Down Expand Up @@ -169,3 +177,18 @@ func (t *Timetrace) editorFromEnvironment() string {

return defaultEditor
}

func (t *Timetrace) assertParent(project Project) error {
allP, err := t.ListProjects()
if err != nil {
return err
}

for _, p := range allP {
if p.Key == project.Parent() {
return nil
}
}

return ErrParentlessModule
}