Skip to content
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
31 changes: 31 additions & 0 deletions .github/workflows/project.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: ProjectStyle
on: [pull_request]
jobs:

build:
name: Build
runs-on: ubuntu-latest
steps:

- name: Set up Go 1.12
uses: actions/setup-go@v1
with:
go-version: 1.12
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v1

- name: InstallTool
run: |
wget -O - -q https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s v1.18.0
./bin/golangci-lint --version

- name: Build
run: go build .

- name: CheckStyle
run: |
./bin/golangci-lint run -c ./golangci.yml ./...


27 changes: 27 additions & 0 deletions golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
run:
deadline: 6m

linters:
disable-all: true
enable:
- bodyclose
- deadcode
- gocritic
- goimports
- golint
- gosimple
- govet
- ineffassign
- interfacer
- misspell
- staticcheck
- structcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
- varcheck
- lll
- prealloc
- maligned
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (
"fmt"
"os"

"github.com/kris-nova/logger"
"github.com/spf13/cobra"
"github.com/streamnative/pulsarctl/pkg/cmdutils"
"github.com/streamnative/pulsarctl/pkg/ctl/cluster"
"github.com/streamnative/pulsarctl/pkg/ctl/completion"
Expand All @@ -33,6 +31,9 @@ import (
"github.com/streamnative/pulsarctl/pkg/ctl/sources"
"github.com/streamnative/pulsarctl/pkg/ctl/tenant"
"github.com/streamnative/pulsarctl/pkg/ctl/topic"

"github.com/kris-nova/logger"
"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
Expand Down
14 changes: 7 additions & 7 deletions pkg/auth/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,38 @@ package auth

import "crypto/tls"

type TlsAuthProvider struct {
type TLSAuthProvider struct {
certificatePath string
privateKeyPath string
}

// NewAuthenticationTLSWithParams initialize the authentication provider with map param.
func NewAuthenticationTLSWithParams(params map[string]string) *TlsAuthProvider {
func NewAuthenticationTLSWithParams(params map[string]string) *TLSAuthProvider {
return NewAuthenticationTLS(
params["tlsCertFile"],
params["tlsKeyFile"],
)
}

// NewAuthenticationTLS initialize the authentication provider
func NewAuthenticationTLS(certificatePath string, privateKeyPath string) *TlsAuthProvider {
return &TlsAuthProvider{
func NewAuthenticationTLS(certificatePath string, privateKeyPath string) *TLSAuthProvider {
return &TLSAuthProvider{
certificatePath: certificatePath,
privateKeyPath: privateKeyPath,
}
}

func (p *TlsAuthProvider) Init() error {
func (p *TLSAuthProvider) Init() error {
// Try to read certificates immediately to provide better error at startup
_, err := p.GetTLSCertificate()
return err
}

func (p *TlsAuthProvider) Name() string {
func (p *TLSAuthProvider) Name() string {
return "tls"
}

func (p *TlsAuthProvider) GetTLSCertificate() (*tls.Certificate, error) {
func (p *TLSAuthProvider) GetTLSCertificate() (*tls.Certificate, error) {
cert, err := tls.LoadX509KeyPair(p.certificatePath, p.privateKeyPath)
return &cert, err
}
11 changes: 6 additions & 5 deletions pkg/cmdutils/cmdutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ import (
"os"
"strings"

"github.com/streamnative/pulsarctl/pkg/pulsar"

"github.com/kris-nova/logger"
"github.com/spf13/cobra"
"github.com/streamnative/pulsarctl/pkg/pulsar"
)

const IncompatibleFlags = "cannot be used at the same time"
Expand Down Expand Up @@ -82,17 +83,17 @@ func NewPulsarClient() pulsar.Client {
return PulsarCtlConfig.Client(pulsar.V2)
}

func NewPulsarClientWithApiVersion(version pulsar.ApiVersion) pulsar.Client {
func NewPulsarClientWithAPIVersion(version pulsar.APIVersion) pulsar.Client {
return PulsarCtlConfig.Client(version)
}

func PrintJson(w io.Writer, obj interface{}) {
func PrintJSON(w io.Writer, obj interface{}) {
b, err := json.MarshalIndent(obj, "", " ")
if err != nil {
fmt.Fprintf(w, "unexpected response type: %v\n", err)
_, _ = fmt.Fprintf(w, "unexpected response type: %v\n", err)
return
}
fmt.Fprintln(w, string(b))
_, _ = fmt.Fprintln(w, string(b))
}

func PrintError(w io.Writer, err error) {
Expand Down
36 changes: 19 additions & 17 deletions pkg/cmdutils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,23 @@
package cmdutils

import (
`log`
"github.com/spf13/pflag"
"log"

"github.com/streamnative/pulsarctl/pkg/pulsar"

"github.com/spf13/pflag"
)

var PulsarCtlConfig = ClusterConfig{}

// the configuration of the cluster that pulsarctl connects to
type ClusterConfig struct {
// the web service url that pulsarctl connects to. Default is http://localhost:8080
WebServiceUrl string
WebServiceURL string
// Set the path to the trusted TLS certificate file
TlsTrustCertsFilePath string
TLSTrustCertsFilePath string
// Configure whether the Pulsar client accept untrusted TLS certificate from broker (default: false)
TlsAllowInsecureConnection bool
TLSAllowInsecureConnection bool

AuthParams string
}
Expand All @@ -43,7 +45,7 @@ func (c *ClusterConfig) FlagSet() *pflag.FlagSet {
pflag.ContinueOnError)

flags.StringVarP(
&c.WebServiceUrl,
&c.WebServiceURL,
"admin-service-url",
"s",
pulsar.DefaultWebServiceURL,
Expand All @@ -53,44 +55,44 @@ func (c *ClusterConfig) FlagSet() *pflag.FlagSet {
&c.AuthParams,
"auth-params",
"",
"Authentication parameters are used to configure the public and private key files required by tls\n" +
"Authentication parameters are used to configure the public and private key files required by tls\n"+
" For example: \"tlsCertFile:val1,tlsKeyFile:val2\"")

flags.BoolVar(
&c.TlsAllowInsecureConnection,
&c.TLSAllowInsecureConnection,
"tls-allow-insecure",
false,
"Allow TLS insecure connection")

flags.StringVar(
&c.TlsTrustCertsFilePath,
&c.TLSTrustCertsFilePath,
"tls-trust-cert-pat",
"",
"Allow TLS trust cert file path")

return flags
}

func (c *ClusterConfig) Client(version pulsar.ApiVersion) pulsar.Client {
func (c *ClusterConfig) Client(version pulsar.APIVersion) pulsar.Client {
config := pulsar.DefaultConfig()

if len(c.WebServiceUrl) > 0 && c.WebServiceUrl != config.WebServiceUrl {
config.WebServiceUrl = c.WebServiceUrl
if len(c.WebServiceURL) > 0 && c.WebServiceURL != config.WebServiceURL {
config.WebServiceURL = c.WebServiceURL
}

if len(c.TlsTrustCertsFilePath) > 0 && c.TlsTrustCertsFilePath != config.TlsOptions.TrustCertsFilePath {
config.TlsOptions.TrustCertsFilePath = c.TlsTrustCertsFilePath
if len(c.TLSTrustCertsFilePath) > 0 && c.TLSTrustCertsFilePath != config.TLSOptions.TrustCertsFilePath {
config.TLSOptions.TrustCertsFilePath = c.TLSTrustCertsFilePath
}

if c.TlsAllowInsecureConnection {
config.TlsOptions.AllowInsecureConnection = true
if c.TLSAllowInsecureConnection {
config.TLSOptions.AllowInsecureConnection = true
}

if len(c.AuthParams) > 0 && c.AuthParams != config.AuthParams {
config.AuthParams = c.AuthParams
}

config.ApiVersion = version
config.APIVersion = version

client, err := pulsar.New(config)
if err != nil {
Expand Down
16 changes: 9 additions & 7 deletions pkg/cmdutils/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ package cmdutils

import (
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"strings"
"unicode"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

// FlagGrouping holds a superset of all flagsets for all commands
Expand Down Expand Up @@ -64,8 +65,8 @@ func (n *NamedFlagSetGroup) InFlagSet(name string, cb func(*pflag.FlagSet)) {
}

nfs := namedFlagSet{
name: name,
fs: &pflag.FlagSet{},
name: name,
fs: &pflag.FlagSet{},
}
cb(nfs.fs)
n.list = append(n.list, nfs)
Expand Down Expand Up @@ -96,7 +97,7 @@ func (g *FlagGrouping) Usage(cmd *cobra.Command) error {
}

if len(cmd.Aliases) > 0 {
usage = append(usage, "\nAliases: " + cmd.NameAndAliases())
usage = append(usage, "\nAliases: "+cmd.NameAndAliases())
}

if group != nil {
Expand All @@ -114,10 +115,11 @@ func (g *FlagGrouping) Usage(cmd *cobra.Command) error {
usage = append(usage, strings.TrimRightFunc(cmd.InheritedFlags().FlagUsages(), unicode.IsSpace))
}

usage = append(usage, fmt.Sprintf("\nUse '%s [command] --help' for more information about a command.\n", cmd.CommandPath()))
usage = append(usage,
fmt.Sprintf("\nUse '%s [command] --help' for more information about a command.\n",
cmd.CommandPath()))

cmd.Println(strings.Join(usage, "\n"))

return nil
}

7 changes: 4 additions & 3 deletions pkg/cmdutils/verb.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
package cmdutils

import (
"os"

"github.com/kris-nova/logger"
"github.com/spf13/cobra"
"os"
)

// VerbCmd holds attributes that most of the commands use
Expand All @@ -31,7 +32,7 @@ type VerbCmd struct {
NameArgs []string

// for testing
NameError error
NameError error
}

// AddVerbCmd create a registers a new command under the given resource command
Expand All @@ -45,7 +46,7 @@ func AddVerbCmd(flagGrouping *FlagGrouping, parentResourceCmd *cobra.Command, ne
parentResourceCmd.AddCommand(verb.Command)
}

func AddVerbCmds(flagGrouping *FlagGrouping, parentResourceCmd *cobra.Command, newVerbCmd ...func(cmd *VerbCmd)) {
func AddVerbCmds(flagGrouping *FlagGrouping, parentResourceCmd *cobra.Command, newVerbCmd ...func(cmd *VerbCmd)) {
for _, cmd := range newVerbCmd {
AddVerbCmd(flagGrouping, parentResourceCmd, cmd)
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/ctl/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
package cluster

import (
"errors"
"github.com/spf13/cobra"
"github.com/streamnative/pulsarctl/pkg/cmdutils"
"github.com/streamnative/pulsarctl/pkg/pulsar"

"github.com/pkg/errors"
"github.com/spf13/cobra"
)

var argsError = pulsar.Output{
Expand Down
3 changes: 2 additions & 1 deletion pkg/ctl/cluster/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
package cluster

import (
"github.com/spf13/pflag"
"github.com/streamnative/pulsarctl/pkg/cmdutils"
"github.com/streamnative/pulsarctl/pkg/pulsar"

"github.com/spf13/pflag"
)

func CreateClusterCmd(vc *cmdutils.VerbCmd) {
Expand Down
5 changes: 3 additions & 2 deletions pkg/ctl/cluster/create_failure_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
package cluster

import (
"errors"
"github.com/spf13/pflag"
"github.com/streamnative/pulsarctl/pkg/cmdutils"
"github.com/streamnative/pulsarctl/pkg/pulsar"

"github.com/pkg/errors"
"github.com/spf13/pflag"
)

func createFailureDomainCmd(vc *cmdutils.VerbCmd) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/ctl/cluster/create_failure_domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
package cluster

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestCreateFailureDomainCmdSuccess(t *testing.T) {
Expand Down
Loading