Skip to content

Commit 6bbee89

Browse files
Add the ability lock settings.json and bindings.json
1 parent 9b3f7ff commit 6bbee89

File tree

5 files changed

+36
-3
lines changed

5 files changed

+36
-3
lines changed

cmd/micro/initlua.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func luaImportMicroConfig() *lua.LTable {
7373
ulua.L.SetField(pkg, "OptionComplete", luar.New(ulua.L, action.OptionComplete))
7474
ulua.L.SetField(pkg, "OptionValueComplete", luar.New(ulua.L, action.OptionValueComplete))
7575
ulua.L.SetField(pkg, "NoComplete", luar.New(ulua.L, nil))
76-
ulua.L.SetField(pkg, "TryBindKey", luar.New(ulua.L, action.TryBindKey))
76+
ulua.L.SetField(pkg, "TryBindKey", luar.New(ulua.L, action.TryBindKeyPlug))
7777
ulua.L.SetField(pkg, "Reload", luar.New(ulua.L, action.ReloadConfig))
7878
ulua.L.SetField(pkg, "AddRuntimeFileFromMemory", luar.New(ulua.L, config.PluginAddRuntimeFileFromMemory))
7979
ulua.L.SetField(pkg, "AddRuntimeFilesFromDirectory", luar.New(ulua.L, config.PluginAddRuntimeFilesFromDirectory))
@@ -88,8 +88,8 @@ func luaImportMicroConfig() *lua.LTable {
8888
ulua.L.SetField(pkg, "RegisterCommonOption", luar.New(ulua.L, config.RegisterCommonOptionPlug))
8989
ulua.L.SetField(pkg, "RegisterGlobalOption", luar.New(ulua.L, config.RegisterGlobalOptionPlug))
9090
ulua.L.SetField(pkg, "GetGlobalOption", luar.New(ulua.L, config.GetGlobalOption))
91-
ulua.L.SetField(pkg, "SetGlobalOption", luar.New(ulua.L, action.SetGlobalOption))
92-
ulua.L.SetField(pkg, "SetGlobalOptionNative", luar.New(ulua.L, action.SetGlobalOptionNative))
91+
ulua.L.SetField(pkg, "SetGlobalOption", luar.New(ulua.L, action.SetGlobalOptionPlug))
92+
ulua.L.SetField(pkg, "SetGlobalOptionNative", luar.New(ulua.L, action.SetGlobalOptionNativePlug))
9393
ulua.L.SetField(pkg, "ConfigDir", luar.New(ulua.L, config.ConfigDir))
9494

9595
return pkg

internal/action/bindings.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,13 @@ func eventsEqual(e1 Event, e2 Event) bool {
256256
return e1 == e2
257257
}
258258

259+
func TryBindKeyPlug(k, v string, overwrite bool) (bool, error) {
260+
if l, ok := config.GlobalSettings["lockbindings"]; ok && l.(bool) {
261+
return false, errors.New("bindings.json file locked by user for all plugins")
262+
}
263+
return TryBindKey(k, v, overwrite)
264+
}
265+
259266
// TryBindKey tries to bind a key by writing to config.ConfigDir/bindings.json
260267
// Returns true if the keybinding already existed and a possible error
261268
func TryBindKey(k, v string, overwrite bool) (bool, error) {

internal/action/command.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,13 @@ func doSetGlobalOptionNative(option string, nativeValue interface{}) error {
634634
return nil
635635
}
636636

637+
func SetGlobalOptionNativePlug(option string, nativeValue interface{}) error {
638+
if l, ok := config.GlobalSettings["locksettings"]; ok && l.(bool) {
639+
return errors.New("settings.json file locked by user for all plugins")
640+
}
641+
return SetGlobalOptionNative(option, nativeValue)
642+
}
643+
637644
func SetGlobalOptionNative(option string, nativeValue interface{}) error {
638645
if err := config.OptionIsValid(option, nativeValue); err != nil {
639646
return err
@@ -660,6 +667,13 @@ func SetGlobalOptionNative(option string, nativeValue interface{}) error {
660667
return config.WriteSettings(filepath.Join(config.ConfigDir, "settings.json"))
661668
}
662669

670+
func SetGlobalOptionPlug(option, value string) error {
671+
if l, ok := config.GlobalSettings["locksettings"]; ok && l.(bool) {
672+
return errors.New("settings.json file locked by user for all plugins")
673+
}
674+
return SetGlobalOption(option, value)
675+
}
676+
663677
func SetGlobalOption(option, value string) error {
664678
if _, ok := config.GlobalSettings[option]; !ok {
665679
return config.ErrInvalidOption

internal/config/settings.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ var DefaultGlobalOnlySettings = map[string]interface{}{
116116
"helpsplit": "hsplit",
117117
"infobar": true,
118118
"keymenu": false,
119+
"lockbindings": false,
120+
"locksettings": false,
119121
"mouse": true,
120122
"multiopen": "tab",
121123
"parsecursor": false,

runtime/help/options.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,16 @@ Here are the available options:
237237

238238
default value: `false`
239239

240+
* `lockbindings`: Disable plugins to modify the `bindings.json` in your config
241+
directory.
242+
243+
default value: `false`
244+
245+
* `locksettings`: Disable plugins to modify the `settings.json` in your config
246+
directory.
247+
248+
default value: `false`
249+
240250
* `matchbrace`: show matching braces for '()', '{}', '[]' when the cursor
241251
is on a brace character or (if `matchbraceleft` is enabled) next to it.
242252

0 commit comments

Comments
 (0)