Improved app structure
.gitlab-ci.yml
0 → 100644
cmd/attachment.go
0 → 100644
cmd/root.go
0 → 100644
cmd/send.go
0 → 100644
cmd/version.go
0 → 100644
constant/version.go
0 → 100644
go.mod
0 → 100644
module gitlab.hedenstroem.com/go/slackcli | ||
go 1.14 | ||
require ( | ||
github.com/joho/godotenv v1.3.0 | ||
github.com/spf13/cobra v0.0.7 | ||
github.com/spf13/viper v1.6.2 | ||
) |
go.sum
0 → 100644
package main | ||
import ( | ||
"fmt" | ||
"github.com/ashwanthkumar/slack-go-webhook" | ||
"github.com/spf13/cobra" | ||
"os" | ||
"strings" | ||
) | ||
// Copyright © 2016 Erik Hedenström <[email protected]> | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
var ( | ||
username string | ||
channel string | ||
iconemoji string | ||
attachments []string | ||
args []string | ||
) | ||
package main | ||
var RootCmd = &cobra.Command{ | ||
Use: "slackcli", | ||
Short: "Small CLI util to send messages to slack", | ||
Long: ``, | ||
Run: func(cmd *cobra.Command, _args []string) { | ||
args = _args | ||
}, | ||
} | ||
import "gitlab.hedenstroem.com/go/slackcli/cmd" | ||
func main() { | ||
webhookUrl := os.Getenv("SLACK_WEBHOOK") | ||
RootCmd.Flags().StringVar(&username, "username", "slackcli", "Name of bot") | ||
RootCmd.Flags().StringVar(&channel, "channel", "#general", "Channel to post to") | ||
RootCmd.Flags().StringVar(&iconemoji, "iconemoji", ":monkey_face:", "Emoji for icon") | ||
RootCmd.Flags().StringArrayVar(&attachments, "attachment", []string{}, "title=value") | ||
RootCmd.Execute() | ||
if len(args) > 0 { | ||
attachment1 := slack.Attachment{} | ||
for _, attach := range attachments { | ||
parts := strings.Split(attach, "=") | ||
attachment1.AddField(slack.Field{Title: parts[0], Value: parts[1] }) | ||
} | ||
payload := slack.Payload{ | ||
Text: strings.Join(args, " "), | ||
Username: username, | ||
Channel: channel, | ||
IconEmoji: iconemoji, | ||
Attachments: []slack.Attachment{attachment1}, | ||
} | ||
err := slack.Send(webhookUrl, "", payload) | ||
if len(err) > 0 { | ||
fmt.Printf("error: %s\n", err) | ||
} | ||
} | ||
cmd.Execute() | ||
} |
slack/slack.go
0 → 100644
Please register or sign in to comment