Passed
Push — master ( 495634...373907 )
by Stanislav
01:20
created

main.main   B

Complexity

Conditions 6

Size

Total Lines 61
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
dl 0
loc 61
rs 8.0586
c 0
b 0
f 0
eloc 37
nop 0

How to fix   Long Method   

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:

1
package main
2
3
import (
4
	"fmt"
5
	"io"
6
	"log"
7
	"os"
8
9
	"flag"
10
	"github.com/popstas/go-toggl"
11
	"github.com/popstas/planfix-go/planfix"
12
	"github.com/viasite/planfix-toggl-server/app/client"
13
	"github.com/viasite/planfix-toggl-server/app/config"
14
	"github.com/viasite/planfix-toggl-server/app/rest"
15
	"github.com/viasite/planfix-toggl-server/app/util"
16
	"io/ioutil"
17
	"runtime"
18
)
19
20
var version string
21
22
func getLogger(cfg config.Config) *log.Logger {
23
	// logging
24
	logger := log.New(os.Stderr, "[planfix-toggl] ", log.LstdFlags)
25
	if cfg.Debug {
26
		logger.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds | log.Lshortfile)
27
	} else {
28
		toggl.DisableLog()
29
	}
30
	if cfg.LogFile != "" {
31
		f, err := os.OpenFile(cfg.LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
32
		if err != nil {
33
			logger.Fatalf("[ERROR] Failed to open log file: %s", cfg.LogFile)
34
		}
35
		defer f.Close()
36
		mw := io.MultiWriter(os.Stdout, f)
37
		logger.SetOutput(mw)
38
	}
39
	return logger
40
}
41
42
func parseFlags(cfg *config.Config){
43
	dryRun := flag.Bool("dry-run", false, "Don't actually change data")
44
	if runtime.GOOS == "windows" {
45
		// Allow user to hide the console window
46
		flag.BoolVar(&cfg.NoConsole, "no-console", false, "Hide console window")
47
	}
48
	flag.Parse()
49
50
	if *dryRun {
51
		cfg.DryRun = true
52
	}
53
}
54
55
func main() {
56
	fmt.Printf("planfix-toggl %s\n", version)
57
58
	cfg := config.GetConfig()
59
60
	parseFlags(&cfg)
61
62
	logger := getLogger(cfg)
63
64
	if cfg.SMTPSecure {
65
		err := "[ERROR] Secure SMTP not implemented"
66
		logger.Fatal(err)
67
		os.Exit(1)
68
	}
69
70
	if cfg.NoConsole {
71
		util.HideConsole()
72
	}
73
74
	// create planfix client
75
	planfixAPI := planfix.New(
76
		cfg.PlanfixAPIUrl,
77
		cfg.PlanfixAPIKey,
78
		cfg.PlanfixAccount,
79
		cfg.PlanfixUserName,
80
		cfg.PlanfixUserPassword,
81
	)
82
	if !cfg.Debug {
83
		planfixAPI.Logger.SetFlags(0)
84
		planfixAPI.Logger.SetOutput(ioutil.Discard)
85
	}
86
	planfixAPI.UserAgent = "planfix-toggl"
87
88
	// create toggl client
89
	togglClient := client.TogglClient{
90
		Session:    toggl.OpenSession(cfg.TogglAPIToken),
91
		Config:     &cfg,
92
		PlanfixAPI: planfixAPI,
93
		Logger:     logger,
94
	}
95
96
	// get planfix and toggl user IDs, for early API check
97
	if cfg.PlanfixUserName != "" && cfg.PlanfixUserPassword != "" {
98
		cfg.PlanfixUserID = togglClient.GetPlanfixUserID()
99
	}
100
	cfg.TogglUserID = togglClient.GetTogglUserID()
101
102
	// start tag cleaner
103
	go togglClient.RunTagCleaner()
104
105
	// start sender
106
	go togglClient.RunSender()
107
108
	// start API server
109
	server := rest.Server{
110
		Version:     version,
111
		TogglClient: togglClient,
112
		Config:      cfg,
113
		Logger:      logger,
114
	}
115
	server.Run()
116
}
117