Skip to content
This repository has been archived by the owner on May 27, 2024. It is now read-only.

Commit

Permalink
Mega Refacto (#31)
Browse files Browse the repository at this point in the history
* first load of changes

* fix lint

* scheduled tasks work

* generate some help

* make logging better

* make linter happy

* plugin commands filter

* add version info in log fields
  • Loading branch information
JulienTant authored Aug 8, 2022
1 parent 39522d8 commit 1371ace
Show file tree
Hide file tree
Showing 37 changed files with 1,654 additions and 833 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ dist
# Dependency directories
vendor/

# config file
config/config.yaml

# IDE
.vscode

Expand Down
6 changes: 5 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,8 @@ linters:
- unused
- varcheck
- whitespace


issues:
exclude-rules:
- path: plugin/(.+)/(.+)\.go
linters: [deadcode] # plugins functions are called dynamically
25 changes: 25 additions & 0 deletions commands/helloworld/helloworld.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
- command: "helloworld"
name: "Hello Who?"
description: "Print a hello world, but getting the name from a modal"
vars:
- name: HELLO
value: "hello"
dialog:
title: Hello world
introduction_text: Say hello to whoever is passed
elements:
- name: WORLD
display_name: World
type: text
subtype: text
optional: false
default: world
help_text: How are we say hello to
exec:
- scripts/helloworld/helloworld.sh
response:
type: "ephemeral"
colors:
- color: "#ff0000"
...

108 changes: 108 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package config

import (
"fmt"
"io/ioutil"

"github.com/pkg/errors"
"gopkg.in/yaml.v3"
)

type RawMessage struct {
unmarshal func(interface{}) error
}

func (msg *RawMessage) UnmarshalYAML(unmarshal func(interface{}) error) error {
msg.unmarshal = unmarshal
return nil
}

func (msg *RawMessage) Unmarshal(v interface{}) error {
return msg.unmarshal(v)
}

type Config struct {
Listen string `yaml:"listen"`
BaseURL string `yaml:"base_url"`
PluginsConfig []PluginConfig `yaml:"plugins"`
CommandConfigurations []CommandConfig `yaml:"commands"`
ScheduledCommands []ScheduledCommandConfig `yaml:"scheduler"`
}

type PluginConfig struct {
Name string `yaml:"name"`
File string `yaml:"file"`
Config RawMessage `yaml:"config"`
}

type CommandConfig struct {
Command string `yaml:"command"`
Token string `yaml:"token"`
DialogURL string `yaml:"dialog_url"`
DialogResponseURL string `yaml:"dialog_response_url"`
SchedulerResponseURL string `yaml:"scheduler_response_url"`
Plugins []CommandPlugin `yaml:"plugins"`
}

type ScheduledCommandConfig struct {
Name string `yaml:"name"`
Cron string `yaml:"cron"`
Command string `yaml:"command"`
Channel string `yaml:"channel"`
ResponseURL string `yaml:"response_url"`
}

func Load(path string) (*Config, error) {
fmt.Println("Loading config from", path)
content, err := ioutil.ReadFile(path)
if err != nil {
return nil, errors.Wrapf(err, "failed to read config file %s", path)
}

fmt.Println("Parsing config")
var config Config
err = yaml.Unmarshal(content, &config)
if err != nil {
return nil, errors.Wrapf(err, "failed to unmarshal config file %s", path)
}

return &config, nil
}

type CommandPlugin struct {
Name string
Only []string
Exclude []string
}

type plainCommandPlugin struct {
Name string `yaml:"name"`
Only []string `yaml:"only"`
Exclude []string `yaml:"exclude"`
}

// UnmarshalYAML implements the Unmarshaler interface.
// The CommandPlugin can either be a string corresponding to the name, or a CommandPlugin
func (cp *CommandPlugin) UnmarshalYAML(unmarshal func(interface{}) error) error {
name := ""
err := unmarshal(&name)
if err == nil {
*cp = CommandPlugin{Name: name}
return nil
}

plugin := plainCommandPlugin{}
err = unmarshal(&plugin)
if err != nil {
return err
}

cp.Name = plugin.Name
cp.Only = plugin.Only
cp.Exclude = plugin.Exclude

if len(cp.Only) > 0 && len(cp.Exclude) > 0 {
return errors.New("only and exclude cannot be both set")
}
return nil
}
48 changes: 38 additions & 10 deletions config/config.sample.yaml
Original file line number Diff line number Diff line change
@@ -1,19 +1,47 @@
---

listen: "0.0.0.0:8080"
token: "TOKEN" # Mattermost slash command token
commands:
- commands/gitlab/gitlab.yaml
- commands/docker/docker.yaml
- commands/fun/fun.yaml
- commands/k8s/k8s.yaml
- commands/release/release.yaml
base_url: "https://op-tools-address.com"
plugins:
- name: bash_useful
file: plugins/bash.so
config:
files:
- commands/gitlab/gitlab.yaml
- commands/docker/docker.yaml
- commands/k8s/k8s.yaml
- commands/release/release.yaml
- name: bash_fun
file: plugins/bash.so
config:
files:
- commands/helloworld/helloworld.yaml
- commands/fun/fun.yaml
commands:
- command: jops
plugins:
- name: bash_useful
- name: bash_fun
exclude: ["fun figlet"]
- name: bash_fun
only: ["fun figlet"]
token: "MATTERMOST_SLASHCOMMAND_TOKEN"
dialog_url: https://my-mattermost-instance.cloud.mattermost.com/api/v4/actions/dialogs/open
dialog_response_url: https://my-mattermost-instance.cloud.mattermost.com/hooks/random-id
scheduler_response_url: https://my-mattermost-instance.cloud.mattermost.com/hooks/random-id
scheduler:
- name: "Check Gitlab Version"
command: "jops gitlab version --check"
channel: "ops-tool-notifications"
cron: "0 * * * *"
- name: "Joke"
channel: "fun"
provider: "fun"
command: "joke"
command: "jops fun joke"
cron: "*/5 * * * *"
hook: "TOKEN"
- name: "Fortune"
channel: "fun"
command: "jops fun fortune"
cron: "*/6 * * *"

...

33 changes: 0 additions & 33 deletions config/config.yaml

This file was deleted.

14 changes: 8 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ require (
github.com/gorilla/schema v1.2.0
github.com/julienschmidt/httprouter v1.3.0
github.com/mattermost/mattermost-server/v6 v6.7.2
github.com/mattn/go-shellwords v1.0.12
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
go.uber.org/zap v1.21.0 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
)

Expand All @@ -22,7 +24,7 @@ require (
github.com/dyatlov/go-opengraph v0.0.0-20210112100619-dae8665a5b09 // indirect
github.com/francoispqt/gojay v1.2.13 // indirect
github.com/go-asn1-ber/asn1-ber v1.5.3 // indirect
github.com/go-co-op/gocron v1.14.0
github.com/go-co-op/gocron v1.15.1
github.com/google/uuid v1.3.0
github.com/gorilla/websocket v1.5.0 // indirect
github.com/graph-gophers/graphql-go v1.3.0 // indirect
Expand All @@ -42,10 +44,10 @@ require (
github.com/pborman/uuid v1.2.1 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/philhofer/fwd v1.1.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pkg/errors v0.9.1
github.com/rs/xid v1.4.0 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/stretchr/testify v1.7.5
github.com/stretchr/testify v1.8.0 // indirect
github.com/tinylib/msgp v1.1.6 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
Expand All @@ -57,5 +59,5 @@ require (
golang.org/x/text v0.3.7 // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v2 v2.4.0 // indirect
)
14 changes: 12 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ github.com/aws/smithy-go v1.8.0/go.mod h1:SObp3lf9smib00L/v3U2eAKG8FyQ7iLrJnQiAm
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=
github.com/beevik/etree v1.1.0/go.mod h1:r8Aw8JqVegEf0w2fDnATrX9VpkMcyFeM0FhwO62wh+A=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/beorn7/perks v0.0.0-20160804104726-4c0e84591b9a/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
Expand Down Expand Up @@ -452,8 +453,8 @@ github.com/go-asn1-ber/asn1-ber v1.3.2-0.20191121212151-29be175fc3a3/go.mod h1:h
github.com/go-asn1-ber/asn1-ber v1.5.3 h1:u7utq56RUFiynqUzgVMFDymapcOtQ/MZkh3H4QYkxag=
github.com/go-asn1-ber/asn1-ber v1.5.3/go.mod h1:hEBeB/ic+5LoWskz+yKT7vGhhPYkProFKoKdwZRWMe0=
github.com/go-check/check v0.0.0-20180628173108-788fd7840127/go.mod h1:9ES+weclKsC9YodN5RgxqK/VD9HM9JsCSh7rNhMZE98=
github.com/go-co-op/gocron v1.14.0 h1:Fphr0ZWKtr6V33jjUdhF7ggwI0idt/RxBxwybIwr6Zo=
github.com/go-co-op/gocron v1.14.0/go.mod h1:HfX00TY7w5syy3awgLeo0Po9MAkYDbz7hQGvwWcQSqg=
github.com/go-co-op/gocron v1.15.1 h1:gOi+Xe88yj9N6GgYfSYAhdoKEWm7Ykx18WtWKcM+WEI=
github.com/go-co-op/gocron v1.15.1/go.mod h1:W/N9G7bntRo5fVQlmjncvqSt74jxCxHfjyHlgcB33T8=
github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q=
github.com/go-fonts/dejavu v0.1.0/go.mod h1:4Wt4I4OU2Nq9asgDCteaAaWZOV24E+0/Pwo0gppep4g=
github.com/go-fonts/latin-modern v0.2.0/go.mod h1:rQVLdDMK+mK1xscDwsqM5J8U2jrRa3T0ecnM9pNujks=
Expand Down Expand Up @@ -923,6 +924,8 @@ github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m
github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-shellwords v1.0.3/go.mod h1:3xCvwCdWdlDJUrvuMn7Wuy9eWs4pE8vqg+NOMyg4B2o=
github.com/mattn/go-shellwords v1.0.12 h1:M2zGm7EW6UQJvDeQxo4T51eKPurbeFbe8WtebGE2xrk=
github.com/mattn/go-shellwords v1.0.12/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y=
github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/mattn/go-sqlite3 v1.14.9/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
Expand Down Expand Up @@ -1274,6 +1277,7 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.5 h1:s5PTfem8p8EbKQOctVV53k6jCJt3UX4IEJzwh+C324Q=
github.com/stretchr/testify v1.7.5/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/syndtr/gocapability v0.0.0-20170704070218-db04d3cc01c8/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
github.com/syndtr/gocapability v0.0.0-20180916011248-d98352740cb2/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
Expand Down Expand Up @@ -1382,14 +1386,20 @@ go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/multierr v1.8.0 h1:dg6GjLku4EH+249NNmoIciG9N/jURbDG+pFlTkhzIC8=
go.uber.org/multierr v1.8.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA=
go.uber.org/zap v1.9.1/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
go.uber.org/zap v1.17.0/go.mod h1:MXVU+bhUf/A7Xi2HNOnopQOrmycQ5Ih87HtOu4q5SSo=
go.uber.org/zap v1.21.0 h1:WefMeulhovoZ2sYXz7st6K0sLj7bBhpiFaud4r4zST8=
go.uber.org/zap v1.21.0/go.mod h1:wjWOCqI0f2ZZrJF/UufIOkiC8ii6tm1iqIsLo76RfJw=
go4.org v0.0.0-20180809161055-417644f6feb5/go.mod h1:MkTOUMDaeVYJUOUsaDXIhWPZYa1yOyC1qaOBpL57BhE=
golang.org/x/build v0.0.0-20190111050920-041ab4dc3f9d/go.mod h1:OWs+y06UdEOHN4y+MfF/py+xQ/tYqIWW03b70/CG9Rw=
golang.org/x/crypto v0.0.0-20171113213409-9f005a07e0d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
Expand Down
Loading

0 comments on commit 1371ace

Please sign in to comment.