`-- peer list` command

This commit is contained in:
Netscape Navigator 2020-09-27 19:56:55 -05:00
parent 73ad2a1c46
commit f17c9eb440
3 changed files with 44 additions and 1 deletions

View File

@ -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 |

View File

@ -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)

View File

@ -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() {}