Skip to content

Commit

Permalink
Merge pull request #5 from typetalk-gadget/patch-4
Browse files Browse the repository at this point in the history
Enable to change port by config
  • Loading branch information
vvatanabe authored Aug 11, 2020
2 parents 198fc8d + d171a93 commit fd1083f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ $ nowplaying-on-typetalk [flags]
-c, --config string config file path (default "~/.nowplaying-on-typetalk/config.yml")
--debug debug mode
-h, --help help for nowplaying-on-typetalk
--port int port number for OAuth (default 18080)
--spotify_client_id string spotify client id [SPOTIFY_CLIENT_ID]
--spotify_client_secret string spotify client secret [SPOTIFY_CLIENT_SECRET]
--status_emoji string typetalk status emoji [STATUS_EMOJI] (default ":musical_note:")
Expand All @@ -57,6 +58,8 @@ $ nowplaying-on-typetalk [flags]

```yaml
debug: true
port: 18080
typetalk_client_id: deadbeef
typetalk_client_id: deadbeef
typetalk_client_secret: deadcode
typetalk_space_key: foo
Expand Down Expand Up @@ -102,7 +105,7 @@ This page explains how to do this.
![5](./capture/5.png)
- Set the following URL in "Redirect URI's" as the URL to be redirected to after authentication.
- http://localhost:18080/nowplaying-on-typetalk
- http://localhost:18080/nowplaying-on-typetalk (* The port number should be as same as your config. )
- Click the "ADD" and Click the "SAVE" and save the setting.
![7](./capture/7.png)
Expand Down
13 changes: 10 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const (
flagNameSpotifyClientID = "spotify_client_id"
flagNameSpotifyClientSecret = "spotify_client_secret"
flagNameStatusEmoji = "status_emoji"
flagNamePort = "port"
)

type Config struct {
Expand All @@ -48,6 +49,7 @@ type Config struct {
SpotifyClientID string `mapstructure:"spotify_client_id"`
SpotifyClientSecret string `mapstructure:"spotify_client_secret"`
StatusEmoji string `mapstructure:"status_emoji"`
Port int `mapstructure:"port"`
}

var (
Expand Down Expand Up @@ -90,6 +92,7 @@ func main() {
flags.String(flagNameSpotifyClientID, "", "spotify client id [SPOTIFY_CLIENT_ID]")
flags.String(flagNameSpotifyClientSecret, "", "spotify client secret [SPOTIFY_CLIENT_SECRET]")
flags.String(flagNameStatusEmoji, ":musical_note:", "typetalk status emoji [STATUS_EMOJI]")
flags.Int(flagNamePort, defaultPort, "port number for OAuth")

_ = viper.BindPFlag(flagNameDebug, flags.Lookup(flagNameDebug))
_ = viper.BindPFlag(flagNameTypetalkClientID, flags.Lookup(flagNameTypetalkClientID))
Expand All @@ -98,6 +101,7 @@ func main() {
_ = viper.BindPFlag(flagNameSpotifyClientID, flags.Lookup(flagNameSpotifyClientID))
_ = viper.BindPFlag(flagNameSpotifyClientSecret, flags.Lookup(flagNameSpotifyClientSecret))
_ = viper.BindPFlag(flagNameStatusEmoji, flags.Lookup(flagNameStatusEmoji))
_ = viper.BindPFlag(flagNamePort, flags.Lookup(flagNamePort))

cobra.OnInitialize(func() {
configFile, err := flags.GetString(flagNameConfig)
Expand Down Expand Up @@ -129,8 +133,8 @@ var (
// You must register an application at Spotify's developer portal
// and enter this value.
// http://localhost:18080/nowplaying-on-typetalk
redirectURI = fmt.Sprintf("http://localhost:%d/%s", defaultPort, cmdName)
auth = spotify.NewAuthenticator(redirectURI, spotify.ScopeUserReadCurrentlyPlaying)
redirectURI string
auth spotify.Authenticator
state = uuid.NewV4().String()
ch = make(chan *spotify.Client)
)
Expand All @@ -139,9 +143,12 @@ func run(c *cobra.Command, args []string) {

// printDebug(fmt.Sprintf("config: %#v\n", config))

redirectURI = fmt.Sprintf("http://localhost:%d/%s", config.Port, cmdName)
auth = spotify.NewAuthenticator(redirectURI, spotify.ScopeUserReadCurrentlyPlaying)

auth.SetAuthInfo(config.SpotifyClientID, config.SpotifyClientSecret)

ln, err := net.Listen("tcp", fmt.Sprintf(":%d", defaultPort))
ln, err := net.Listen("tcp", fmt.Sprintf(":%d", config.Port))
if err != nil {
printFatal(err)
}
Expand Down

0 comments on commit fd1083f

Please sign in to comment.