Skip to content

Commit

Permalink
Merge pull request #62 from rneatherway/authcmd
Browse files Browse the repository at this point in the history
Add an auth command
  • Loading branch information
rneatherway authored Mar 23, 2024
2 parents 7a7ee0b + 4be4b98 commit 05d68ed
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 17 deletions.
88 changes: 88 additions & 0 deletions cmd/gh-slack/cmd/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package cmd

import (
"fmt"
"net/url"

"github.com/cli/go-gh/pkg/config"
"github.com/rneatherway/slack"
"github.com/spf13/cobra"
)

var authCmd = &cobra.Command{
Use: "auth [flags]",
Short: "Prints authentication information for the Slack API (treat output as secret)",
Long: "Prints authentication information for the Slack API (treat output as secret).",
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := config.Read()
if err != nil {
return err
}

team, err := getFlagOrElseConfig(cfg, cmd.Flags(), "team")
if err != nil {
return err
}

auth, err := slack.GetCookieAuth(team)
if err != nil {
return err
}

vals := url.Values{}
for k, v := range auth.Cookies {
vals.Add(k, v)
}

fmt.Printf("export SLACK_TOKEN=%s\n", auth.Token)
fmt.Printf("export SLACK_COOKIES=%s\n", vals.Encode())
return nil
},
Example: ` eval $(gh-slack auth [-t <team-name>])
# Example configuration (add to gh's configuration file at $HOME/.config/gh/config.yml):
extensions:
slack:
team: foo`,
}

func init() {
authCmd.Flags().StringP("team", "t", "", "Slack team name (required here or in config)")
authCmd.SetHelpTemplate(authCmdUsageTemplate)
authCmd.SetUsageTemplate(authCmdUsageTemplate)
}

const authCmdUsageTemplate string = `Usage:{{if .Runnable}}
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
{{.CommandPath}}{{end}}{{if gt (len .Aliases) 0}}
Aliases:
{{.NameAndAliases}}{{end}}{{if .HasExample}}
Security:
Treat the output of this command as secret and do not share it with anyone!
It can be used to impersonate you. If you suspect it has been compromised,
log out of the Slack app to revoke the token and cookies.
Examples:
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}{{$cmds := .Commands}}{{if eq (len .Groups) 0}}
Available Commands:{{range $cmds}}{{if (or .IsAvailableCommand)}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{else}}{{range $group := .Groups}}
{{.Title}}{{range $cmds}}{{if (and (eq .GroupID $group.ID) (or .IsAvailableCommand (eq .Name "help")))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if not .AllChildCommandsHaveGroup}}
Additional Commands:{{range $cmds}}{{if (and (eq .GroupID "") (or .IsAvailableCommand (eq .Name "help")))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
Flags:
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
Global Flags:
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}
Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
`
21 changes: 19 additions & 2 deletions cmd/gh-slack/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,25 @@ package cmd
import (
"os"

"github.com/cli/go-gh/pkg/config"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

const sendConfigExample = `
func getFlagOrElseConfig(cfg *config.Config, flags *pflag.FlagSet, key string) (string, error) {
value, err := flags.GetString(key)
if err != nil {
return "", err
}

if value != "" {
return value, nil

}
return cfg.Get([]string{"extensions", "slack", key})
}

const sendConfigEample = `
# Example configuration (add to gh's configuration file at $HOME/.config/gh/config.yml):
extensions:
slack:
Expand All @@ -25,7 +40,8 @@ var rootCmd = &cobra.Command{
gh-slack read -i <issue-url> <slack-permalink>
gh-slack send -m <message> -c <channel-name> -t <team-name>
gh-slack api post chat.postMessage -b '{"channel":"123","blocks":[...]}
` + sendConfigExample,
eval $(gh-slack auth -t <team-name>)
` + sendConfigEample,
}

func Execute() error {
Expand All @@ -43,6 +59,7 @@ func init() {
rootCmd.AddCommand(readCmd)
rootCmd.AddCommand(sendCmd)
rootCmd.AddCommand(apiCmd)
rootCmd.AddCommand(authCmd)
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Show verbose debug information")
rootCmd.SetHelpTemplate(rootCmdUsageTemplate)
rootCmd.SetUsageTemplate(rootCmdUsageTemplate)
Expand Down
16 changes: 1 addition & 15 deletions cmd/gh-slack/cmd/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,8 @@ import (
"github.com/cli/go-gh/pkg/config"
"github.com/rneatherway/gh-slack/internal/slackclient"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

func getFlagOrElseConfig(cfg *config.Config, flags *pflag.FlagSet, key string) (string, error) {
value, err := flags.GetString(key)
if err != nil {
return "", err
}

if value != "" {
return value, nil

}
return cfg.Get([]string{"extensions", "slack", key})
}

var sendCmd = &cobra.Command{
Use: "send [flags]",
Short: "Sends a message to a Slack channel",
Expand Down Expand Up @@ -74,7 +60,7 @@ var sendCmd = &cobra.Command{
},
Example: ` gh-slack send -t <team-name> -c <channel-name> -m <message> -b <bot-name>
gh-slack send -m <message> -w # If bot is specified in config
` + sendConfigExample,
` + sendConfigEample,
}

// sendMessage sends a message to a Slack channel.
Expand Down

0 comments on commit 05d68ed

Please sign in to comment.