From f3749c52b910fbf74737878387a5ddbe651d5d2a Mon Sep 17 00:00:00 2001 From: dudaodong Date: Mon, 17 Jan 2022 11:54:03 +0800 Subject: [PATCH] feat: add system package --- system/os.go | 69 +++++++++++++++++++++++++++++++++++++++++++++++ system/os_test.go | 37 +++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 system/os.go create mode 100644 system/os_test.go diff --git a/system/os.go b/system/os.go new file mode 100644 index 0000000..7158bad --- /dev/null +++ b/system/os.go @@ -0,0 +1,69 @@ +// Copyright 2021 dudaodong@gmail.com. All rights reserved. +// Use of this source code is governed by MIT license + +// Package system contain some functions about os, runtime, shell command. +package system + +import ( + "bytes" + "os" + "os/exec" + "runtime" +) + +// IsWindows check if current os is windows +func IsWindows() bool { + return runtime.GOOS == "windows" +} + +// IsLinux check if current os system is linux +func IsLinux() bool { + return runtime.GOOS == "linux" +} + +// IsMac check if current os is macos +func IsMac() bool { + return runtime.GOOS == "darwin" +} + +// GetOsEnv gets the value of the environment variable named by the key. +func GetOsEnv(key string) string { + return os.Getenv(key) +} + +// SetOsEnv sets the value of the environment variable named by the key. +func SetOsEnv(key, value string) error { + return os.Setenv(key, value) +} + +// RemoveOsEnv remove a single environment variable. +func RemoveOsEnv(key string) error { + return os.Unsetenv(key) +} + +// CompareOsEnv gets env named by the key and compare it with comparedEnv +func CompareOsEnv(key, comparedEnv string) bool { + env := GetOsEnv(key) + if env == "" { + return false + } + return env == comparedEnv +} + +// ExecCommand use shell /bin/bash -c to execute command +func ExecCommand(command string) (err error, stdout, stderr string) { + var out bytes.Buffer + var errout bytes.Buffer + + cmd := exec.Command("/bin/bash", "-c", command) + cmd.Stdout = &out + cmd.Stderr = &errout + err = cmd.Run() + + if err != nil { + stderr = string(errout.Bytes()) + } + stdout = string(out.Bytes()) + + return +} diff --git a/system/os_test.go b/system/os_test.go new file mode 100644 index 0000000..4126d70 --- /dev/null +++ b/system/os_test.go @@ -0,0 +1,37 @@ +package system + +import ( + "testing" + + "github.com/duke-git/lancet/internal" +) + +func TestOsEnvOperation(t *testing.T) { + assert := internal.NewAssert(t, "TestOsEnvOperation") + + envNotExist := GetOsEnv("foo") + assert.Equal("", envNotExist) + + SetOsEnv("foo", "foo_value") + envExist := GetOsEnv("foo") + assert.Equal("foo_value", envExist) + + assert.Equal(true, CompareOsEnv("foo", "foo_value")) + assert.Equal(false, CompareOsEnv("foo", "abc")) +} + +func TestExecCommand(t *testing.T) { + assert := internal.NewAssert(t, "TestExecCommand") + + err, out, errout := ExecCommand("ls") + assert.IsNil(err) + + err, out, errout = ExecCommand("abc") + t.Log("std out: ", out) + t.Log("std err: ", errout) + if err != nil { + t.Logf("error: %v\n", err) + } + + assert.IsNotNil(err) +}