osexec

package module
v0.0.17 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2024 License: MIT Imports: 16 Imported by: 3

README

GitHub Workflow Status (branch) GoDoc Coverage Status Supported Go Versions GitHub Release Go Report Card

osexec

Simple utilities to use Golang's os/exec package.

CHINESE README

中文说明

Features

  • Custom Execution Configurations: Execute commands with customizable environment variables, working paths, and shell options.

Installation

go get github.com/yyle88/osexec  

CommandConfig Structure and Methods

CommandConfig structure provides a flexible way to configure and execute commands. You can set custom environment variables, directories, shell types, and debug options using a chainable interface.

NewCommandConfig() *CommandConfig

Creates and returns a new CommandConfig instance.

Example:
config := osexec.NewCommandConfig()
Chainable Methods
  • *WithEnvs(envs []string) CommandConfig: Sets custom environment variables.
  • *WithPath(path string) CommandConfig: Sets the working path.
  • *WithShellType(shellType string) CommandConfig: Sets the shell type (e.g., bash).
  • *WithShellFlag(shellFlag string) CommandConfig: Sets the shell flag (e.g., -c).
  • *WithShell(shellType, shellFlag string) CommandConfig: Sets shell type and flag.
  • *WithBash() CommandConfig: Configures the command to use bash -c.
  • *WithZsh() CommandConfig: Configures the command to use zsh -c.
  • *WithSh() CommandConfig: Configures the command to use sh -c.
  • *WithDebugMode(debugMode bool) CommandConfig: Enables / disables debug mode.
Example:
package main

import (
	"fmt"
	"github.com/yyle88/osexec"
)

func main() {
	// Create a new CommandConfig instance and set the working directory and debug mode
	config := osexec.NewCommandConfig().
		WithPath("/path/to/directoryName").
		WithDebugMode(true)

	output, err := config.Exec("echo", "Hello, World!")
	if err != nil {
		fmt.Println("Error:", err)
	} else {
		fmt.Println("Output:", string(output))
	}
}

License

osexec is open-source and released under the MIT License. See the LICENSE file for more information.


Support

Welcome to contribute to this project by submitting pull requests or reporting issues.

If you find this package helpful, give it a star on GitHub!

Thank you for your support!

Happy Coding with osexec! 🎉

Give me stars. Thank you!!!

Starring

starring

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Exec

func Exec(name string, args ...string) ([]byte, error)

Exec executes a command. Exec 执行一个命令且获得结果。

func ExecInEnvs

func ExecInEnvs(envs []string, name string, args ...string) ([]byte, error)

ExecInEnvs executes a command with custom environment variables. ExecInEnvs 使用自定义环境变量执行一个命令。

func ExecInPath

func ExecInPath(path string, name string, args ...string) ([]byte, error)

ExecInPath executes a command in a specified directory. ExecInPath 在指定的目录中执行一个命令。

func ExecXshRun

func ExecXshRun(shellType, shellFlag string, name string, args ...string) ([]byte, error)

ExecXshRun executes a command using a specific shell type and shell flag. ExecXshRun 使用指定的 shell 类型和 shell 标志执行一个命令。

func SetDebugMode added in v0.0.8

func SetDebugMode(enable bool)

Types

type CMC added in v0.0.2

type CMC = CommandConfig

func NewCMC added in v0.0.2

func NewCMC() *CMC

type CommandConfig added in v0.0.5

type CommandConfig struct {
	Envs      []string // Optional environment variables. // 填写可选的环境变量。
	Path      string   // Optional execution path. // 填写可选的执行路径。
	ShellType string   // Optional type of shell to use, e.g., bash, zsh. // 填写可选的 shell 类型,例如 bash,zsh。
	ShellFlag string   // Optional shell flag, e.g., "-c". // 填写可选的 Shell 参数,例如 "-c"。
	DebugMode bool     // enable debug mode. // 是否启用调试模式,即打印调试的日志。
	MatchPipe func(line string) bool
	MatchMore bool
}

CommandConfig represents the configuration for executing shell commands. CommandConfig 表示执行 shell 命令的配置。

func NewCommandConfig added in v0.0.5

func NewCommandConfig() *CommandConfig

NewCommandConfig creates and returns a new CommandConfig instance. NewCommandConfig 创建并返回一个新的 CommandConfig 实例。

func (*CommandConfig) Exec added in v0.0.5

func (c *CommandConfig) Exec(name string, args ...string) ([]byte, error)

Exec executes a shell command with the specified name and arguments, using the CommandConfig configuration. Exec 使用 CommandConfig 的配置执行带有指定名称和参数的 shell 命令。

func (*CommandConfig) ExecInPipe added in v0.0.11

func (c *CommandConfig) ExecInPipe(name string, args ...string) ([]byte, error)

func (*CommandConfig) Must added in v0.0.6

func (*CommandConfig) Omit added in v0.0.6

func (*CommandConfig) Soft added in v0.0.6

func (*CommandConfig) StreamExec added in v0.0.9

func (c *CommandConfig) StreamExec(name string, args ...string) ([]byte, error)

func (*CommandConfig) WithBash added in v0.0.5

func (c *CommandConfig) WithBash() *CommandConfig

WithBash sets the shell to bash with the "-c" flag and returns the updated instance. WithBash 设置 shell 为 bash 并附带 "-c" 参数,返回更新后的实例。

func (*CommandConfig) WithDebug added in v0.0.15

func (c *CommandConfig) WithDebug() *CommandConfig

func (*CommandConfig) WithDebugMode added in v0.0.5

func (c *CommandConfig) WithDebugMode(debugMode bool) *CommandConfig

WithDebugMode sets the debug mode for CommandConfig and returns the updated instance. WithDebugMode 设置 CommandConfig 的调试模式并返回更新后的实例。

func (*CommandConfig) WithEnvs added in v0.0.5

func (c *CommandConfig) WithEnvs(envs []string) *CommandConfig

WithEnvs sets the environment variables for CommandConfig and returns the updated instance. WithEnvs 设置 CommandConfig 的环境变量并返回更新后的实例。

func (*CommandConfig) WithMatchMore added in v0.0.14

func (c *CommandConfig) WithMatchMore(matchMore bool) *CommandConfig

func (*CommandConfig) WithMatchPipe added in v0.0.12

func (c *CommandConfig) WithMatchPipe(matchPipe func(line string) bool) *CommandConfig

func (*CommandConfig) WithPath added in v0.0.5

func (c *CommandConfig) WithPath(path string) *CommandConfig

WithPath sets the execution path for CommandConfig and returns the updated instance. WithPath 设置 CommandConfig 的执行路径并返回更新后的实例。

func (*CommandConfig) WithSh added in v0.0.5

func (c *CommandConfig) WithSh() *CommandConfig

WithSh sets the shell to sh with the "-c" flag and returns the updated instance. WithSh 设置 shell 为 sh 并附带 "-c" 参数,返回更新后的实例。

func (*CommandConfig) WithShell added in v0.0.5

func (c *CommandConfig) WithShell(shellType, shellFlag string) *CommandConfig

WithShell sets both the shell type and shell flag for CommandConfig and returns the updated instance. WithShell 同时设置 CommandConfig 的 shell 类型和 shell 参数,并返回更新后的实例。

func (*CommandConfig) WithShellFlag added in v0.0.5

func (c *CommandConfig) WithShellFlag(shellFlag string) *CommandConfig

WithShellFlag sets the shell flag for CommandConfig and returns the updated instance. WithShellFlag 设置 CommandConfig 的 shell 参数并返回更新后的实例。

func (*CommandConfig) WithShellType added in v0.0.5

func (c *CommandConfig) WithShellType(shellType string) *CommandConfig

WithShellType sets the shell type for CommandConfig and returns the updated instance. WithShellType 设置 CommandConfig 的 shell 类型并返回更新后的实例。

func (*CommandConfig) WithZsh added in v0.0.5

func (c *CommandConfig) WithZsh() *CommandConfig

WithZsh sets the shell to zsh with the "-c" flag and returns the updated instance. WithZsh 设置 shell 为 zsh 并附带 "-c" 参数,返回更新后的实例。

type CommandConfig88Must added in v0.0.6

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

func (*CommandConfig88Must) Exec added in v0.0.6

func (T *CommandConfig88Must) Exec(name string, args ...string) (res []byte)

func (*CommandConfig88Must) ExecInPipe added in v0.0.16

func (T *CommandConfig88Must) ExecInPipe(name string, args ...string) (res []byte)

func (*CommandConfig88Must) StreamExec added in v0.0.10

func (T *CommandConfig88Must) StreamExec(name string, args ...string) (res []byte)

func (*CommandConfig88Must) WithBash added in v0.0.6

func (T *CommandConfig88Must) WithBash() (res *CommandConfig)

func (*CommandConfig88Must) WithDebug added in v0.0.16

func (T *CommandConfig88Must) WithDebug() (res *CommandConfig)

func (*CommandConfig88Must) WithDebugMode added in v0.0.6

func (T *CommandConfig88Must) WithDebugMode(debugMode bool) (res *CommandConfig)

func (*CommandConfig88Must) WithEnvs added in v0.0.6

func (T *CommandConfig88Must) WithEnvs(envs []string) (res *CommandConfig)

func (*CommandConfig88Must) WithMatchMore added in v0.0.16

func (T *CommandConfig88Must) WithMatchMore(matchMore bool) (res *CommandConfig)

func (*CommandConfig88Must) WithMatchPipe added in v0.0.16

func (T *CommandConfig88Must) WithMatchPipe(matchPipe func(line string) bool) (res *CommandConfig)

func (*CommandConfig88Must) WithPath added in v0.0.6

func (T *CommandConfig88Must) WithPath(path string) (res *CommandConfig)

func (*CommandConfig88Must) WithSh added in v0.0.6

func (T *CommandConfig88Must) WithSh() (res *CommandConfig)

func (*CommandConfig88Must) WithShell added in v0.0.6

func (T *CommandConfig88Must) WithShell(shellType string, shellFlag string) (res *CommandConfig)

func (*CommandConfig88Must) WithShellFlag added in v0.0.6

func (T *CommandConfig88Must) WithShellFlag(shellFlag string) (res *CommandConfig)

func (*CommandConfig88Must) WithShellType added in v0.0.6

func (T *CommandConfig88Must) WithShellType(shellType string) (res *CommandConfig)

func (*CommandConfig88Must) WithZsh added in v0.0.6

func (T *CommandConfig88Must) WithZsh() (res *CommandConfig)

type CommandConfig88Omit added in v0.0.6

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

func (*CommandConfig88Omit) Exec added in v0.0.6

func (T *CommandConfig88Omit) Exec(name string, args ...string) (res []byte)

func (*CommandConfig88Omit) ExecInPipe added in v0.0.16

func (T *CommandConfig88Omit) ExecInPipe(name string, args ...string) (res []byte)

func (*CommandConfig88Omit) StreamExec added in v0.0.10

func (T *CommandConfig88Omit) StreamExec(name string, args ...string) (res []byte)

func (*CommandConfig88Omit) WithBash added in v0.0.6

func (T *CommandConfig88Omit) WithBash() (res *CommandConfig)

func (*CommandConfig88Omit) WithDebug added in v0.0.16

func (T *CommandConfig88Omit) WithDebug() (res *CommandConfig)

func (*CommandConfig88Omit) WithDebugMode added in v0.0.6

func (T *CommandConfig88Omit) WithDebugMode(debugMode bool) (res *CommandConfig)

func (*CommandConfig88Omit) WithEnvs added in v0.0.6

func (T *CommandConfig88Omit) WithEnvs(envs []string) (res *CommandConfig)

func (*CommandConfig88Omit) WithMatchMore added in v0.0.16

func (T *CommandConfig88Omit) WithMatchMore(matchMore bool) (res *CommandConfig)

func (*CommandConfig88Omit) WithMatchPipe added in v0.0.16

func (T *CommandConfig88Omit) WithMatchPipe(matchPipe func(line string) bool) (res *CommandConfig)

func (*CommandConfig88Omit) WithPath added in v0.0.6

func (T *CommandConfig88Omit) WithPath(path string) (res *CommandConfig)

func (*CommandConfig88Omit) WithSh added in v0.0.6

func (T *CommandConfig88Omit) WithSh() (res *CommandConfig)

func (*CommandConfig88Omit) WithShell added in v0.0.6

func (T *CommandConfig88Omit) WithShell(shellType string, shellFlag string) (res *CommandConfig)

func (*CommandConfig88Omit) WithShellFlag added in v0.0.6

func (T *CommandConfig88Omit) WithShellFlag(shellFlag string) (res *CommandConfig)

func (*CommandConfig88Omit) WithShellType added in v0.0.6

func (T *CommandConfig88Omit) WithShellType(shellType string) (res *CommandConfig)

func (*CommandConfig88Omit) WithZsh added in v0.0.6

func (T *CommandConfig88Omit) WithZsh() (res *CommandConfig)

type CommandConfig88Soft added in v0.0.6

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

func (*CommandConfig88Soft) Exec added in v0.0.6

func (T *CommandConfig88Soft) Exec(name string, args ...string) (res []byte)

func (*CommandConfig88Soft) ExecInPipe added in v0.0.16

func (T *CommandConfig88Soft) ExecInPipe(name string, args ...string) (res []byte)

func (*CommandConfig88Soft) StreamExec added in v0.0.10

func (T *CommandConfig88Soft) StreamExec(name string, args ...string) (res []byte)

func (*CommandConfig88Soft) WithBash added in v0.0.6

func (T *CommandConfig88Soft) WithBash() (res *CommandConfig)

func (*CommandConfig88Soft) WithDebug added in v0.0.16

func (T *CommandConfig88Soft) WithDebug() (res *CommandConfig)

func (*CommandConfig88Soft) WithDebugMode added in v0.0.6

func (T *CommandConfig88Soft) WithDebugMode(debugMode bool) (res *CommandConfig)

func (*CommandConfig88Soft) WithEnvs added in v0.0.6

func (T *CommandConfig88Soft) WithEnvs(envs []string) (res *CommandConfig)

func (*CommandConfig88Soft) WithMatchMore added in v0.0.16

func (T *CommandConfig88Soft) WithMatchMore(matchMore bool) (res *CommandConfig)

func (*CommandConfig88Soft) WithMatchPipe added in v0.0.16

func (T *CommandConfig88Soft) WithMatchPipe(matchPipe func(line string) bool) (res *CommandConfig)

func (*CommandConfig88Soft) WithPath added in v0.0.6

func (T *CommandConfig88Soft) WithPath(path string) (res *CommandConfig)

func (*CommandConfig88Soft) WithSh added in v0.0.6

func (T *CommandConfig88Soft) WithSh() (res *CommandConfig)

func (*CommandConfig88Soft) WithShell added in v0.0.6

func (T *CommandConfig88Soft) WithShell(shellType string, shellFlag string) (res *CommandConfig)

func (*CommandConfig88Soft) WithShellFlag added in v0.0.6

func (T *CommandConfig88Soft) WithShellFlag(shellFlag string) (res *CommandConfig)

func (*CommandConfig88Soft) WithShellType added in v0.0.6

func (T *CommandConfig88Soft) WithShellType(shellType string) (res *CommandConfig)

func (*CommandConfig88Soft) WithZsh added in v0.0.6

func (T *CommandConfig88Soft) WithZsh() (res *CommandConfig)

type OsCommand added in v0.0.9

type OsCommand = CommandConfig

func NewOsCommand added in v0.0.9

func NewOsCommand() *OsCommand

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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