mozpref

package module
v0.0.0-...-31b2a04 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2018 License: BSD-3-Clause Imports: 9 Imported by: 0

README

go-mozpref

GoDoc

go-mozpref is a Go library for reading and writing preference files as used by Mozilla Firefox and Thunderbird. The parser consists of a Ragel-generated state machine.

Usage

import mozpref "github.com/hansmi/go-mozpref"

Preferences can be read from any io.Reader:

file, err := os.Open("prefs.js")
if err != nil {
	// Handle error
}

prefs, err := mozpref.ReadFrom(file)
if err != nil {
	// Handle error
}

for name, p := range prefs {
	fmt.Printf("%s = %s\n", name, p.Value)
}

Writing works likewise with any io.Writer:

prefs := mozpref.PrefMap{
	"example": mozpref.Pref{
		Value: true,
		Flags: mozpref.Locked,
	},
}

_, err := prefs.WriteTo(os.Stdout)
if err != nil {
	// Handle error
}

Preferences can be marked as locked or sticky:

prefs["example"].Flags |= mozpref.Sticky
prefs["other"] = &mozpref.Pref{
	Value: "Hello World",
	Flags: mozpref.Locked | mozpref.Sticky,
}

Entries suitable for user.js files can be written using the mozpref.UserPref flag.

Versioning

go-mozpref follows semver.

License

This library is distributed under the BSD-style license found in the LICENSE file.

Documentation

Overview

Package mozpref is a Go library for reading and writing preference files as used by Mozilla Firefox and Thunderbird.

Index

Constants

View Source
const (
	// Sticky preferences are retained in the configuration even when they
	// match the application default.
	Sticky = 1 << iota

	// Locked preferences can't be changed in the application user
	// interface.
	Locked

	// UserPref are used in "user.js"
	UserPref
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Pref

type Pref struct {
	// Value must be of type string, int or boolean
	Value interface{}

	// Bitfield with flags
	Flags uint
}

Pref holds the value of a preference and associated flags.

type PrefMap

type PrefMap map[string]*Pref

PrefMap is a collection of preferences.

func FromMap

func FromMap(prefs map[string]interface{}, flags uint) PrefMap

FromMap copies all entries in a map[string]interface{} into a PrefMap. All entries are given the same flags.

func Parse

func Parse(b []byte) (PrefMap, error)

Parse reads preferences from a byte slice.

func ReadFrom

func ReadFrom(r io.Reader) (PrefMap, error)

ReadFrom reads preferences from given reader and returns a map with the parsed values.

func (PrefMap) ToMap

func (p PrefMap) ToMap() map[string]interface{}

ToMap copies all entries in a PrefMap to a map[string]interface{}. Individual entry flags are ignored.

func (PrefMap) WriteTo

func (p PrefMap) WriteTo(w io.Writer) (int64, error)

WriteTo writes all preferences in map to an io.Writer using the standard format, sorted by key.

Jump to

Keyboard shortcuts

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