~imperator/quartiermeister-devel

Remove qm-remind v1 APPLIED

Stefan Tatschner: 3
 Remove qm-remind
 treewide: cleanups (remove usage of println, comments, …)
 qm-create: Unify error handling

 6 files changed, 23 insertions(+), 174 deletions(-)
ACK. Appl.

Am Samstag, den 06.03.2021, 10:10 +0100 schrieb Stefan Tatschner:
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~imperator/quartiermeister-devel/patches/20795/mbox | git am -3
Learn more about email & git

[PATCH 1/3] Remove qm-remind Export this patch

The code is broken and at maximum a PoC. So delete this crap.
---
 Makefile              |   6 +-
 bin/qm-remind/main.go | 146 ------------------------------------------
 2 files changed, 1 insertion(+), 151 deletions(-)
 delete mode 100644 bin/qm-remind/main.go

diff --git a/Makefile b/Makefile
index 04179b9..7a4614a 100644
--- a/Makefile
+++ b/Makefile
@@ -8,14 +8,10 @@ qm-create:
qm-list:
	$(GO) build $(GOFLAGS) -o $@ ./bin/$@

qm-remind:
	$(GO) build $(GOFLAGS) -o $@ ./bin/$@

qm-show:
	$(GO) build $(GOFLAGS) -o $@ ./bin/$@

clean:
	$(RM) qm-create qm-list qm-remind qm-show

.PHONY: qm-create qm-list qm-remind qm-show

.PHONY: qm-create qm-list qm-show
diff --git a/bin/qm-remind/main.go b/bin/qm-remind/main.go
deleted file mode 100644
index fee9d61..0000000
--- a/bin/qm-remind/main.go
@@ -1,146 +0,0 @@
package main

import (
	"context"
	"fmt"
	"os"
	"time"

	qm "git.sr.ht/~imperator/quartiermeister/lib"
	work "git.sr.ht/~sircmpwn/dowork"
	"github.com/godbus/dbus/v5"
	"github.com/godbus/dbus/v5/introspect"
)

const intro = `
<node>
	<interface name="org.rumpelsepp.QMRemind">
		<method name="ScheduleReminder">
			<arg direction="in" type="s"/>
		</method>
	</interface>` + introspect.IntrospectDataString + `</node> `

const (
	dBusInterface = "org.rumpelsepp.QMRemind"
	dBusObject    = "/org/rumpelsepp/QMRemind"
	dBusService   = "org.rumpelsepp.QMRemind"
)

type service struct {
	conn  *dbus.Conn
	queue *work.Queue
}

func newDBusService() (*service, error) {
	conn, err := dbus.SessionBus()
	if err != nil {
		return nil, err
	}

	s := &service{conn: conn, queue: work.NewQueue("notificts")}
	if err := conn.Export(s, dBusObject, dBusInterface); err != nil {
		return nil, err
	}
	if err := conn.Export(
		introspect.Introspectable(intro),
		dBusObject,
		"org.freedesktop.DBus.Introspectable",
	); err != nil {
		return nil, err
	}

	reply, err := conn.RequestName(dBusService, dbus.NameFlagDoNotQueue)
	if err != nil {
		return nil, err
	}
	if reply != dbus.RequestNameReplyPrimaryOwner {
		return nil, fmt.Errorf("name already taken")
	}

	return s, nil
}

func (s *service) notify(title, msg string) error {
	obj := s.conn.Object("org.freedesktop.Notifications",
		"/org/freedesktop/Notifications")
	call := obj.Call(
		"org.freedesktop.Notifications.Notify",
		0,
		"qm-remind",
		uint32(0),
		"",
		title,
		msg,
		[]string{},
		map[string]dbus.Variant{},
		int32(0),
	)
	return call.Err
}

func (s *service) notifyAt(ctx context.Context,
		date time.Time,
		title, msg string) error {

	closure := func(context.Context) error {
		return s.notify(title, msg)
	}
	t := work.NewTask(closure).NotBefore(date)
	return s.queue.Enqueue(t)
}

func (s *service) Close() error {
	return s.conn.Close()
}

func (s service) ScheduleReminder(timespec string) *dbus.Error {
	_, err := time.Parse(time.RFC3339, timespec)
	if err != nil {
		return dbus.MakeFailedError(err)
	}
	return nil
}

func handleNonOverdue(s *service, t *qm.Task, notificts map[int]context.Context) {
	if _, ok := notificts[t.ID]; !ok {
		ctx := context.Background()
		s.notifyAt(ctx, t.DueAt, t.Title, t.Body)
		notificts[t.ID] = ctx
	}
}

func worker(s *service, p *qm.Pool, notificts map[int]context.Context) {
	tasks, err := p.AllTasks()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	for _, task := range tasks {
		if task.DueAt.IsZero() {
			continue
		}
		if !task.Overdue() {
			handleNonOverdue(s, task, notificts)
		}
	}
	time.Sleep(1 * time.Minute)
}

func main() {
	s, err := newDBusService()
	if err != nil {
		panic(err)
	}
	defer s.Close()

	s.queue.Start(context.Background())

	var (
		p             = qm.Pool{Path: os.Getenv("QMPATH")}
		notificts = make(map[int]context.Context)
	)

	for {
		worker(s, &p, notificts)
	}
}
-- 
2.30.1

[PATCH 2/3] treewide: cleanups (remove usage of println, comments, …) Export this patch

---
 bin/qm-create/main.go | 4 ++--
 bin/qm-list/main.go   | 7 +++----
 lib/collection.go     | 5 ++---
 3 files changed, 7 insertions(+), 9 deletions(-)

diff --git a/bin/qm-create/main.go b/bin/qm-create/main.go
index d29fdce..073e74e 100644
--- a/bin/qm-create/main.go
@@ -2,10 +2,10 @@ package main

import (
	"fmt"
	"github.com/integrii/flaggy"
	"os"

	qm "git.sr.ht/~imperator/quartiermeister/lib"
	"github.com/integrii/flaggy"
)

type options struct {
@@ -40,7 +40,7 @@ func createItem(c *qm.Collection, opts *options) {
	}

out:
	println("Successfully created item, Kommandant.")
	fmt.Println("Successfully created item, Kommandant.")
}

func createProject(c *qm.Collection, opts *options) {
diff --git a/bin/qm-list/main.go b/bin/qm-list/main.go
index f33b2ba..512baca 100644
--- a/bin/qm-list/main.go
@@ -2,9 +2,9 @@ package main

import (
	"fmt"
	"github.com/integrii/flaggy"

	qm "git.sr.ht/~imperator/quartiermeister/lib"
	"github.com/integrii/flaggy"
)

func listItems(c *qm.Collection) {
@@ -31,10 +31,9 @@ func listProjs(c *qm.Collection) {

func main() {
	var (
	//qmpath = os.Getenv("QMPATH")
		itemCmd = flaggy.NewSubcommand("items")
		projCmd = flaggy.NewSubcommand("projs")
	)
	itemCmd := flaggy.NewSubcommand("items")
	projCmd := flaggy.NewSubcommand("projs")
	flaggy.AttachSubcommand(itemCmd, 1)
	flaggy.AttachSubcommand(projCmd, 1)
	flaggy.Parse()
diff --git a/lib/collection.go b/lib/collection.go
index 22ae2d8..37784ff 100644
--- a/lib/collection.go
+++ b/lib/collection.go
@@ -81,7 +81,6 @@ func getProjMeta(path string) (*Project, error) {
		path = filepath.Join(path, "meta.yml")
	}

	//	println("pfad: ", path)
	b, err := ioutil.ReadFile(path)
	if err != nil {
		return nil, err
@@ -117,12 +116,12 @@ func (p *Project) GetSubprojectsN(depth uint) ([]*Project, error) {
	var subprojs []*Project

	glob := p.Path + buildLayerNGlob(depth)
	println("glob: ", glob)
	fmt.Println("glob: ", glob)
	matches, err := filepath.Glob(glob)
	if err != nil {
		return nil, err
	}
	println("nr of matches: ", len(matches))
	fmt.Println("nr of matches: ", len(matches))

	for _, m := range matches {
		p, err := getProjMeta(m)
-- 
2.30.1

[PATCH 3/3] qm-create: Unify error handling Export this patch

---
 bin/qm-create/main.go | 29 +++++++++++++++--------------
 1 file changed, 15 insertions(+), 14 deletions(-)

diff --git a/bin/qm-create/main.go b/bin/qm-create/main.go
index 073e74e..6bd02e2 100644
--- a/bin/qm-create/main.go
@@ -14,13 +14,12 @@ type options struct {
	parent       string
}

func createItem(c *qm.Collection, opts *options) {
func createItem(c *qm.Collection, opts *options) error {
	var parent *qm.Project

	it, err := qm.CreateItem(opts.title, opts.descr)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(-1)
		return err
	}

	it.Flush()
@@ -30,31 +29,29 @@ func createItem(c *qm.Collection, opts *options) {

	parent, err = c.ProjectByTitle(opts.parent)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(-1)
		return err
	}

	if err = parent.AssignItem(it); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(-1)
		return err
	}

out:
	fmt.Println("Successfully created item, Kommandant.")
	return nil
}

func createProject(c *qm.Collection, opts *options) {
func createProject(c *qm.Collection, opts *options) error {
	pr, err := qm.CreateProject(opts.title, opts.parent, opts.descr)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(-1)
		return err
	}
	if err = pr.Flush(); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(-1)
		return err
	}

	fmt.Fprintln(os.Stderr, "Created project, Herr Kommandant.")
	return nil
}

func main() {
@@ -78,8 +75,12 @@ func main() {
	}

	if opts.proj { // we'll treat item as a default for now
		createProject(c, &opts)
		err = createProject(c, &opts)
	} else {
		createItem(c, &opts)
		err = createItem(c, &opts)
	}
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}
-- 
2.30.1
ACK. Appl.

Am Samstag, den 06.03.2021, 10:10 +0100 schrieb Stefan Tatschner: