diff --git a/cache/cache.go b/cache/cache.go index 22f9a02..ebbf0ef 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -1,22 +1,22 @@ package cache import ( - "encoding/json" + "encoding/json" - "github.com/mrusme/superhighway84/models" - "github.com/tidwall/buntdb" + "github.com/mrusme/superhighway84/models" + "github.com/tidwall/buntdb" ) type Cache struct { db *buntdb.DB - dbPath string + dbPath string } func NewCache(dbPath string) (*Cache, error) { var err error cache := new(Cache) - cache.dbPath = dbPath + cache.dbPath = dbPath cache.db, err = buntdb.Open(cache.dbPath) if err != nil { return nil, err diff --git a/common/player-psfm.go b/common/player-psfm.go index e10ac45..cbe5f0d 100644 --- a/common/player-psfm.go +++ b/common/player-psfm.go @@ -3,7 +3,7 @@ package common import( - "github.com/mrusme/go-poolsuite" + "github.com/mrusme/go-poolsuite" ) type Player struct { diff --git a/config/config.go b/config/config.go index f275e71..8eb5ce3 100644 --- a/config/config.go +++ b/config/config.go @@ -1,17 +1,17 @@ package config import ( - "bytes" - "errors" - "fmt" - "io/ioutil" - "os" - "path/filepath" - "strconv" - "strings" - - "github.com/BurntSushi/toml" - "github.com/gdamore/tcell/v2" + "bytes" + "errors" + "fmt" + "io/ioutil" + "os" + "path/filepath" + "strconv" + "strings" + + "github.com/BurntSushi/toml" + "github.com/gdamore/tcell/v2" ) type ConfigProfile struct { @@ -37,7 +37,7 @@ type Config struct { ConnectionString string CachePath string // Deprecated, should be removed soon - DatabaseCachePath string + DatabaseCachePath string ProgramCachePath string Logfile string @@ -153,10 +153,10 @@ func (cfg *Config) Persist() (error) { func (cfg *Config) WasSetup() (bool) { if cfg.DatabaseCachePath == "" || - cfg.ProgramCachePath == "" || + cfg.ProgramCachePath == "" || cfg.ConnectionString == "" || cfg.Logfile == "" || - cfg.Profile.From == "" { + cfg.Profile.From == "" { return false } @@ -167,9 +167,9 @@ func (cfg *Config) Setup() (error) { fmt.Printf("\nSUPERHIGHWAY84\n\nInitial Setup\n-------------\n\n") defaultConnectionString := "/orbitdb/bafyreifdpagppa7ve45odxuvudz5snbzcybwyfer777huckl4li4zbc5k4/superhighway84" - if cfg.ConnectionString != "" { - defaultConnectionString = cfg.ConnectionString - } + if cfg.ConnectionString != "" { + defaultConnectionString = cfg.ConnectionString + } fmt.Printf("Database connection string [%s]: ", defaultConnectionString) fmt.Scanln(&cfg.ConnectionString) if strings.TrimSpace(cfg.ConnectionString) == "" { @@ -182,10 +182,10 @@ func (cfg *Config) Setup() (error) { } defaultDatabaseCachePath := filepath.Join(cacheDir, "superhighway84", "database") - // Migration step from old CachePath to new DatabaseCachePath - if cfg.CachePath != "" { - defaultDatabaseCachePath = cfg.CachePath - } + // Migration step from old CachePath to new DatabaseCachePath + if cfg.CachePath != "" { + defaultDatabaseCachePath = cfg.CachePath + } fmt.Printf("Database cache path [%s]: ", defaultDatabaseCachePath) fmt.Scanln(&cfg.DatabaseCachePath) if strings.TrimSpace(cfg.DatabaseCachePath) == "" { @@ -194,12 +194,12 @@ func (cfg *Config) Setup() (error) { os.MkdirAll(filepath.Dir(cfg.DatabaseCachePath), 0755) defaultProgramCachePath := filepath.Join(cacheDir, "superhighway84", "program") - // Migration step from old CachePath to new DatabaseCachePath - if cfg.CachePath != "" { - // If the previous CachePath was used, the folder already contains the - // OrbitDB, hence we need to find a different place - defaultProgramCachePath = filepath.Join(cacheDir, "superhighway84.program") - } + // Migration step from old CachePath to new DatabaseCachePath + if cfg.CachePath != "" { + // If the previous CachePath was used, the folder already contains the + // OrbitDB, hence we need to find a different place + defaultProgramCachePath = filepath.Join(cacheDir, "superhighway84.program") + } fmt.Printf("Program cache path [%s]: ", defaultProgramCachePath) fmt.Scanln(&cfg.ProgramCachePath) if strings.TrimSpace(cfg.ProgramCachePath) == "" { @@ -208,9 +208,9 @@ func (cfg *Config) Setup() (error) { os.MkdirAll(filepath.Dir(cfg.ProgramCachePath), 0755) defaultLogfile := filepath.Join(cacheDir, "superhighway84.log") - if cfg.Logfile != "" { - defaultLogfile = cfg.Logfile - } + if cfg.Logfile != "" { + defaultLogfile = cfg.Logfile + } fmt.Printf("Logfile path [%s]: ", defaultLogfile) fmt.Scanln(&cfg.Logfile) if strings.TrimSpace(cfg.Logfile) == "" { @@ -221,19 +221,19 @@ func (cfg *Config) Setup() (error) { fmt.Printf("\nProfile information\n-------------------\n\n") defaultProfileFrom := fmt.Sprintf("%s@localhost", os.Getenv("USER")) - if cfg.Profile.From != "" { - defaultProfileFrom = cfg.Profile.From - } + if cfg.Profile.From != "" { + defaultProfileFrom = cfg.Profile.From + } fmt.Printf("From [%s]: ", defaultProfileFrom) fmt.Scanln(&cfg.Profile.From) if strings.TrimSpace(cfg.Profile.From) == "" { cfg.Profile.From = defaultProfileFrom } - defaultProfileOrganization := "" - if cfg.Profile.Organization != "" { - defaultProfileOrganization = cfg.Profile.Organization - } + defaultProfileOrganization := "" + if cfg.Profile.Organization != "" { + defaultProfileOrganization = cfg.Profile.Organization + } fmt.Printf("Organization [%s]: ", defaultProfileOrganization) fmt.Scanln(&cfg.Profile.Organization) diff --git a/database/database.go b/database/database.go index a49c345..b234cf7 100644 --- a/database/database.go +++ b/database/database.go @@ -1,26 +1,26 @@ package database import ( - "context" - "sort" - "sync" - - orbitdb "berty.tech/go-orbit-db" - "berty.tech/go-orbit-db/accesscontroller" - "berty.tech/go-orbit-db/events" - "berty.tech/go-orbit-db/iface" - "berty.tech/go-orbit-db/stores" - "berty.tech/go-orbit-db/stores/documentstore" - config "github.com/ipfs/go-ipfs-config" - "github.com/ipfs/go-ipfs/core" - icore "github.com/ipfs/interface-go-ipfs-core" - "github.com/libp2p/go-libp2p-core/crypto" - "github.com/libp2p/go-libp2p-core/peer" - "github.com/mitchellh/mapstructure" - "go.uber.org/zap" - - "github.com/mrusme/superhighway84/cache" - "github.com/mrusme/superhighway84/models" + "context" + "sort" + "sync" + + orbitdb "berty.tech/go-orbit-db" + "berty.tech/go-orbit-db/accesscontroller" + "berty.tech/go-orbit-db/events" + "berty.tech/go-orbit-db/iface" + "berty.tech/go-orbit-db/stores" + "berty.tech/go-orbit-db/stores/documentstore" + config "github.com/ipfs/go-ipfs-config" + "github.com/ipfs/go-ipfs/core" + icore "github.com/ipfs/interface-go-ipfs-core" + "github.com/libp2p/go-libp2p-core/crypto" + "github.com/libp2p/go-libp2p-core/peer" + "github.com/mitchellh/mapstructure" + "go.uber.org/zap" + + "github.com/mrusme/superhighway84/cache" + "github.com/mrusme/superhighway84/models" ) type Database struct { @@ -97,27 +97,27 @@ func(db *Database) GetOwnPubKey() crypto.PubKey { } func(db *Database) connectToPeers() error { - var wg sync.WaitGroup + var wg sync.WaitGroup peerInfos, err := config.DefaultBootstrapPeers() if err != nil { return err } - wg.Add(len(peerInfos)) - for _, peerInfo := range peerInfos { - go func(peerInfo *peer.AddrInfo) { - defer wg.Done() - err := db.IPFSCoreAPI.Swarm().Connect(db.ctx, *peerInfo) - if err != nil { + wg.Add(len(peerInfos)) + for _, peerInfo := range peerInfos { + go func(peerInfo *peer.AddrInfo) { + defer wg.Done() + err := db.IPFSCoreAPI.Swarm().Connect(db.ctx, *peerInfo) + if err != nil { db.Logger.Debug("failed to connect", zap.String("peerID", peerInfo.ID.String()), zap.Error(err)) - } else { + } else { db.Logger.Debug("connected!", zap.String("peerID", peerInfo.ID.String())) } - }(&peerInfo) - } - wg.Wait() - return nil + }(&peerInfo) + } + wg.Wait() + return nil } func NewDatabase( @@ -142,8 +142,8 @@ func NewDatabase( } if err := setupPlugins(defaultPath); err != nil { - return nil, err - } + return nil, err + } db.IPFSNode, db.IPFSCoreAPI, err = createNode(ctx, defaultPath) if err != nil { @@ -168,14 +168,14 @@ func (db *Database) Connect(onReady func(address string)) (error) { // } // } - // go func() { - err = db.connectToPeers() - if err != nil { + // go func() { + err = db.connectToPeers() + if err != nil { db.Logger.Debug("failed to connect: %s", zap.Error(err)) } else { db.Logger.Debug("connected to peer!") } - // }() + // }() // log.Println(db.Store.ReplicationStatus().GetBuffered()) // log.Println(db.Store.ReplicationStatus().GetQueued()) diff --git a/database/helpers.go b/database/helpers.go index f22c281..ddb0103 100644 --- a/database/helpers.go +++ b/database/helpers.go @@ -1,57 +1,57 @@ package database import ( - "context" - "fmt" - "os" - "path/filepath" - - files "github.com/ipfs/go-ipfs-files" - "github.com/ipfs/go-ipfs/core" - "github.com/ipfs/go-ipfs/core/coreapi" - "github.com/ipfs/go-ipfs/core/node/libp2p" - "github.com/ipfs/go-ipfs/plugin/loader" - "github.com/ipfs/go-ipfs/repo/fsrepo" - icore "github.com/ipfs/interface-go-ipfs-core" - "github.com/mitchellh/mapstructure" + "context" + "fmt" + "os" + "path/filepath" + + files "github.com/ipfs/go-ipfs-files" + "github.com/ipfs/go-ipfs/core" + "github.com/ipfs/go-ipfs/core/coreapi" + "github.com/ipfs/go-ipfs/core/node/libp2p" + "github.com/ipfs/go-ipfs/plugin/loader" + "github.com/ipfs/go-ipfs/repo/fsrepo" + icore "github.com/ipfs/interface-go-ipfs-core" + "github.com/mitchellh/mapstructure" ) func setupPlugins(path string) error { - plugins, err := loader.NewPluginLoader(filepath.Join(path, "plugins")) - if err != nil { - return fmt.Errorf("error loading plugins: %s", err) - } + plugins, err := loader.NewPluginLoader(filepath.Join(path, "plugins")) + if err != nil { + return fmt.Errorf("error loading plugins: %s", err) + } - if err := plugins.Initialize(); err != nil { - return fmt.Errorf("error initializing plugins: %s", err) - } + if err := plugins.Initialize(); err != nil { + return fmt.Errorf("error initializing plugins: %s", err) + } - if err := plugins.Inject(); err != nil { - return fmt.Errorf("error initializing plugins: %s", err) - } + if err := plugins.Inject(); err != nil { + return fmt.Errorf("error initializing plugins: %s", err) + } - return nil + return nil } func createNode(ctx context.Context, repoPath string) (*core.IpfsNode, icore.CoreAPI, error) { - repo, err := fsrepo.Open(repoPath) - if err != nil { - return nil, nil, err - } - - nodeOptions := &core.BuildCfg{ - Online: true, - Routing: libp2p.DHTClientOption, // DHTOption - Repo: repo, + repo, err := fsrepo.Open(repoPath) + if err != nil { + return nil, nil, err + } + + nodeOptions := &core.BuildCfg{ + Online: true, + Routing: libp2p.DHTClientOption, // DHTOption + Repo: repo, ExtraOpts: map[string]bool{ "pubsub": true, }, - } + } - node, err := core.NewNode(ctx, nodeOptions) - if err != nil { - return nil, nil, err - } + node, err := core.NewNode(ctx, nodeOptions) + if err != nil { + return nil, nil, err + } coreAPI, err := coreapi.NewCoreAPI(node) if err != nil { @@ -62,17 +62,17 @@ func createNode(ctx context.Context, repoPath string) (*core.IpfsNode, icore.Cor } func getUnixfsNode(path string) (files.Node, error) { - st, err := os.Stat(path) - if err != nil { - return nil, err - } + st, err := os.Stat(path) + if err != nil { + return nil, err + } - f, err := files.NewSerialFile(path, false, st) - if err != nil { - return nil, err - } + f, err := files.NewSerialFile(path, false, st) + if err != nil { + return nil, err + } - return f, nil + return f, nil } func structToMap(v interface{}) (map[string]interface{}, error) { diff --git a/models/article.go b/models/article.go index 97b45b8..c7052c2 100644 --- a/models/article.go +++ b/models/article.go @@ -1,10 +1,10 @@ package models import ( - "time" + "time" - "github.com/go-playground/validator/v10" - "github.com/google/uuid" + "github.com/go-playground/validator/v10" + "github.com/google/uuid" ) type Article struct { diff --git a/superhighway84.go b/superhighway84.go index 6728d43..b69e523 100644 --- a/superhighway84.go +++ b/superhighway84.go @@ -1,24 +1,24 @@ package main import ( - "context" - "embed" - "encoding/json" - "net/http" - "net/url" - "os" - "runtime" - "strings" - "time" - - "log" - - "github.com/mrusme/superhighway84/cache" - "github.com/mrusme/superhighway84/config" - "github.com/mrusme/superhighway84/database" - "github.com/mrusme/superhighway84/models" - "github.com/mrusme/superhighway84/tui" - "go.uber.org/zap" + "context" + "embed" + "encoding/json" + "net/http" + "net/url" + "os" + "runtime" + "strings" + "time" + + "log" + + "github.com/mrusme/superhighway84/cache" + "github.com/mrusme/superhighway84/config" + "github.com/mrusme/superhighway84/database" + "github.com/mrusme/superhighway84/models" + "github.com/mrusme/superhighway84/tui" + "go.uber.org/zap" ) //go:embed superhighway84.jpeg diff --git a/tui/helpers.go b/tui/helpers.go index e965890..806d759 100644 --- a/tui/helpers.go +++ b/tui/helpers.go @@ -1,16 +1,16 @@ package tui import ( - "errors" - "fmt" - "io/ioutil" - "log" - "os" - "os/exec" - "strings" - "time" - - "github.com/mrusme/superhighway84/models" + "errors" + "fmt" + "io/ioutil" + "log" + "os" + "os/exec" + "strings" + "time" + + "github.com/mrusme/superhighway84/models" ) func MillisecondsToDate(ms int64) (string) { diff --git a/tui/mainscreen.go b/tui/mainscreen.go index 3fbd6d1..386b7f9 100644 --- a/tui/mainscreen.go +++ b/tui/mainscreen.go @@ -1,15 +1,15 @@ package tui import ( - "fmt" - "regexp" - "sort" - "strings" - "time" - - "github.com/gdamore/tcell/v2" - "github.com/mrusme/superhighway84/models" - "github.com/rivo/tview" + "fmt" + "regexp" + "sort" + "strings" + "time" + + "github.com/gdamore/tcell/v2" + "github.com/mrusme/superhighway84/models" + "github.com/rivo/tview" ) var HEADER_LOGO = diff --git a/tui/splashscreen.go b/tui/splashscreen.go index 3ae8a52..fc33b78 100644 --- a/tui/splashscreen.go +++ b/tui/splashscreen.go @@ -1,13 +1,13 @@ package tui import ( - "bytes" - "fmt" - "image/color" + "bytes" + "fmt" + "image/color" - "github.com/eliukblau/pixterm/pkg/ansimage" - "github.com/gdamore/tcell/v2" - "github.com/rivo/tview" + "github.com/eliukblau/pixterm/pkg/ansimage" + "github.com/gdamore/tcell/v2" + "github.com/rivo/tview" ) @@ -22,9 +22,9 @@ func(t *TUI) NewSplashscreen(logo *[]byte) (*Splashscreen) { splashscreen.T = t canvas := tview.NewTextView(). - SetDynamicColors(true). - SetRegions(true). - SetWrap(true) + SetDynamicColors(true). + SetRegions(true). + SetWrap(true) canvas.SetBorder(false) canvas.Clear() @@ -54,7 +54,7 @@ func(splashscreen *Splashscreen) Refresh() { return } // splashscreen.Canvas.Clear() - fmt.Fprint(splashscreen.Canvas, tview.TranslateANSI(logoImage.RenderExt(false, false))) + fmt.Fprint(splashscreen.Canvas, tview.TranslateANSI(logoImage.RenderExt(false, false))) } func (splashscreen *Splashscreen) HandleInput(event *tcell.EventKey) (*tcell.EventKey) { diff --git a/tui/tui.go b/tui/tui.go index bb5879f..56534fc 100644 --- a/tui/tui.go +++ b/tui/tui.go @@ -1,19 +1,19 @@ package tui import ( - "embed" - "log" - "strconv" - "time" - "unicode" - - "github.com/gdamore/tcell/v2" - "github.com/mrusme/superhighway84/cache" - "github.com/mrusme/superhighway84/common" - "github.com/mrusme/superhighway84/config" - "github.com/mrusme/superhighway84/models" - "github.com/rivo/tview" - "go.uber.org/zap" + "embed" + "log" + "strconv" + "time" + "unicode" + + "github.com/gdamore/tcell/v2" + "github.com/mrusme/superhighway84/cache" + "github.com/mrusme/superhighway84/common" + "github.com/mrusme/superhighway84/config" + "github.com/mrusme/superhighway84/models" + "github.com/rivo/tview" + "go.uber.org/zap" ) type TUI struct { @@ -121,17 +121,17 @@ func (t *TUI) getInputEvent(event *tcell.EventKey) (string) { } func (t *TUI) initInput() { - t.App.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + t.App.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { action := t.getInputEvent(event) - switch action { - case "refresh": + switch action { + case "refresh": t.RefreshMainscreen() t.SetInfo(true) t.App.Sync() - return nil - case "quit": - t.App.Stop() + return nil + case "quit": + t.App.Stop() return nil case "play": t.Player.Play() @@ -151,8 +151,8 @@ func (t *TUI) initInput() { } else { return t.Views[t.ActiveView].HandleInput(event) } - } - }) + } + }) } func (t *TUI) Launch() {