Philipp Stanner: 2 bin: create: add item.Flush() error check all: base all Create functions on a Collection 9 files changed, 58 insertions(+), 34 deletions(-)
Copy & paste the following snippet into your terminal to import this patchset into git:
curl -s https://lists.sr.ht/~imperator/quartiermeister-devel/patches/20815/mbox | git am -3Learn more about email & git
--- bin/qm-create/main.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/bin/qm-create/main.go b/bin/qm-create/main.go index 6bd02e2..c07d427 100644 --- a/bin/qm-create/main.go @@ -22,7 +22,10 @@ func createItem(c *qm.Collection, opts *options) error { return err } - it.Flush() + if err = it.Flush(); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } if opts.parent == "" { goto out } -- 2.20.1
also partially fix subproject creation. --- Makefile | 4 ++-- bin/qm-create/main.go | 29 ++++++++++++++--------------- go.sum | 2 ++ lib/container.go | 20 +++++++++++++++----- lib/item.go | 4 ++-- lib/project.go | 15 ++++++++++----- lib/tag.go | 11 +++++++---- qm | 2 ++ 8 files changed, 54 insertions(+), 33 deletions(-) diff --git a/Makefile b/Makefile index 7a4614a..619489d 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ GO ?= go -all: qm-create qm-list qm-remind qm-show +all: qm-create qm-list qm-show qm-create: $(GO) build $(GOFLAGS) -o $@ ./bin/$@ @@ -12,6 +12,6 @@ qm-show: $(GO) build $(GOFLAGS) -o $@ ./bin/$@ clean: - $(RM) qm-create qm-list qm-remind qm-show + $(RM) qm-create qm-list qm-show .PHONY: qm-create qm-list qm-show diff --git a/bin/qm-create/main.go b/bin/qm-create/main.go index c07d427..4c74c18 100644 --- a/bin/qm-create/main.go @@ -14,38 +14,37 @@ type options struct { parent string } -func createItem(c *qm.Collection, opts *options) error { - var parent *qm.Project - - it, err := qm.CreateItem(opts.title, opts.descr) +func setParent(c *qm.Collection, it *qm.Item, parent string) error { + parentProj, err := c.ProjectByTitle(parent) if err != nil { return err } - if err = it.Flush(); err != nil { - fmt.Fprintln(os.Stderr, err) - os.Exit(-1) - } - if opts.parent == "" { - goto out - } + err = parentProj.AssignItem(it) + return err +} - parent, err = c.ProjectByTitle(opts.parent) +func createItem(c *qm.Collection, opts *options) error { + it, err := c.CreateItem(opts.title, opts.descr) if err != nil { return err } - if err = parent.AssignItem(it); err != nil { + if err = it.Flush(); err != nil { return err } + if opts.parent != "" { + if err = setParent(c, it, opts.parent); err != nil { + return err + } + } -out: fmt.Println("Successfully created item, Kommandant.") return nil } func createProject(c *qm.Collection, opts *options) error { - pr, err := qm.CreateProject(opts.title, opts.parent, opts.descr) + pr, err := c.CreateProject(opts.title, opts.descr, opts.parent) if err != nil { return err } diff --git a/go.sum b/go.sum index ae1e6de..d0c2171 100644 --- a/go.sum +++ b/go.sum @@ -39,6 +39,8 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/integrii/flaggy v1.4.4 h1:8fGyiC14o0kxhTqm2VBoN19fDKPZsKipP7yggreTMDc= +github.com/integrii/flaggy v1.4.4/go.mod h1:tnTxHeTJbah0gQ6/K0RW0J7fMUBk9MCF5blhm43LNpI= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= diff --git a/lib/container.go b/lib/container.go index 280bb67..5859a42 100644 --- a/lib/container.go +++ b/lib/container.go @@ -21,17 +21,26 @@ type Container struct { parent string } -func CreateContainer(title, descr, type_, parent string) *Container { +func checkParentExists(c *Collection, title string) error { + _, err := c.ProjectByTitle(title) + return err +} + +func (c *Collection) CreateContainer(title, + descr, type_, parent string) (*Container, error) { var path string slug := Slug(title) if parent == "" { - path = filepath.Join(CollectionPath(), type_, slug) + path = filepath.Join(c.Path, type_, slug) } else { - path = filepath.Join(CollectionPath(), type_, slug, parent) + if err := checkParentExists(c, parent); err != nil { + return nil, err + } + path = filepath.Join(c.Path, type_, parent, slug) } - c := &Container{ + cont := &Container{ Title: title, Slug: slug, Description: descr, @@ -39,7 +48,8 @@ func CreateContainer(title, descr, type_, parent string) *Container { Path: path, parent: parent, } - return c + + return cont, nil } func (c *Container) AssignItem(g *Item) error { diff --git a/lib/item.go b/lib/item.go index 13c69c7..7d0c449 100644 --- a/lib/item.go +++ b/lib/item.go @@ -31,14 +31,14 @@ type Item struct { Tags []*Tag `json:"-" yaml:"-"` } -func CreateItem(title, description string) (*Item, error) { +func (c *Collection) CreateItem(title, description string) (*Item, error) { it := &Item{ Title: title, Slug: Slug(title), Description: description, UUID: uuid.New(), CreatedAt: time.Now(), - Path: filepath.Join(PoolPath(), Slug(title)+".qm"), + Path: filepath.Join(c.PoolPath, Slug(title)+".qm"), } return it, nil } diff --git a/lib/project.go b/lib/project.go index 7f70da4..946b3d0 100644 --- a/lib/project.go +++ b/lib/project.go @@ -19,10 +19,15 @@ type Project struct { //Progress uint `json:"progress" yaml:"progress"` } -func CreateProject(name, parent, description string) (*Project, error) { +func (c *Collection) CreateProject(title, description, + parent string) (*Project, error) { p := &Project{} - c := CreateContainer(name, description, "projects", parent) - p.Container = *c + cont, err := c.CreateContainer(title, description, "projects", parent) + if err != nil { + return nil, err + } + p.Container = *cont + return p, nil } @@ -47,8 +52,8 @@ func (p *Project) Flush() error { if err != nil { return err } - filename := "meta.yml" - err = ioutil.WriteFile(filepath.Join(p.Path, filename), b, 0644) + metapath := filepath.Join(p.Path, "meta.yml") + err = ioutil.WriteFile(metapath, b, 0644) if err != nil { return err } diff --git a/lib/tag.go b/lib/tag.go index 0349865..869a054 100644 --- a/lib/tag.go +++ b/lib/tag.go @@ -4,10 +4,13 @@ type Tag struct { Container } -func CreateTag(name, description string) (*Tag, error) { +func (c *Collection) CreateTag(name, description string) (*Tag, error) { var t Tag - c := CreateContainer(name, description, "tags", "") - t.Container = *c + cont, err := c.CreateContainer(name, description, "tags", "") - return &t, nil + if err == nil { + t.Container = *cont + } + + return &t, err } diff --git a/qm b/qm index 8f8ef47..a3b2d6f 100755 --- a/qm +++ b/qm @@ -18,6 +18,8 @@ main() { if [[ ! -d "$QM_PATH" ]]; then echo "\"$QM_PATH\" created." mkdir -p "$QM_PATH" + mkdir -p "$QM_PATH"/$QM_COLLECTION/pool + mkdir -p "$QM_PATH"/$QM_COLLECTION/projects fi if which "qm-$cmd" >& /dev/null; then -- 2.20.1