Simple utilities to use Golang's os/exec
package.
- Custom Execution Configurations: Execute commands with customizable environment variables, working paths, and shell options.
go get github.com/yyle88/osexec
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.
Creates and returns a new CommandConfig
instance.
config := osexec.NewCommandConfig()
- *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.
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("Reason:", err)
} else {
fmt.Println("Output:", string(output))
}
}
MIT License. See LICENSE.
Contributions are welcome! To contribute:
- Fork the repo on GitHub (using the webpage interface).
- Clone the forked project (
git clone https://github.com/yourname/repo-name.git
). - Navigate to the cloned project (
cd repo-name
) - Create a feature branch (
git checkout -b feature/xxx
). - Stage changes (
git add .
) - Commit changes (
git commit -m "Add feature xxx"
). - Push to the branch (
git push origin feature/xxx
). - Open a pull request on GitHub (on the GitHub webpage).
Please ensure tests pass and include relevant documentation updates.
Welcome to contribute to this project by submitting pull requests and reporting issues.
If you find this package valuable, give me some stars on GitHub! Thank you!!!
Thank you for your support!
Happy Coding with this package! 🎉
Give me stars. Thank you!!!