jsonexec

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: May 19, 2023 License: MIT Imports: 6 Imported by: 7

README

jsonexec

Run command and convert output (stdout) JSON into provided object

How to use

package main

import (
	"fmt"
	"github.com/sirkon/jsonexec"
)

func main() {
	var dest map[string]interface{}
	if err := jsonexec.Run(&dest, "ls"); err != nil {
		jsonexec.HandleError(err, func(lsOutput string) {
			// will show something like
			// unmarshal command output: invalid character 'L' looking for beginning of value
			// Command output:
			// LICENCE
			// README.md
			// ...
			fmt.Println(err, "\nCommand output:\n"+lsOutput)
		})
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HandleError

func HandleError(err error, details func(string))

HandleError jsonexec internal unified handling

func Run

func Run(dest interface{}, name string, arg ...string) error

Run run `name arg...` command and treats its stdout as a JSON, unmarshaling it into provided dest object. Returns ErrorExecution if execution failed and ErrorUnmarshal if JSON unmarshal is failed.

func Rung added in v0.0.2

func Rung[T any](name string, args ...string) (res T, err error)

Rung generic version of Run. Can be a bit more convenient.

Types

type ErrorExecution

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

ErrorExecution ошибка исполнения команды

func (ErrorExecution) Details

func (e ErrorExecution) Details() string

Details error details

func (ErrorExecution) Error

func (e ErrorExecution) Error() string

type ErrorUnmarshal

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

ErrorUnmarshal ошибка декодирования выхлопа команды трактуемого как JSON

func (ErrorUnmarshal) Details

func (e ErrorUnmarshal) Details() string

Details error details

func (ErrorUnmarshal) Error

func (e ErrorUnmarshal) Error() string

Jump to

Keyboard shortcuts

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