Skip to content

Commit

Permalink
Linted code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Lin committed Oct 30, 2019
1 parent 86da73a commit 083b7b8
Show file tree
Hide file tree
Showing 20 changed files with 135 additions and 115 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ test-no-ci: ## Run no_ci tests
${OPTS} go test ${TEST_OPTS_NOCI} ./pkg/transport/... -run "TCP|PubKeyTable"

install-linters: ## Install linters
- VERSION=1.17.1 ./ci_scripts/install-golangci-lint.sh
- VERSION=1.21.0 ./ci_scripts/install-golangci-lint.sh
# GO111MODULE=off go get -u github.com/FiloSottile/vendorcheck
# For some reason this install method is not recommended, see https://github.com/golangci/golangci-lint#install
# However, they suggest `curl ... | bash` which we should not do
Expand Down
4 changes: 2 additions & 2 deletions ci_scripts/install-golangci-lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

set -e -o pipefail

if [ -z "$VERSION" ]; then
if [[ -z "$VERSION" ]]; then
echo "VERSION must be set"
exit 1
fi

# In alpine linux (as it does not come with curl by default)
wget -O - -q https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $GOPATH/bin v$VERSION
wget -O - -q https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b "$GOPATH/bin" "v$VERSION"

golangci-lint --version
1 change: 1 addition & 0 deletions cmd/dmsgpty/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func readDstAddr() error {
}
}

// Execute executes the root command.
func Execute() {
if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
Expand Down
1 change: 0 additions & 1 deletion cmd/dmsgpty/commands/whitelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"math/big"
"sort"


"github.com/SkycoinProject/dmsg/cipher"
"github.com/spf13/cobra"

Expand Down
6 changes: 3 additions & 3 deletions cmd/skywire-cli/commands/node/gen-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@ func defaultConfig() *visor.Config {

func defaultDmsgPtyConfig() visor.DmsgPtyConfig {
return visor.DmsgPtyConfig{
Port: skyenv.DefaultDmsgPtyPort,
Port: skyenv.DefaultDmsgPtyPort,
AuthFile: "./skywire/dmsgpty/whitelist.json",
CLINet: skyenv.DefaultDmsgPtyCLINet,
CLIAddr: skyenv.DefaultDmsgPtyCLIAddr,
CLINet: skyenv.DefaultDmsgPtyCLINet,
CLIAddr: skyenv.DefaultDmsgPtyCLIAddr,
}
}

Expand Down
4 changes: 3 additions & 1 deletion cmd/skywire-visor/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"log"
"log/syslog"
"net/http"
_ "net/http/pprof" // used for HTTP profiling
"os"
"os/signal"
"path/filepath"
Expand All @@ -28,6 +27,9 @@ import (
"github.com/SkycoinProject/skywire-mainnet/pkg/visor"
)

// TODO(evanlinjin): Determine if this is still needed.
//import _ "net/http/pprof" // used for HTTP profiling

const configEnv = "SW_CONFIG"
const defaultShutdownTimeout = visor.Duration(10 * time.Second)

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ require (
go.etcd.io/bbolt v1.3.3
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550
golang.org/x/net v0.0.0-20191014212845-da9a3fd4c582
golang.org/x/tools v0.0.0-20191030062658-86caa796c7ab // indirect
)

// Uncomment for tests with alternate branches of 'dmsg'
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,10 @@ golang.org/x/tools v0.0.0-20190627182818-9947fec5c3ab/go.mod h1:/rFqwRUd4F7ZHNgw
golang.org/x/tools v0.0.0-20190918181022-2c18af7e64b2/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20190929041059-e7abfedfabcf h1:NvypsVlesF+lEDKVK5RNkww4fzArJXChZxNin79j05M=
golang.org/x/tools v0.0.0-20190929041059-e7abfedfabcf/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191014205221-18e3458ac98b h1:EsQHTYgcM562dq02r6y2Yt9VpvvLNIyNECx96XQeolA=
golang.org/x/tools v0.0.0-20191014205221-18e3458ac98b/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191030062658-86caa796c7ab h1:tpc/nJ4vD66vAk/2KN0sw/DvQIz2sKmCpWvyKtPmfMQ=
golang.org/x/tools v0.0.0-20191030062658-86caa796c7ab/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=
Expand Down
6 changes: 6 additions & 0 deletions pkg/dmsgpty/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/sirupsen/logrus"
)

// CLI represents the command line interface for communicating with the dmsgpty-host.
type CLI struct {
Log logrus.FieldLogger `json:"-"`

Expand All @@ -33,6 +34,7 @@ type CLI struct {
Arg []string `json:"command_arguments"`
}

// SetDefaults sets the fields with nil-values to default values.
func (c *CLI) SetDefaults() {
if c.Log == nil {
c.Log = logging.MustGetLogger("dmsgpty-cli")
Expand All @@ -51,6 +53,7 @@ func (c *CLI) SetDefaults() {
}
}

// dials to the dmsgpty-host with a given request.
func (c *CLI) dial(req Request) (net.Conn, error) {
conn, err := net.Dial(c.Net, c.Addr)
if err != nil {
Expand All @@ -68,10 +71,12 @@ func (c *CLI) dial(req Request) (net.Conn, error) {
return conn, nil
}

// RequestCfg dials a request of type 'Cfg'.
func (c *CLI) RequestCfg() (net.Conn, error) {
return c.dial(new(CfgReq))
}

// RequestPty dials a request of type 'Pty'.
func (c *CLI) RequestPty() error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down Expand Up @@ -136,6 +141,7 @@ func (c *CLI) RequestPty() error {
return nil
}

// Loop that informs the remote of changes to the local CLI terminal window size.
func (c *CLI) ptyResizeLoop(ctx context.Context, ptyC *pty.Client) error {
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGWINCH)
Expand Down
27 changes: 14 additions & 13 deletions pkg/dmsgpty/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/SkycoinProject/skywire-mainnet/pkg/dmsgpty/ptyutil"
)

// HostConfig configures a dmsgpty host.
type HostConfig struct {
PubKey cipher.PubKey `json:"public_key"`
SecKey cipher.SecKey `json:"secret_key"`
Expand All @@ -37,6 +38,7 @@ type HostConfig struct {
CLIAddr string `json:"cli_address"`
}

// SetDefaults sets nil-valued fields to default values.
func (c *HostConfig) SetDefaults() {
if c.DmsgDiscAddr == "" {
c.DmsgDiscAddr = skyenv.DefaultDmsgDiscAddr
Expand All @@ -61,7 +63,7 @@ func (c *HostConfig) SetDefaults() {
// Host hosts a dmsgpty server.
// TODO(evanlinjin): Change this to use `snet.Network` instead of `dmsg.Client` directly.
type Host struct {
log logrus.FieldLogger
log logrus.FieldLogger

pk cipher.PubKey
cliNet string
Expand All @@ -73,11 +75,9 @@ type Host struct {

cliL net.Listener // Listens for CLI connections.
cliI int32 // CLI index.

done chan struct{}
once sync.Once
}

// NewHost instantiates a new host with a given configuration.
func NewHost(ctx context.Context, log logrus.FieldLogger, conf HostConfig) (*Host, error) {
conf.SetDefaults()

Expand All @@ -102,6 +102,7 @@ func NewHost(ctx context.Context, log logrus.FieldLogger, conf HostConfig) (*Hos
conf.CLIAddr)
}

// NewHostFromDmsgClient instantiates a new host with a given dmsg client (and additional arguments).
func NewHostFromDmsgClient(
log logrus.FieldLogger,
dmsgC *dmsg.Client,
Expand Down Expand Up @@ -138,14 +139,14 @@ func NewHostFromDmsgClient(
return nil, err
}
return &Host{
log: log,
pk: pk,
cliNet: cliNet,
log: log,
pk: pk,
cliNet: cliNet,
cliAddr: cliAddr,
dmsgC: dmsgC,
dmsgL: dmsgL,
ptyS: ptyS,
cliL: cliL,
dmsgC: dmsgC,
dmsgL: dmsgL,
ptyS: ptyS,
cliL: cliL,
}, nil
}

Expand Down Expand Up @@ -222,7 +223,7 @@ func (h *Host) handleCLIConn(ctx context.Context, cliConn net.Conn) {

switch req.Type() {
case CfgReqType:
rpcSrv, err = h.handleCfgReq(ctx, log)
rpcSrv, err = h.handleCfgReq(ctx)
case PtyReqType:
rpcSrv, err = h.handlePtyReq(ctx, log, req.(*PtyReq))
}
Expand All @@ -234,7 +235,7 @@ func (h *Host) handleCLIConn(ctx context.Context, cliConn net.Conn) {
rpcSrv.ServeConn(cliConn)
}

func (h *Host) handleCfgReq(ctx context.Context, log logrus.FieldLogger) (*rpc.Server, error) {
func (h *Host) handleCfgReq(ctx context.Context) (*rpc.Server, error) {
rpcS := rpc.NewServer()
if err := rpcS.RegisterName(ptycfg.GatewayName, ptycfg.NewGateway(ctx, h.ptyS.Auth())); err != nil {
return nil, fmt.Errorf("failed to register 'CfgGateway': %v", err)
Expand Down
66 changes: 37 additions & 29 deletions pkg/dmsgpty/pty/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ import (
"net/rpc"
"os"

"github.com/SkycoinProject/dmsg"
"github.com/SkycoinProject/dmsg/cipher"
"github.com/SkycoinProject/skycoin/src/util/logging"
"github.com/creack/pty"
"github.com/sirupsen/logrus"
)

var empty = &struct{}{}

// Client is a pty client.
type Client struct {
ctx context.Context
log logrus.FieldLogger
rpcC *rpc.Client
}

// NewPtyClient creates a new pty client.
func NewPtyClient(ctx context.Context, log logrus.FieldLogger, conn io.ReadWriteCloser) *Client {
if log == nil {
log = logging.MustGetLogger("dmsgpty-client")
Expand All @@ -32,40 +32,43 @@ func NewPtyClient(ctx context.Context, log logrus.FieldLogger, conn io.ReadWrite
}
}

func NewPtyClientWithTp(log logrus.FieldLogger, _ cipher.SecKey, tp *dmsg.Transport) (*Client, error) {
if log == nil {
log = logging.MustGetLogger("dmsgpty-client")
}

// TODO(evanlinjin): Wrap connection with noise.
//ns, err := noise.New(noise.HandshakeXK, noise.Config{
// LocalPK: tp.LocalPK(),
// LocalSK: sk,
// RemotePK: tp.RemotePK(),
// Initiator: true,
//})
//if err != nil {
// log.WithError(err).Fatal("NewPtyClientWithTp: failed to init noise")
// return nil, err
//}
//conn, err := noise.WrapConn(tp, ns, noise.AcceptHandshakeTimeout)
//if err != nil {
// return nil, err
//}

return &Client{
log: log,
rpcC: rpc.NewClient(tp),
}, nil
}

// TODO(evanlinjin): determine if needed.
//func NewPtyClientWithTp(log logrus.FieldLogger, _ cipher.SecKey, tp *dmsg.Transport) (*Client, error) {
// if log == nil {
// log = logging.MustGetLogger("dmsgpty-client")
// }
//
// // TODO(evanlinjin): Wrap connection with noise.
// //ns, err := noise.New(noise.HandshakeXK, noise.Config{
// // LocalPK: tp.LocalPK(),
// // LocalSK: sk,
// // RemotePK: tp.RemotePK(),
// // Initiator: true,
// //})
// //if err != nil {
// // log.WithError(err).Fatal("NewPtyClientWithTp: failed to init noise")
// // return nil, err
// //}
// //conn, err := noise.WrapConn(tp, ns, noise.AcceptHandshakeTimeout)
// //if err != nil {
// // return nil, err
// //}
//
// return &Client{
// log: log,
// rpcC: rpc.NewClient(tp),
// }, nil
//}

// Close closes the pty and closes the connection to the remote.
func (c *Client) Close() error {
if err := c.Stop(); err != nil {
c.log.WithError(err).Warn("failed to stop remote pty")
}
return c.rpcC.Close()
}

// Start starts the pty.
func (c *Client) Start(name string, arg ...string) error {
size, err := pty.GetsizeFull(os.Stdin)
if err != nil {
Expand All @@ -75,23 +78,27 @@ func (c *Client) Start(name string, arg ...string) error {
return c.call("Start", &CommandReq{Name: name, Arg: arg, Size: size}, empty)
}

// Stop stops the pty.
func (c *Client) Stop() error {
return c.call("Stop", empty, empty)
}

// Read reads from the pty.
func (c *Client) Read(b []byte) (int, error) {
reqN := len(b)
var respB []byte
err := c.call("Read", &reqN, &respB)
return copy(b, respB), processRPCError(err)
}

// Write writes to the pty.
func (c *Client) Write(b []byte) (int, error) {
var n int
err := c.call("Write", &b, &n)
return n, processRPCError(err)
}

// SetPtySize sets the pty size.
func (c *Client) SetPtySize(size *pty.Winsize) error {
return c.call("SetPtySize", size, empty)
}
Expand All @@ -110,6 +117,7 @@ func ptyMethod(m string) string {
return GatewayName + "." + m
}

// GetPtySize obtains the size of the local terminal.
func GetPtySize(t *os.File) (*pty.Winsize, error) { return pty.GetsizeFull(t) }

func processRPCError(err error) error {
Expand Down
Loading

0 comments on commit 083b7b8

Please sign in to comment.