Add basic desktop UI - systray

This commit is contained in:
braginini
2022-03-08 16:14:00 +01:00
parent 347a668bd5
commit 9c3cd1a5db
4 changed files with 166 additions and 0 deletions

96
client/ui/client_ui.go Normal file
View File

@@ -0,0 +1,96 @@
package main
import (
"context"
"fmt"
"io/ioutil"
"time"
"github.com/getlantern/systray"
log "github.com/sirupsen/logrus"
"github.com/skratchdot/open-golang/open"
"github.com/wiretrustee/wiretrustee/client/internal"
"github.com/wiretrustee/wiretrustee/client/proto"
"github.com/wiretrustee/wiretrustee/client/ui/config"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func main() {
systray.Run(onReady, nil)
}
// TODO: implementation for SSO Logins
func onReady() {
wtIcon, err := ioutil.ReadFile("wiretrustee.ico")
if err != nil {
log.Warn(err)
}
if wtIcon != nil {
systray.SetTemplateIcon(wtIcon, wtIcon)
}
go func() {
up := systray.AddMenuItem("Up", "Up")
down := systray.AddMenuItem("Down", "Down")
mUrl := systray.AddMenuItem("Open UI", "wiretrustee website")
systray.AddSeparator()
mQuitOrig := systray.AddMenuItem("Quit", "Quit the whole app")
go func() {
<-mQuitOrig.ClickedCh
fmt.Println("Requesting quit")
systray.Quit()
fmt.Println("Finished quitting")
}()
for {
select {
case <-mUrl.ClickedCh:
open.Run("https://app.wiretrustee.com")
case <-up.ClickedCh:
upCmdExec()
case <-down.ClickedCh:
fmt.Println("Clicked down")
}
}
}()
}
func handleUp() {
// This is where
}
func upCmdExec() {
log.Println("executing up command..")
cfg := config.Config()
ctx := internal.CtxInitState(context.Background())
conn, err := grpc.DialContext(ctx, cfg.DaemonAddr(),
grpc.WithTimeout(5*time.Second),
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithBlock())
if err != nil {
log.Errorf("failed to connect to service CLI interface; %v", err)
return
}
daemonClient := proto.NewDaemonServiceClient(conn)
status, err := daemonClient.Status(ctx, &proto.StatusRequest{})
if err != nil {
log.Errorf("get status: %v", err)
return
}
if status.Status != string(internal.StatusIdle) {
log.Warnf("already connected")
return
}
_, err = daemonClient.Up(ctx, &proto.UpRequest{})
if err != nil {
log.Errorf("Failed to start up client; %v", err)
return
}
}

View File

@@ -0,0 +1,41 @@
package config
import (
"os"
"runtime"
)
type ClientConfig struct {
configPath string
logFile string
daemonAddr string
}
// We are creating this package to extract utility functions from the cmd package
// reading and parsing the configurations for the client should be done here
func Config() *ClientConfig {
defaultConfigPath := "/etc/wiretrustee/config.json"
defaultLogFile := "/var/log/wiretrustee/client.log"
if runtime.GOOS == "windows" {
defaultConfigPath = os.Getenv("PROGRAMDATA") + "\\Wiretrustee\\" + "config.json"
defaultLogFile = os.Getenv("PROGRAMDATA") + "\\Wiretrustee\\" + "client.log"
}
defaultDaemonAddr := "unix:///var/run/wiretrustee.sock"
if runtime.GOOS == "windows" {
defaultDaemonAddr = "tcp://127.0.0.1:41731"
}
return &ClientConfig{
configPath: defaultConfigPath,
logFile: defaultLogFile,
daemonAddr: defaultDaemonAddr,
}
}
func (c *ClientConfig) DaemonAddr() string {
return c.daemonAddr
}
func (c *ClientConfig) LogFile() string {
return c.logFile
}