| 1 |
package commands |
| 2 |
|
| 3 |
import ( |
| 4 |
"os" |
| 5 |
"os/signal" |
| 6 |
"syscall" |
| 7 |
|
| 8 |
"git.sr.ht/~rjarry/aerc/app" |
| 9 |
"git.sr.ht/~rjarry/aerc/config" |
| 10 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 11 |
"git.sr.ht/~rjarry/aerc/lib/ui" |
| 12 |
) |
| 13 |
|
| 14 |
type Reload struct { |
| 15 |
Binds bool `opt:"-B" desc:"Reload binds.conf."` |
| 16 |
Conf bool `opt:"-C" desc:"Reload aerc.conf."` |
| 17 |
Style string `opt:"-s" complete:"CompleteStyle" desc:"Reload the specified styleset."` |
| 18 |
} |
| 19 |
|
| 20 |
func init() { |
| 21 |
Register(Reload{}) |
| 22 |
sig := make(chan os.Signal, 1) |
| 23 |
signal.Notify(sig, syscall.SIGHUP) |
| 24 |
go func() { |
| 25 |
defer log.PanicHandler() |
| 26 |
for range sig { |
| 27 |
log.Infof("received SIGHUP, reloading configuration") |
| 28 |
err := Reload{}.Execute(nil) |
| 29 |
if err != nil { |
| 30 |
log.Errorf("reload failed: %s", err) |
| 31 |
} |
| 32 |
} |
| 33 |
}() |
| 34 |
} |
| 35 |
|
| 36 |
func (Reload) Description() string { |
| 37 |
return "Hot-reload configuration files." |
| 38 |
} |
| 39 |
|
| 40 |
func (r *Reload) CompleteStyle(s string) []string { |
| 41 |
var files []string |
| 42 |
for _, dir := range config.Ui().StyleSetDirs { |
| 43 |
entries, err := os.ReadDir(dir) |
| 44 |
if err != nil { |
| 45 |
log.Debugf("could not read directory '%s': %v", dir, |
| 46 |
err) |
| 47 |
continue |
| 48 |
} |
| 49 |
for _, e := range entries { |
| 50 |
if e.IsDir() { |
| 51 |
continue |
| 52 |
} |
| 53 |
files = append(files, e.Name()) |
| 54 |
} |
| 55 |
} |
| 56 |
return FilterList(files, s, nil) |
| 57 |
} |
| 58 |
|
| 59 |
func (Reload) Context() CommandContext { |
| 60 |
return GLOBAL |
| 61 |
} |
| 62 |
|
| 63 |
func (Reload) Aliases() []string { |
| 64 |
return []string{"reload"} |
| 65 |
} |
| 66 |
|
| 67 |
func (r Reload) Execute(args []string) error { |
| 68 |
if !r.Binds && !r.Conf && r.Style == "" { |
| 69 |
r.Binds = true |
| 70 |
r.Conf = true |
| 71 |
r.Style = config.Ui().StyleSetName |
| 72 |
} |
| 73 |
|
| 74 |
reconfigure := false |
| 75 |
|
| 76 |
if r.Binds { |
| 77 |
f, err := config.ReloadBinds() |
| 78 |
if err != nil { |
| 79 |
return err |
| 80 |
} |
| 81 |
app.PushSuccess("Binds reloaded: " + f) |
| 82 |
} |
| 83 |
|
| 84 |
if r.Conf { |
| 85 |
f, err := config.ReloadConf() |
| 86 |
if err != nil { |
| 87 |
return err |
| 88 |
} |
| 89 |
app.PushSuccess("Conf reloaded: " + f) |
| 90 |
reconfigure = true |
| 91 |
} |
| 92 |
|
| 93 |
if r.Style != "" { |
| 94 |
config.Ui().ClearCache() |
| 95 |
config.Ui().StyleSetName = r.Style |
| 96 |
err := config.Ui().LoadStyle() |
| 97 |
if err != nil { |
| 98 |
return err |
| 99 |
} |
| 100 |
app.PushSuccess("Styleset: " + r.Style) |
| 101 |
reconfigure = true |
| 102 |
} |
| 103 |
|
| 104 |
if !reconfigure { |
| 105 |
return nil |
| 106 |
} |
| 107 |
|
| 108 |
// reload account views and message stores |
| 109 |
for _, name := range app.AccountNames() { |
| 110 |
|
| 111 |
// rebuild account view |
| 112 |
view, err := app.Account(name) |
| 113 |
if err != nil { |
| 114 |
continue |
| 115 |
} |
| 116 |
|
| 117 |
dirlist := view.Directories() |
| 118 |
if dirlist == nil { |
| 119 |
continue |
| 120 |
} |
| 121 |
|
| 122 |
wantTree := config.Ui().ForAccount(name).DirListTree |
| 123 |
dirlist = adjustDirlist(dirlist, wantTree) |
| 124 |
view.SetDirectories(dirlist) |
| 125 |
|
| 126 |
// now rebuild grid with correct dirlist |
| 127 |
view.Configure() |
| 128 |
|
| 129 |
// reconfigure the message stores |
| 130 |
for _, dir := range dirlist.List() { |
| 131 |
store, ok := dirlist.MsgStore(dir) |
| 132 |
if !ok { |
| 133 |
continue |
| 134 |
} |
| 135 |
uiConf := dirlist.UiConfig(dir) |
| 136 |
store.Configure(view.SortCriteria(uiConf)) |
| 137 |
} |
| 138 |
ui.Invalidate() |
| 139 |
} |
| 140 |
|
| 141 |
// reload message viewers |
| 142 |
doTabs(func(tab *ui.Tab) { |
| 143 |
mv, ok := tab.Content.(*app.MessageViewer) |
| 144 |
if !ok { |
| 145 |
return |
| 146 |
} |
| 147 |
reloaded, err := app.NewMessageViewer( |
| 148 |
mv.SelectedAccount(), |
| 149 |
mv.MessageView(), |
| 150 |
) |
| 151 |
if err != nil { |
| 152 |
app.PushError(err.Error()) |
| 153 |
return |
| 154 |
} |
| 155 |
app.ReplaceTab(mv, reloaded, tab.Name, false) |
| 156 |
}) |
| 157 |
|
| 158 |
// reload composers |
| 159 |
doTabs(func(tab *ui.Tab) { |
| 160 |
c, ok := tab.Content.(*app.Composer) |
| 161 |
if !ok { |
| 162 |
return |
| 163 |
} |
| 164 |
_ = c.SwitchAccount(c.Account()) |
| 165 |
}) |
| 166 |
|
| 167 |
return nil |
| 168 |
} |
| 169 |
|
| 170 |
func adjustDirlist(d app.DirectoryLister, wantTree bool) app.DirectoryLister { |
| 171 |
switch d := d.(type) { |
| 172 |
case *app.DirectoryList: |
| 173 |
if wantTree { |
| 174 |
log.Tracef("dirlist: build tree") |
| 175 |
tree := app.NewDirectoryTree(d) |
| 176 |
tree.SelectedMsgStore() |
| 177 |
return tree |
| 178 |
} |
| 179 |
log.Tracef("dirlist: already dirlist") |
| 180 |
return d |
| 181 |
case *app.DirectoryTree: |
| 182 |
if !wantTree { |
| 183 |
log.Tracef("dirtree: get dirlist") |
| 184 |
return d.DirectoryList |
| 185 |
} |
| 186 |
log.Tracef("dirtree: already tree") |
| 187 |
return d |
| 188 |
default: |
| 189 |
return d |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
func doTabs(do func(*ui.Tab)) { |
| 194 |
var tabname string |
| 195 |
if t := app.SelectedTab(); t != nil { |
| 196 |
tabname = t.Name |
| 197 |
} |
| 198 |
for i := range app.TabNames() { |
| 199 |
tab := app.GetTab(i) |
| 200 |
if tab == nil { |
| 201 |
continue |
| 202 |
} |
| 203 |
do(tab) |
| 204 |
} |
| 205 |
if tabname != "" { |
| 206 |
app.SelectTab(tabname) |
| 207 |
} |
| 208 |
} |