~chrisppy/titan

ref: 3fe56b6f75ca6d6475e7b9dae0ab00bc03eeee1a titan/app.go -rw-r--r-- 2.6 KiB
3fe56b6fChris Palmer move to some constants and change from browser to capsules 3 months ago
                                                                                
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
// This file is part of titan.
//
// Copyright © 2020-2021 Chris Palmer <chris@red-oxide.org>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package main

import (
	"fmt"
	"log"
	"os"
	"path/filepath"

	"git.sr.ht/~adnano/go-gemini"
	"git.sr.ht/~adnano/go-gemini/tofu"
	"git.sr.ht/~sircmpwn/getopt"
)

type app struct {
	Dirs   dirs
	Config *config
	Gemini *gmni
	Logger *log.Logger
	UI     *ui
}

type dirs struct {
	Config string
	Data   string
	Log    string
	Gemini string
}

type gmni struct {
	Hosts     tofu.KnownHosts
	HostsFile *tofu.HostWriter
	Client    *gemini.Client
}

func (a *app) init() {
	opts, optind, err := getopt.Getopts(os.Args, "c:d:hV")
	if err != nil {
		if _, err := fmt.Fprintln(os.Stderr, err); err != nil {
			panic(fmt.Sprintf("error writing to stderr: %e\n", err))
		}
		os.Exit(1)
	}

	for _, opt := range opts {
		switch opt.Option {
		case 'c':
			a.Dirs.Config = opt.Value
		case 'd':
			a.Dirs.Data = opt.Value
		case 'h':
			printHelp()
		case 'V':
			printVersion()
		}
	}

	links := os.Args[optind:]

	if _, err := os.Stat(filepath.Clean(a.Dirs.Data)); os.IsNotExist(err) {
		if errDir := os.MkdirAll(a.Dirs.Data, 0750); errDir != nil {
			a.Logger.Fatalf("unable to create data dir due to %s", errDir.Error())
			return
		}
	}
	if _, err := os.Stat(filepath.Clean(a.Dirs.Log)); os.IsNotExist(err) {
		if errDir := os.MkdirAll(a.Dirs.Log, 0750); errDir != nil {
			a.Logger.Fatalf("unable to create log dir due to %s", errDir.Error())
			return
		}
	}

	a.loadConfig()
	a.loadGemini()
	a.loadUI(links)

	a.Dirs.Log = filepath.Join(a.Dirs.Log, "titan.log")
	f, err := os.OpenFile(filepath.Clean(a.Dirs.Log), os.O_WRONLY|os.O_CREATE|os.O_APPEND, 00600)
	if err != nil {
		a.Logger.Fatalf("Error: unable to load logfile due to %s", err.Error())
	}
	a.Logger.SetOutput(f)
}

func (a *app) run() error {
	a.UI.Application.SetRoot(a.UI.MainView, true)

	a.UI.Panels.SetCurrentPanel(capsulesName)
	a.UI.Application.SetFocus(a.UI.BrowserPanel.TabbedPanels)

	return a.UI.Application.Run()
}