Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
derveloper committed Sep 5, 2020
0 parents commit 3e41b9b
Show file tree
Hide file tree
Showing 9 changed files with 260 additions and 0 deletions.
87 changes: 87 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# Generated files
.idea/**/contentModel.xml

# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr

# CMake
cmake-build-*/

# Mongo Explorer plugin
.idea/**/mongoSettings.xml

# File-based project format
*.iws

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# Editor-based Rest Client
.idea/httpRequests


# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

bin/

# Dependency directories (remove the comment below to include it)
# vendor/
9 changes: 9 additions & 0 deletions .idea/gotee.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
compile:
echo "Compiling for linux"
GOOS=linux GOARCH=amd64 go build -ldflags "-s -w" -o bin/gotee ./cmd/gotee

all: compile
70 changes: 70 additions & 0 deletions cmd/gotee/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package main

import (
"flag"
"fmt"
"gotee/pkg/gotee"
"log"
"os"
"strings"
"time"

"github.com/ilyakaznacheev/cleanenv"
"github.com/slack-go/slack"
)

type ConfigGoTee struct {
Token string `yaml:"slack_token" env:"GOTEE_SLACK_TOKEN" env-default:""`
Channel string `yaml:"slack_channel" env:"GOTEE_SLACK_CHANNEL" env-default:""`
}

func main() {
var cfg ConfigGoTee

err := cleanenv.ReadConfig("/etc/gotee/config.yml", &cfg)
if err != nil {
log.Println(err)
}

output := flag.String("output", "slack", "output method to use")
flag.Parse()

lineFunc := func(lines []string) {
fmt.Print(lines)
}

switch *output {
case "slack":
api := slack.New(
cfg.Token,
slack.OptionDebug(true),
slack.OptionLog(log.New(os.Stdout, "slack-bot: ", log.Lshortfile|log.LstdFlags)),
)
var timestamp string
lines := make([]string, 0)
lineFunc = func(newLines []string) {
lines = append(lines, newLines...)
msg := strings.Join(lines, "\n")
if timestamp != "" {
_, _, _, err := api.UpdateMessage(cfg.Channel, timestamp, slack.MsgOptionText(msg, false))
if err != nil {
log.Println(err)
} else {
log.Println("updated message")
}
} else {
_, ts, _, err := api.SendMessage(cfg.Channel,
slack.MsgOptionText(msg, false),
slack.MsgOptionAsUser(true))
if err != nil {
log.Println(err)
}
timestamp = ts
log.Printf("posted new message %s\n", timestamp)
}
}
break
}

gotee.Tee(lineFunc, 5*time.Second)
}
2 changes: 2 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
slack_token: "<YOUR API TOKEN>"
slack_channel: "C00000000"
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module gotee

go 1.15

require (
github.com/ilyakaznacheev/cleanenv v1.2.5
github.com/slack-go/slack v0.6.6
)
26 changes: 26 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-test/deep v1.0.4 h1:u2CU3YKy9I2pmu9pX0eq50wCgjfGIt539SqR7FbHiho=
github.com/go-test/deep v1.0.4/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/ilyakaznacheev/cleanenv v1.2.5 h1:/SlcF9GaIvefWqFJzsccGG/NJdoaAwb7Mm7ImzhO3DM=
github.com/ilyakaznacheev/cleanenv v1.2.5/go.mod h1:/i3yhzwZ3s7hacNERGFwvlhwXMDcaqwIzmayEhbRplk=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/slack-go/slack v0.6.6 h1:ln0fO794CudStSJEfhZ08Ok5JanMjvW6/k2xBuHqedU=
github.com/slack-go/slack v0.6.6/go.mod h1:FGqNzJBmxIsZURAxh2a8D21AnOVvvXZvGligs4npPUM=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
olympos.io/encoding/edn v0.0.0-20200308123125-93e3b8dd0e24 h1:sreVOrDp0/ezb0CHKVek/l7YwpxPJqv+jT3izfSphA4=
olympos.io/encoding/edn v0.0.0-20200308123125-93e3b8dd0e24/go.mod h1:oVgVk4OWVDi43qWBEyGhXgYxt7+ED4iYNpTngSLX2Iw=
45 changes: 45 additions & 0 deletions pkg/gotee/tee.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package gotee

import (
"bufio"
"os"
"time"
)

func Tee(lineFunc func([]string), flushInterval time.Duration) {
reader := bufio.NewReader(os.Stdin)
scanner := bufio.NewScanner(reader)
scanner.Split(bufio.ScanLines)
lines := make([]string, 0)

ticker := time.NewTicker(flushInterval)
quit := make(chan struct{})

go func() {
for {
select {
case <- ticker.C:
lines = flush(lines, lineFunc)
case <-quit:
ticker.Stop()
return
}
}
}()

for scanner.Scan() {
lines = append(lines, scanner.Text())
}

close(quit)

flush(lines, lineFunc)
}

func flush(lines []string, lineFunc func([]string)) []string {
if len(lines) != 0 {
lineFunc(lines)
lines = make([]string, 0)
}
return lines
}

0 comments on commit 3e41b9b

Please sign in to comment.