Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically format imports #151

Merged
merged 3 commits into from
Dec 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,5 @@ tags
.vscode/*
.history
# End of https://www.gitignore.io/api/go,vim,emacs,visualstudiocode
fmt.log
import.log
90 changes: 90 additions & 0 deletions .travis/import-order-cleanup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import argparse

def cleanup_imports_and_return(imports):
os_packages = []
jaeger_packages = []
thirdparty_packages = []
for i in imports:
if i.strip() == "":
continue
if i.find("github.com/jaegertracing/jaeger-operator/") != -1:
jaeger_packages.append(i)
elif i.find(".com") != -1 or i.find(".net") != -1 or i.find(".org") != -1 or i.find(".in") != -1 or i.find("k8s.") != -1:
thirdparty_packages.append(i)
else:
os_packages.append(i)

l = []
needs_new_line = False
if os_packages:
l.extend(os_packages)
needs_new_line = True
if thirdparty_packages:
if needs_new_line:
l.append("")
l.extend(thirdparty_packages)
needs_new_line = True
if jaeger_packages:
if needs_new_line:
l.append("")
l.extend(jaeger_packages)

imports_reordered = imports != l
l.insert(0, "import (")
l.append(")")
return l, imports_reordered

def parse_go_file(f):
with open(f, 'r') as go_file:
lines = [i.rstrip() for i in go_file.readlines()]
in_import_block = False
imports_reordered = False
imports = []
output_lines = []
for line in lines:
if in_import_block:
endIdx = line.find(")")
if endIdx != -1:
in_import_block = False
ordered_imports, imports_reordered = cleanup_imports_and_return(imports)
output_lines.extend(ordered_imports)
imports = []
continue
imports.append(line)
else:
importIdx = line.find("import (")
if importIdx != -1:
in_import_block = True
continue
output_lines.append(line)
output_lines.append("")
return "\n".join(output_lines), imports_reordered


def main():
parser = argparse.ArgumentParser(
description='Tool to make cleaning up import orders easily')

parser.add_argument('-o', '--output', default='stdout',
choices=['inplace', 'stdout'],
help='output target [default: stdout]')

parser.add_argument('-t', '--target',
help='list of filenames to operate upon',
nargs='+',
required=True)

args = parser.parse_args()
output = args.output
go_files = args.target

for f in go_files:
parsed, imports_reordered = parse_go_file(f)
if output == "stdout" and imports_reordered:
print(f + " imports out of order")
else:
with open(f, 'w') as ofile:
ofile.write(parsed)

if __name__ == '__main__':
main()
5 changes: 5 additions & 0 deletions .travis/import-order-cleanup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

set -e

python .travis/import-order-cleanup.py -o $1 -t $(git ls-files "*\.go" | grep -v -e vendor)
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ GO_FLAGS ?= GOOS=linux GOARCH=amd64 CGO_ENABLED=0
KUBERNETES_CONFIG ?= "$(HOME)/.kube/config"
WATCH_NAMESPACE ?= default
BIN_DIR ?= "build/_output/bin"
IMPORT_LOG=import.log
FMT_LOG=fmt.log

OPERATOR_NAME ?= jaeger-operator
NAMESPACE ?= "$(USER)"
Expand All @@ -20,7 +22,9 @@ PACKAGES := $(shell go list ./cmd/... ./pkg/...)
.PHONY: check
check:
@echo Checking...
@$(foreach file, $(shell go fmt $(PACKAGES) 2>&1), echo "Some files need formatting. Failing." || exit 1)
@go fmt $(PACKAGES) > $(FMT_LOG)
@.travis/import-order-cleanup.sh stdout > $(IMPORT_LOG)
@[ ! -s "$(FMT_LOG)" -a ! -s "$(IMPORT_LOG)" ] || (echo "Go fmt, license check, or import ordering failures, run 'make format'" | cat - $(FMT_LOG) $(IMPORT_LOG) && false)

.PHONY: ensure-generate-is-noop
ensure-generate-is-noop: generate
Expand All @@ -29,6 +33,7 @@ ensure-generate-is-noop: generate
.PHONY: format
format:
@echo Formatting code...
@.travis/import-order-cleanup.sh inplace
@go fmt $(PACKAGES)

.PHONY: lint
Expand Down
3 changes: 1 addition & 2 deletions pkg/service/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package service
import (
"fmt"

"k8s.io/apimachinery/pkg/util/intstr"

"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"

"github.com/jaegertracing/jaeger-operator/pkg/apis/io/v1alpha1"
)
Expand Down
3 changes: 2 additions & 1 deletion pkg/storage/dependency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package storage
import (
"testing"

"github.com/jaegertracing/jaeger-operator/pkg/apis/io/v1alpha1"
"github.com/stretchr/testify/assert"

"github.com/jaegertracing/jaeger-operator/pkg/apis/io/v1alpha1"
)

func TestDefaultDependencies(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions pkg/strategy/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"context"
"strings"

"github.com/jaegertracing/jaeger-operator/pkg/apis/io/v1alpha1"
"github.com/jaegertracing/jaeger-operator/pkg/storage"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
batchv1 "k8s.io/api/batch/v1"
"k8s.io/apimachinery/pkg/runtime"

"github.com/jaegertracing/jaeger-operator/pkg/apis/io/v1alpha1"
"github.com/jaegertracing/jaeger-operator/pkg/storage"
)

// S knows what type of deployments to build based on a given spec
Expand Down
3 changes: 2 additions & 1 deletion pkg/util/util.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package util

import (
"github.com/jaegertracing/jaeger-operator/pkg/apis/io/v1alpha1"
"k8s.io/api/core/v1"

"github.com/jaegertracing/jaeger-operator/pkg/apis/io/v1alpha1"
)

// removeDuplicatedVolumes returns a unique list of Volumes based on Volume names. Only the first item is kept.
Expand Down
7 changes: 4 additions & 3 deletions test/e2e/all_in_one_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import (
"testing"
"time"

"github.com/jaegertracing/jaeger-operator/pkg/apis/io/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
framework "github.com/operator-framework/operator-sdk/pkg/test"
"github.com/operator-framework/operator-sdk/pkg/test/e2eutil"
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"

"github.com/jaegertracing/jaeger-operator/pkg/apis/io/v1alpha1"
)

const TrackingID = "MyTrackingId"
Expand Down
5 changes: 3 additions & 2 deletions test/e2e/cassandra.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"fmt"
"testing"

"github.com/jaegertracing/jaeger-operator/pkg/apis/io/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
framework "github.com/operator-framework/operator-sdk/pkg/test"
"github.com/operator-framework/operator-sdk/pkg/test/e2eutil"
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/jaegertracing/jaeger-operator/pkg/apis/io/v1alpha1"
)

// Cassandra runs a test with Cassandra as the backing storage
Expand Down
9 changes: 5 additions & 4 deletions test/e2e/daemonset.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ import (
"testing"
"time"

"github.com/jaegertracing/jaeger-operator/pkg/apis/io/v1alpha1"
framework "github.com/operator-framework/operator-sdk/pkg/test"
"github.com/operator-framework/operator-sdk/pkg/test/e2eutil"
"github.com/sirupsen/logrus"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/wait"
framework "github.com/operator-framework/operator-sdk/pkg/test"
"github.com/operator-framework/operator-sdk/pkg/test/e2eutil"
"github.com/sirupsen/logrus"

"github.com/jaegertracing/jaeger-operator/pkg/apis/io/v1alpha1"
)

// DaemonSet runs a test with the agent as DaemonSet
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/jaeger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"testing"
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
framework "github.com/operator-framework/operator-sdk/pkg/test"
"github.com/operator-framework/operator-sdk/pkg/test/e2eutil"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/jaegertracing/jaeger-operator/pkg/apis"
"github.com/jaegertracing/jaeger-operator/pkg/apis/io/v1alpha1"
Expand Down
5 changes: 3 additions & 2 deletions test/e2e/production_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"fmt"
"testing"

"github.com/jaegertracing/jaeger-operator/pkg/apis/io/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
framework "github.com/operator-framework/operator-sdk/pkg/test"
"github.com/operator-framework/operator-sdk/pkg/test/e2eutil"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/jaegertracing/jaeger-operator/pkg/apis/io/v1alpha1"
)

func SimpleProd(t *testing.T) {
Expand Down
9 changes: 5 additions & 4 deletions test/e2e/sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ import (
"testing"
"time"

"github.com/jaegertracing/jaeger-operator/pkg/apis/io/v1alpha1"
"github.com/jaegertracing/jaeger-operator/pkg/inject"
framework "github.com/operator-framework/operator-sdk/pkg/test"
"github.com/operator-framework/operator-sdk/pkg/test/e2eutil"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/wait"
framework "github.com/operator-framework/operator-sdk/pkg/test"
"github.com/operator-framework/operator-sdk/pkg/test/e2eutil"

"github.com/jaegertracing/jaeger-operator/pkg/apis/io/v1alpha1"
"github.com/jaegertracing/jaeger-operator/pkg/inject"
)

// Sidecar runs a test with the agent as sidecar
Expand Down
5 changes: 3 additions & 2 deletions test/e2e/simplest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"fmt"
"testing"

"github.com/jaegertracing/jaeger-operator/pkg/apis/io/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
framework "github.com/operator-framework/operator-sdk/pkg/test"
"github.com/operator-framework/operator-sdk/pkg/test/e2eutil"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/jaegertracing/jaeger-operator/pkg/apis/io/v1alpha1"
)

func SimplestJaeger(t *testing.T) {
Expand Down