Ai
4 Star 6 Fork 4

王军/golib

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
os.go 2.16 KB
一键复制 编辑 原始数据 按行查看 历史
王军 提交于 2025-05-05 17:21 +08:00 . 修复错误
package easyjs
import (
"os"
"os/exec"
"time"
"gitee.com/haodreams/golib/logs"
"gitee.com/haodreams/libs/config"
"gitee.com/haodreams/libs/easy"
"gitee.com/haodreams/libs/routine"
"github.com/atotto/clipboard"
ping "github.com/prometheus-community/pro-bing"
)
type OS struct {
}
// 是否是工作日
func (m *OS) IsWorkday() bool {
lines, err := easy.ReadLines("data/holiday.csv")
if err == nil {
if len(lines) >= 0 {
now := easy.FormatNow(easy.YMD)
for _, line := range lines {
if line == now {
return false
}
}
}
}
// 获取当前工作日
day := time.Now().Weekday()
if day == 0 || day == 6 {
return false // 示例代码,返回 false 表明是周六或周日,非工作日
}
return true // 示例代码,返回 true 表明是工作日
}
// 复制到剪切板
func (m *OS) Copy(text string) {
clipboard.WriteAll(text)
}
func (m *OS) Ping(host string) int {
pinger, err := ping.NewPinger(host)
if err != nil {
return 0
}
pinger.SetPrivileged(true)
pinger.Count = 1
pinger.Timeout = time.Second
err = pinger.Run()
if err != nil {
return 0
}
stats := pinger.Statistics()
if stats.PacketsRecv == 0 {
return 0
}
return 1
}
/**
* @description: 获取配置文件
* @param {string} key
* @return {*}
*/
func (m *OS) GetValue(key string) string {
return config.String(key)
}
// 系统退出函数
func (m *OS) Exit() {
routine.Stop()
}
func (m *OS) SaveConfig() any {
return config.Save()
}
// 执行命令
func (m *OS) Exec(cmd ...string) {
if len(cmd) == 0 {
return
}
command := exec.Command(cmd[0], cmd[1:]...)
command.Stdout = os.Stdout
command.Stderr = os.Stderr
err := command.Run()
if err != nil {
logs.Warn(err)
}
}
func (m *OS) Sleep(ms int) {
if ms == 0 {
time.Sleep(time.Millisecond * 1000)
return
}
time.Sleep(time.Millisecond * time.Duration(ms))
}
// 获取一个进程的PID
func (m *OS) Kill(pid int) any {
process, err := os.FindProcess(int(pid))
if err != nil {
return err.Error()
}
err = process.Kill()
if err != nil {
return err.Error()
}
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/haodreams/golib.git
git@gitee.com:haodreams/golib.git
haodreams
golib
golib
v1.0.0

搜索帮助