diff --git a/README.md b/README.md index d102e7c..95ae1da 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ You can override this value by specifying a `PIGEON_PATH` ENV var. - [ ] Figure out a system for where to place the default data storage directory - [ ] Get a good CI system going? Run tests at PR time, provide prebuilt binaries, prevent coverage slips, etc.. - [ ] Add a `transact()` helper to ensure all transactions are closed out. + - [ ] Switch to [SQLX](https://github.com/jmoiron/sqlx) for extra sanity. - [ ] Finish all the things below: |Done?|Noun |Verb | Flag / arg 1 | Flag 2 | diff --git a/project/cli.go b/project/cli.go index 2bf4887..455a1a0 100644 --- a/project/cli.go +++ b/project/cli.go @@ -75,6 +75,16 @@ var peerBlockCmd = &cobra.Command{ }, } +var peerListCmd = &cobra.Command{ + Use: "list", + Short: "show a list of peers by their status (blocked, following, etc..)", + Run: func(cmd *cobra.Command, args []string) { + for _, peer := range listPeers() { + fmt.Printf("%s %s\n", peer.mhash, peer.status) + } + }, +} + var peerFollowCmd = &cobra.Command{ Use: "follow", Short: "Follow a peer and replicate their feed when possible.", @@ -95,6 +105,8 @@ func BootstrapCLI() { rootCmd.AddCommand(peerRootCmd) peerRootCmd.AddCommand(peerBlockCmd) + peerRootCmd.AddCommand(peerFollowCmd) + peerRootCmd.AddCommand(peerListCmd) if err := rootCmd.Execute(); err != nil { fmt.Println(err) diff --git a/project/peers.go b/project/peers.go index 967aacf..1cad14a 100644 --- a/project/peers.go +++ b/project/peers.go @@ -15,8 +15,14 @@ const ( unknown = "unknown" ) +type peer struct { + mhash string + status PeerStatus +} + const createPeer = "INSERT INTO peers(mhash, status) VALUES(?1, ?2)" const findPeerByStatus = "SELECT status FROM peers WHERE mhash=$1;" +const getAllPeers = "SELECT mhash, status FROM peers ORDER BY status DESC, mhash ASC;" func getPeerStatus(mHash string) PeerStatus { var status PeerStatus @@ -52,7 +58,31 @@ func addPeer(mHash string, status PeerStatus) { } } -// func showPeers() {} +func listPeers() []peer { + var ( + status PeerStatus + mhash string + ) + rows, err := getDB().Query(getAllPeers) + if err != nil { + panicf("showPeers query failure: %s", err) + } + defer rows.Close() + output := []peer{} + for rows.Next() { + err := rows.Scan(&mhash, &status) + if err != nil { + panicf("Show peers row scan failure: %s", err) + } + output = append(output, peer{mhash: mhash, status: status}) + } + err = rows.Err() + if err != nil { + panicf("showPeers row error: %s", err) + } + return output +} + // func showBlockedPeers() {} // func blockPeer() {} // func unblockPeer() {}