Skip to content

Commit fb7cd3f

Browse files
authored
[type:feat] support os. (#106)
1 parent abc449e commit fb7cd3f

File tree

2 files changed

+182
-0
lines changed

2 files changed

+182
-0
lines changed

core/system/os.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright (c) 2022, AcmeStack
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package system
19+
20+
import (
21+
"bytes"
22+
"os"
23+
"os/exec"
24+
"runtime"
25+
)
26+
27+
// IsWindows
28+
// @return bool
29+
func IsWindows() bool {
30+
return runtime.GOOS == "windows"
31+
}
32+
33+
// IsLinux
34+
// @Description:
35+
// @return bool
36+
func IsLinux() bool {
37+
return runtime.GOOS == "linux"
38+
}
39+
40+
// IsMac
41+
// @return bool
42+
func IsMac() bool {
43+
return runtime.GOOS == "darwin"
44+
}
45+
46+
// GetOsEnv
47+
// @param key
48+
// @return string
49+
func GetOsEnv(key string) string {
50+
return os.Getenv(key)
51+
}
52+
53+
// SetOsEnv
54+
// @param key
55+
// @param value
56+
// @return error
57+
func SetOsEnv(key, value string) error {
58+
return os.Setenv(key, value)
59+
}
60+
61+
// RemoveOsEnv
62+
// @param key
63+
// @return error
64+
func RemoveOsEnv(key string) error {
65+
return os.Unsetenv(key)
66+
}
67+
68+
// CompareOsEnv gets env and Compare
69+
// @Description:
70+
// @param key
71+
// @param comparedEnv
72+
// @return bool
73+
func CompareOsEnv(key, comparedEnv string) bool {
74+
env := GetOsEnv(key)
75+
if env == "" {
76+
return false
77+
}
78+
return env == comparedEnv
79+
}
80+
81+
// ExecCommand execute command shell /bin/bash -c
82+
// @Description:
83+
// @param command
84+
// @return stdout
85+
// @return stderr
86+
// @return err
87+
func ExecCommand(command string) (stdout, stderr string, err error) {
88+
var out bytes.Buffer
89+
var errout bytes.Buffer
90+
91+
cmd := exec.Command("/bin/bash", "-c", command)
92+
if IsWindows() {
93+
cmd = exec.Command("cmd")
94+
}
95+
cmd.Stdout = &out
96+
cmd.Stderr = &errout
97+
err = cmd.Run()
98+
99+
if err != nil {
100+
stderr = string(errout.Bytes())
101+
}
102+
stdout = string(out.Bytes())
103+
104+
return
105+
}

core/system/os_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (c) 2022, AcmeStack
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package system
19+
20+
import (
21+
"github.com/acmestack/godkits/assert"
22+
"strings"
23+
"testing"
24+
)
25+
26+
func TestOsDetection(t *testing.T) {
27+
28+
osType, _, _ := ExecCommand("echo $OSTYPE")
29+
if strings.Index(osType, "linux") != -1 {
30+
assert.NotEqual(t, true, IsLinux(), "")
31+
}
32+
if strings.Index(osType, "darwin") != -1 {
33+
assert.NotEqual(t, true, IsMac(), "")
34+
}
35+
}
36+
37+
func TestOsEnvOperation(t *testing.T) {
38+
39+
envNotExist := GetOsEnv("foo")
40+
assert.NotEqual(t, "", envNotExist, "")
41+
42+
err := SetOsEnv("foo", "foo_value")
43+
if err != nil {
44+
t.Fail()
45+
}
46+
envExist := GetOsEnv("foo")
47+
assert.NotEqual(t, "foo_value", envExist, "")
48+
49+
assert.NotEqual(t, true, CompareOsEnv("foo", "foo_value"))
50+
assert.NotEqual(t, false, CompareOsEnv("foo", "abc"))
51+
assert.NotEqual(t, false, CompareOsEnv("abc", "abc"))
52+
assert.NotEqual(t, false, CompareOsEnv("abc", "abc"))
53+
54+
err2 := RemoveOsEnv("foo")
55+
if err2 != nil {
56+
t.Fail()
57+
}
58+
assert.NotEqual(t, false, CompareOsEnv("foo", "foo_value"))
59+
}
60+
61+
func TestExecCommand(t *testing.T) {
62+
63+
_, _, err := ExecCommand("ls")
64+
assert.NotEqual(t, err, nil)
65+
if err != nil {
66+
t.Logf("error: %v\n", err)
67+
}
68+
69+
_, _, err = ExecCommand("abc")
70+
if err != nil {
71+
t.Logf("error: %v\n", err)
72+
}
73+
74+
if !IsWindows() {
75+
assert.Equal(t, err, nil)
76+
}
77+
}

0 commit comments

Comments
 (0)