Skip to content

yyle88/osexec

Repository files navigation

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