Debian Codegraph @

internal/app/builder.go

raw symbols SCIP
1 package app
2
3 import (
4 "fmt"
5 "os"
6
7 "github.com/isacikgoz/gitbatch/internal/gui"
8 )
9
10 // The App struct is responsible to hold app-wide related entities. Currently
11 // it has only the gui.Gui pointer for interface entity.
12 type App struct {
13 Config *Config
14 }
15
16 // Config is an assembler data to initiate a setup
17 type Config struct {
18 Directories []string
19 LogLevel string
20 Depth int
21 QuickMode bool
22 Mode string
23 }
24
25 // New will handle pre-required operations. It is designed to be a wrapper for
26 // main method right now.
27 func New(argConfig *Config) (*App, error) {
28 // initiate the app and give it initial values
29 app := &App{}
30 if len(argConfig.Directories) <= 0 {
31 d, _ := os.Getwd()
32 argConfig.Directories = []string{d}
33 }
34 presetConfig, err := loadConfiguration()
35 if err != nil {
36 return nil, err
37 }
38 app.Config = overrideConfig(presetConfig, argConfig)
39
40 return app, nil
41 }
42
43 // Run starts the application.
44 func (a *App) Run() error {
45 dirs := generateDirectories(a.Config.Directories, a.Config.Depth)
46 if a.Config.QuickMode {
47 return a.execQuickMode(dirs)
48 }
49 // create a gui.Gui struct and run the gui
50 gui, err := gui.New(a.Config.Mode, dirs)
51 if err != nil {
52 return err
53 }
54 return gui.Run()
55 }
56
57 func overrideConfig(appConfig, setupConfig *Config) *Config {
58 if len(setupConfig.Directories) > 0 {
59 appConfig.Directories = setupConfig.Directories
60 }
61 if len(setupConfig.LogLevel) > 0 {
62 appConfig.LogLevel = setupConfig.LogLevel
63 }
64 if setupConfig.Depth > 0 {
65 appConfig.Depth = setupConfig.Depth
66 }
67 if setupConfig.QuickMode {
68 appConfig.QuickMode = setupConfig.QuickMode
69 }
70 if len(setupConfig.Mode) > 0 {
71 appConfig.Mode = setupConfig.Mode
72 }
73 return appConfig
74 }
75
76 func (a *App) execQuickMode(directories []string) error {
77 if a.Config.Mode != "fetch" && a.Config.Mode != "pull" {
78 return fmt.Errorf("unrecognized quick mode: " + a.Config.Mode)
79 }
80
81 return quick(directories, a.Config.Mode)
82 }