~chrisppy/beagles

ref: v0.2.0 beagles/main.go -rw-r--r-- 3.1 KiB
c14a6fceChris Palmer Updates for 0.2.0 release 1 year, 18 days 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// This file is part of beagles.
//
// Copyright © 2020 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 (
	"log"
	"os"
	"path/filepath"

	"git.sr.ht/~chrisppy/beagles/config"
	"git.sr.ht/~chrisppy/beagles/db"
	"git.sr.ht/~chrisppy/beagles/ui"
	wlog "github.com/DataDrake/waterlog"
	"github.com/DataDrake/waterlog/format"
	"github.com/DataDrake/waterlog/level"
)

var (
	// Version of the application
	Version string
)

func main() {
	logger := wlog.New(os.Stdout, "", log.Ltime)
	logger.SetLevel(level.Info)
	logger.SetFormat(format.Un)

	logDir := os.Getenv("BEAGLES_LOG")
	if logDir == "" {
		d, err := os.UserCacheDir()
		if err != nil {
			wlog.Fatal(err.Error())
		}

		logDir = filepath.Join(d, "beagles")

		if _, err = os.Stat(logDir); os.IsNotExist(err) {
			if errDir := os.MkdirAll(logDir, 0750); errDir != nil {
				wlog.Fatal(err)
			}
		}
	}

	logPath := filepath.Clean(filepath.Join(logDir, "beagles.log"))
	f, err := os.OpenFile(logPath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 00600)
	if err != nil {
		wlog.Fatal(err.Error())
	}
	logger.SetOutput(f)

	configDir := os.Getenv("BEAGLES_CFG")
	if configDir == "" {
		d, err := os.UserConfigDir()
		if err != nil {
			wlog.Fatal(err.Error())
		}

		configDir = filepath.Join(d, "beagles")

		if _, err = os.Stat(configDir); os.IsNotExist(err) {
			if errDir := os.MkdirAll(configDir, 0750); errDir != nil {
				wlog.Fatal(err)
			}
		}
	}

	dbDir := os.Getenv("BEAGLES_DB")
	if dbDir == "" {
		d, err := os.UserHomeDir()
		if err != nil {
			wlog.Fatal(err.Error())
		}

		dbDir = filepath.Join(d, ".local", "share", "beagles")

		if _, err = os.Stat(dbDir); os.IsNotExist(err) {
			if errDir := os.MkdirAll(dbDir, 0750); errDir != nil {
				wlog.Fatal(err)
			}
		}
	}

	dlDir := os.Getenv("BEAGLES_DL")
	if dlDir == "" {
		d, err := os.UserHomeDir()
		if err != nil {
			wlog.Fatal(err.Error())
		}

		dlDir = filepath.Join(d, ".local", "share", "beagles")

		if _, err = os.Stat(dlDir); os.IsNotExist(err) {
			if errDir := os.MkdirAll(dlDir, 0750); errDir != nil {
				wlog.Fatal(err)
			}
		}
	}

	db, err := db.ReadDB(filepath.Clean(filepath.Join(dbDir, "beagles.db")))
	if err != nil {
		wlog.Fatal(err.Error())
	}

	cfg := config.Load(configDir)

	i := &ui.UI{
		Version:      Version,
		DB:           db,
		Config:       cfg,
		DownloadPath: dlDir,
		Logger:       logger,
	}

	i.Init()

	if cfg.AutoUpdateInterval > 0 {
		go i.UpdateLoop(cfg.AutoUpdateInterval)
	}

	if err := i.Run(); err != nil {
		wlog.Fatal(err.Error())
	}
}