Skip to content

Commit

Permalink
Merge pull request #181 from davrodpin/fix-linter
Browse files Browse the repository at this point in the history
Fix issues found by linter
  • Loading branch information
davrodpin committed Oct 6, 2021
2 parents ff55983 + 8f6d17f commit eaee4e5
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 30 deletions.
7 changes: 5 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,12 @@ The full address is kept on $HOME/.mole/<id>.`)
// id is a hidden flag used to carry the unique identifier of the instance to
// the child process when the `--detached` flag is used.
cmd.Flags().StringVarP(&conf.Id, mole.IdFlagName, "", "", "")
cmd.Flags().MarkHidden(mole.IdFlagName)
err := cmd.Flags().MarkHidden(mole.IdFlagName)
if err != nil {
return err
}

err := cmd.MarkFlagRequired("server")
err = cmd.MarkFlagRequired("server")
if err != nil {
return err
}
Expand Down
4 changes: 1 addition & 3 deletions cmd/start_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ var startLocalCmd = &cobra.Command{
}

func init() {
var err error

err = bindFlags(conf, startLocalCmd)
err := bindFlags(conf, startLocalCmd)
if err != nil {
log.WithError(err).Error("error parsing command line arguments")
os.Exit(1)
Expand Down
6 changes: 5 additions & 1 deletion fsutils/fsutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@ func CreatePidFile(id string) (string, error) {
return "", fmt.Errorf("could not create pid file for application instance %s: %v", id, err)
}
defer pf.Close()
pf.WriteString(strconv.Itoa(os.Getpid()))

_, err = pf.WriteString(strconv.Itoa(os.Getpid()))
if err != nil {
return "", err
}

return pfl, nil
}
Expand Down
18 changes: 13 additions & 5 deletions mole/mole.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,10 @@ func (c *Client) handleSignals() {
signal.Notify(c.sigs, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
sig := <-c.sigs
log.Debugf("process signal %s received", sig)
c.Stop()
err := c.Stop()
if err != nil {
log.WithError(err).Error("instance not properly stopped")
}
}

// Merge overwrites Configuration from the given Alias.
Expand All @@ -264,9 +267,7 @@ func (c *Client) handleSignals() {
// only if they are found on the givenFlags which should contain the name of
// all flags given by the user through UI (e.g. CLI).
func (c *Configuration) Merge(al *alias.Alias, givenFlags []string) error {
var fl flags

fl = givenFlags
var fl flags = givenFlags

if !fl.lookup("verbose") {
c.Verbose = al.Verbose
Expand Down Expand Up @@ -417,7 +418,14 @@ func startDaemonProcess(instanceConf *DetachedInstance) error {
os.Exit(0)
}

defer cntxt.Release()
defer func() {
err := cntxt.Release()
if err != nil {
log.WithFields(log.Fields{
"id": instanceConf.Id,
}).WithError(err).Error("error detaching the mole instance")
}
}()

return nil
}
Expand Down
5 changes: 4 additions & 1 deletion mole/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ func ShowRpc(params interface{}) (json.RawMessage, error) {
return nil, fmt.Errorf("client configuration could not be found.")
}

runtime := cli.Runtime()
runtime, err := cli.Runtime()
if err != nil {
return nil, err
}

cj, err := json.Marshal(runtime)
if err != nil {
Expand Down
18 changes: 14 additions & 4 deletions mole/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,33 @@ func (ir InstancesRuntime) ToToml() (string, error) {
return buf.String(), nil
}

func (c *Client) Runtime() *Runtime {
func (c *Client) Runtime() (*Runtime, error) {
runtime := Runtime(*c.Conf)

if c.Tunnel != nil {
source := &AddressInputList{}
destination := &AddressInputList{}

for _, channel := range c.Tunnel.Channels() {
source.Set(channel.Source)
destination.Set(channel.Destination)
var err error

err = source.Set(channel.Source)
if err != nil {
return nil, err
}

err = destination.Set(channel.Destination)
if err != nil {
return nil, err
}

}

runtime.Source = *source
runtime.Destination = *destination
}

return &runtime
return &runtime, nil
}

// Running checks if an instance of mole is running on the system.
Expand Down
89 changes: 76 additions & 13 deletions rpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,33 @@ func (h *Handler) Handle(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2
log.Errorf("rpc request method %s not supported", req.Method)

if !req.Notif {
var err error

resp := &jsonrpc2.Response{}
resp.SetResult(jsonrpc2.Error{
err = resp.SetResult(jsonrpc2.Error{
Code: jsonrpc2.CodeMethodNotFound,
Message: fmt.Sprintf("method %s not found", req.Method),
})
if err != nil {
log.WithFields(log.Fields{
"notification": req.Notif,
"method": req.Method,
"id": req.ID,
}).WithError(err).Error("could not send rpc error response")

return
}

sendResponse(ctx, conn, req, resp)
err = sendResponse(ctx, conn, req, resp)
if err != nil {
log.WithFields(log.Fields{
"notification": req.Notif,
"method": req.Method,
"id": req.ID,
}).WithError(err).Error("could not send rpc response")

return
}
}

return
Expand All @@ -86,13 +106,33 @@ func (h *Handler) Handle(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2
}).WithError(err).Warn("error executing rpc method.")

if !req.Notif {
var err error
resp := &jsonrpc2.Response{}
resp.SetResult(jsonrpc2.Error{

err = resp.SetResult(jsonrpc2.Error{
Code: jsonrpc2.CodeInternalError,
Message: fmt.Sprintf("error executing rpc method %s", req.Method),
})
if err != nil {
log.WithFields(log.Fields{
"notification": req.Notif,
"method": req.Method,
"id": req.ID,
}).WithError(err).Error("could not send rpc error response")

return
}

sendResponse(ctx, conn, req, resp)
err = sendResponse(ctx, conn, req, resp)
if err != nil {
log.WithFields(log.Fields{
"notification": req.Notif,
"method": req.Method,
"id": req.ID,
}).WithError(err).Error("could not send rpc response")

return
}
}

return
Expand All @@ -108,13 +148,33 @@ func (h *Handler) Handle(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2
}).WithError(err).Warn("error executing rpc method.")

if !req.Notif {
var err error

resp := &jsonrpc2.Response{}
resp.SetResult(jsonrpc2.Error{
err = resp.SetResult(jsonrpc2.Error{
Code: jsonrpc2.CodeInternalError,
Message: fmt.Sprintf("error executing rpc method %s", req.Method),
})
if err != nil {
log.WithFields(log.Fields{
"notification": req.Notif,
"method": req.Method,
"id": req.ID,
}).WithError(err).Error("could not send rpc error response")

return
}

sendResponse(ctx, conn, req, resp)
err = sendResponse(ctx, conn, req, resp)
if err != nil {
log.WithFields(log.Fields{
"notification": req.Notif,
"method": req.Method,
"id": req.ID,
}).WithError(err).Error("could not send rpc response")

return
}
}

return
Expand All @@ -123,7 +183,16 @@ func (h *Handler) Handle(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2
if !req.Notif {
resp := &jsonrpc2.Response{ID: req.ID, Result: &rm}

sendResponse(ctx, conn, req, resp)
err = sendResponse(ctx, conn, req, resp)
if err != nil {
log.WithFields(log.Fields{
"notification": req.Notif,
"method": req.Method,
"id": req.ID,
}).WithError(err).Error("could not send rpc response")

return
}
}
}

Expand All @@ -137,12 +206,6 @@ type Method func(params interface{}) (json.RawMessage, error)

func sendResponse(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request, resp *jsonrpc2.Response) error {
if err := conn.SendResponse(ctx, resp); err != nil {
log.WithFields(log.Fields{
"notification": req.Notif,
"method": req.Method,
"id": req.ID,
}).WithError(err).Warn("error sending rpc response.")

return err
}

Expand Down
2 changes: 1 addition & 1 deletion tunnel/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (k PemKey) IsEncrypted() (bool, error) {
return false, err
}

return x509.IsEncryptedPEMBlock(p), nil
return x509.IsEncryptedPEMBlock(p), nil //nolint
}

// Parse translates a pem key to a signer to create signatures that verify
Expand Down

0 comments on commit eaee4e5

Please sign in to comment.