GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Test Setup Failed
Push — master ( e8c327...d95ec3 )
by Amir
17:24
created

redis.handleCommands   D

Complexity

Conditions 13

Size

Total Lines 38
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 34
dl 0
loc 38
rs 4.2
c 0
b 0
f 0
nop 1

How to fix   Complexity   

Complexity

Complex classes like redis.handleCommands often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
package redis
2
3
import (
4
	"crypto/tls"
5
	"github.com/arazmj/gerdu/cache"
6
	log "github.com/sirupsen/logrus"
7
	"github.com/tidwall/redcon"
8
	"strings"
9
)
10
11
func Serve(host string, gerdu cache.UnImplementedCache) {
12
	go log.Infof("Gerdu started listening Redis at %s", host)
13
	err := redcon.ListenAndServe(host,
14
		handleCommands(gerdu),
15
		handleAccept,
16
		handleClose,
17
	)
18
	if err != nil {
19
		log.Fatal(err)
20
	}
21
22
}
23
24
func handleClose(conn redcon.Conn, err error) {
25
	log.Printf("closed: %s, err: %v", conn.RemoteAddr(), err)
26
}
27
28
func handleAccept(conn redcon.Conn) bool {
29
	log.Printf("accept: %s", conn.RemoteAddr())
30
	return true
31
}
32
33
func ServeTLS(host string, tlsCert, tlsKey string, gerdu cache.UnImplementedCache) {
34
	go log.Infof("Gerdu started listening Redis at %s", host)
35
	certificate, err := tls.LoadX509KeyPair(tlsCert, tlsKey)
36
	err = redcon.ListenAndServeTLS(host,
37
		handleCommands(gerdu),
38
		func(conn redcon.Conn) bool {
39
			log.Printf("accept: %s", conn.RemoteAddr())
40
			return true
41
		},
42
		func(conn redcon.Conn, err error) {
43
			log.Printf("closed: %s, err: %v", conn.RemoteAddr(), err)
44
		},
45
46
		&tls.Config{Certificates: []tls.Certificate{certificate}},
47
	)
48
49
	if err != nil {
50
		log.Fatal(err)
51
	}
52
}
53
54
func handleCommands(gerdu cache.UnImplementedCache) func(conn redcon.Conn, cmd redcon.Command) {
55
	return func(conn redcon.Conn, cmd redcon.Command) {
56
		switch strings.ToLower(string(cmd.Args[0])) {
57
		default:
58
			conn.WriteError("ERR unknown command '" + string(cmd.Args[0]) + "'")
59
		case "ping":
60
			conn.WriteString("PONG")
61
		case "quit":
62
			conn.WriteString("OK")
63
			conn.Close()
64
		case "set":
65
			if len(cmd.Args) != 3 {
66
				conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
67
				return
68
			}
69
			gerdu.Put(string(cmd.Args[1]), string(cmd.Args[2]))
70
			conn.WriteString("OK")
71
		case "get":
72
			if len(cmd.Args) != 2 {
73
				conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
74
				return
75
			}
76
			val, ok := gerdu.Get(string(cmd.Args[1]))
77
			if !ok {
78
				conn.WriteNull()
79
			} else {
80
				conn.WriteBulk([]byte(val))
81
			}
82
		case "del":
83
			if len(cmd.Args) != 2 {
84
				conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
85
				return
86
			}
87
			ok := gerdu.Delete(string(cmd.Args[1]))
88
			if !ok {
89
				conn.WriteInt(0)
90
			} else {
91
				conn.WriteInt(1)
92
			}
93
		}
94
	}
95
}
96