1
0
Fork 0
mirror of https://github.com/Luzifer/cmd-slack.git synced 2024-10-18 04:34:24 +00:00

Initial version

This commit is contained in:
Knut Ahlers 2016-05-22 19:50:50 +02:00
commit 13cd0ed2e2
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
4 changed files with 80 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
cmd-slack

13
LICENSE Normal file
View file

@ -0,0 +1,13 @@
Copyright 2016 Knut Ahlers <knut@ahlers.me>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

7
README.md Normal file
View file

@ -0,0 +1,7 @@
[![Download on GoBuilder](http://badge.luzifer.io/v1/badge?title=Download%20on&text=GoBuilder)](https://gobuilder.me/github.com/Luzifer/cmd-slack)
[![License: Apache v2.0](https://badge.luzifer.io/v1/badge?color=5d79b5&title=license&text=Apache+v2.0)](http://www.apache.org/licenses/LICENSE-2.0)
[![Go Report Card](https://goreportcard.com/badge/github.com/Luzifer/cmd-slack)](https://goreportcard.com/report/github.com/Luzifer/cmd-slack)
# Luzifer / cmd-slack
`cmd-slack` executes a command and posts the output to slack

59
main.go Normal file
View file

@ -0,0 +1,59 @@
package main
import (
"bytes"
"encoding/json"
"log"
"net/http"
"os/exec"
"github.com/Luzifer/rconfig"
)
var (
cfg = struct {
Hook string `flag:"hook,h" default:"" description:"Slack incoming webhook"`
Username string `flag:"username,u" default:"CmdSlack" description:"Username to use in WebHook command"`
Icon string `flag:"icon,i" default:":package:" description:"Icon to use for the webhook"`
Channel string `flag:"channel,c" default:"" description:"Channel to send the message to"`
}{}
version = "dev"
)
type slack struct {
Username string `json:"username,omitempty"`
Icon string `json:"icon_emoji,omitempty"`
Channel string `json:"channel,omitempty"`
Text string `json:"text"`
}
func main() {
rconfig.Parse(&cfg)
cmdline := rconfig.Args()[1:]
buf := bytes.NewBuffer([]byte{})
cmd := exec.Command(cmdline[0], cmdline[1:]...)
cmd.Stdout = buf
if err := cmd.Run(); err != nil {
log.Fatalf("Command error: %s", err)
}
slo := slack{
Username: cfg.Username,
Icon: cfg.Icon,
Channel: cfg.Channel,
Text: "```\n" + buf.String() + "```",
}
body := bytes.NewBuffer([]byte{})
if err := json.NewEncoder(body).Encode(slo); err != nil {
log.Fatalf("Encoder error: %s", err)
}
if _, err := http.Post(cfg.Hook, "application/json", body); err != nil {
log.Fatalf("Posting error: %s", err)
}
log.Printf("Posted successfully")
}