Skip to content

Commit

Permalink
Merge pull request #42 from heetch/chore/linting
Browse files Browse the repository at this point in the history
Linting over code
  • Loading branch information
sixstone-qq authored Nov 19, 2021
2 parents 5423271 + f0e7e05 commit 77bb68d
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 61 deletions.
4 changes: 2 additions & 2 deletions admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ func (cmd *adminCmd) addFlags(flags *flag.FlagSet) {

func (cmd *adminCmd) environFlags() map[string]string {
return map[string]string{
"timeout": "KT_ADMIN_TIMEOUT",
"brokers": "KT_BROKERS",
"timeout": ENV_ADMIN_TIMEOUT,
"brokers": ENV_BROKERS,
}
}

Expand Down
40 changes: 2 additions & 38 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ import (
"sync"

"github.com/Shopify/sarama"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/term"
)

const (
ENV_AUTH = "KT_AUTH"
ENV_ADMIN_TIMEOUT = "KT_ADMIN_TIMEOUT"
ENV_BROKERS = "KT_BROKERS"
ENV_TOPIC = "KT_TOPIC"
ENV_REGISTRY = "KT_REGISTRY"
)

Expand Down Expand Up @@ -151,7 +150,7 @@ type printer struct {

func newPrinter(pretty bool) *printer {
marshal := json.Marshal
if pretty && terminal.IsTerminal(1) {
if pretty && term.IsTerminal(1) {
marshal = func(i interface{}) ([]byte, error) { return json.MarshalIndent(i, "", " ") }
}
return &printer{
Expand Down Expand Up @@ -194,41 +193,6 @@ func sanitizeUsername(u string) string {
return invalidClientIDCharactersRegExp.ReplaceAllString(u, "")
}

// setUpCerts takes the paths to a tls certificate, CA, and certificate key in
// a PEM format and returns a constructed tls.Config object.
func setUpCerts(certPath, caPath, keyPath string) (*tls.Config, error) {
if certPath == "" && caPath == "" && keyPath == "" {
return nil, nil
}

if certPath == "" || caPath == "" || keyPath == "" {
return nil, fmt.Errorf("certificate, CA and key path are required - got cert=%#v ca=%#v key=%#v", certPath, caPath, keyPath)
}

caString, err := ioutil.ReadFile(caPath)
if err != nil {
return nil, err
}

caPool := x509.NewCertPool()
ok := caPool.AppendCertsFromPEM(caString)
if !ok {
return nil, fmt.Errorf("unable to add cert at %s to certificate pool", caPath)
}

clientCert, err := tls.LoadX509KeyPair(certPath, keyPath)
if err != nil {
return nil, err
}

bundle := &tls.Config{
RootCAs: caPool,
Certificates: []tls.Certificate{clientCert},
}
bundle.BuildNameToCertificate()
return bundle, nil
}

// setFlagsFromEnv sets unset flags in fs from environment
// variables as specified by the flags map, which maps
// from flag name to the environment variable for that name.
Expand Down
26 changes: 13 additions & 13 deletions consume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -683,13 +683,17 @@ func TestConsume(t *testing.T) {
target.topic = "hans"
target.brokerStrs = []string{"localhost:9092"}

go target.consume(map[int32]resolvedInterval{
1: {1, 5},
2: {1, 5},
}, map[int32]int64{
1: 1,
2: 1,
})
go func() {
err := target.consume(map[int32]resolvedInterval{
1: {1, 5},
2: {1, 5},
}, map[int32]int64{
1: 1,
2: 1,
})
c.Check(err, qt.IsNil)
}()

defer close(closer)

var actual []tConsumePartition
Expand Down Expand Up @@ -795,7 +799,6 @@ func (c tConsumer) HighWaterMarks() map[string]map[int32]int64 {

func TestConsumeParseArgsUsesEnvVar(t *testing.T) {
c := qt.New(t)
defer c.Done()

registry := "localhost:8084"
broker := "hans:2000"
Expand All @@ -813,7 +816,6 @@ func TestConsumeParseArgsUsesEnvVar(t *testing.T) {
// brokers default to localhost:9092
func TestConsumeParseArgsDefault(t *testing.T) {
c := qt.New(t)
defer c.Done()

c.Setenv(ENV_BROKERS, "")
c.Setenv(ENV_REGISTRY, "")
Expand All @@ -827,7 +829,6 @@ func TestConsumeParseArgsDefault(t *testing.T) {

func TestConsumeParseArgsFlagsOverrideEnv(t *testing.T) {
c := qt.New(t)
defer c.Done()

registry := "localhost:8084"
broker := "hans:2000"
Expand All @@ -845,7 +846,6 @@ func TestConsumeParseArgsFlagsOverrideEnv(t *testing.T) {

func TestConsumeAvroMessage(t *testing.T) {
c := qt.New(t)
defer c.Done()

type record struct {
A int
Expand Down Expand Up @@ -947,7 +947,7 @@ func newTestRegistry(c *qt.C) *testRegistry {
RetryStrategy: retry.Regular{},
})
c.Assert(err, qt.IsNil)
c.Defer(func() {
c.Cleanup(func() {
err := reg.registry.DeleteSubject(ctx, reg.sub)
c.Check(err, qt.IsNil)
if reg.srv != nil {
Expand Down Expand Up @@ -984,7 +984,7 @@ func (reg *testRegistry) fakeServerHandler(w http.ResponseWriter, r *http.Reques

w.Header().Set("Content-Type", "application/vnd.schemaregistry.v1+json")
w.WriteHeader(http.StatusOK)
w.Write(body)
_, _ = w.Write(body)
}

// createAvroMessage is a helper to create Avro message.
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ require (
github.com/pierrec/lz4 v2.6.1+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rogpeppe/go-internal v1.8.0
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa // indirect
golang.org/x/net v0.0.0-20210917221730-978cfadd31cf // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1
golang.org/x/text v0.3.7 // indirect
gopkg.in/retry.v1 v1.0.3
)
Expand Down
2 changes: 1 addition & 1 deletion group.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func (cmd *groupCmd) printGroupTopicOffset(out *printer, grp, top string, parts
})
}
go func() {
wg.Wait()
_ = wg.Wait()
close(results)
}()
for res := range results {
Expand Down
3 changes: 0 additions & 3 deletions produce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

func TestProduceParseArgsUsesEnvVar(t *testing.T) {
c := qt.New(t)
defer c.Done()

c.Setenv(ENV_BROKERS, "hans:2000")

Expand All @@ -26,7 +25,6 @@ func TestProduceParseArgsUsesEnvVar(t *testing.T) {
// brokers default to localhost:9092
func TestProduceParseArgsDefault(t *testing.T) {
c := qt.New(t)
defer c.Done()

c.Setenv(ENV_BROKERS, "")

Expand All @@ -38,7 +36,6 @@ func TestProduceParseArgsDefault(t *testing.T) {

func TestProduceParseArgsFlagsOverrideEnv(t *testing.T) {
c := qt.New(t)
defer c.Done()

// command line arg wins
c.Setenv(ENV_BROKERS, "BLABB")
Expand Down
3 changes: 0 additions & 3 deletions topic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

func TestTopicParseArgsUsesEnvVar(t *testing.T) {
c := qt.New(t)
defer c.Done()

c.Setenv(ENV_BROKERS, "hans:2000")

Expand All @@ -21,7 +20,6 @@ func TestTopicParseArgsUsesEnvVar(t *testing.T) {
// brokers default to localhost:9092
func TestTopicParseArgsDefault(t *testing.T) {
c := qt.New(t)
defer c.Done()

c.Setenv(ENV_BROKERS, "")

Expand All @@ -33,7 +31,6 @@ func TestTopicParseArgsDefault(t *testing.T) {

func TestTopicParseArgsFlagsOverrideEnv(t *testing.T) {
c := qt.New(t)
defer c.Done()

// command line arg wins
c.Setenv(ENV_BROKERS, "BLABB")
Expand Down

0 comments on commit 77bb68d

Please sign in to comment.