-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented basic CLI auth flow & timeline request
- Loading branch information
Showing
6 changed files
with
191 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |