Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add putf #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions acme/acme.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

fn acme_read(rpath) {
var fpath = "acme/"+$winid+"/"+$rpath

var content, status <= 9p read $fpath

if $status != "0" {
return "", format("failed to read 9p path: %s", $fpath)
}
Expand All @@ -12,21 +14,27 @@ fn acme_read(rpath) {

fn acme_write(rpath, data) {
var fpath = "acme/"+$winid+"/"+$rpath

var _, errmsg, status <= echo -n $data | 9p write $fpath

if $status != "0" {
return format("failed to write to: (%s): %s", $fpath, $errmsg)
}

return ""
}

fn acme_fname() {
var path = "acme/"+$winid+"/tag"

var tag, status <= 9p read $path

if $status != "0" {
return "", format("Failed to read tag: %s", $path)
}

var fname, status <= echo -n $tag | cut -d " " -f1

if $status != "0" {
return "", format("failed to get tag filename: %s", $tag)
}
Expand All @@ -36,11 +44,13 @@ fn acme_fname() {

fn acme_savefile(path, data) {
var _, status <= test -f $path

if $status != "0" {
return format("file exists: %s", $path)
}

_, status <= echo -n $data > $path

if $status != "0" {
return format("failed to write content: %s", $path)
}
Expand All @@ -52,6 +62,7 @@ fn acme_savefile(path, data) {
# and an error (if any)
fn acme_mktmpfile() {
var tmp, status <= mktemp /tmp/acme-XXXX

if $status != "0" {
return "", format("failed to create tmp file")
}
Expand All @@ -67,6 +78,7 @@ fn acme_mktmpfile() {
# and return the modified body or an error.
fn acme_simplefmt(fmtfn) {
var tmpsrc, err <= acme_mktmpfile()

if $err != "" {
return $err
}
Expand All @@ -76,23 +88,28 @@ fn acme_simplefmt(fmtfn) {
}

var body, err <= acme_read("body")

if $err != "" {
clean()
clean()

return $err
}

var err <= acme_savefile($tmpsrc, $body)

if $err != "" {
clean()

return $err
}

var newbody, err <= $fmtfn($tmpsrc)

clean()

if $err != "" {
return $err
}

if $body != $newbody {
# code changed, we need to update buffer
var operations = (
Expand All @@ -108,11 +125,16 @@ fn acme_simplefmt(fmtfn) {

for op in $operations {
var err <= acme_write($op[0], $op[1])

if $err != "" {
return $err
}
}
}

return ""
}

fn acme_put() {
return acme_write("ctl", "put")
}
2 changes: 1 addition & 1 deletion bin/fmt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# universal formatter

import "io"
import "acme/acme.sh"
import "acme/acme"

fn abortonerr(status, msg) {
if $status != "0" {
Expand Down
18 changes: 18 additions & 0 deletions bin/putf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env nash

import "io"
import "acme/acme"

var out, status <= fmt

if $status != "0" {
io_println("error formatting code: %s", $out)
io_println("file will be saved anyway")
}

var err <= acme_put()

if $err != "" {
io_println("error saving file: %s", $err)
exit("1")
}