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 ( 92bfe4...20f92c )
by Amir
01:20
created

main.main   F

Complexity

Conditions 23

Size

Total Lines 85
Code Lines 68

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 23
eloc 68
nop 0
dl 0
loc 85
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like main.main 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 main
2
3
import (
4
	"flag"
5
	"github.com/arazmj/gerdu/cache"
6
	"github.com/arazmj/gerdu/grpcserver"
7
	"github.com/arazmj/gerdu/httpserver"
8
	"github.com/arazmj/gerdu/lfucache"
9
	"github.com/arazmj/gerdu/lrucache"
10
	"github.com/arazmj/gerdu/memcached"
11
	"github.com/arazmj/gerdu/weakcache"
12
	"github.com/inhies/go-bytesize"
13
	log "github.com/sirupsen/logrus"
14
	"os"
15
	"strconv"
16
	"strings"
17
	"sync"
18
)
19
20
var gerdu cache.UnImplementedCache
21
var wg = sync.WaitGroup{}
22
23
var (
24
	loglevel = flag.String("log", "error",
25
		"log level can be any of values of 'panic', 'fatal', 'error', 'warn', 'info', 'debug', 'trace'")
26
	capacityStr = flag.String("capacity", "64MB",
27
		"The size of cache, once cache reached this capacity old values will evicted.\n"+
28
			"Specify a numerical value followed by one of the following units (not case sensitive)"+
29
			"\nK or KB: Kilobytes"+
30
			"\nM or MB: Megabytes"+
31
			"\nG or GB: Gigabytes"+
32
			"\nT or TB: Terabytes")
33
	httpPort  = flag.Int("httpport", 8080, "the http server port number")
34
	grpcPort  = flag.Int("grpcport", 8081, "the grpc server port number")
35
	mcdPort   = flag.Int("mcdport", 11211, "the memcached server port number")
36
	kind      = flag.String("type", "lru", "type of cache, lru or lfu, weak")
37
	protocols = flag.String("protocols", "http",
38
		"protocol 'grpc', 'http' or 'mcd' (memcached), multiple values can be selected separated by comma")
39
	tlsKey  = flag.String("key", "", "SSL certificate private key")
40
	tlsCert = flag.String("cert", "", "SSL certificate public key")
41
	host    = flag.String("host", "127.0.0.1", "The host that server listens")
42
	secure  = len(*tlsCert) > 0 && len(*tlsKey) > 0
43
)
44
45
func main() {
46
	flag.Parse()
47
	switch *loglevel {
48
	case "panic":
49
		log.SetLevel(log.PanicLevel)
50
	case "fatal":
51
		log.SetLevel(log.FatalLevel)
52
	case "error":
53
		log.SetLevel(log.ErrorLevel)
54
	case "warn":
55
		log.SetLevel(log.WarnLevel)
56
	case "info":
57
		log.SetLevel(log.InfoLevel)
58
	case "debug":
59
		log.SetLevel(log.DebugLevel)
60
	case "trace":
61
		log.SetLevel(log.DebugLevel)
62
	default:
63
		log.Fatalf("Invalid log level value %s\n", *loglevel)
64
		os.Exit(1)
65
	}
66
67
	capacity, err := bytesize.Parse(*capacityStr)
68
69
	if err != nil {
70
		log.Fatal("Invalid value for capacity", err.Error())
71
	}
72
73
	if strings.ToLower(*kind) == "lru" {
74
		gerdu = lrucache.NewCache(capacity)
75
	} else if strings.ToLower(*kind) == "lfu" {
76
		gerdu = lfucache.NewCache(capacity)
77
	} else if strings.ToLower(*kind) == "weak" {
78
		gerdu = weakcache.NewWeakCache()
79
	} else {
80
		log.Fatalf("Invalid value for type")
81
		os.Exit(1)
82
	}
83
84
	*protocols = strings.ToLower(*protocols)
85
	var validProtocol bool
86
	if strings.Contains(*protocols, "http") {
87
		validProtocol = true
88
		wg.Add(1)
89
		go func() {
90
			defer wg.Done()
91
			httpHost := *host + ":" + strconv.Itoa(*httpPort)
92
			if secure {
93
				httpserver.HttpServeTLS(httpHost, *tlsCert, *tlsKey, gerdu)
94
			} else {
95
				httpserver.HttpServe(httpHost, gerdu)
96
			}
97
		}()
98
	}
99
	if strings.Contains(*protocols, "grpc") {
100
		validProtocol = true
101
		wg.Add(1)
102
		go func() {
103
			defer wg.Done()
104
			grpcHost := *host + ":" + strconv.Itoa(*grpcPort)
105
			if secure {
106
				grpcserver.GrpcServeTLS(grpcHost, *tlsCert, *tlsKey, gerdu)
107
			} else {
108
				grpcserver.GrpcServe(grpcHost, gerdu)
109
			}
110
		}()
111
	}
112
	if strings.Contains(*protocols, "mcd") {
113
		validProtocol = true
114
		wg.Add(1)
115
		go func() {
116
			defer wg.Done()
117
			mcdHost := *host + ":" + strconv.Itoa(*mcdPort)
118
			if secure {
119
				log.Fatalln("Memcached protocol does not support TLS")
120
				os.Exit(1)
121
			}
122
			memcached.MemcachedServe(mcdHost, gerdu)
123
		}()
124
	}
125
	if !validProtocol {
126
		log.Fatalf("Invalid value for protocol")
127
		os.Exit(1)
128
	}
129
	wg.Wait()
130
}
131