Skip to content

Commit

Permalink
Update boilerplate code
Browse files Browse the repository at this point in the history
  • Loading branch information
chetanyakan committed Dec 20, 2018
1 parent 369093d commit 98e2820
Show file tree
Hide file tree
Showing 14 changed files with 267 additions and 105 deletions.
13 changes: 10 additions & 3 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,26 @@ insert_final_newline = true
trim_trailing_whitespace = true
charset = utf-8

[*.{go,scss,js,jsx,json,html}]
[*.go]
indent_style = tab

[webapp/package.json]
[*.{js,jsx,json,html}]
indent_style = space
indent_size = 4

[webapp/package.json]
indent_size = 2

[webapp/.eslintrc.json]
indent_style = tab
indent_size = 2

[Makefile]
indent_style = tab

[*.scss]
indent_style = space
indent_size = 4

[*.md]
trim_trailing_whitespace = false
indent_style = space
30 changes: 18 additions & 12 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
# Binaries for programs and plugins
*.exe
*.dll
*.so
*.dylib

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

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

# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
.glide/

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
Expand All @@ -14,12 +28,6 @@ pids
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

Expand All @@ -36,9 +44,6 @@ build/Release
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# Optional npm cache directory
.npm

Expand All @@ -62,8 +67,9 @@ typings/

# Plugin generated files
dist
webapp/dist
webapp/node_modules
plugin.exe
.npminstall
*.tar.gz
server/vendor/
.history/
coverage.txt
93 changes: 75 additions & 18 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
GO := go
GOARCH=amd64
GO:=go
GOARCH:=amd64
GOOS=$(shell uname -s | tr '[:upper:]' '[:lower:]')
GOPATH ?= $(GOPATH:)

PLUGINNAME=boilerplate
PLUGINVERSION=v$(shell echo `cat plugin.json | grep -Po '"'"version"'"\s*:\s*"\K([^"]*)'`)
MANIFEST_FILE ?= plugin.json

PLUGINNAME=$(shell echo `grep -Po '"'"id"'"\s*:\s*"\K([^"]*)' $(MANIFEST_FILE)`)
PLUGINVERSION=v$(shell echo `grep -Po '"'"version"'"\s*:\s*"\K([^"]*)' $(MANIFEST_FILE)`)
PACKAGENAME=mattermost-plugin-$(PLUGINNAME)-$(PLUGINVERSION)
HAS_WEBAPP=$(shell if [ "$(shell grep -E '[\^"]webapp["][ ]*[:]' $(MANIFEST_FILE) | wc -l)" -gt "0" ]; then echo "true"; fi)
HAS_SERVER=$(shell if [ "$(shell grep -E '[\^"]server["][ ]*[:]' $(MANIFEST_FILE) | wc -l)" -gt "0" ]; then echo "true"; fi)
TMPFILEGOLINT=golint.tmp
BLACK=`tput setaf 0`
RED=`tput setaf 1`
GREEN=`tput setaf 2`
Expand All @@ -20,30 +27,44 @@ BOLD=`tput bold`
INVERSE=`tput rev`
RESET=`tput sgr0`
.PHONY: default test clean check-style checkjs checkgo govet golint gofmt .distclean dist fix fixjs fixgo github-release
.PHONY: default pre-run test clean check-style check-js check-go govet golint gofmt .distclean dist fix format fix-js fix-go github-release
default: check-style test dist
pre-run:
ifneq ($(HAS_WEBAPP),)
@echo "export const PLUGIN_NAME = '`echo $(PLUGINNAME)`';\
" > webapp/src/constants/manifest.js
endif
ifneq ($(HAS_SERVER),)
@echo "package config\n\nconst (\n\
PluginName = \""`echo $(PLUGINNAME)`"\"\n)" > server/config/manifest.go
endif
github-release:
@if [ $$(git status --porcelain | wc -l) != "0" -o $$(git rev-list HEAD@{upstream}..HEAD | wc -l) != "0" ]; \
then echo ${RED}"local repo is not clean"${RESET}; exit 1; fi;
@echo ${BOLD}"Creating a tag to trigger circleci github release\n"${RESET}
@echo ${BOLD}"Creating a tag to trigger circleci build-and-release job\n"${RESET}
git tag $(PLUGINVERSION)
git push origin $(PLUGINVERSION)
check-style: .npminstall vendor
@echo ${BOLD}"Checking for style guide compliance\n"${RESET}
@make checkjs
@make checkgo
@make check-js
@make check-go
checkjs: webapp
check-js:
ifneq ($(HAS_WEBAPP),)
@echo ${BOLD}Running ESLINT${RESET}
@cd webapp && npm run lint
@echo ${GREEN}"eslint success\n"${RESET}
endif
checkgo: server govet golint gofmt
check-go: server govet golint gofmt
govet:
ifneq ($(HAS_SERVER),)
@go tool vet 2>/dev/null ; if [ $$? -eq 3 ]; then \
echo "--> installing govet"; \
go get golang.org/x/tools/cmd/vet; \
Expand All @@ -53,28 +74,41 @@ govet:
$(eval PKGS := $(shell go list ./... | grep -v /vendor/))
@$(GO) vet $(PKGS)
@echo ${GREEN}"govet success\n"${RESET}
endif
golint:
ifneq ($(HAS_SERVER),)
@command -v golint >/dev/null ; if [ $$? -ne 0 ]; then \
echo "--> installing golint"; \
go get -u golang.org/x/lint/golint; \
fi
@echo ${BOLD}Running GOLINT${RESET}${RED}
@cd server
$(eval PKGS := $(shell go list ./... | grep -v /vendor/))
@# grep -v is used to ignore specific linting rules and not echo their failure terminal
@touch $(TMPFILEGOLINT)
-@for pkg in $(PKGS) ; do \
golint $$pkg | grep -v "have comment" | grep -v "comment on exported" | grep -v "lint suggestions" ; \
done && echo ${RED}"golint failure\n"${RESET} || echo ${GREEN}"golint success\n"${RESET}
echo `golint $$pkg | grep -v "have comment" | grep -v "comment on exported" | grep -v "lint suggestions"` >> $(TMPFILEGOLINT) ; \
done
@grep -Ev "^$$" $(TMPFILEGOLINT) || true
@if [ "$$(grep -Ev "^$$" $(TMPFILEGOLINT) | wc -l)" -gt "0" ]; then \
rm -f $(TMPFILEGOLINT); echo "golint failure\n"${RESET}; exit 1; else \
rm -f $(TMPFILEGOLINT); echo ${GREEN}"golint success\n"${RESET}; \
fi
endif
format: fix
fix: fixjs fixgo
fix: fix-js fix-go
fixjs:
fix-js:
ifneq ($(HAS_WEBAPP),)
@echo ${BOLD}Formatting js giles${RESET}
@cd webapp && npm run fix
@echo "formatted js files\n"
endif
fixgo:
fix-go:
ifneq ($(HAS_SERVER),)
@command -v goimports >/dev/null ; if [ $$? -ne 0 ]; then \
echo "--> installing goimports"; \
go get golang.org/x/tools/cmd/goimports; \
Expand All @@ -83,8 +117,10 @@ fixgo:
@cd server
@find ./ -type f -name "*.go" -not -path "./server/vendor/*" -exec goimports -w {} \;
@echo "formatted go files\n"
endif
gofmt:
ifneq ($(HAS_SERVER),)
@echo ${BOLD}Running GOFMT${RESET}${RED}
@for package in $$(go list ./server/...); do \
files=$$(go list -f '{{range .GoFiles}}{{$$.Dir}}/{{.}} {{end}}' $$package); \
Expand All @@ -98,36 +134,50 @@ gofmt:
fi; \
done
@echo ${GREEN}"gofmt success\n"${RESET}
endif
test:
ifneq ($(HAS_SERVER),)
go test -v -coverprofile=coverage.txt ./...
endif
.npminstall: webapp/package-lock.json
ifneq ($(HAS_WEBAPP),)
@echo ${BOLD}"Getting dependencies using npm\n"${RESET}
cd webapp && npm install
@echo "\n"
endif
vendor: server/glide.lock
ifneq ($(HAS_SERVER),)
@echo ${BOLD}"Getting dependencies using glide\n"${RESET}
cd server && go get github.com/Masterminds/glide
cd server && $(shell go env GOPATH)/bin/glide install
@echo "\n"
endif
dist: .distclean check-style plugin.json
dist: .distclean check-style $(MANIFEST_FILE)
@echo ${BOLD}"Building plugin\n"${RESET}
ifneq ($(HAS_WEBAPP),)
# Build and copy files from webapp
cd webapp && npm run build
mkdir -p dist/$(PLUGINNAME)/webapp
cp -r webapp/dist/* dist/$(PLUGINNAME)/webapp/
else ifneq ($(HAS_SERVER),)
mkdir -p dist/$(PLUGINNAME)/
endif
ifneq ($(HAS_SERVER),)
# Build files from server
cd server && go get github.com/mitchellh/gox
$(shell go env GOPATH)/bin/gox -osarch='darwin/amd64 linux/amd64 windows/amd64' -output 'dist/intermediate/plugin_{{.OS}}_{{.Arch}}' ./server
endif
# Copy plugin files
cp plugin.json dist/$(PLUGINNAME)/
cp $(MANIFEST_FILE) dist/$(PLUGINNAME)/
ifneq ($(HAS_SERVER),)
# Copy server executables & compress plugin
mkdir -p dist/$(PLUGINNAME)/server
Expand All @@ -139,14 +189,21 @@ dist: .distclean check-style plugin.json
mv dist/intermediate/plugin_windows_amd64.exe dist/$(PLUGINNAME)/server/plugin.exe
cd dist && tar -zcvf $(PACKAGENAME)-windows-amd64.tar.gz $(PLUGINNAME)/*
else ifneq ($(HAS_WEBAPP),)
cd dist && tar -zcvf $(PACKAGENAME).tar.gz $(PLUGINNAME)/*
endif
# Clean up temp files
rm -rf dist/$(PLUGINNAME)
rm -rf dist/intermediate
ifneq ($(HAS_SERVER),)
@echo Linux plugin built at: dist/$(PACKAGENAME)-linux-amd64.tar.gz
@echo MacOS X plugin built at: dist/$(PACKAGENAME)-darwin-amd64.tar.gz
@echo Windows plugin built at: dist/$(PACKAGENAME)-windows-amd64.tar.gz
else ifneq ($(HAS_WEBAPP),)
@echo Cross-platform plugin built at: dist/$(PACKAGENAME)-amd64.tar.gz
endif
.distclean:
@echo ${BOLD}"Cleaning dist files\n"${RESET}
Expand Down
1 change: 0 additions & 1 deletion server/config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
)

const (
PluginName = "boilerplate"
CommandPrefix = PluginName
URLMappingKeyPrefix = "url_"
ServerExeToWebappRootPath = "/../webapp"
Expand Down
5 changes: 5 additions & 0 deletions server/config/manifest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package config

const (
PluginName = "boilerplate"
)
2 changes: 1 addition & 1 deletion server/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ var Endpoints = map[string]*Endpoint{
// Authenticated verifies if provided request is performed by a logged-in Mattermost user.
func Authenticated(w http.ResponseWriter, r *http.Request) bool {
userID := r.Header.Get(config.HeaderMattermostUserID)

if userID == "" {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return false
}

return true
}
10 changes: 5 additions & 5 deletions server/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (p *Plugin) OnActivate() error {
}

func (p *Plugin) setupStaticFileServer() error {
var exe, err = os.Executable()
exe, err := os.Executable()
if err != nil {
return err
}
Expand All @@ -52,7 +52,6 @@ func (p *Plugin) setupStaticFileServer() error {

func (p *Plugin) OnConfigurationChange() error {
if config.Mattermost != nil {

var configuration config.Configuration

if err := config.Mattermost.LoadPluginConfiguration(&configuration); err != nil {
Expand Down Expand Up @@ -120,15 +119,16 @@ func (p *Plugin) prepareContext(args *model.CommandArgs) command.Context {
}

func (p *Plugin) ServeHTTP(c *plugin.Context, w http.ResponseWriter, r *http.Request) {
var conf = config.GetConfig()
conf := config.GetConfig()

if err := conf.IsValid(); err != nil {
p.API.LogError("This plugin is not configured: " + err.Error())
http.Error(w, "This plugin is not configured.", http.StatusNotImplemented)
return
}

var path = r.URL.Path
var endpoint = controller.Endpoints[path]
path := r.URL.Path
endpoint := controller.Endpoints[path]

if endpoint == nil {
p.handler.ServeHTTP(w, r)
Expand Down
2 changes: 1 addition & 1 deletion server/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func Min(a, b int) int {
return b
}

// CommandError can be used to return an empheral message as the response for a slash command
// CommandError can be used to return an ephemeral message as the response for a slash command
func CommandError(msg string) (*model.CommandResponse, *model.AppError) {
return &model.CommandResponse{
Type: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
Expand Down
Loading

0 comments on commit 98e2820

Please sign in to comment.