shellparse

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2021 License: MIT Imports: 6 Imported by: 1

README

Parse strings à la shell

GoDoc Go Report Card Codecov CodeQL CI Build

Whenever you need parse command and arguments from a config file, you facing quotes/escaping problem. Library hides complexity of parsing such strings.

Features

  • Ability to parse complex and multiline strings
  • Comments for multiline strings via # char
  • Useful helpers to parse string into:
    • command and arguments
    • map
    • slice
  • Ability to expand variables like${VAR} with provided k/v map
  • DotEnv-like file parser
  • Remove unnecessary quotes from commands and arguments

Installation

go get -u github.com/arkady-emelyanov/go-shellparse

Quick Start

Simple usage:

bin, args, err := shellparse.Command(`bash -c 'echo "It\'s awesome"'`)
// bin: bash
// args: []string{"-c", "echo \"It's awesome\""}

With current environment

// export SLEEP=1

bin, args, err := shellparse.CommandWithEnv(`bash -c 'sleep ${SLEEP}'`)
// bin: bash
// args: []string{"-c", "sleep 1"}

With custom variables:

vars := map[string]string{}{
    "USER": "johndoe",
}
bin, args, err := shellparse.CommandWithMap(`echo ${USER}`, vars)
// bin: echo
// args: []string{"johndoe"}

If string contains ${VAR} which is not present in provided map, error will be raised. To avoid, escape variable in the following way: \${VAR}.

Please note, *WithVars functions will never lookup current environment directly. All key=value var replacements expected to be present in a provided map.

Other helpers

parts, err := shellparse.StringToSlice(`one 'two' "three"`)
// []string{"one", "two", "three"}

parts, err := shellparse.StringToMap(`foo=bar foobar=baz`)
// []map[string]string{"foo": "bar", "foobar": "baz"}

Library supports comments defined as # char. The rest of the line will be ignored.

License

Licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Command

func Command(src string) (string, []string, error)

Command parses string into binary and arguments and removes unnecessary escape runes.

func CommandWithEnv added in v1.0.3

func CommandWithEnv(src string) (string, []string, error)

func CommandWithMap added in v1.0.3

func CommandWithMap(src string, vars map[string]string) (string, []string, error)

CommandWithMap same as Command, but additionally performs replacement of ${VAR} with provided k/v map.

func ParseVarsFile

func ParseVarsFile(file string) (map[string]string, error)

ParseVarsFile is helper for parsing dotenv compatible files. If file path is prepended with '-' char, file read error will not be raised

func ParseVarsFileWithEnv added in v1.0.3

func ParseVarsFileWithEnv(file string) (map[string]string, error)

func ParseVarsFileWithMap added in v1.0.3

func ParseVarsFileWithMap(file string, extraEnv map[string]string) (map[string]string, error)

ParseVarsFileWithMap same as ParseVarsFile, but additionally performs replacement of ${VAR} with provided k/v map. If file path is prepended with '-' char, file read error will not be raised

func StringToMap

func StringToMap(src string) (map[string]string, error)

StringToMap parses key value pairs into map. Handles multiline strings and comments.

func StringToMapWithMap added in v1.0.3

func StringToMapWithMap(src string, vars map[string]string) (map[string]string, error)

StringToMapWithMap the same as StringToMap, but additionally performs replacement of ${VAR} with provided k/v map.

func StringToSlice

func StringToSlice(src string) ([]string, error)

StringToSlice parses string into slice.

func StringToSliceWithEnv added in v1.0.3

func StringToSliceWithEnv(src string) ([]string, error)

StringToSliceWithEnv same as StringToSlice, but expand current environment variables in string.

func StringToSliceWithMap added in v1.0.3

func StringToSliceWithMap(input string, vars map[string]string) ([]string, error)

StringToSliceWithMap same as StringToSlice, but additionally performs replacement of ${VAR} with provided k/v map.

Types

This section is empty.

Jump to

Keyboard shortcuts

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