Debian Codegraph @

main.go

raw symbols SCIP
1 // gomuks - A terminal Matrix client written in Go.
2 // Copyright (C) 2020 Tulir Asokan
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
13 //
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17 package main
18
19 import (
20 "errors"
21 "fmt"
22 "os"
23 "os/exec"
24 "path/filepath"
25 "runtime"
26 "strings"
27 "time"
28
29 flag "maunium.net/go/mauflag"
30
31 "maunium.net/go/gomuks/debug"
32 ifc "maunium.net/go/gomuks/interface"
33 "maunium.net/go/gomuks/matrix"
34 "maunium.net/go/gomuks/ui"
35 )
36
37 var MainUIProvider ifc.UIProvider = ui.NewGomuksUI
38
39 var wantVersion = flag.MakeFull("v", "version", "Show the version of gomuks", "false").Bool()
40 var clearCache = flag.MakeFull("c", "clear-cache", "Clear the cache directory instead of starting", "false").Bool()
41 var clearData = flag.Make().LongKey("clear-all-data").Usage("Clear all data instead of starting").Default("false").Bool()
42 var skipVersionCheck = flag.MakeFull("s", "skip-version-check", "Skip the homeserver version checks at startup and login", "false").Bool()
43 var wantHelp, _ = flag.MakeHelpFlag()
44
45 func main() {
46 flag.SetHelpTitles(
47 "gomuks - A terminal Matrix client written in Go.",
48 "gomuks [-vch] [--clear-all-data]",
49 )
50 err := flag.Parse()
51 if err != nil {
52 fmt.Println(err)
53 os.Exit(1)
54 } else if *wantHelp {
55 flag.PrintHelp()
56 return
57 } else if *wantVersion {
58 fmt.Println(VersionString)
59 return
60 }
61 debugDir := os.Getenv("DEBUG_DIR")
62 if len(debugDir) > 0 {
63 debug.LogDirectory = debugDir
64 }
65 debugLevel := strings.ToLower(os.Getenv("DEBUG"))
66 if debugLevel == "1" || debugLevel == "t" || debugLevel == "true" {
67 debug.RecoverPrettyPanic = false
68 debug.DeadlockDetection = true
69 debug.WriteLogs = true
70 }
71 debug.Initialize()
72 defer debug.Recover()
73
74 var configDir, dataDir, cacheDir, downloadDir string
75
76 configDir, err = UserConfigDir()
77 if err != nil {
78 _, _ = fmt.Fprintln(os.Stderr, "Failed to get config directory:", err)
79 os.Exit(3)
80 }
81 dataDir, err = UserDataDir()
82 if err != nil {
83 _, _ = fmt.Fprintln(os.Stderr, "Failed to get data directory:", err)
84 os.Exit(3)
85 }
86 cacheDir, err = UserCacheDir()
87 if err != nil {
88 _, _ = fmt.Fprintln(os.Stderr, "Failed to get cache directory:", err)
89 os.Exit(3)
90 }
91 downloadDir, err = UserDownloadDir()
92 if err != nil {
93 _, _ = fmt.Fprintln(os.Stderr, "Failed to get download directory:", err)
94 os.Exit(3)
95 }
96
97 debug.Print("Config directory:", configDir)
98 debug.Print("Data directory:", dataDir)
99 debug.Print("Cache directory:", cacheDir)
100 debug.Print("Download directory:", downloadDir)
101
102 matrix.SkipVersionCheck = *skipVersionCheck
103 gmx := NewGomuks(MainUIProvider, configDir, dataDir, cacheDir, downloadDir)
104
105 if *clearCache {
106 debug.Print("Clearing cache as requested by CLI flag")
107 gmx.config.Clear()
108 fmt.Printf("Cleared cache at %s\n", gmx.config.CacheDir)
109 return
110 } else if *clearData {
111 debug.Print("Clearing all data as requested by CLI flag")
112 gmx.config.Clear()
113 gmx.config.ClearData()
114 _ = os.RemoveAll(gmx.config.Dir)
115 fmt.Printf("Cleared cache at %s, data at %s and config at %s\n", gmx.config.CacheDir, gmx.config.DataDir, gmx.config.Dir)
116 return
117 }
118
119 gmx.Start()
120
121 // We use os.Exit() everywhere, so exiting by returning from Start() shouldn't happen.
122 time.Sleep(5 * time.Second)
123 fmt.Println("Unexpected exit by return from gmx.Start().")
124 os.Exit(2)
125 }
126
127 func getRootDir(subdir string) string {
128 rootDir := os.Getenv("GOMUKS_ROOT")
129 if rootDir == "" {
130 return ""
131 }
132 return filepath.Join(rootDir, subdir)
133 }
134
135 func UserCacheDir() (dir string, err error) {
136 dir = os.Getenv("GOMUKS_CACHE_HOME")
137 if dir == "" {
138 dir = getRootDir("cache")
139 }
140 if dir == "" {
141 dir, err = os.UserCacheDir()
142 dir = filepath.Join(dir, "gomuks")
143 }
144 return
145 }
146
147 func UserDataDir() (dir string, err error) {
148 dir = os.Getenv("GOMUKS_DATA_HOME")
149 if dir != "" {
150 return
151 }
152 if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
153 return UserConfigDir()
154 }
155 dir = getRootDir("data")
156 if dir == "" {
157 dir = os.Getenv("XDG_DATA_HOME")
158 }
159 if dir == "" {
160 dir = os.Getenv("HOME")
161 if dir == "" {
162 return "", errors.New("neither $XDG_DATA_HOME nor $HOME are defined")
163 }
164 dir = filepath.Join(dir, ".local", "share")
165 }
166 dir = filepath.Join(dir, "gomuks")
167 return
168 }
169
170 func getXDGUserDir(name string) (dir string, err error) {
171 cmd := exec.Command("xdg-user-dir", name)
172 var out strings.Builder
173 cmd.Stdout = &out
174 err = cmd.Run()
175 dir = strings.TrimSpace(out.String())
176 return
177 }
178
179 func UserDownloadDir() (dir string, err error) {
180 dir = os.Getenv("GOMUKS_DOWNLOAD_HOME")
181 if dir != "" {
182 return
183 }
184 dir, _ = getXDGUserDir("DOWNLOAD")
185 if dir != "" {
186 return
187 }
188 dir, err = os.UserHomeDir()
189 dir = filepath.Join(dir, "Downloads")
190 return
191 }
192
193 func UserConfigDir() (dir string, err error) {
194 dir = os.Getenv("GOMUKS_CONFIG_HOME")
195 if dir == "" {
196 dir = getRootDir("config")
197 }
198 if dir == "" {
199 dir, err = os.UserConfigDir()
200 dir = filepath.Join(dir, "gomuks")
201 }
202 return
203 }