From 3b888d4fc7f801a14ca265cada84077209714599 Mon Sep 17 00:00:00 2001 From: Andinus Date: Sat, 25 Apr 2020 19:39:51 +0530 Subject: Add parseConfig func --- parseconfig.go | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 parseconfig.go 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) + } +} -- cgit 1.4.1-2-gfad0