commit 13cd0ed2e2793123fd3a835e23f6eea6ecbcc39a Author: Knut Ahlers Date: Sun May 22 19:50:50 2016 +0200 Initial version diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3fcaf93 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +cmd-slack diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..789575a --- /dev/null +++ b/LICENSE @@ -0,0 +1,13 @@ +Copyright 2016 Knut Ahlers + +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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..2d7eb52 --- /dev/null +++ b/README.md @@ -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 diff --git a/main.go b/main.go new file mode 100644 index 0000000..56cabbc --- /dev/null +++ b/main.go @@ -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") +}