Skip to content

Commit

Permalink
Implemented basic CLI auth flow & timeline request
Browse files Browse the repository at this point in the history
  • Loading branch information
mrusme committed Mar 23, 2021
1 parent 8068ded commit 5a1fb48
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 0 deletions.
71 changes: 71 additions & 0 deletions cli/authenticateCmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package cli

import (
"fmt"
"log"
"context"
"bufio"
"strings"
"os"

"github.com/spf13/cobra"
"github.com/mattn/go-mastodon"
)

var authenticateCmd = &cobra.Command{
Use: "authenticate",
Short: "Authenticate",
Long: "Authenticate against a Mastodon instance.",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
server := args[0]

mastodonApp, err := mastodon.RegisterApp(context.Background(), &mastodon.AppConfig{
Server: server,
ClientName: "Gomphotherium",
Scopes: "read write follow",
Website: "https://github.com/mrusme/gomphotherium",
})
if err != nil {
log.Fatal(err)
}

fmt.Printf("Please open the following URL to authenticate: %s\n\n", mastodonApp.AuthURI)
fmt.Printf("Paste authentication code here and press enter: ")

reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
if err != nil {
fmt.Println("An error occured while reading input. Please try again", err)
os.Exit(-1)
}
authCode := strings.TrimSuffix(input, "\n")

mastodonClient := mastodon.NewClient(&mastodon.Config{
Server: server,
ClientID: mastodonApp.ClientID,
ClientSecret: mastodonApp.ClientSecret,
})

err = mastodonClient.AuthenticateToken(context.Background(), authCode, "urn:ietf:wg:oauth:2.0:oob")
if err != nil {
log.Fatal(err)
os.Exit(-1)
}

fmt.Printf("Success!\nPlease either export the following variables to your session:\n\n")

fmt.Printf("export GOMPHOTHERIUM_SERVER='%s'", server)
fmt.Printf("export GOMPHOTHERIUM_ACCESS_TOKEN='%s'", mastodonClient.Config.AccessToken)

fmt.Printf("\n... or call gomphotherium with the following flags:\n\n")

fmt.Printf("gomphotherium --server '%s' --access-token '%s' ...\n\n", server, mastodonClient.Config.AccessToken)

return
},
}

func init() {
rootCmd.AddCommand(authenticateCmd)
}
14 changes: 14 additions & 0 deletions cli/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cli

import (
"os"
)

func LookupStrEnv(name string, dflt string) (string) {
strEnvStr, ok := os.LookupEnv(name)
if ok == false {
return dflt
}

return strEnvStr
}
44 changes: 44 additions & 0 deletions cli/rootCmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cli

import (
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/mattn/go-mastodon"
)

var server string
var accessToken string
// var clientID string
// var clientSecret string

// var MastodonApp *mastodon.Application
var MastodonClient *mastodon.Client

var rootCmd = &cobra.Command{
Use: "gomphotherium",
Short: "Command line Mastodon client",
Long: `A command line client for Mastodon.`,
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Printf("%+v\n", err)
os.Exit(-1)
}
}

func init() {
cobra.OnInitialize(initConfig)

rootCmd.PersistentFlags().StringVar(&server, "server", LookupStrEnv("GOMPHOTHERIUM_SERVER",""), "Mastodon server")
rootCmd.PersistentFlags().StringVar(&accessToken, "access-token", LookupStrEnv("GOMPHOTHERIUM_ACCESS_TOKEN",""), "Mastodon access token")
}

func initConfig() {
MastodonClient = mastodon.NewClient(&mastodon.Config{
Server: server,
AccessToken: accessToken,
})
}
31 changes: 31 additions & 0 deletions cli/timelineCmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cli

import (
"fmt"
"log"
"context"

"github.com/spf13/cobra"
// "github.com/mattn/go-mastodon"
)

var timelineCmd = &cobra.Command{
Use: "timeline",
Short: "Display timeline",
Long: "Display different timelines.",
Run: func(cmd *cobra.Command, args []string) {
timeline, err := MastodonClient.GetTimelineHome(context.Background(), nil)
if err != nil {
log.Fatal(err)
}
for i := len(timeline) - 1; i >= 0; i-- {
fmt.Println(timeline[i])
}

return
},
}

func init() {
rootCmd.AddCommand(timelineCmd)
}
22 changes: 22 additions & 0 deletions cli/versionCmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cli

import (
"fmt"

"github.com/spf13/cobra"
)

var VERSION string

func init() {
rootCmd.AddCommand(versionCmd)
}

var versionCmd = &cobra.Command{
Use: "version",
Short: "Display version",
Long: `The version of gomphotherium.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("gomphotherium", VERSION)
},
}
9 changes: 9 additions & 0 deletions gomphotherium.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import (
"github.com/mrusme/gomphotherium/cli"
)

func main() {
cli.Execute()
}

0 comments on commit 5a1fb48

Please sign in to comment.