Skip to content

Commit

Permalink
Miperel/lint (microsoft#199)
Browse files Browse the repository at this point in the history
Added golint
  • Loading branch information
michaelperel authored Nov 8, 2019
1 parent fb2d779 commit bc6ba36
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 44 deletions.
7 changes: 7 additions & 0 deletions .azdo/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ jobs:
- bash: |
#!/usr/bin/env bash
cd "$BUILD_SOURCESDIRECTORY"
./scripts/fmt-check-go.sh
displayName: 'Run Format Check'
- bash: |
#!/usr/bin/env bash
cd "$BUILD_SOURCESDIRECTORY"
go get -u golang.org/x/lint/golint
./scripts/lint-check-go.sh
displayName: 'Run Lint Check'
Expand Down
1 change: 0 additions & 1 deletion .devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
],
"settings": {
"terminal.integrated.shell.linux": "/bin/bash",
"go.inferGopath": true,
"go.useLanguageServer": true
},
"extensions": [
Expand Down
41 changes: 21 additions & 20 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
FROM ubuntu:bionic
ARG GO_VERSION=1.13
ARG TERRAFORM_VERSION=0.12.13
RUN apt-get update && \
apt-get install wget unzip gcc git -y && \
wget "https://dl.google.com/go/go${GO_VERSION}.linux-amd64.tar.gz" && \
tar -xvf "go${GO_VERSION}.linux-amd64.tar.gz" && \
mv go /usr/local && \
wget "https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip" && \
unzip "./terraform_${TERRAFORM_VERSION}_linux_amd64.zip" -d /usr/local/bin/ && \
rm "go${GO_VERSION}.linux-amd64.tar.gz" && \
rm "terraform_${TERRAFORM_VERSION}_linux_amd64.zip" && \
rm -rf /var/lib/apt/lists/*
ENV PATH="${PATH}:/usr/local/go/bin"
RUN git clone https://github.com/microsoft/terraform-provider-azuredevops.git && \
cd terraform-provider-azuredevops && \
./scripts/build.sh && \
./scripts/local-install.sh && \
rm -rf ../terrform-provider-azuredevops
WORKDIR /root/workspace
FROM ubuntu:bionic
ARG GO_VERSION=1.13
ARG TERRAFORM_VERSION=0.12.13
RUN apt-get update && \
apt-get install wget unzip gcc git -y && \
wget "https://dl.google.com/go/go${GO_VERSION}.linux-amd64.tar.gz" && \
tar -xvf "go${GO_VERSION}.linux-amd64.tar.gz" && \
mv go /usr/local && \
wget "https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip" && \
unzip "./terraform_${TERRAFORM_VERSION}_linux_amd64.zip" -d /usr/local/bin/ && \
rm "go${GO_VERSION}.linux-amd64.tar.gz" && \
rm "terraform_${TERRAFORM_VERSION}_linux_amd64.zip" && \
rm -rf /var/lib/apt/lists/*
ENV GOPATH=/root/go
ENV PATH="${PATH}:${GOPATH}/bin:/usr/local/go/bin"
RUN git clone https://github.com/microsoft/terraform-provider-azuredevops.git && \
cd terraform-provider-azuredevops && \
./scripts/build.sh && \
./scripts/local-install.sh && \
rm -rf ../terrform-provider-azuredevops
WORKDIR /root/workspace
4 changes: 2 additions & 2 deletions azuredevops/resource_build_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func createBuildDefinition(clients *aggregatedClient, buildDefinition *build.Bui

func resourceBuildDefinitionRead(d *schema.ResourceData, m interface{}) error {
clients := m.(*aggregatedClient)
projectID, buildDefinitionID, err := tfhelper.ParseProjectIdAndResourceId(d)
projectID, buildDefinitionID, err := tfhelper.ParseProjectIDAndResourceID(d)

if err != nil {
return err
Expand All @@ -170,7 +170,7 @@ func resourceBuildDefinitionDelete(d *schema.ResourceData, m interface{}) error
}

clients := m.(*aggregatedClient)
projectID, buildDefinitionID, err := tfhelper.ParseProjectIdAndResourceId(d)
projectID, buildDefinitionID, err := tfhelper.ParseProjectIDAndResourceID(d)
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions azuredevops/resource_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
"github.com/microsoft/azure-devops-go-api/azuredevops/operations"
)

var projectCreateTimeoutSeconds time.Duration = 60
var projectDeleteTimeoutSeconds time.Duration = 60
var projectCreateTimeout time.Duration = 60
var projectDeleteTimeout time.Duration = 60

func resourceProject() *schema.Resource {
return &schema.Resource{
Expand Down Expand Up @@ -82,7 +82,7 @@ func resourceProjectCreate(d *schema.ResourceData, m interface{}) error {
return fmt.Errorf("Error converting terraform data model to Azure DevOps project reference: %+v", err)
}

err = createProject(clients, project, projectCreateTimeoutSeconds)
err = createProject(clients, project, projectCreateTimeout)
if err != nil {
return fmt.Errorf("Error creating project: %v", err)
}
Expand Down Expand Up @@ -176,7 +176,7 @@ func resourceProjectUpdate(d *schema.ResourceData, m interface{}) error {
return fmt.Errorf("Error converting terraform data model to AzDO project reference: %+v", err)
}

err = updateProject(clients, project, projectCreateTimeoutSeconds)
err = updateProject(clients, project, projectCreateTimeout)
if err != nil {
return fmt.Errorf("Error updating project: %v", err)
}
Expand All @@ -203,7 +203,7 @@ func resourceProjectDelete(d *schema.ResourceData, m interface{}) error {
clients := m.(*aggregatedClient)
id := d.Id()

err := deleteProject(clients, id, projectDeleteTimeoutSeconds)
err := deleteProject(clients, id, projectDeleteTimeout)
if err != nil {
return fmt.Errorf("Error deleting project: %v", err)
}
Expand Down
6 changes: 3 additions & 3 deletions azuredevops/resource_variable_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func resourceVariableGroupCreate(d *schema.ResourceData, m interface{}) error {
func resourceVariableGroupRead(d *schema.ResourceData, m interface{}) error {
clients := m.(*aggregatedClient)

projectID, variableGroupID, err := tfhelper.ParseProjectIdAndResourceId(d)
projectID, variableGroupID, err := tfhelper.ParseProjectIDAndResourceID(d)
if err != nil {
return fmt.Errorf("Error parsing the variable group ID from the Terraform resource data: %v", err)
}
Expand All @@ -110,7 +110,7 @@ func resourceVariableGroupUpdate(d *schema.ResourceData, m interface{}) error {
clients := m.(*aggregatedClient)
variableGroupParams, projectID := expandVariableGroupParameters(d)

_, variableGroupID, err := tfhelper.ParseProjectIdAndResourceId(d)
_, variableGroupID, err := tfhelper.ParseProjectIDAndResourceID(d)
if err != nil {
return fmt.Errorf("Error parsing the variable group ID from the Terraform resource data: %v", err)
}
Expand All @@ -126,7 +126,7 @@ func resourceVariableGroupUpdate(d *schema.ResourceData, m interface{}) error {

func resourceVariableGroupDelete(d *schema.ResourceData, m interface{}) error {
clients := m.(*aggregatedClient)
projectID, variableGroupID, err := tfhelper.ParseProjectIdAndResourceId(d)
projectID, variableGroupID, err := tfhelper.ParseProjectIDAndResourceID(d)
if err != nil {
return fmt.Errorf("Error parsing the variable group ID from the Terraform resource data: %v", err)
}
Expand Down
2 changes: 2 additions & 0 deletions azuredevops/utils/suppress/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

// CaseDifference reports whether old and new, interpreted as UTF-8 strings,
// are equal under Unicode case-folding.
func CaseDifference(_, old, new string, _ *schema.ResourceData) bool {
return strings.EqualFold(old, new)
}
3 changes: 2 additions & 1 deletion azuredevops/utils/tfhelper/tfhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ func GenerateSecreteMemoSchema(secretKey string) (string, *schema.Schema) {
return calcSecretHashKey(secretKey), &out
}

func ParseProjectIdAndResourceId(d *schema.ResourceData) (string, int, error) {
// ParseProjectIDAndResourceID parses from the schema's resource data.
func ParseProjectIDAndResourceID(d *schema.ResourceData) (string, int, error) {
projectID := d.Get("project_id").(string)
resourceID, err := strconv.Atoi(d.Id())

Expand Down
5 changes: 4 additions & 1 deletion azuredevops/utils/validate/uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
"github.com/hashicorp/go-uuid"
)

//uuid regex helper
// UUIDRegExp is a regex helper.
var UUIDRegExp = regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$")

// UUID parses a UUID, returning warnings and errors.
func UUID(i interface{}, k string) (warnings []string, errors []error) {
v, ok := i.(string)
if !ok {
Expand All @@ -23,6 +24,8 @@ func UUID(i interface{}, k string) (warnings []string, errors []error) {

return warnings, errors
}

// UUIDOrEmpty parses a UUID, returning nil for warnings if i is empty.
func UUIDOrEmpty(i interface{}, k string) (warnings []string, errors []error) {
v, ok := i.(string)
if !ok {
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ require (
github.com/stretchr/testify v1.3.0
github.com/vmihailenco/msgpack v4.0.1+incompatible // indirect
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586
golang.org/x/lint v0.0.0-20190930215403-16217165b5de // indirect
golang.org/x/net v0.0.0-20191009170851-d66e71096ffb // indirect
golang.org/x/sys v0.0.0-20190804053845-51ab0e2deafa // indirect
golang.org/x/tools v0.0.0-20191107185733-c07e1c6ef61c // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,10 @@ golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTk
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/lint v0.0.0-20190409202823-959b441ac422 h1:QzoH/1pFpZguR8NrRHLcO6jKqfv2zpuSqZLgdm7ZmjI=
golang.org/x/lint v0.0.0-20190409202823-959b441ac422/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180811021610-c39426892332/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -274,6 +277,9 @@ golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBn
golang.org/x/tools v0.0.0-20190606124116-d0a3d012864b/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20190628153133-6cdbf07be9d0 h1:Dh6fw+p6FyRl5x/FvNswO1ji0lIGzm3KP8Y9VkS9PTE=
golang.org/x/tools v0.0.0-20190628153133-6cdbf07be9d0/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20191107185733-c07e1c6ef61c h1:Qt1WYboHEeHaS/fP0fLmCKuwYQtLpZ4GkGewvC29TaE=
golang.org/x/tools v0.0.0-20191107185733-c07e1c6ef61c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M=
google.golang.org/api v0.8.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg=
Expand Down
21 changes: 21 additions & 0 deletions scripts/fmt-check-go.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash

set -euo pipefail

. $(dirname $0)/commons.sh

info "Formatting Go Files... If this fails, run 'find . -name \"*.go\" | xargs gofmt -s -w' to fix"
(
cd "$SOURCE_DIR"

# This runs a go fmt on each file without using the 'go fmt ./...' syntax.
# This is advantageous because it avoids having to download all of the go
# dependencies that would have been triggered by using the './...' syntax.
FILES_WITH_FMT_ISSUES=$(find . -name "*.go" | xargs gofmt -s -l | wc -l)

# convert to integer...
FILES_WITH_FMT_ISSUES=$(($FILES_WITH_FMT_ISSUES + 0))

info "Found $FILES_WITH_FMT_ISSUES with formatting issues..."
exit $FILES_WITH_FMT_ISSUES
)
14 changes: 3 additions & 11 deletions scripts/lint-check-go.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,10 @@ set -euo pipefail

. $(dirname $0)/commons.sh

info "Linting Go Files... If this fails, run 'find . -name \"*.go\" | xargs gofmt -s -w' to fix"
info "Linting Go Files... If this fails, run 'golint ./...' to see errors"
(
cd "$SOURCE_DIR"

# This runs a go fmt on each file without using the 'go fmt ./...' syntax.
# This is advantageous because it avoids having to download all of the go
# dependencies that would have been triggered by using the './...' syntax.
FILES_WITH_FMT_ISSUES=$(find . -name "*.go" | xargs gofmt -s -l | wc -l)

# convert to integer...
FILES_WITH_FMT_ISSUES=$(($FILES_WITH_FMT_ISSUES + 0))

info "Found $FILES_WITH_FMT_ISSUES with linting issues..."
exit $FILES_WITH_FMT_ISSUES
GOLINT="$(go list -f {{.Target}} golang.org/x/lint/golint)"
"$GOLINT" -set_exit_status ./...
)

0 comments on commit bc6ba36

Please sign in to comment.