aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/system/xhost.go
diff options
context:
space:
mode:
authorOphestra Umiker <cat@ophivana.moe>2024-10-16 01:31:23 +0900
committerOphestra Umiker <cat@ophivana.moe>2024-10-16 01:31:23 +0900
commit430f1a5b4e4e2b164297391390e3a0cbe2ad2eb2 (patch)
tree76aab87e648c6e8fd6c8afc3d314bc73563ee552 /internal/system/xhost.go
parent0fd63e85e730d44abd488c892be377b332f5458e (diff)
system: isolate app/system into generic implementation
This improves maintainability and extensibility of system operations, makes writing tests for them possible, and operations now apply and revert in order, instead of being bunched up into their own categories. Signed-off-by: Ophestra Umiker <cat@ophivana.moe>
Diffstat (limited to 'internal/system/xhost.go')
-rw-r--r--internal/system/xhost.go54
1 files changed, 54 insertions, 0 deletions
diff --git a/internal/system/xhost.go b/internal/system/xhost.go
new file mode 100644
index 00000000..2cdddd56
--- /dev/null
+++ b/internal/system/xhost.go
@@ -0,0 +1,54 @@
+package system
+
+import (
+ "fmt"
+
+ "git.ophivana.moe/cat/fortify/internal/fmsg"
+ "git.ophivana.moe/cat/fortify/internal/state"
+ "git.ophivana.moe/cat/fortify/internal/verbose"
+ "git.ophivana.moe/cat/fortify/xcb"
+)
+
+// ChangeHosts appends an X11 ChangeHosts command Op.
+func (sys *I) ChangeHosts(username string) {
+ sys.lock.Lock()
+ defer sys.lock.Unlock()
+
+ sys.ops = append(sys.ops, XHost(username))
+}
+
+type XHost string
+
+func (x XHost) Type() state.Enablement {
+ return state.EnableX
+}
+
+func (x XHost) apply(_ *I) error {
+ verbose.Printf("inserting entry %s to X11\n", x)
+ return fmsg.WrapErrorSuffix(xcb.ChangeHosts(xcb.HostModeInsert, xcb.FamilyServerInterpreted, "localuser\x00"+string(x)),
+ fmt.Sprintf("cannot insert entry %s to X11:", x))
+}
+
+func (x XHost) revert(_ *I, ec *Criteria) error {
+ if ec.hasType(x) {
+ verbose.Printf("deleting entry %s from X11\n", x)
+ return fmsg.WrapErrorSuffix(xcb.ChangeHosts(xcb.HostModeDelete, xcb.FamilyServerInterpreted, "localuser\x00"+string(x)),
+ fmt.Sprintf("cannot delete entry %s from X11:", x))
+ } else {
+ verbose.Printf("skipping entry %s in X11\n", x)
+ return nil
+ }
+}
+
+func (x XHost) Is(o Op) bool {
+ x0, ok := o.(XHost)
+ return ok && x == x0
+}
+
+func (x XHost) Path() string {
+ return string(x)
+}
+
+func (x XHost) String() string {
+ return string("SI:localuser:" + x)
+}