Skip to content
This repository has been archived by the owner on May 22, 2023. It is now read-only.

Commit

Permalink
added link subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
Derric Williams committed May 9, 2020
1 parent 5d9ffe3 commit e0c56ca
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
8 changes: 7 additions & 1 deletion cmd/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"

"github.com/derricw/siggo/signal"
"github.com/spf13/cobra"
)

Expand All @@ -13,8 +14,13 @@ func init() {
var linkCmd = &cobra.Command{
Use: "link",
Short: "link a device",
Long: ``,
Long: `Generates a QR code that can be scanned by an existing signal device to link to.
Example:
$ siggo link work_laptop`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("linking...")
sig := signal.NewSignal(User)
sig.Link(args[0])
},
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.13

require (
github.com/gdamore/tcell v1.3.0
github.com/pgavlin/femto v0.0.0-20191028012355-31a9964a50b5
github.com/mdp/qrterminal/v3 v3.0.0
github.com/rivo/tview v0.0.0-20200329194346-7cc182c5846e
github.com/sirupsen/logrus v1.2.0
github.com/spf13/cobra v0.0.7
Expand Down
3 changes: 1 addition & 2 deletions model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ type Message struct {

func (m *Message) String() string {
var fromStr = m.From
fromLen := len(m.From)
template := "%s|%s%s| %" + fmt.Sprintf("%dv", fromLen) + ": %s\n"
template := "%s|%s%s| %" + fmt.Sprintf("%dv", len(fromStr)) + ": %s\n"
data := fmt.Sprintf(template,
// lets come up with a way to avoid the *1000000
// Magical Ref Data: Mon Jan 2 15:04:05 MST 2006
Expand Down
1 change: 0 additions & 1 deletion signal/link.go

This file was deleted.

26 changes: 22 additions & 4 deletions signal/signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"os/user"
"strings"
"syscall"
"time"

qr "github.com/mdp/qrterminal/v3"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -95,10 +97,6 @@ func (s *Signal) Exec(args ...string) ([]byte, error) {
return out.Bytes(), nil
}

// Link a device
// not implemented but wouldn't it be cool
func (s *Signal) Link() {}

// Version returns the current version of signal-cli
func (s *Signal) Version() (string, error) {
b, err := s.Exec("-v")
Expand Down Expand Up @@ -200,6 +198,26 @@ func (s *Signal) SendDbus(dest, msg string) error {
return nil
}

func (s *Signal) Link(deviceName string) error {
cmd := exec.Command("signal-cli", "link", "-n", deviceName)
out, err := cmd.StdoutPipe()
if err != nil {
return err
}
err = cmd.Start()
if err != nil {
return err
}
r := bufio.NewReader(out)
line, _, err := r.ReadLine()
if err != nil {
return err
}
fmt.Printf("link text: %s\n", line)
qr.Generate(fmt.Sprintf("%s", line), qr.L, os.Stdout)
return cmd.Wait()
}

func (s *Signal) GetUserData() (*SignalUserData, error) {
usr, err := user.Current()
if err != nil {
Expand Down

0 comments on commit e0c56ca

Please sign in to comment.