system

package
v2.2.7 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2023 License: MIT Imports: 8 Imported by: 2

Documentation

Overview

Package system contain some functions about os, runtime, shell command.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompareOsEnv

func CompareOsEnv(key, comparedEnv string) bool

CompareOsEnv gets env named by the key and compare it with comparedEnv. Play: https://go.dev/play/p/BciHrKYOHbp

Example
err := SetOsEnv("foo", "abc")
if err != nil {
	return
}

result := CompareOsEnv("foo", "abc")

fmt.Println(result)
Output:

true

func ExecCommand

func ExecCommand(command string, opts ...Option) (stdout, stderr string, err error)

ExecCommand execute command, return the stdout and stderr string of command, and error if error occur param `command` is a complete command string, like, ls -a (linux), dir(windows), ping 127.0.0.1 in linux, use /bin/bash -c to execute command in windows, use powershell.exe to execute command Play: https://go.dev/play/p/n-2fLyZef-4

Example
_, stderr, err := ExecCommand("ls")
// fmt.Println(stdout)
fmt.Println(stderr)
fmt.Println(err)
Output:


<nil>

func GetOsBits added in v2.1.3

func GetOsBits() int

GetOsBits return current os bits (32 or 64). Play: https://go.dev/play/p/ml-_XH3gJbW

Example
osBits := GetOsBits()

fmt.Println(osBits)
Output:

64

func GetOsEnv

func GetOsEnv(key string) string

GetOsEnv gets the value of the environment variable named by the key. Play: https://go.dev/play/p/D88OYVCyjO-

Example
ok := SetOsEnv("foo", "abc")
result := GetOsEnv("foo")

fmt.Println(ok)
fmt.Println(result)
Output:

<nil>
abc

func IsLinux

func IsLinux() bool

IsLinux check if current os is linux. Play: https://go.dev/play/p/zIflQgZNuxD

func IsMac

func IsMac() bool

IsMac check if current os is macos. Play: https://go.dev/play/p/Mg4Hjtyq7Zc

func IsWindows

func IsWindows() bool

IsWindows check if current os is windows. Play: https://go.dev/play/p/XzJULbzmf9m

func RemoveOsEnv

func RemoveOsEnv(key string) error

RemoveOsEnv remove a single environment variable. Play: https://go.dev/play/p/fqyq4b3xUFQ

Example
err1 := SetOsEnv("foo", "abc")
result1 := GetOsEnv("foo")

err2 := RemoveOsEnv("foo")
result2 := GetOsEnv("foo")

fmt.Println(err1)
fmt.Println(err2)
fmt.Println(result1)
fmt.Println(result2)
Output:

<nil>
<nil>
abc

func SetOsEnv

func SetOsEnv(key, value string) error

SetOsEnv sets the value of the environment variable named by the key. Play: https://go.dev/play/p/D88OYVCyjO-

Example
err := SetOsEnv("foo", "abc")
result := GetOsEnv("foo")

fmt.Println(err)
fmt.Println(result)
Output:

<nil>
abc

Types

type Option added in v2.1.19

type Option func(*exec.Cmd)

func WithForeground added in v2.1.19

func WithForeground() Option

func WithWinHide added in v2.1.20

func WithWinHide() Option

Jump to

Keyboard shortcuts

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