cfgedit

package module
v0.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-cfgedit

Go


Byte-preserving single-setting editors for BepInEx .cfg, MelonLoader MelonPreferences.cfg, INI, MSBuild project files and JSON config files. Package cfgedit, module github.com/Rethunk-Tech/go-cfgedit.

Each editor takes the file's text, changes one setting and returns the new text. Every other byte stays as it was, including a byte order mark and each line's own line ending, so a hand-edited config keeps its comments, order and spacing. Nothing here reads or writes a file.

Quick start

go get github.com/Rethunk-Tech/go-cfgedit

A worked example and the contracts: HUMANS.md.

Features

  • BepInEx .cfg: typed entries read from the # comment lines, values validated the way BepInEx parses them.
  • MelonLoader MelonPreferences.cfg: types read off the TOML values, edits written as TOML and re-checked as a whole.
  • INI: ini.Set edits one key = value in a section, adding the key or section when missing.
  • MSBuild: msbuild reads and edits properties, items and attributes of a .csproj, .props or .targets in place.
  • JSON: jsonfile parses a mod's JSON config leniently, then changes one value at a dotted path, keeping key order and every other value.
  • Refusals are typed: ValueError carries a stable Problem code, the bound broken and the acceptable values.

Documentation

Doc Audience
HUMANS.md Running and using the package
AGENTS.md Internals, invariants, and why
CONTRIBUTING.md Process — commits, tests, docs
SECURITY.md Vulnerability disclosure
CHANGELOG.md Release notes

License

MIT — see LICENSE. © 2026 Rethunk.Tech, LLC.

Documentation

Overview

Package cfgedit edits one setting of a BepInEx .cfg or a MelonLoader MelonPreferences.cfg and returns the new text, leaving every other byte as it was. The ini and msbuild subpackages do the same for INI and MSBuild files.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoSetting = errors.New("no setting")

ErrNoSetting is wrapped by Set when the section has no such key. The wrapped text is "no setting <key> in [<section>]".

Functions

This section is empty.

Types

type BepInEx

type BepInEx struct {
	// contains filtered or unexported fields
}

BepInEx is a parsed BepInEx .cfg. It keeps the original lines, so a Set changes only the edited line.

func ParseBepInEx

func ParseBepInEx(text string) *BepInEx

ParseBepInEx reads a BepInEx .cfg. It never fails: a line it does not understand is skipped.

func (*BepInEx) Entries

func (d *BepInEx) Entries() []Entry

Entries lists the settings in file order.

func (*BepInEx) GUID

func (d *BepInEx) GUID() string

GUID is the plugin GUID from the "Plugin GUID: <GUID>" header, or "".

func (*BepInEx) Plugin

func (d *BepInEx) Plugin() string

Plugin is the plugin name from the "Settings file was created by plugin <Name> v<Version>" header, or "".

func (*BepInEx) Set

func (d *BepInEx) Set(section, key, value string) (string, error)

Set returns the file text with one value replaced and every other byte as it was, or an *ValueError when BepInEx would not read the value back as typed, or an error wrapping ErrNoSetting.

type Entry

type Entry struct {
	Section, Key string
	Type         Type
	// Value is the BepInEx value as the file holds it, escapes included; a Melon string is unquoted and unescaped.
	Value       string
	Default     string // BepInEx only.
	HasDefault  bool
	Description string
	Min, Max    *float64 // An acceptable range the file states.
	Values      []string // The acceptable values of an enum.
	Flags       bool     // An enum whose values combine with commas.
}

Entry is one setting, in file order.

type Melon

type Melon struct {
	// contains filtered or unexported fields
}

Melon is a parsed MelonPreferences.cfg. It keeps the original lines, so a Set changes only the edited value.

func ParseMelon

func ParseMelon(text string) *Melon

ParseMelon reads a MelonPreferences.cfg. It never fails: a line it does not understand is skipped.

func (*Melon) Entries

func (d *Melon) Entries() []Entry

Entries lists the settings in file order.

func (*Melon) Set

func (d *Melon) Set(section, key, value string) (string, error)

Set returns the file text with one value replaced, written as TOML, and every other byte as it was. It refuses a value of the wrong type and one that would leave a file that parsed unparsable (BreaksTOML); a missing setting wraps ErrNoSetting.

type Problem

type Problem string

Problem is why a value was refused. Its string is stable and safe to put on a wire.

const (
	MultiLine  Problem = "line"         // A value holding a line break.
	NotBool    Problem = "bool"         // Neither true nor false.
	NotInteger Problem = "whole_number" // Also a TOML integer with a leading zero.
	NotNumber  Problem = "number"       // Also NaN and infinities.
	NotAllowed Problem = "enum"         // Not one of Values.
	OutOfRange Problem = "range"        // The file's range, the .NET type's, or Decimal's exact bound.
	NotColor   Problem = "color"        // Not 3, 4, 6 or 8 hex digits.
	NotList    Problem = "list"         // A TOML array without its brackets.
	Empty      Problem = "empty"        // A TypeRaw value left blank.
	BreaksTOML Problem = "toml"         // Melon: the edit would leave a file that parsed unparsable.
)

The reasons Set refuses a value.

type Type

type Type string

Type is how an entry's value is written and validated.

const (
	TypeBool   Type = "bool"
	TypeInt    Type = "int"
	TypeFloat  Type = "float"
	TypeEnum   Type = "enum"
	TypeString Type = "string"
	TypeColor  Type = "color"
	TypeList   Type = "list"
	// TypeRaw is a Melon value the parser cannot type (an inline table, a date, a hex integer): its TOML text is
	// shown and written as typed, never quoted.
	TypeRaw Type = "raw"
)

The entry types. TypeRaw is a Melon value the parser cannot type.

type ValueError

type ValueError struct {
	Key     string
	Problem Problem
	// Min and Max are the bound broken for OutOfRange, from the file or the .NET type; either may be nil.
	Min, Max *float64
	Values   []string // The acceptable values for NotAllowed.
}

ValueError is a value Set refuses. Error is a plain English sentence naming the key, for logs and CLIs; callers word their own sentence for people from Problem and the bounds.

func (*ValueError) Error

func (e *ValueError) Error() string

Directories

Path Synopsis
Package ini sets `key = value` lines in an ini-style file's text, leaving every other byte as it was.
Package ini sets `key = value` lines in an ini-style file's text, leaving every other byte as it was.
internal
textdoc
Package textdoc splits text into lines that remember their own ending, so an edit rewrites one line and every other byte comes back as it was.
Package textdoc splits text into lines that remember their own ending, so an edit rewrites one line and every other byte comes back as it was.
Package jsonfile edits one value in a mod's JSON config file while keeping its key order and every other value as written.
Package jsonfile edits one value in a mod's JSON config file while keeping its key order and every other value as written.
Package msbuild edits MSBuild files (.csproj, .props, .targets) in place: it parses them to find each element's exact bytes, then changes only those, so the developer's formatting, comments and attribute order stay as they wrote them.
Package msbuild edits MSBuild files (.csproj, .props, .targets) in place: it parses them to find each element's exact bytes, then changes only those, so the developer's formatting, comments and attribute order stay as they wrote them.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL