Skip to content

Commit

Permalink
chore: use rofi pkg to craft the menu
Browse files Browse the repository at this point in the history
  • Loading branch information
marianozunino committed Jul 18, 2024
1 parent 1fa56ed commit c54219c
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 42 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/marianozunino/sdm-ui
go 1.21.6

require (
git.sr.ht/~jcmuller/go-rofi v0.2.0
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966
github.com/stretchr/testify v1.8.4
github.com/zalando/go-keyring v0.2.3
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
git.sr.ht/~jcmuller/go-rofi v0.2.0 h1:tVJHqll6nTjshJ8CwtFQyZv+bqk9Wq1s0obJmSfDRqc=
git.sr.ht/~jcmuller/go-rofi v0.2.0/go.mod h1:nsHQ9DuQzpVqtjXbNL8TY7p4cF1+L1XEZGAg4yhf+iE=
github.com/akavel/rsrc v0.10.2 h1:Zxm8V5eI1hW4gGaYsJQUhxpjkENuG91ki8B4zCrvEsw=
github.com/akavel/rsrc v0.10.2/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c=
github.com/alessio/shellescape v1.4.1 h1:V7yhSDDn8LP4lc4jS8pFkt0zCnzVJlG5JXy9BVKJUX0=
Expand Down
17 changes: 16 additions & 1 deletion internal/program/list.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
package program

import "io"
import (
"fmt"
"io"
)

func (p *Program) executeList(w io.Writer) error {
dataSources, err := p.db.RetrieveDatasources(p.account)
if err != nil {
return err
}

if len(dataSources) == 0 {
fmt.Printf("[list] No data sources found, syncing...\n")
if err := p.executeSync(); err != nil {
return err
}

dataSources, err = p.db.RetrieveDatasources(p.account)
if err != nil {
return err
}
}

printDataSources(dataSources, w)
return nil
}
17 changes: 8 additions & 9 deletions internal/program/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,7 @@ func (p *Program) Run() error {
case commandList:
return p.executeList(os.Stdout)
case commandRofi:
if err := p.executeRofi(args); err != nil {
return err
}
return p.executeSync()
return p.executeRofi(args)
default:
return fmt.Errorf("invalid command: '%s'", command)
}
Expand Down Expand Up @@ -151,11 +148,13 @@ func (p *Program) retryCommand(command func() error) error {
switch sdmErr.Code {
case sdm.Unauthorized:
notify.Notify("SDM CLI", "Authenticating... 🔐", "", "")
return p.retryCommand(func() error {
p.sdmWrapper.Login(p.account, p.password)
return command()
})

err := p.sdmWrapper.Login(p.account, p.password)
if err != nil {
p.keyring.DeleteSecret(p.account)
notify.Notify("SDM CLI", "Authentication error 🔐", err.Error(), "")
return err
}
return command()
case sdm.InvalidCredentials:
notify.Notify("SDM CLI", "Authentication error 🔐", "Invalid credentials", "")
p.keyring.DeleteSecret(p.account)
Expand Down
107 changes: 75 additions & 32 deletions internal/program/rofi.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package program

import (
"bytes"
"context"
"fmt"
"os/exec"
"regexp"
"log"
"strings"

"git.sr.ht/~jcmuller/go-rofi/dmenu"
"git.sr.ht/~jcmuller/go-rofi/entry"
"github.com/marianozunino/sdm-ui/internal/storage"
"github.com/martinlindhe/notify"
"github.com/skratchdot/open-golang/open"
"github.com/zyedidia/clipper"
Expand All @@ -20,46 +23,86 @@ func (p *Program) executeRofi(args []string) error {
return err
}

cmd := exec.Command("rofi", "-dmenu", "-i", "-p", "Select Data Source")
cmd.Stdin = bytesOut
rofiOut, err := cmd.Output()
entries := p.createEntriesFromBuffer(bytesOut)
selectedEntry, err := p.getSelectionFromDmenu(entries)
if err != nil {
fmt.Println("[rofi] Failed to execute rofi")
return err
}

return p.handleSelectedEntry(selectedEntry)
}

func (p *Program) createEntriesFromBuffer(buf *bytes.Buffer) []*entry.Entry {
var entries []*entry.Entry
for _, line := range bytes.Split(buf.Bytes(), []byte("\n")) {
entries = append(entries, entry.New(string(line)))
}
return entries
}

func (p *Program) getSelectionFromDmenu(entries []*entry.Entry) (string, error) {
d := dmenu.New(
dmenu.WithPrompt("Select Data Source"),
dmenu.WithEntries(entries...),
)

ctx := context.Background()
s, err := d.Select(ctx)
if err != nil {
log.Printf("[rofi] Selection error: %v", err)
return "", err
}

fmt.Printf("[rofi] Output: %s\n", s)
return s, nil
}

func (p *Program) handleSelectedEntry(selectedEntry string) error {
fields := strings.Fields(selectedEntry)

if len(fields) < 2 {
notify.Notify("SDM CLI", "Resource not found 🔐", "", "")
return nil
}
rofiOut = regexp.MustCompile(`\s+`).ReplaceAll(rofiOut, []byte(" "))

fmt.Printf("[rofi] Output: %s\n", rofiOut)
selectedDS := fields[0]
fmt.Printf("[rofi] DataSource: %s\n", selectedDS)

fmt.Printf("[rofi] Selected: %s\n", rofiOut)
dataSource := strings.Split(string(rofiOut), " ")[0]
fmt.Printf("[rofi] DataSource: %s\n", dataSource)
dataSourcePort := strings.Split(string(rofiOut), " ")[1]
fmt.Printf("[rofi] DataSourcePort: %s\n", dataSourcePort)
if selectedDS == "" {
notify.Notify("SDM CLI", "Resource not found 🔐", "", "")
return nil
}

if dataSource != "" {
if err := p.retryCommand(func() error {
return p.sdmWrapper.Connect(dataSource)
}); err != nil {
return err
}
ds, err := p.db.GetDatasource(p.account, selectedDS)
if err != nil {
notify.Notify("SDM CLI", "Resource not found 🔐", "", "")
return nil
}

title := "Data Source Connected 🔌"
message := fmt.Sprintf(dataSource)
message += fmt.Sprintf("\n📋 <b>%s</b>", dataSourcePort)
if err := p.retryCommand(func() error {
return p.sdmWrapper.Connect(ds.Name)
}); err != nil {
return err
}

p.notifyDataSourceConnected(ds)
return p.executeSync()
}

if strings.HasPrefix(dataSourcePort, "http") {
open.Start(dataSourcePort)
func (p *Program) notifyDataSourceConnected(ds storage.DataSource) {
title := "Data Source Connected 🔌"
message := fmt.Sprintf("%s\n📋 <b>%s</b>", ds.Name, ds.Address)

if strings.HasPrefix(ds.Address, "http") {
open.Start(ds.Address)
} else {
if clip, err := clipper.GetClipboard(clipper.Clipboards...); err != nil {
printDebug("[clipper] Failed to get clipboard: " + err.Error())
} else {
if clip, err := clipper.GetClipboard(clipper.Clipboards...); err != nil {
printDebug("[clipper] Failed to get clipboard: " + err.Error())
} else {
clip.WriteAll(clipper.RegClipboard, []byte(dataSourcePort))
}
clip.WriteAll(clipper.RegClipboard, []byte(ds.Address))
}

notify.Notify("SDM CLI", title, message, "")
}

return nil
notify.Notify("SDM CLI", title, message, "")
}

11 changes: 11 additions & 0 deletions internal/storage/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ func (s *Storage) RetrieveDatasources(account string) ([]DataSource, error) {
return datasources, err
}

func (s *Storage) GetDatasource(account string, name string) (DataSource, error) {
datasource := DataSource{}
err := s.View(func(tx *bolt.Tx) error {
b := tx.Bucket(buildBucketKey(account))
v := b.Get([]byte(name))
datasource.Decode(v)
return nil
})
return datasource, err
}

func (s *Storage) Close() error {
return s.DB.Close()
}

0 comments on commit c54219c

Please sign in to comment.