Last active
December 18, 2019 07:56
Star
You must be signed in to star a gist
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
// Change in the Plugin. | |
func (p *Plugin) ExecuteCommand(c *plugin.Context, args *model.CommandArgs) (*model.CommandResponse, *model.AppError) { | |
return p.Helpers.ExecuteCommand(c, args) | |
} | |
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
type Helpers interface { | |
... | |
// Minimum server version: 5.2 | |
RegisterSlashCommand(c *cobra.Command, mcmd *model.Command) error | |
// Minimum server version: 5.2 | |
ExecuteCommand(c *Context, args *model.CommandArgs) (*model.CommandResponse, *model.AppError) | |
// Minimum server version: 5.2 | |
CommandResponse(r *model.CommandResponse, err *model.AppError) | |
// Minimum server version: 5.2 | |
CommandContext(contextID string) (*model.CommandArgs, *Context) | |
} | |
type HelpersImpl struct { | |
API API | |
commandContext | |
} |
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
type commandContext struct { | |
m sync.Map | |
c *cobra.Command | |
} | |
func (p *HelpersImpl) RegisterSlashCommand(c *cobra.Command, mcmd *model.Command) error { | |
p.c = c | |
if err := p.API.RegisterCommand(mcmd); err != nil { | |
return err | |
} | |
return nil | |
} | |
func (p *HelpersImpl) ExecuteCommand(c *Context, args *model.CommandArgs) (*model.CommandResponse, *model.AppError) { | |
p.m.Store(c.RequestId, struct { | |
*Context | |
*model.CommandArgs | |
}{c, args}) | |
p.c.SetArgs(strings.Fields(args.Command + "--context-id " + c.RequestId)[1:]) | |
p.c.Execute() | |
value, _ := p.m.Load(c.RequestId) | |
p.m.Delete(c.RequestId) | |
s := value.(struct { | |
r *model.CommandResponse | |
err *model.AppError | |
}) | |
return s.r, s.err | |
} | |
func (p *HelpersImpl) CommandResponse(requestID string, r *model.CommandResponse, err *model.AppError) { | |
p.m.Store(requestID, struct { | |
*model.CommandResponse | |
*model.AppError | |
}{r, err}) | |
} | |
func (p *HelpersImpl) CommandContext(requestID string) (*model.CommandArgs, *Context, error) { | |
value, ok := p.m.Load(requestID) | |
if ok { | |
return nil, nil, errors.New("some error") | |
} | |
s := value.(struct { | |
c *Context | |
args *model.CommandArgs | |
}) | |
return s.args, s.c, nil | |
} |
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
func (p *Plugin) connect(command *cobra.Command, args []string) { | |
config := p.API.GetConfig() | |
requestID := command.Flag("context-id") | |
commandArgs, _ := p.Helpers.CommandContext(requestID.Value.String()) | |
if config.ServiceSettings.SiteURL == nil { | |
p.postCommandResponse(commandArgs, "Encountered an error connecting to GitHub.") | |
p.Helpers.CommandResponse(&model.CommandResponse{}, nil) | |
} | |
p.postCommandResponse(commandArgs, fmt.Sprintf("[Click here to link your GitHub account.](%s/plugins/github/oauth/connect)", *config.ServiceSettings.SiteURL)) | |
p.Helpers.CommandResponse(&model.CommandResponse{}, nil) | |
} | |
func (p *Plugin) unsubscribe(command *cobra.Command, args []string) { | |
repo := args[0] | |
commandContext, _ := p.Helpers.CommandContext(command.Flag("context-id").Value.String()) | |
if err := p.Unsubscribe(commandContext.ChannelId, repo); err != nil { | |
mlog.Error(err.Error()) | |
p.postCommandResponse(commandContext, "Encountered an error trying to unsubscribe. Please try again.") | |
p.Helpers.CommandResponse(&model.CommandResponse{}, nil) | |
} | |
p.postCommandResponse(commandContext, fmt.Sprintf("Succesfully unsubscribed from %s.", repo)) | |
p.Helpers.CommandResponse(&model.CommandResponse{}, nil) | |
} | |
func handleArgs(p *Plugin) cobra.PositionalArgs { | |
return func(cmd *cobra.Command, args []string) error { | |
flag := cmd.Flag("context-id") | |
commandContext, _ := p.Helpers.CommandContext(flag.Value.String()) | |
if len(args) == 0 { | |
p.postCommandResponse(commandContext, "Please specify a repository.") | |
return errors.New("repository is not specified") | |
} | |
return nil | |
} | |
} | |
func (p *Plugin) OnActivate() error { | |
githubC := &cobra.Command{Use: "/github"} | |
connect := &cobra.Command{Use: "connect", Run: p.connect} | |
unsubscribe := &cobra.Command{Use: "unsubscribe", Run: p.unsubscribe, Args: handleArgs(p)} | |
githubC.AddCommand(connect) | |
githubC.AddCommand(unsubscribe) | |
p.Helpers.RegisterSlashCommand(githubC, getCommand()) | |
.... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Improvements: