Skip to content

Commit

Permalink
minor ipfs client cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
treethought committed Sep 11, 2021
1 parent f5d62bc commit 7d48b0f
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 27 deletions.
20 changes: 16 additions & 4 deletions ipfs/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@ import (
"io/ioutil"

api "github.com/ipfs/go-ipfs-api"
ipld "github.com/ipfs/go-ipld-format"
"gopkg.in/yaml.v2"
)

type DagData struct {
Data string
Links []ipld.Link
}

type Client struct {
nodeURL string
sh *api.Shell
}

func NewClient(url string) *Client {

c := &Client{
nodeURL: url,
sh: api.NewLocalShell(),
Expand All @@ -23,6 +30,7 @@ func NewClient(url string) *Client {
}

func (c *Client) ReadFile(path string, entry *api.MfsLsEntry) ([]byte, error) {

if entry.Type == api.TDirectory {
return []byte("directory"), nil
}
Expand All @@ -33,10 +41,14 @@ func (c *Client) ReadFile(path string, entry *api.MfsLsEntry) ([]byte, error) {
return ioutil.ReadAll(r)
}

func (c *Client) GetDag(ref string) (out map[string]interface{}, err error) {
out = make(map[string]interface{})
err = c.sh.DagGet(ref, &out)
return out, err
func (c *Client) GetDag(ref string) (dag *DagData, err error) {
dag = &DagData{}

err = c.sh.DagGet(ref, dag)
if err != nil {
return nil, err
}
return dag, nil
}

func (c *Client) ListFiles(path string) (entries []*api.MfsLsEntry, err error) {
Expand Down
12 changes: 6 additions & 6 deletions ui/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@ import (
md "github.com/JohannesKaufmann/html-to-markdown"
"github.com/eliukblau/pixterm/pkg/ansimage"
"github.com/gdamore/tcell/v2"

api "github.com/ipfs/go-ipfs-api"
)

type Content struct {
*cview.TextView

app *App
entry *api.MfsLsEntry
app *App
}

func NewContentView(app *App) *Content {
Expand All @@ -41,9 +38,8 @@ func (c *Content) Update() {
current := c.app.state.currentFile
path, entry := current.path, current.entry
c.Clear()
// go c.app.ui.QueueUpdateDraw(func() {

data, err := c.app.client.ReadFile(path, entry)
data, err := c.app.ipfs.ReadFile(path, entry)
if err != nil {
panic(err)
}
Expand All @@ -52,8 +48,12 @@ func (c *Content) Update() {
c.SetText(err.Error())
}

c.SetTextAlign(cview.AlignLeft)

switch contentType {
case "image/png", "image/jpeg":

c.SetTextAlign(cview.AlignCenter)
c.SetDynamicColors(true)
_, _, w, h := c.GetRect()
r := bytes.NewReader(data)
Expand Down
9 changes: 1 addition & 8 deletions ui/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ import (
ipld "github.com/ipfs/go-ipld-format"
)

type DagData struct {
Data string
Links []ipld.Link
}

type DagInfo struct {
*cview.TreeView
app *App
Expand Down Expand Up @@ -100,9 +95,7 @@ func (i *DagInfo) Update() {

go i.app.ui.QueueUpdateDraw(func() {

dag := &DagData{}

err := i.app.ipfs.DagGet(i.currentHash, &dag)
dag, err := i.app.ipfs.GetDag(i.currentHash)
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions ui/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (r *RepoTree) buildNodes(basePath string, entries ...*api.MfsLsEntry) []*cv

if i.Type == api.TDirectory {

children, err := r.app.client.ListFiles(ref.path)
children, err := r.app.ipfs.ListFiles(ref.path)
if err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down Expand Up @@ -73,7 +73,7 @@ func NewRepoTree(app *App) *RepoTree {
m.inputHandler = cbind.NewConfiguration()
m.initBindings()

entries, err := app.client.ListFiles("/")
entries, err := app.ipfs.ListFiles("/")
if err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion ui/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (i *FileInfo) Update() {
i.Clear()

go i.app.ui.QueueUpdateDraw(func() {
stat, err := i.app.client.StatFile(current.path, current.entry)
stat, err := i.app.ipfs.StatFile(current.path, current.entry)
if err != nil {
i.SetText(fmt.Sprintf("%s\n%v", current.path, err))
return
Expand Down
2 changes: 1 addition & 1 deletion ui/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewPeerList(app *App) *PeerList {
m.inputHandler = cbind.NewConfiguration()
m.initBindings()

peers, err := app.client.GetPeers()
peers, err := app.ipfs.GetPeers()
if err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
7 changes: 2 additions & 5 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ import (
"code.rocketnine.space/tslocum/cbind"
"code.rocketnine.space/tslocum/cview"
"github.com/gdamore/tcell/v2"
api "github.com/ipfs/go-ipfs-api"
"github.com/treethought/tipfs/ipfs"
)

type App struct {
ipfs *api.Shell
client *ipfs.Client
ipfs *ipfs.Client
ui *cview.Application
root *cview.Flex
dataPanels *cview.TabbedPanels
Expand Down Expand Up @@ -46,7 +44,7 @@ func (s *State) SetItem(e TreeEntry) {

func New() *App {
app := &App{
ipfs: api.NewLocalShell(),
ipfs: ipfs.NewClient(""),
widgets: make([]Widget, 0),
}

Expand Down Expand Up @@ -133,7 +131,6 @@ func (app *App) initInputHandler(widgets ...cview.Primitive) {
}

func (app *App) Start() {
app.client = ipfs.NewClient("localhost:5001")

// Initialize application
app.ui = cview.NewApplication()
Expand Down

0 comments on commit 7d48b0f

Please sign in to comment.