Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jwijenbergh committed Jun 21, 2024
1 parent f14cf1c commit 526e1a6
Show file tree
Hide file tree
Showing 12 changed files with 218 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
/.envrc
/geteduroam-cli
/geteduroam-gui
/geteduroam-notifcheck
/shell.nix
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ help: ## Print this help message

.DEFAULT_GOAL := help

.build-notifcheck:
go build -o geteduroam-notifcheck ./cmd/geteduroam-notifcheck

.build-cli:
go build -o geteduroam-cli ./cmd/geteduroam-cli

Expand All @@ -18,12 +21,18 @@ lint: ## Lint the codebase using Golangci-lint
fmt: ## Format the codebase using Gofumpt
gofumpt -w .

build-notifcheck: .build-notifcheck ## Build notification checker
@echo "Done building, run 'make run-notifcheck' to run the notification checker"

build-cli: .build-cli ## Build CLI version
@echo "Done building, run 'make run-cli' to run the CLI"

build-gui: .build-gui ## Build GUI version
@echo "Done building, run 'make run-gui' to run the GUI"

run-notifcheck: .build-notifcheck ## Run notification checker
./geteduroam-notifcheck

run-cli: .build-cli ## Run CLI version
./geteduroam-cli

Expand All @@ -32,6 +41,7 @@ run-gui: .build-gui ## Run GUI version

clean: ## Clean the project
go clean
rm -rf geteduroam-notifcheck
rm -rf geteduroam-cli
rm -rf geteduroam-gui

Expand Down
25 changes: 22 additions & 3 deletions cmd/geteduroam-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/geteduroam/linux-app/internal/instance"
"github.com/geteduroam/linux-app/internal/log"
"github.com/geteduroam/linux-app/internal/network"
"github.com/geteduroam/linux-app/internal/notification"
"github.com/geteduroam/linux-app/internal/utils"
"github.com/geteduroam/linux-app/internal/version"
)
Expand Down Expand Up @@ -448,7 +449,7 @@ func main() {
if !IsTerminal() {
msg := "Not starting the CLI as it is not run in a terminal. You might want to install the GUI: https://github.com/geteduroam/linux-app/releases"
slog.Error(msg)
_ = exec.Command("notify-send", "geteduroam-cli", msg).Start()
notification.Send(msg)

Check failure on line 452 in cmd/geteduroam-cli/main.go

View workflow job for this annotation

GitHub Actions / Lint Go

Error return value of `notification.Send` is not checked (errcheck)
os.Exit(1)
}
var v *time.Time
Expand All @@ -458,7 +459,25 @@ func main() {
v = doDiscovery()
}
fmt.Println("\nYour eduroam connection has been added to NetworkManager with the name: \"eduroam (from geteduroam)\"")
if v != nil {
fmt.Printf("Your connection is valid for: %d days\n", utils.ValidityDays(*v))
if v == nil {
return
}
fmt.Printf("Your connection is valid for: %d days\n", utils.ValidityDays(*v))
if !notification.HasDaemonSupport() {
return
}
in := ask("Do you want to enable notifications that warns for expiry (requires SystemD and notify-send) (y/n)?: ", func(msg string) bool {
if msg != "y" && msg != "n" {
fmt.Fprintln(os.Stderr, "Please enter y/n")
return false
}
return true
})
if in != "y" {
return
}
err = notification.EnableDameon()
if err != nil {
slog.Error("failed to enale notification support", "err", err)
}
}
70 changes: 70 additions & 0 deletions cmd/geteduroam-notifcheck/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package main

import (
"fmt"
"os/exec"
"time"

"golang.org/x/exp/slog"

"github.com/geteduroam/linux-app/internal/config"
"github.com/geteduroam/linux-app/internal/log"
"github.com/geteduroam/linux-app/internal/nm"
"github.com/geteduroam/linux-app/internal/notification"
)

func sendnotif(notif string) error {

Check failure on line 16 in cmd/geteduroam-notifcheck/main.go

View workflow job for this annotation

GitHub Actions / Lint Go

func `sendnotif` is unused (unused)
_, err := exec.Command("notify-send", "geteduroam", notif).Output()
if err != nil {
return err
}
return nil
}

func main() {
log.Initialize("geteduroam-notifcheck", false)
cfg, err := config.Load()
if err != nil {
slog.Error("no previous state", "error", err)
return
}
con, err := nm.PreviousCon(cfg.UUID)
if err != nil {
slog.Error("no connection with uuid", "uuid", cfg.UUID, "error", err)
return
}
if con == nil {
slog.Error("connection is nil")
return
}

if cfg.Validity == nil {
slog.Info("validity is nil")
return
}
valid := *cfg.Validity
now := time.Now()
diff := valid.Sub(now)
days := int(diff.Hours() / 24)

var text string
if days > 10 {
slog.Info("It is still more than 10 days", "days", days)
return
}
if days < 0 {
text = "connection is expired"
}
if days == 0 {
text = "connection expires today"
}
if days > 0 {
text = fmt.Sprintf("connection expires in %d days", days)
}
msg := fmt.Sprintf("Your eduroam %s. Re-run geteduroam to renew the connection", text)
err = notification.Send(msg)
if err != nil {
slog.Error("failed to send notification", "error", err)
return
}
}
16 changes: 16 additions & 0 deletions goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ builds:
goarch:
- amd64
binary: geteduroam-gui
- main: ./cmd/geteduroam-notifcheck
id: geteduroam-notifcheck
goos:
- linux
goarch:
- amd64
binary: geteduroam-notifcheck

nfpms:
- file_name_template: '{{ .Binary }}_{{ .Os }}_{{ .Arch }}'
Expand All @@ -24,6 +31,7 @@ nfpms:
homepage: https://geteduroam.org/
builds:
- geteduroam-cli
- geteduroam-notifcheck
formats:
- deb
- rpm
Expand All @@ -32,6 +40,10 @@ nfpms:
release: 1
description: |-
Geteduroam CLI client for Linux distributions.
contents:
- src: systemd/user/
dst: /etc/systemd/user/
type: tree

overrides:
deb:
Expand All @@ -48,6 +60,7 @@ nfpms:
homepage: https://geteduroam.org/
builds:
- geteduroam-gui
- geteduroam-notifcheck
formats:
- deb
- rpm
Expand All @@ -60,6 +73,9 @@ nfpms:
- src: cmd/geteduroam-gui/resources/share/
dst: /usr/share
type: tree
- src: systemd/user/
dst: /etc/systemd/user/
type: tree

overrides:
deb:
Expand Down
4 changes: 3 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"os"
"path/filepath"
"time"

"golang.org/x/exp/slog"

Expand All @@ -13,7 +14,8 @@ import (

// Config is the main structure for the configuration
type Config struct {
UUID string `json:"uuid"`
UUID string `json:"uuid"`
Validity *time.Time `json:"validity,omitempty"`
}

// Versioned contains the actual config data prefixed with a version field when marshalled as JSON
Expand Down
3 changes: 2 additions & 1 deletion internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ func (h Handlers) Configure(eap []byte) (*time.Time, error) {
}
// save the config with the uuid
nc := config.Config{
UUID: uuid,
UUID: uuid,
Validity: valid,
}
err = nc.Write()
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions internal/nm/nm.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func encodeFileBytes(name string, contents []byte) ([]byte, error) {
return encodePath(p), nil
}

// previousCon gets a connection object using the previous UUID
func previousCon(pUUID string) (*connection.Connection, error) {
// PreviousCon gets a connection object using the previous UUID
func PreviousCon(pUUID string) (*connection.Connection, error) {
if pUUID == "" {
return nil, errors.New("UUID is empty")
}
Expand All @@ -53,7 +53,7 @@ func previousCon(pUUID string) (*connection.Connection, error) {
// if a previous connection was found with pUUID, it updates that one instead
// it returns the newly created or updated connection object
func createCon(pUUID string, args connection.SettingsArgs) (*connection.Connection, error) {
prev, err := previousCon(pUUID)
prev, err := PreviousCon(pUUID)
// previous connection found, update it with the new settings args
if err == nil {
return prev, prev.Update(args)
Expand Down
25 changes: 25 additions & 0 deletions internal/notification/notification.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package notification

import (
"os/exec"

"github.com/geteduroam/linux-app/internal/notification/systemd"
)

// Send sends a single notification with notify-send
func Send(msg string) error {
_, err := exec.Command("notify-send", "geteduroam", msg).Output()
return err
}

// HasDaemonSupport returns whether or not notifications can be enabled globally
func HasDaemonSupport() bool {
// currently we only support systemd
return systemd.HasDaemonSupport()
}

// EnableDaemon enables the notification using SystemD's user daemon
func EnableDameon() error {
// currently we only support systemd
return systemd.EnableDaemon()
}
50 changes: 50 additions & 0 deletions internal/notification/systemd/systemd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package systemd

import (
"os"
"os/exec"

"golang.org/x/exp/slog"
)

func hasSystemD() bool {
if _, err := os.Stat("/run/systemd/system"); !os.IsNotExist(err) {
return true
}
return false
}

func hasUnit(unit string) bool {
_, err := exec.Command("systemctl", "--user", "list-unit-files", unit).Output()
return err == nil
}

func hasUnitFiles() bool {
if !hasUnit("geteduroam-notifs.service") {
slog.Error("geteduroam-notifs.service is not installed anywhere")
return false
}
if !hasUnit("geteduroam-notifs.timer") {
slog.Error("geteduroam-notifs.timer is not installed anywhere")
return false
}
return true
}

// HasDaemonSupport returns whether or not notifications can be enabled globally
// This depends on if systemd is used and if the unit is ready to be enabled
func HasDaemonSupport() bool {
if !hasSystemD() {
return false
}
if !hasUnitFiles() {
return false
}
return true
}

// EnableDaemon enables the notification daemon using systemctl commands
func EnableDaemon() error {
_, err := exec.Command("systemctl", "--user", "enable", "--now", "geteduroam-notifs.timer").Output()
return err
}
7 changes: 7 additions & 0 deletions systemd/user/geteduroam-notifs.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[Unit]
Description=This service runs a geteduroam command that checks if the eduroam \
connection is about to expire and if so it shows a notification

[Service]
Type=oneshot
ExecStart=/usr/bin/geteduroam-notifcheck
9 changes: 9 additions & 0 deletions systemd/user/geteduroam-notifs.timer
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[Unit]
Description=Run geteduroam-notifs.service daily

[Timer]
OnCalendar=*-*-* 12:00:00
Persistent=true

[Install]
WantedBy=timers.target

0 comments on commit 526e1a6

Please sign in to comment.