summaryrefslogtreecommitdiff
path: root/app/app.go
blob: b8eebc87a171382fb97e64a078f5459de3acc476 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package app

import (
	"os"

	"github.com/isacikgoz/gitbatch/gui"
	log "github.com/sirupsen/logrus"
)

// The App struct is responsible to hold app-wide related entities. Currently
// it has only the gui.Gui pointer for interface entity.
type App struct {
	Gui    *gui.Gui
	Config *SetupConfig
}

// SetupConfig is an assembler data to initiate a setup
type SetupConfig struct {
	Directories []string
	LogLevel    string
	Depth       int
	QuickMode   bool
	Mode        string
}

// Setup will handle pre-required operations. It is designed to be a wrapper for
// main method right now.
func Setup(setupConfig *SetupConfig) (*App, error) {
	// initiate the app and give it initial values
	app := &App{}
	if len(setupConfig.Directories) <= 0 {
		d, _ := os.Getwd()
		setupConfig.Directories = []string{d}
	}

	appConfig, err := overrideDefaults(setupConfig)
	if err != nil {
		return nil, err
	}

	setLogLevel(appConfig.LogLevel)
	directories := generateDirectories(appConfig.Directories, appConfig.Depth)

	if appConfig.QuickMode {
		x := appConfig.Mode == "fetch"
		y := appConfig.Mode == "pull"
		if x == y {
			log.Error("Unrecognized quick mode: " + appConfig.Mode)
			os.Exit(1)
		}
		quick(directories, appConfig.Depth, appConfig.Mode)
		os.Exit(0)
	}

	// create a gui.Gui struct and set it as App's gui
	app.Gui, err = gui.NewGui(appConfig.Mode, directories)
	if err != nil {
		// the error types and handling is not considered yet
		return nil, err
	}
	// hopefull everything went smooth as butter
	log.Trace("App configuration completed")
	return app, nil
}

// Close function will handle if any cleanup is required. e.g. closing streams
// or cleaning temproray files so on and so forth
func (app *App) Close() error {
	return nil
}

// set the level of logging it is fatal by default
func setLogLevel(logLevel string) {
	switch logLevel {
	case "trace":
		log.SetLevel(log.TraceLevel)
	case "debug":
		log.SetLevel(log.DebugLevel)
	case "info":
		log.SetLevel(log.InfoLevel)
	case "warn":
		log.SetLevel(log.WarnLevel)
	case "error":
		log.SetLevel(log.ErrorLevel)
	default:
		log.SetLevel(log.FatalLevel)
	}
	log.WithFields(log.Fields{
		"level": logLevel,
	}).Trace("logging level has been set")
}

func overrideDefaults(setupConfig *SetupConfig) (appConfig *SetupConfig, err error) {
	appConfig, err = LoadConfiguration()
	if len(setupConfig.Directories) > 0 {
		appConfig.Directories = setupConfig.Directories
	}
	if len(setupConfig.LogLevel) > 0 {
		appConfig.LogLevel = setupConfig.LogLevel
	}
	if setupConfig.Depth > 0 {
		appConfig.Depth = setupConfig.Depth
	}
	if setupConfig.QuickMode {
		appConfig.QuickMode = setupConfig.QuickMode
	}
	if len(setupConfig.Mode) > 0 {
		appConfig.Mode = setupConfig.Mode
	}
	return appConfig, err
}