Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated cli #7

Merged
merged 3 commits into from
Oct 26, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
can pick which server to interact with once signed in
  • Loading branch information
jrudio committed Oct 25, 2017
commit 55297795376606a8c8a7850d7a2ea7f897948e89
129 changes: 106 additions & 23 deletions cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,85 @@ func checkPIN(c *cli.Context) error {
return nil
}

func pickServer(db store) func(c *cli.Context) error {
return func(c *cli.Context) error {
// look up servers - hopefully a token is already in store
plexToken, err := db.getPlexToken()

if err != nil {
db.Close()
return fmt.Errorf("failed to retreive plex token: %v", err)
}

plexConn, err := plex.New("", plexToken)

if err != nil {
db.Close()
return err
}

// load list of servers
servers, err := plexConn.GetServers()

if err != nil {
db.Close()
return fmt.Errorf("failed getting plex servers: %v", err)
}

fmt.Println("Server list:")

for i, server := range servers {
fmt.Printf("[%d] - %s\n", i, server.Name)
}

fmt.Print("\nSelect a server: ")

var serverIndex int
fmt.Scanln(&serverIndex)

// bound check input
if serverIndex < 0 || serverIndex > (len(servers)-1) {
db.Close()
return errors.New("invalid selection")
}

selectedServer := servers[serverIndex]

// choose to connect via local or remote
fmt.Printf("\nshowing local and remote addresses for %s:\n", selectedServer.Name)

for i, conn := range selectedServer.Connection {
fmt.Printf("\t[%d] uri: %s, is local: %t\n", i, conn.Address, conn.Local == 1)
}

fmt.Print("\nPick the appropriate address: ")

var urlIndex int
fmt.Scanln(&urlIndex)

// bound check again
if urlIndex < 0 || urlIndex > (len(selectedServer.Connection)-1) {
db.Close()
return errors.New("invalid selection")
}

// persist selection to disk
fmt.Printf("\nsetting %s as the default server using url %s...\n", selectedServer.Name, selectedServer.Connection[urlIndex].URI)

if err := db.savePlexServer(server{
Name: selectedServer.Name,
URL: selectedServer.Connection[urlIndex].URI,
}); err != nil {
db.Close()
return fmt.Errorf("failed to save server info: %v", err)
}

fmt.Println("success!")

return nil
}
}

// signIn displays the auth token on successful sign in
func signIn(db store) func(c *cli.Context) error {
return func(c *cli.Context) error {
Expand Down Expand Up @@ -259,37 +338,41 @@ func signIn(db store) func(c *cli.Context) error {
}
}

func getLibraries(c *cli.Context) error {
plexToken := c.String("token")
plexURL := c.String("url")
func getLibraries(db store) func(c *cli.Context) error {
return func(c *cli.Context) error {
plexToken, err := db.getPlexToken()

if plexToken == "" {
return errors.New("token is required for this command")
}
if err != nil {
db.Close()
return fmt.Errorf("failed to retreive plex token: %v", err)
}

if plexURL == "" {
return errors.New("url is required for this command")
}
plexServer, err := db.getPlexServer()

fmt.Println("using plex token:", plexToken)
if err != nil {
db.Close()
return fmt.Errorf("failed to retreive plex server url: %v", err)
}

plexConn, err := plex.New(plexURL, plexToken)
plexConn, err := plex.New(plexServer.URL, plexToken)

if err != nil {
return err
}
if err != nil {
db.Close()
return err
}

fmt.Println("getting libraries...")
fmt.Println("getting libraries...")

libraries, err := plexConn.GetLibraries()
libraries, err := plexConn.GetLibraries()

if err != nil {
return err
}
if err != nil {
return err
}

for _, dir := range libraries.MediaContainer.Directory {
fmt.Println(dir.Title)
}
for _, dir := range libraries.MediaContainer.Directory {
fmt.Println(dir.Title)
}

return nil
return nil
}
}
37 changes: 24 additions & 13 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/json"
"fmt"
"os"
"path/filepath"
Expand All @@ -23,6 +24,23 @@ var (
appSecret = []byte("iAmAseCReTuSEdTOENcrYp")
)

type server struct {
Name string `json:"name"`
URL string `json:"url"`
}

func (s server) Serialize() ([]byte, error) {
return json.Marshal(s)
}

func unserializeServer(serializedServer []byte) (server, error) {
var s server

err := json.Unmarshal(serializedServer, &s)

return s, err
}

func main() {
app := cli.NewApp()

Expand Down Expand Up @@ -116,19 +134,7 @@ func main() {
{
Name: "library",
Usage: "display your libraries",
Action: getLibraries,
Flags: []cli.Flag{
cli.StringFlag{
Name: "token",
Value: "",
Usage: "plex token is required to access your server",
},
cli.StringFlag{
Name: "url",
Value: "",
Usage: "url to your plex server",
},
},
Action: getLibraries(db),
},
{
Name: "request-pin",
Expand Down Expand Up @@ -156,6 +162,11 @@ func main() {
Usage: "use your username and password to receive a plex auth token",
Action: signIn(db),
},
{
Name: "pick-server",
Usage: "choose a server to interact with",
Action: pickServer(db),
},
}

app.Run(os.Args)
Expand Down
52 changes: 48 additions & 4 deletions cmd/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ type store struct {
}

type storeKeys struct {
appSecret []byte
plexToken []byte
appSecret []byte
plexToken []byte
plexServer []byte
}

func initDataStore(dirName string) (store, error) {
Expand Down Expand Up @@ -53,12 +54,14 @@ func initDataStore(dirName string) (store, error) {

db.db = kvStore
db.keys = storeKeys{
appSecret: []byte("app-secret"),
plexToken: []byte("plex-token"),
appSecret: []byte("app-secret"),
plexToken: []byte("plex-token"),
plexServer: []byte("plex-server"),
}

return db, nil
}

func (s store) Close() {
if err := s.db.Close(); err != nil {
fmt.Printf("data store failed to closed: %v\n", err)
Expand Down Expand Up @@ -159,3 +162,44 @@ func (s store) savePlexToken(token string) error {

return nil
}

func (s store) getPlexServer() (server, error) {
var plexServer server

err := s.db.View(func(txn *badger.Txn) error {
item, err := txn.Get(s.keys.plexServer)

if err != nil {
return err
}

serializedServer, err := item.Value()

if err != nil {
return err
}

_plexServer, err := unserializeServer(serializedServer)

if err != nil {
return err
}

plexServer = _plexServer

return nil
})

return plexServer, err
}

func (s store) savePlexServer(plexServer server) error {
serializedServer, err := plexServer.Serialize()
if err != nil {
return err
}

return s.db.Update(func(txn *badger.Txn) error {
return txn.Set(s.keys.plexServer, serializedServer, 0)
})
}