Skip to content

Commit

Permalink
net/helper: use stdin for passing data
Browse files Browse the repository at this point in the history
fixes #168
  • Loading branch information
verbit committed Aug 16, 2024
1 parent f5c21d3 commit cd66f03
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
7 changes: 1 addition & 6 deletions apps/virtuerl/src/virtuerl_net.erl
Original file line number Diff line number Diff line change
Expand Up @@ -379,11 +379,6 @@ run_helper(Command, Contents) ->
{ok, Val} -> Val; undefined -> "virtuerl_helper"
end,

Path = iolist_to_binary(["/tmp/virtuerl/", "helper_", virtuerl_util:uuid4()]),
ok = filelib:ensure_dir(Path),
ok = file:write_file(Path, Contents),

{ok, HelperOut} = virtuerl_util:cmd(iolist_to_binary([HelperPath, " ", Command, " -f ", Path])),
{ok, HelperOut} = virtuerl_util:cmd(iolist_to_binary([HelperPath, " ", Command, " -f -"]), Contents),
?LOG_DEBUG(#{helper_out => HelperOut}),
file:delete(Path),
ok.
34 changes: 20 additions & 14 deletions helper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,29 @@ func main() {
case "ip":
ipCmd.Parse(os.Args[2:])
fmt.Printf("ip / %s\n", *ipBatchFile)
cmd := exec.Command("/usr/bin/ip", "-b", *ipBatchFile)
ipOutput, err := cmd.CombinedOutput()
if err != nil {
fmt.Println(string(ipOutput))
log.Fatal(err)
}
cmd := exec.Command("/usr/bin/ip", "-b", *ipBatchFile)
if *ipBatchFile == "-" {
cmd.Stdin = os.Stdin
}
ipOutput, err := cmd.CombinedOutput()
if err != nil {
fmt.Println(string(ipOutput))
log.Fatal(err)
}
case "nft":
nftCmd.Parse(os.Args[2:])
fmt.Printf("nft / %s\n", *nftFile)
cmd := exec.Command("/usr/sbin/nft", "-f", *nftFile)
cmd.SysProcAttr = &syscall.SysProcAttr{}
cmd.SysProcAttr.Credential = &syscall.Credential{Uid: uint32(0), Gid: uint32(0)}
nftOutput, err := cmd.CombinedOutput()
if err != nil {
fmt.Println(string(nftOutput))
log.Fatal(err)
}
cmd := exec.Command("/usr/sbin/nft", "-f", *nftFile)
if *nftFile == "-" {
cmd.Stdin = os.Stdin
}
cmd.SysProcAttr = &syscall.SysProcAttr{}
cmd.SysProcAttr.Credential = &syscall.Credential{Uid: uint32(0), Gid: uint32(0)}
nftOutput, err := cmd.CombinedOutput()
if err != nil {
fmt.Println(string(nftOutput))
log.Fatal(err)
}
default:
fmt.Println(`Expected "ip" or "nft" subcommand`)
os.Exit(2)
Expand Down

0 comments on commit cd66f03

Please sign in to comment.