turbo-editors/turbo-corepublic Fork 0
v1.0.1
Commits
Clone
git clone https://git.rickub.com/turbo-editors/turbo-core.git
git clone ssh://git@rickub.com/turbo-editors/turbo-core.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

🛟 Updated. 28d5985 · on v1.0.1 · k33g · 16h ago
README.md · 44 lines · 2.2 KBmarkdown
Blame HistoryOpen raw

projectfile

Writes the TOML files a project keeps in the editor's own directory — .turbo-go, .turbo-rust — namely its settings, its snippets and its tools.

Why it exists

Three packages were writing one identically — the same temporary file, the same rename, the same mode. A fourth would have been the point at which nobody remembered which copy was the correct one, and the project's own standard is to extract at the second occurrence.

The quality gate is what surfaced it: qlty reported the duplication as a smell across settings, snippets and tools, and the only honest way past it was to write the thing once.

What it does not do

It is not what buffer does for a source file. That one preserves the mode of the file it is replacing, because it is saving over something the user already had, and it takes care to remove the temporary on every failure path. This one is creating a file the editor is introducing, and a fixed 0644 is the right answer for it — these files are meant to be committed and read by a team.

The two look alike and are not the same operation. Merging them would mean parameterising the mode and giving up the different error text, for no gain.

How

The write goes to a temporary file in the same directory as its destination, which is then renamed over it. A rename is only atomic within one filesystem, and an interrupted write must leave the previous file intact rather than half of the new one.

CreateTemp makes a file readable by its owner only, so the mode is set before the rename — otherwise the permissions would silently tighten on a file a team shares.

The destination's directory is created if it is not there, so a caller does not have to.

Public API

Name What it does
Write(path string, data []byte) error Creates or replaces a project file atomically, making its directory if need be
if err := projectfile.Write(".turbo-go/tools.toml", []byte(template)); err != nil {
    return err
}

Tests

make test
go test ./projectfile/

One of them checks that no temporary file is left behind — a failed write littering somebody's project directory with .projectfile-*.toml is the kind of thing that has to be explained rather than noticed.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# projectfile

Writes the TOML files a project keeps in the editor's own directory — `.turbo-go`, `.turbo-rust` — namely its settings, its snippets and its tools.

## Why it exists

Three packages were writing one identically — the same temporary file, the same rename, the same mode. A fourth would have been the point at which nobody remembered which copy was the correct one, and the project's own standard is to extract at the second occurrence.

The quality gate is what surfaced it: `qlty` reported the duplication as a smell across `settings`, `snippets` and `tools`, and the only honest way past it was to write the thing once.

## What it does not do

**It is not what `buffer` does for a source file.** That one preserves the mode of the file it is replacing, because it is saving over something the user already had, and it takes care to remove the temporary on every failure path. This one is creating a file the editor is introducing, and a fixed `0644` is the right answer for it — these files are meant to be committed and read by a team.

The two look alike and are not the same operation. Merging them would mean parameterising the mode and giving up the different error text, for no gain.

## How

The write goes to a temporary file **in the same directory** as its destination, which is then renamed over it. A rename is only atomic within one filesystem, and an interrupted write must leave the previous file intact rather than half of the new one.

`CreateTemp` makes a file readable by its owner only, so the mode is set before the rename — otherwise the permissions would silently tighten on a file a team shares.

The destination's directory is created if it is not there, so a caller does not have to.

## Public API

| Name | What it does |
| --- | --- |
| `Write(path string, data []byte) error` | Creates or replaces a project file atomically, making its directory if need be |

```go
if err := projectfile.Write(".turbo-go/tools.toml", []byte(template)); err != nil {
    return err
}
```

## Tests

```sh
make test
go test ./projectfile/
```

One of them checks that no temporary file is left behind — a failed write littering somebody's project directory with `.projectfile-*.toml` is the kind of thing that has to be explained rather than noticed.