~rensoliemans/listen-ereader

6a5445c9c8ef4b5b4464c45dc3bb106ef82ebe48 — Rens Oliemans 7 months ago
Initial commit
4 files changed, 78 insertions(+), 0 deletions(-)

A .gitignore
A go.mod
A go.sum
A listen.go
A  => .gitignore +29 -0
@@ 1,29 @@
# Created by https://www.toptal.com/developers/gitignore/api/go
# Edit at https://www.toptal.com/developers/gitignore?templates=go

### Go ###
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# binary of this module
listen-ereader

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work

# End of https://www.toptal.com/developers/gitignore/api/go

A  => go.mod +10 -0
@@ 1,10 @@
module listen-ereader

go 1.21.9

require github.com/jochenvg/go-udev v0.0.0-20171110120927-d6b62d56d37b

require (
	github.com/jkeiser/iter v0.0.0-20200628201005-c8aa0ae784d1 // indirect
	golang.org/x/sys v0.19.0 // indirect
)

A  => go.sum +6 -0
@@ 1,6 @@
github.com/jkeiser/iter v0.0.0-20200628201005-c8aa0ae784d1 h1:smvLGU3obGU5kny71BtE/ibR0wIXRUiRFDmSn0Nxz1E=
github.com/jkeiser/iter v0.0.0-20200628201005-c8aa0ae784d1/go.mod h1:fP/NdyhRVOv09PLRbVXrSqHhrfQypdZwgE2L4h2U5C8=
github.com/jochenvg/go-udev v0.0.0-20171110120927-d6b62d56d37b h1:dgF9Rx3oPIz2d816jKSjnShkJfmtYc/N/DxGDFv2CGk=
github.com/jochenvg/go-udev v0.0.0-20171110120927-d6b62d56d37b/go.mod h1:IBDUGq30U56w969YNPomhMbRje1GrhUsCh7tHdwgLXA=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=

A  => listen.go +33 -0
@@ 1,33 @@
package main

import "fmt"
import "context"
import "os/exec"
import "github.com/jochenvg/go-udev"

const ereader_uuid = "4F00-5826"

func main() {
	u := udev.Udev{}
	m := u.NewMonitorFromNetlink("udev")

	m.FilterAddMatchSubsystemDevtype("block", "disk")
	m.FilterAddMatchTag("systemd")

	ctx, cancel := context.WithCancel(context.Background())

	// Start monitor goroutine and get receive channel
	ch, _ := m.DeviceChan(ctx)

	for d := range ch {
		if d.PropertyValue("ID_FS_UUID") == ereader_uuid && d.Action() == "add" {
			fmt.Println("E-reader connected. Opening calibre")
			cmd := exec.Command("/usr/bin/calibre")
			if err := cmd.Run(); err != nil {
				fmt.Println("Error while launching calibre: ", err)
			}
		}
	}

	cancel()
}