Skip to content

Commit

Permalink
Add ci build and release flow (launchdarkly-labs#6)
Browse files Browse the repository at this point in the history
Also make a bunch of lint fixes and start running golangci lint.
  • Loading branch information
ashanbrown authored Jan 11, 2019
1 parent 009cbe0 commit c1dd36e
Show file tree
Hide file tree
Showing 16 changed files with 435 additions and 231 deletions.
105 changes: 105 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
version: 2

experimental:
notify:
branches:
only:
- master

defaults:
environment: &environment
CIRCLE_TEST_REPORTS: /tmp/circle-reports
CIRCLE_ARTIFACTS: /tmp/circle-artifacts
COMMON_GO_PACKAGES: >
github.com/jstemmer/go-junit-report
github.com/kyoh86/richgo
build_steps: &build_steps
working_directory: &working_dir /go/src/github.com/launchdarkly/ldc
steps:
- checkout
- run: go get -u $COMMON_GO_PACKAGES
- run: curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
- run: dep ensure -dry-run
- run: make init
- run: make lint
- run:
name: Set up Code Climate test-reporter
command: |
curl -sS -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
chmod +x ./cc-test-reporter
- run:
name: Run tests
command: |
mkdir -p $CIRCLE_TEST_REPORTS
mkdir -p $CIRCLE_ARTIFACTS
trap "go-junit-report < $CIRCLE_ARTIFACTS/report.txt > $CIRCLE_TEST_REPORTS/junit.xml" EXIT
if [ -z "$DISABLE_COVERAGE" ]; then
go_cover_args="-covermode=atomic -coverpkg=./... -coverprofile /tmp/circle-artifacts/coverage.txt"
fi
go test -race $go_cover_args -v $(go list ./... | grep -v /vendor/) | tee >(richgo testfilter) > $CIRCLE_ARTIFACTS/report.txt
if [[ -z "$DISABLE_COVERAGE" && -n "$CC_TEST_REPORTER_ID" ]]; then
./cc-test-reporter format-coverage $CIRCLE_ARTIFACTS/coverage.txt -t gocov --output $CIRCLE_ARTIFACTS/coverage.json
./cc-test-reporter upload-coverage --input $CIRCLE_ARTIFACTS/coverage.json
fi
- run:
name: Generate coverage report
command: |
if [ -z "$DISABLE_COVERAGE" ]; then
go tool cover -html=$CIRCLE_ARTIFACTS/coverage.txt -o $CIRCLE_ARTIFACTS/coverage.html
fi
when: always
- store_test_results:
path: /tmp/circle-reports
- store_artifacts:
path: /tmp/circle-artifacts

jobs:
go-test:
docker:
- &build_image
image: circleci/golang:1.11
environment:
<<: *environment

<<: *build_steps

test-publish:
docker:
- <<: *build_image

working_directory: *working_dir
steps:
- checkout
- run: make release-snapshot
- store_artifacts:
path: dist/

publish:
docker:
- <<: *build_image

working_directory: *working_dir
steps:
- checkout
- run:
name: Releasing and publishing
command: |
make release
- store_artifacts:
path: dist/

workflows:
version: 2
test:
jobs:
- go-test
- test-publish
- publish:
filters:
tags:
only: /\d+\.\d+\.\d+(-.*)?/
branches:
only: /v\d+/
requires:
- go-test
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
/ldc
/ldc
/bin
/dist

28 changes: 28 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
project_name: ldc

run:
deadline: 120s
tests: false

linters:
enable-all: true
disable:
- gochecknoinits
- gochecknoglobals
- gofmt
- lll
- maligned
- prealloc
- unparam
fast: false

linter-settings:
goimports:
local-prefixes: github.com/launchdarkly/ldc

issues:
exclude:
- "G104: Errors unhandled." # Let errcheck handle these
exclude-use-default: false
max-same-issues: 1000
max-per-linter: 1000
10 changes: 10 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
project_name: ldc

builds:
- env:
- CGO_ENABLED=0
main: .
binary: ldc
# Default is `-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}`.
ldflags:
- -s -w -X cmd.Version={{.Version}}
28 changes: 28 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
GOLANGCI_VERSION=v1.12.5

SHELL=/bin/bash

test: lint
go test ./...

lint:
./bin/golangci-lint run ./...

init:
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s $(GOLANGCI_VERSION)

RELEASE_CMD=curl -sL https://git.io/goreleaser | bash -s -- --rm-dist

publish:
$(RELEASE_CMD)

publish-snapshot:
$(RELEASE_CMD)

release:
$(RELEASE_CMD) --skip-publish --skip-validate

release-snapshot:
$(RELEASE_CMD) --skip-publish --skip-validate --snapshot

.PHONY: docker init lint publish-snapshot release release-snapshot
43 changes: 28 additions & 15 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,76 @@ import (
"context"
"net/http"

"github.com/launchdarkly/api-client-go"
ldapi "github.com/launchdarkly/api-client-go"
)

// Auth is the authorization context used by the ali client
var Auth context.Context

// Client is the api client
var Client *ldapi.APIClient

const DefaultServer = "https://app.launchdarkly.com/api/v2"
const defaultServer = "https://app.launchdarkly.com/api/v2"

// CurrentToken is the api token
var CurrentToken string

// CurrentServer is the url of the api to use
var CurrentServer string

// CurrentProject is the project to use
var CurrentProject = "default"

// CurrentEnvironment is the environment to use
var CurrentEnvironment = "production"

type LoggingTransport struct{}
// HTTPClient is an underlying http client with logging transport
var HTTPClient *http.Client

// UserAgent is the current user agent for this version of the command
var UserAgent string

func (lt *LoggingTransport) RoundTrip(req *http.Request) (*http.Response, error) {
type loggingTransport struct{}

func (lt *loggingTransport) RoundTrip(req *http.Request) (*http.Response, error) {
resp, err := http.DefaultTransport.RoundTrip(req)

// TODO this is bad, don't do this
//resp.Body = ioutil.NopCloser(io.TeeReader(resp.Body, os.Stdout))
return resp, err
}

var HttpClient *http.Client

var UserAgent string

// Initialize sets up api for use with a given user agent string
func Initialize(userAgent string) {
UserAgent = userAgent

HttpClient = &http.Client{
Transport: &LoggingTransport{},
HTTPClient = &http.Client{
Transport: &loggingTransport{},
}

Client = ldapi.NewAPIClient(&ldapi.Configuration{
HTTPClient: HttpClient,
HTTPClient: HTTPClient,
UserAgent: UserAgent,
})
}

func init() {
SetServer(DefaultServer)
SetServer(defaultServer)
}

// TODO
// SetServer sets the server url to use
func SetServer(newServer string) {
CurrentServer = newServer
Client = ldapi.NewAPIClient(&ldapi.Configuration{
BasePath: newServer,
HTTPClient: &http.Client{
Transport: &LoggingTransport{},
Transport: &loggingTransport{},
},
UserAgent: "ldc/0.0.1/go",
})

}

// SetToken sets the authorization token
func SetToken(newToken string) {
CurrentToken = newToken
Auth = context.WithValue(context.Background(), ldapi.ContextAPIKey, ldapi.APIKey{
Expand Down
4 changes: 2 additions & 2 deletions cmd/audit_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/launchdarkly/ldc/api"
)

func AddAuditLogCommands(shell *ishell.Shell) {
func addAuditLogCommands(shell *ishell.Shell) {
root := &ishell.Cmd{
Name: "log",
Help: "search audit log entries",
Expand All @@ -38,7 +38,7 @@ func AddAuditLogCommands(shell *ishell.Shell) {
}
table.Render()
if buf.Len() > 1000 {
c.ShowPaged(buf.String())
c.Err(c.ShowPaged(buf.String()))
} else {
c.Println(buf.String())
}
Expand Down
23 changes: 10 additions & 13 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package cmd

import (
"errors"
"gopkg.in/abiosoft/ishell.v2"

ishell "gopkg.in/abiosoft/ishell.v2"

"github.com/launchdarkly/ldc/api"
)

func listConfigs() (map[string]Config, error) {
func listConfigs() (map[string]config, error) {
return configFile, nil
}

Expand All @@ -32,7 +33,7 @@ func configCompleter(args []string) []string {
return completions
}

func getConfigArg(c *ishell.Context) (string, *Config) {
func getConfigArg(c *ishell.Context) (string, *config) {
configs, err := listConfigs()
if err != nil {
c.Err(err)
Expand All @@ -53,19 +54,15 @@ func getConfigArg(c *ishell.Context) (string, *Config) {
return options[choice], &config
}

var foundConfig *Config
configKey := c.Args[0]
for c, v := range configs {
if c == configKey {
foundConfig = &v
break
return c, &v // nolint:scopelint // ok because we break
}
}
if foundConfig == nil {
c.Err(errors.New("config does not exist"))
return "", nil
}
return configKey, foundConfig

c.Err(errors.New("config does not exist"))
return "", nil
}

func selectConfig(c *ishell.Context) {
Expand All @@ -78,10 +75,10 @@ func selectConfig(c *ishell.Context) {
printCurrentSettings(c)
}

func setConfig(name string, config Config) {
func setConfig(name string, config config) {
currentConfig = name
api.CurrentProject = config.DefaultProject
api.CurrentEnvironment = config.DefaultEnvironment
api.SetToken(config.ApiToken)
api.SetToken(config.APIToken)
api.SetServer(config.Server)
}
Loading

0 comments on commit c1dd36e

Please sign in to comment.