summary refs log tree commit diff stats
path: root/parseconfig.go
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2020-04-25 19:39:51 +0530
committerAndinus <andinus@nand.sh>2020-04-25 19:39:51 +0530
commit3b888d4fc7f801a14ca265cada84077209714599 (patch)
treec509bd3ee48d994314b57da83b337b5f888b2654 /parseconfig.go
parentda1d5980e285a52d30ed7a122553ec947b028b71 (diff)
downloadpavo-3b888d4fc7f801a14ca265cada84077209714599.tar.gz
Add parseConfig func
Diffstat (limited to 'parseconfig.go')
-rw-r--r--parseconfig.go110
1 files changed, 110 insertions, 0 deletions
diff --git a/parseconfig.go b/parseconfig.go
new file mode 100644
index 0000000..6f5abec
--- /dev/null
+++ b/parseconfig.go
@@ -0,0 +1,110 @@
+package main
+
+import (
+	"encoding/json"
+	"flag"
+	"fmt"
+	"io/ioutil"
+	"os"
+
+	"tildegit.org/andinus/lynx"
+)
+
+type Pavo struct {
+	Config []Config `json:"config"`
+}
+
+type Unveil struct {
+	Permissions string `json:"permissions"`
+	Path        string `json:"path"`
+}
+
+type Config struct {
+	Command          string   `json:"command"`
+	Unveil           []Unveil `json:"unveil"`
+	CommandsToUnveil []string `json:"commandstounveil"`
+	Execpromises     string   `json:"execpromises"`
+}
+
+func parseConfig() {
+	// Read the config file
+	data, err := ioutil.ReadFile(configFile())
+	if err != nil {
+		fmt.Printf("%s :: %s",
+			"Failed to read the config file",
+			err.Error())
+		os.Exit(1)
+	}
+
+	var Pavo Pavo
+
+	// Unmarshal data to struct.
+	err = json.Unmarshal(data, &Pavo)
+	if err != nil {
+		fmt.Printf("%s :: %s\n",
+			"Failed to unmarshal the config file",
+			err.Error())
+		os.Exit(1)
+	}
+
+	// Get command & flags from flag.Args().
+	flag.Parse()
+	flags = flag.Args()[1:]
+
+	for k, v := range Pavo.Config {
+		// If we find the command's config then break.
+		if v.Command == flag.Args()[0] {
+			cmd = v
+			break
+		}
+
+		// If we don't find the config in file then return an
+		// error.
+		if k == len(Pavo.Config)-1 {
+			fmt.Printf("%s %s",
+				"Failed to find config for",
+				flag.Args()[0])
+			os.Exit(1)
+		}
+	}
+
+	// Commands to unveil.
+	commands := []string{cmd.Command}
+
+	// Get commands to unveil from config.
+	for _, v := range cmd.CommandsToUnveil {
+		commands = append(commands, v)
+	}
+
+	// Unveil all values of $PATH/command.
+	err = lynx.UnveilCommands(commands)
+	if err != nil {
+		fmt.Printf("%s :: %s",
+			"Failed to unveil command",
+			err.Error())
+		os.Exit(1)
+	}
+
+	// Set execpromises from config.
+	err = lynx.PledgeExecpromises(cmd.Execpromises)
+	if err != nil {
+		fmt.Printf("%s :: %s",
+			"PledgeExecpromises failed",
+			err.Error())
+		os.Exit(1)
+	}
+
+	// Unveil all paths from config.
+	paths := make(map[string]string)
+	for _, v := range cmd.Unveil {
+		paths[v.Path] = v.Permissions
+	}
+
+	err = lynx.UnveilPaths(paths)
+	if err != nil {
+		fmt.Printf("%s :: %s",
+			"Failed to unveil paths",
+			err.Error())
+		os.Exit(1)
+	}
+}
id='n267' href='#n267'>267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362