-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add channel list and channel info commands (#35)
- Loading branch information
1 parent
3c36c5c
commit deca8d2
Showing
4 changed files
with
159 additions
and
1 deletion.
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,88 @@ | ||
package channels | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/pusher/cli/api" | ||
"github.com/pusher/cli/commands" | ||
"github.com/pusher/pusher-http-go" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ChannelInfo = &cobra.Command{ | ||
Use: "channel-info", | ||
Short: "Get info about a specific channel", | ||
Args: cobra.NoArgs, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
if commands.AppID == "" { | ||
fmt.Fprintf(os.Stderr, "Please supply --app-id\n") | ||
os.Exit(1) | ||
return | ||
} | ||
|
||
if commands.ChannelName == "" { | ||
fmt.Fprintf(os.Stderr, "Please supply --channel\n") | ||
os.Exit(1) | ||
return | ||
} | ||
|
||
app, err := api.GetApp(commands.AppID) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Could not get app the app: %s\n", err.Error()) | ||
os.Exit(1) | ||
return | ||
} | ||
|
||
token, err := api.GetToken(commands.AppID) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Could not get app token: %s\n", err.Error()) | ||
os.Exit(1) | ||
return | ||
} | ||
|
||
client := pusher.Client{ | ||
AppId: commands.AppID, | ||
Key: token.Key, | ||
Secret: token.Secret, | ||
Cluster: app.Cluster, | ||
} | ||
|
||
infoValues := []string{} | ||
|
||
if commands.FetchUserCount { | ||
infoValues = append(infoValues, "user_count") | ||
} | ||
if commands.FetchSubscriptionCount { | ||
infoValues = append(infoValues, "subscription_count") | ||
} | ||
|
||
additionalQueries := map[string]string{} | ||
|
||
if len(infoValues) > 0 { | ||
infoString := infoValues[0] | ||
for i := 1; i < len(infoValues); i++ { | ||
infoString += "," + infoValues[i] | ||
} | ||
additionalQueries["info"] = infoString | ||
} | ||
|
||
channel, err := client.Channel(commands.ChannelName, additionalQueries) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Could not get channel: %s\n", err.Error()) | ||
return | ||
} | ||
|
||
channelJSONBytes, _ := json.MarshalIndent(channel, "", " ") | ||
fmt.Println(string(channelJSONBytes)) | ||
}, | ||
} | ||
|
||
func init() { | ||
ChannelInfo.PersistentFlags().StringVar(&commands.AppID, "app-id", "", "Channels App ID") | ||
ChannelInfo.PersistentFlags().StringVar(&commands.ChannelName, "channel", "", "Channel name") | ||
ChannelInfo.PersistentFlags().BoolVar(&commands.FetchUserCount, "fetch-user-count", false, "Fetch user count for the presence channel") | ||
ChannelInfo.PersistentFlags().BoolVar(&commands.FetchSubscriptionCount, "fetch-subscription-count", false, "Fetch subscription count for the channel") | ||
} |
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,67 @@ | ||
package channels | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/pusher/cli/api" | ||
"github.com/pusher/cli/commands" | ||
"github.com/pusher/pusher-http-go" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ListChannels = &cobra.Command{ | ||
Use: "list-channels", | ||
Short: "List all channels that have at least one subscriber", | ||
Args: cobra.NoArgs, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
if commands.AppID == "" { | ||
fmt.Fprintf(os.Stderr, "Please supply --app-id\n") | ||
os.Exit(1) | ||
return | ||
} | ||
|
||
app, err := api.GetApp(commands.AppID) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Could not get app the app: %s\n", err.Error()) | ||
os.Exit(1) | ||
return | ||
} | ||
|
||
token, err := api.GetToken(commands.AppID) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Could not get app token: %s\n", err.Error()) | ||
os.Exit(1) | ||
return | ||
} | ||
|
||
client := pusher.Client{ | ||
AppId: commands.AppID, | ||
Key: token.Key, | ||
Secret: token.Secret, | ||
Cluster: app.Cluster, | ||
} | ||
|
||
opts := map[string]string{} | ||
|
||
if commands.FilterByPrefix != "" { | ||
opts["filter_by_prefix"] = commands.FilterByPrefix | ||
} | ||
|
||
channelsList, err := client.Channels(opts) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Could not get channel list: %s\n", err.Error()) | ||
return | ||
} | ||
|
||
channelsListJSONBytes, _ := json.MarshalIndent(channelsList.Channels, "", " ") | ||
fmt.Println(string(channelsListJSONBytes)) | ||
}, | ||
} | ||
|
||
func init() { | ||
ListChannels.PersistentFlags().StringVar(&commands.AppID, "app-id", "", "Channels App ID") | ||
ListChannels.PersistentFlags().StringVar(&commands.FilterByPrefix, "filter-by-prefix", "", "A channel name prefix, e.g. 'presence-'") | ||
} |
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
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