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

Fix issues found by linter #181

Merged
merged 1 commit into from
Oct 6, 2021
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
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