Skip to content

Commit 67c3f43

Browse files
committed
Add LICENSE and README
Signed-off-by: Paweł Gronowski <[email protected]>
1 parent b5b06b6 commit 67c3f43

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

LICENSE

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
BSD 3-Clause License
2+
3+
Copyright (c) 2024, Paweł Gronowski
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are met:
7+
8+
1. Redistributions of source code must retain the above copyright notice, this
9+
list of conditions and the following disclaimer.
10+
11+
2. Redistributions in binary form must reproduce the above copyright notice,
12+
this list of conditions and the following disclaimer in the documentation
13+
and/or other materials provided with the distribution.
14+
15+
3. Neither the name of the copyright holder nor the names of its
16+
contributors may be used to endorse or promote products derived from
17+
this software without specific prior written permission.
18+
19+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# livefile
2+
3+
## Overview
4+
The `livefile` package provides an easy, read-write access to a live-reloadable struct instance backed by a JSON file with a transaction-like API.
5+
6+
## Features
7+
8+
- **Live Reload**: Automatically reloads the struct instance when the underlying JSON file changes.
9+
- **Transaction-like API**: Provides a safe and consistent way to update the struct instance.
10+
11+
## Installation
12+
13+
To install `livefile`, use the following command:
14+
15+
```sh
16+
go get woland.xyz/livefile
17+
```
18+
19+
## Usage
20+
21+
1. Define your state struct.
22+
2. Create an instance of `livefile`.
23+
3. Use the `View` method to read the current state.
24+
4. Use the `Update` method to modify the state.
25+
5. Use the `Peek` method to get a copy of the current state (a simpler version of the `View` method).
26+
27+
### Example
28+
29+
```go
30+
import (
31+
"context"
32+
"fmt"
33+
34+
"woland.xyz/livefile"
35+
)
36+
37+
type MyState struct {
38+
Value int
39+
Name string
40+
}
41+
42+
func main() {
43+
ctx := context.Background()
44+
lf := livefile.New[MyState]("state.json")
45+
46+
// View the initial state
47+
lf.View(ctx, func(s *MyState) {
48+
fmt.Println("Updated State:", *s)
49+
})
50+
51+
// Update the state
52+
err := lf.Update(ctx, func(s *MyState) error {
53+
s.Value = 42
54+
s.Name = "Updated"
55+
return nil
56+
})
57+
if err != nil {
58+
panic(err)
59+
}
60+
61+
// View the modified state
62+
lf.View(ctx, func(s *MyState) {
63+
fmt.Println("Updated State:", *s)
64+
})
65+
66+
// Perform external modification
67+
if err := os.WriteFile("state.json", []byte(`{"Name":"foo","Value":100}`), 0o777); err != nil {
68+
panic(err)
69+
}
70+
71+
// Get a copy of the state
72+
s := lf.Peek(ctx)
73+
fmt.Println("Final state:", s)
74+
}
75+
```
76+

0 commit comments

Comments
 (0)