inifile

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2020 License: MIT Imports: 7 Imported by: 1

README

inifile

Package inifile implements parsing a simple INI file

GoDoc

Installation

To install inifile package:

go get -u github.com/codeation/inifile

Examples

sample.ini file:

port=8080
host=127.0.0.1

[node]
name=server.local
ip=10.0.0.11

sample.go file:

package main

import (
	"fmt"

	"github.com/codeation/inifile"
)

func main() {
	ini, err := inifile.Read("sample.ini")
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println("port=", ini.Get("", "port"))
	fmt.Println("node.name=", ini.Get("node", "name"))
}

output:

$ go run sample.go 
port= 8080
node.name= server.local

Environment variable

You can specify a full path to INI file via the environment variable. For example:

$ SAMPLE_INI=/etc/server_configuration.ini go run sample.go

The environment variable is checked only if the parameter of inifile.Read does not contain the directory name. The name of the environment variable is the file name in uppercase, the dot is replaced by an underscore.

Command output encapsulation

Please note that executing external commands can lead to application vulnerabilities. Command output encapsulation is disabled by default.

You can use command output as INI file variable value. For example, as string of INI file:

one_time_password=$(pwgen -s 16)

To enable command output encapsulation, call inifile.Command in golang file:

    ini, _ := inifile.Read("sample.ini")
    ini.Command(true)
    fmt.Println("One time password is", ini.Get("", "one_time_password"))

See the documentation for details.

Documentation

Overview

Package inifile implements parsing a simple ini-file.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IniFile

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

IniFile stores the parsed values from a ini-file.

func Read

func Read(filename string) (*IniFile, error)

Read parses the specified ini-file. You can specify a full path to INI file via the environment variable. The environment variable is checked only if the filename does not contain the directory name. The name of the environment variable is the file name in uppercase, the dot is replaced by an underscore.

func (*IniFile) Command

func (ini *IniFile) Command(enabled bool)

Command enables or disables external commands encapsulation. Note that executing external commands can lead to application vulnerabilities.

func (*IniFile) Get

func (ini *IniFile) Get(section string, name string) string

Get returns the value of variable from the specified section (use "" for an unnamed section). Get makes substitution if commands encapsulation is enabled. "$(<filename)" format for the file contents. "$(command line)" format to encapsulate command output.

func (*IniFile) Sections

func (ini *IniFile) Sections() []string

Sections returns a list of partitions.

Jump to

Keyboard shortcuts

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