Skip to content

Commit

Permalink
super basic store for workspace options that is saved on connect
Browse files Browse the repository at this point in the history
  • Loading branch information
rogchap committed Jul 28, 2020
1 parent 3544f02 commit 654fb7d
Show file tree
Hide file tree
Showing 9 changed files with 568 additions and 7 deletions.
5 changes: 1 addition & 4 deletions internal/app/output_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ func (c *outputController) TagRPC(ctx context.Context, _ *stats.RPCTagInfo) cont
}

func (c *outputController) HandleRPC(ctx context.Context, stat stats.RPCStats) {
fmt.Printf("%T: %+[1]v\n\n", stat)
statStr := ""
switch s := stat.(type) {
case *stats.Begin:
Expand All @@ -153,9 +152,7 @@ func (c *outputController) TagConn(ctx context.Context, _ *stats.ConnTagInfo) co
return ctx
}

func (c *outputController) HandleConn(ctx context.Context, stat stats.ConnStats) {
fmt.Printf("%T: %+[1]v\n\n", stat)
}
func (c *outputController) HandleConn(ctx context.Context, stat stats.ConnStats) {}

func formatMetadata(md metadata.MD) string {
keys := make([]string, 0, len(md))
Expand Down
1 change: 0 additions & 1 deletion internal/app/startup.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ var (
// Startup is the main startup of the application
func Startup() int {
core.QCoreApplication_SetApplicationName(appname)
core.QCoreApplication_SetOrganizationName("Roger Chapman")
core.QCoreApplication_SetAttribute(core.Qt__AA_EnableHighDpiScaling, true)

app := gui.NewQGuiApplication(len(os.Args), os.Args)
Expand Down
45 changes: 45 additions & 0 deletions internal/app/workspace_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/therecipe/qt/core"
"google.golang.org/grpc"

"rogchap.com/wombat/internal/db"
"rogchap.com/wombat/internal/model"
)

Expand All @@ -23,6 +24,7 @@ type workspaceController struct {

grpcConn *grpc.ClientConn
cancelCtxFunc context.CancelFunc
store *db.Store

_ func() `constructor:"init"`

Expand Down Expand Up @@ -50,6 +52,33 @@ func (c *workspaceController) init() {
c.ConnectProcessProtos(c.processProtos)
c.ConnectConnect(c.connect)
c.ConnectSend(c.send)

dbPath := core.QStandardPaths_WritableLocation(core.QStandardPaths__AppDataLocation)
if isDebug {
dbPath = filepath.Join(".", ".data")
}
c.store = db.NewStore(dbPath)

go func() {
w := c.store.Get()
if w == nil {
return
}

opts := c.Options()
MainThread.Run(func() {
opts.SetReflect(w.Reflect)
opts.SetInsecure(w.Insecure)
opts.SetPlaintext(w.Plaintext)
opts.SetRootca(w.Rootca)
opts.SetClientcert(w.Clientcert)
opts.SetClientkey(w.Clientkey)
opts.ProtoListModel().SetStringList(w.ProtoFiles)
opts.ImportListModel().SetStringList(w.ImportFiles)
c.ProcessProtos()
c.connect(w.Addr)
})
}()
}

func (c *workspaceController) findProtoFiles(path string) {
Expand Down Expand Up @@ -127,6 +156,22 @@ func (c *workspaceController) connect(addr string) error {
}()

c.Options().SetAddr(addr)

go func() {
opts := c.Options()
w := &db.Workspace{
Addr: addr,
Reflect: opts.IsReflect(),
Insecure: opts.IsInsecure(),
Plaintext: opts.IsPlaintext(),
Rootca: opts.Rootca(),
Clientcert: opts.Clientcert(),
Clientkey: opts.Clientkey(),
ProtoFiles: opts.ProtoListModel().StringList(),
ImportFiles: opts.ImportListModel().StringList(),
}
c.store.Put(w)
}()
return nil
}

Expand Down
5 changes: 5 additions & 0 deletions internal/db/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright 2020 Rogchap. All Rights Reserved.

package db

//go:generate protoc --go_out=:. workspace.proto
226 changes: 226 additions & 0 deletions internal/db/rogchap.com/wombat/internal/db/workspace.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions internal/db/store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2020 Rogchap. All Rights Reserved.

package db

import (
"io/ioutil"
"os"
"path/filepath"

"google.golang.org/protobuf/proto"
)

type Store struct {
dbPath string
}

// NewStore is a poor mans DB, should replace with domething like BadgerDB
func NewStore(path string) *Store {
if _, err := os.Stat(path); os.IsNotExist(err) {
os.MkdirAll(path, 0700)
}
dbPath := filepath.Join(path, "workspace.db")
return &Store{dbPath}
}

func (s *Store) Get() *Workspace {
raw, err := ioutil.ReadFile(s.dbPath)
if err != nil {
println(err.Error())
return nil
}
w := &Workspace{}
if err := proto.Unmarshal(raw, w); err != nil {
println(err.Error())
return nil
}
return w
}

func (s *Store) Put(w *Workspace) {
raw, err := proto.Marshal(w)
if err != nil {
println(err.Error())
return
}
if err := ioutil.WriteFile(s.dbPath, raw, 0644); err != nil {
println(err.Error())
}
}
Loading

0 comments on commit 654fb7d

Please sign in to comment.