import{_ as l,o as p,c as o,k as s,a,X as n}from"./chunks/framework.6e839c56.js";const C=JSON.parse('{"title":"System","description":"","frontmatter":{},"headers":[],"relativePath":"en/api/packages/system.md","filePath":"en/api/packages/system.md"}'),e={name:"en/api/packages/system.md"},t=s("h1",{id:"System",tabindex:"-1"},[a("System "),s("a",{class:"header-anchor",href:"#System","aria-label":'Permalink to "System"'},"​")],-1),c=s("p",null,"Package system contains some functions about os, runtime, shell command.",-1),r=s("div",{STYLE:"page-break-after: always;"},null,-1),y=s("h2",{id:"Source-",tabindex:"-1"},[a("Source: "),s("a",{class:"header-anchor",href:"#Source-","aria-label":'Permalink to "Source:"'},"​")],-1),i=s("ul",null,[s("li",null,[s("a",{href:"https://github.com/duke-git/lancet/blob/main/system/os.go",target:"_blank",rel:"noreferrer"},"https://github.com/duke-git/lancet/blob/main/system/os.go")])],-1),F=s("div",{STYLE:"page-break-after: always;"},null,-1),u=n(`

Usage:

go
import (
    "github.com/duke-git/lancet/v2/system"
)
import (
    "github.com/duke-git/lancet/v2/system"
)
`,2),E=s("div",{STYLE:"page-break-after: always;"},null,-1),d=n('

Index

',2),m=s("div",{STYLE:"page-break-after: always;"},null,-1),A=n(`

Documentation

IsWindows

Check if current os is windows.

Signature:

go
func IsWindows() bool
func IsWindows() bool

Example:Run

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

func main() {
    isOsWindows := system.IsWindows()
    fmt.Println(isOsWindows)
}
import (
    "fmt"
    "github.com/duke-git/lancet/v2/system"
)

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

IsLinux

Check if current os is linux.

Signature:

go
func IsLinux() bool
func IsLinux() bool

Example:Run

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

func main() {
    isOsLinux := system.IsLinux()
    fmt.Println(isOsLinux)
}
import (
    "fmt"
    "github.com/duke-git/lancet/v2/system"
)

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

IsMac

Check if current os is macos.

Signature:

go
func IsMac() bool
func IsMac() bool

Example:Run

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

func main() {
    isOsMac := system.IsMac()
    fmt.Println(isOsMac)
}
import (
    "fmt"
    "github.com/duke-git/lancet/v2/system"
)

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

GetOsEnv

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

Signature:

go
func GetOsEnv(key string) string
func GetOsEnv(key string) string

Example:Run

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

func main() {
    err := system.SetOsEnv("foo", "abc")
    result := system.GetOsEnv("foo")

    fmt.Println(err)
    fmt.Println(result)
    // Output:
    // <nil>
    // abc
}
import (
    "fmt"
    "github.com/duke-git/lancet/v2/system"
)

func main() {
    err := system.SetOsEnv("foo", "abc")
    result := system.GetOsEnv("foo")

    fmt.Println(err)
    fmt.Println(result)
    // Output:
    // <nil>
    // abc
}

SetOsEnv

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

Signature:

go
func SetOsEnv(key, value string) error
func SetOsEnv(key, value string) error

Example:Run

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

func main() {
    err := system.SetOsEnv("foo", "abc")
    result := system.GetOsEnv("foo")

    fmt.Println(err)
    fmt.Println(result)
    // Output:
    // <nil>
    // abc
}
import (
    "fmt"
    "github.com/duke-git/lancet/v2/system"
)

func main() {
    err := system.SetOsEnv("foo", "abc")
    result := system.GetOsEnv("foo")

    fmt.Println(err)
    fmt.Println(result)
    // Output:
    // <nil>
    // abc
}

RemoveOsEnv

Remove a single environment variable.

Signature:

go
func RemoveOsEnv(key string) error
func RemoveOsEnv(key string) error

Example:Run

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

func main() {
    err1 := system.SetOsEnv("foo", "abc")
    result1 := GetOsEnv("foo")

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

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

    // Output:
    // <nil>
    // <nil>
    // abc
    //
}
import (
    "fmt"
    "github.com/duke-git/lancet/v2/system"
)

func main() {
    err1 := system.SetOsEnv("foo", "abc")
    result1 := GetOsEnv("foo")

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

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

    // Output:
    // <nil>
    // <nil>
    // abc
    //
}

CompareOsEnv

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

Signature:

go
func CompareOsEnv(key, comparedEnv string) bool
func CompareOsEnv(key, comparedEnv string) bool

Example:Run

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

func main() {
    err := system.SetOsEnv("foo", "abc")
    if err != nil {
        return
    }

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

    fmt.Println(result)

    // Output:
    // true
}
import (
    "fmt"
    "github.com/duke-git/lancet/v2/system"
)

func main() {
    err := system.SetOsEnv("foo", "abc")
    if err != nil {
        return
    }

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

    fmt.Println(result)

    // Output:
    // true
}

ExecCommand

Execute shell 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.

Signature:

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

Example:Run

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

func main() {
    // linux or mac
    stdout, stderr, err := system.ExecCommand("ls")
    fmt.Println("std out: ", stdout)
    fmt.Println("std err: ", stderr)
    assert.Equal("", stderr)

    // windows
    stdout, stderr, err = system.ExecCommand("dir")
    fmt.Println("std out: ", stdout)
    fmt.Println("std err: ", stderr)

    // error command
    stdout, stderr, err = system.ExecCommand("abc")
    fmt.Println("std out: ", stdout)
    fmt.Println("std err: ", stderr)
    if err != nil {
        fmt.Println(err.Error())
    }
}
import (
    "fmt"
    "github.com/duke-git/lancet/v2/system"
)

func main() {
    // linux or mac
    stdout, stderr, err := system.ExecCommand("ls")
    fmt.Println("std out: ", stdout)
    fmt.Println("std err: ", stderr)
    assert.Equal("", stderr)

    // windows
    stdout, stderr, err = system.ExecCommand("dir")
    fmt.Println("std out: ", stdout)
    fmt.Println("std err: ", stderr)

    // error command
    stdout, stderr, err = system.ExecCommand("abc")
    fmt.Println("std out: ", stdout)
    fmt.Println("std err: ", stderr)
    if err != nil {
        fmt.Println(err.Error())
    }
}

GetOsBits

Get current os bits, 32bit or 64bit. return 32 or 64

Signature:

go
func GetOsBits() int
func GetOsBits() int

Example:Run

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

func main() {
    osBit := system.GetOsBits()
    fmt.Println(osBit) // 32 or 64
}
import (
    "fmt"
    "github.com/duke-git/lancet/v2/system"
)

func main() {
    osBit := system.GetOsBits()
    fmt.Println(osBit) // 32 or 64
}
`,55),g=[t,c,r,y,i,F,u,E,d,m,A];function h(v,f,q,B,b,D){return p(),o("div",null,g)}const O=l(e,[["render",h]]);export{C as __pageData,O as default};