Latest Threat ResearchGlassWorm Loader Hits Open VSX via Developer Account Compromise.Details
Socket
Book a DemoInstallSign in
Socket

woland.xyz/livefile

Package Overview
Dependencies
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

woland.xyz/livefile

Go Modules
Version
v0.1.0
Version published
Created
Source

livefile

Overview

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.

Features

  • Live Reload: Automatically reloads the struct instance when the underlying JSON file changes.
  • Transaction-like API: Provides a safe and consistent way to update the struct instance.

Installation

To install livefile, use the following command:

go get grono.dev/livefile

Usage

  • Define your state struct.
  • Create an instance of livefile.
  • Use the View method to read the current state.
  • Use the Update method to modify the state.
  • Use the Peek method to get a copy of the current state (a simpler version of the View method).

Example

import (
    "context"
    "fmt"

    "grono.dev/livefile"
)

type MyState struct {
    Value int
    Name  string
}

func main() {
    ctx := context.Background()
    lf := livefile.New[MyState]("state.json")

    // View the initial state
    lf.View(ctx, func(s *MyState) {
        fmt.Println("Updated State:", *s)
    })

    // Update the state
    err := lf.Update(ctx, func(s *MyState) error {
        s.Value = 42
        s.Name = "Updated"
        return nil
    })
    if err != nil {
        panic(err)
    }

    // View the modified state
    lf.View(ctx, func(s *MyState) {
        fmt.Println("Updated State:", *s)
    })

    // Perform external modification
    if err := os.WriteFile("state.json", []byte(`{"Name":"foo","Value":100}`), 0o777); err != nil {
        panic(err)
    }

    // Get a copy of the state
    s := lf.Peek(ctx)
    fmt.Println("Final state:", s)
}

FAQs

Package last updated on 11 Jun 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts