1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 21:02:27 +08:00
Files
lancet/docs/system.md
2023-04-19 14:26:15 +08:00

3.8 KiB
Raw Blame History

System

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

Source:

https://github.com/duke-git/lancet/blob/v1/system/os.go

Usage:

import (
    "github.com/duke-git/lancet/system"
)

Index

Documentation

IsWindows

Check if current os is windows.

Signature:

func IsWindows() bool

Example:

import (
    "fmt"
    "github.com/duke-git/lancet/system"
)

func main() {
    isOsWindows := system.IsWindows()
    fmt.Println(isOsWindows)
}

IsLinux

Check if current os is linux.

Signature:

func IsLinux() bool

Example:

import (
    "fmt"
    "github.com/duke-git/lancet/system"
)

func main() {
    isOsLinux := system.IsLinux()
    fmt.Println(isOsLinux)
}

IsMac

Check if current os is macos.

Signature:

func IsMac() bool

Example:

import (
    "fmt"
    "github.com/duke-git/lancet/system"
)

func main() {
    isOsMac := system.IsMac
    fmt.Println(isOsMac)
}

GetOsEnv

Gets the value of the environment variable named by the key.

Signature:

func GetOsEnv(key string) string

Example:

import (
    "fmt"
    "github.com/duke-git/lancet/system"
)

func main() {
    fooEnv := system.GetOsEnv("foo")
    fmt.Println(fooEnv)
}

SetOsEnv

Sets the value of the environment variable named by the key.

Signature:

func SetOsEnv(key, value string) error

Example:

import (
    "fmt"
    "github.com/duke-git/lancet/system"
)

func main() {
    err := system.SetOsEnv("foo", "foo_value")
    fmt.Println(err)
}

RemoveOsEnv

Remove a single environment variable.

Signature:

func RemoveOsEnv(key string) error

Example:

import (
    "fmt"
    "github.com/duke-git/lancet/system"
)

func main() {
    err := system.RemoveOsEnv("foo")
    if err != nil {
        fmt.Println(err)
    }
}

CompareOsEnv

Get env named by the key and compare it with comparedEnv.

Signature:

func CompareOsEnv(key, comparedEnv string) bool

Example:

import (
    "fmt"
    "github.com/duke-git/lancet/system"
)

func main() {
    system.SetOsEnv("foo", "foo_value")
    res := system.CompareOsEnv("foo", "foo_value")
    fmt.Println(res) //true
}

ExecCommand

use shell /bin/bash -c(linux) or cmd (windows) to execute command.

Signature:

type (
	Option func(*exec.Cmd)
)
func ExecCommand(command string, opts ...Option) (stdout, stderr string, err error)

Example:

import (
    "fmt"
    "github.com/duke-git/lancet/system"
)

func main() {
    out, errout, err := system.ExecCommand("ls", system.WithForeground())
    fmt.Println(out)
    fmt.Println(errout)
    fmt.Println(err)
}

GetOsBits

获取当前操作系统位数返回32或64

函数签名:

func GetOsBits() int

例子:

import (
    "fmt"
    "github.com/duke-git/lancet/system"
)

func main() {
    osBit := system.GetOsBits()
    fmt.Println(osBit)
}