general

package
v1.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 12, 2024 License: GPL-3.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Name    string = "Eniac"                 // 程序名
	Version string = "v1.4.1"                // 程序版本
	Project string = "github.com/yhyj/eniac" // 项目地址
)

Variables

View Source
var (
	FgBlackText        = color.FgBlack.Render        // 前景色 - 黑色
	FgWhiteText        = color.FgWhite.Render        // 前景色 - 白色
	FgLightWhiteText   = color.FgLightWhite.Render   // 前景色 - 亮白色
	FgGrayText         = color.FgGray.Render         // 前景色 - 灰色
	FgRedText          = color.FgRed.Render          // 前景色 - 红色
	FgLightRedText     = color.FgLightRed.Render     // 前景色 - 亮红色
	FgGreenText        = color.FgGreen.Render        // 前景色 - 绿色
	FgLightGreenText   = color.FgLightGreen.Render   // 前景色 - 亮绿色
	FgYellowText       = color.FgYellow.Render       // 前景色 - 黄色
	FgLightYellowText  = color.FgLightYellow.Render  // 前景色 - 亮黄色
	FgBlueText         = color.FgBlue.Render         // 前景色 - 蓝色
	FgLightBlueText    = color.FgLightBlue.Render    // 前景色 - 亮蓝色
	FgMagentaText      = color.FgMagenta.Render      // 前景色 - 品红
	FgLightMagentaText = color.FgLightMagenta.Render // 前景色 - 亮品红
	FgCyanText         = color.FgCyan.Render         // 前景色 - 青色
	FgLightCyanText    = color.FgLightCyan.Render    // 前景色 - 亮青色

	BgBlackText        = color.BgBlack.Render        // 背景色 - 黑色
	BgWhiteText        = color.BgWhite.Render        // 背景色 - 白色
	BgLightWhiteText   = color.BgLightWhite.Render   // 背景色 - 亮白色
	BgGrayText         = color.BgGray.Render         // 背景色 - 灰色
	BgRedText          = color.BgRed.Render          // 背景色 - 红色
	BgLightRedText     = color.BgLightRed.Render     // 背景色 - 亮红色
	BgGreenText        = color.BgGreen.Render        // 背景色 - 绿色
	BgLightGreenText   = color.BgLightGreen.Render   // 背景色 - 亮绿色
	BgYellowText       = color.BgYellow.Render       // 背景色 - 黄色
	BgLightYellowText  = color.BgLightYellow.Render  // 背景色 - 亮黄色
	BgBlueText         = color.BgBlue.Render         // 背景色 - 蓝色
	BgLightBlueText    = color.BgLightBlue.Render    // 背景色 - 亮蓝色
	BgMagentaText      = color.BgMagenta.Render      // 背景色 - 品红
	BgLightMagentaText = color.BgLightMagenta.Render // 背景色 - 亮品红
	BgCyanText         = color.BgCyan.Render         // 背景色 - 青色
	BgLightCyanText    = color.BgLightCyan.Render    // 背景色 - 亮青色

	InfoText      = color.Info.Render      // Info 文本
	NoteText      = color.Note.Render      // Note 文本
	LightText     = color.Light.Render     // Light 文本
	ErrorText     = color.Error.Render     // Error 文本
	DangerText    = color.Danger.Render    // Danger 文本
	NoticeText    = color.Notice.Render    // Notice 文本
	SuccessText   = color.Success.Render   // Success 文本
	CommentText   = color.Comment.Render   // Comment 文本
	PrimaryText   = color.Primary.Render   // Primary 文本
	WarnText      = color.Warn.Render      // Warn 文本
	QuestionText  = color.Question.Render  // Question 文本
	SecondaryText = color.Secondary.Render // Secondary 文本
)
View Source
var (
	GitCommitHash string = "Unknown" // Git 提交 Hash
	BuildTime     string = "Unknown" // 编译时间
	BuildBy       string = "Unknown" // 编译者
)
View Source
var Arch = runtime.GOARCH // 系统架构
View Source
var GenealogyName = map[string]map[string]string{
	"BIOSVendor":         {"zh": "BIOS 厂商", "en": "Vendor"},
	"BIOSVersion":        {"zh": "BIOS 版本", "en": "Version"},
	"BIOSDate":           {"zh": "BIOS 发布", "en": "Date"},
	"BoardVendor":        {"zh": "主板厂商", "en": "Vendor"},
	"BoardName":          {"zh": "主板名称", "en": "Name"},
	"BoardVersion":       {"zh": "主板版本", "en": "Version"},
	"CPUModel":           {"zh": "处理器型号", "en": "Model"},
	"CPUNumber":          {"zh": "处理器数量", "en": "Number"},
	"CPUCores":           {"zh": "处理器核心", "en": "Cores"},
	"CPUThreads":         {"zh": "处理器线程", "en": "Threads"},
	"CPUCache":           {"zh": "处理器缓存", "en": "Cache"},
	"GPUAddress":         {"zh": "显卡地址", "en": "Address"},
	"GPUDriver":          {"zh": "显卡驱动", "en": "Driver"},
	"GPUProduct":         {"zh": "显卡型号", "en": "Product"},
	"GPUVendor":          {"zh": "显卡厂商", "en": "Vendor"},
	"OS":                 {"zh": "操作系统", "en": "OS"},
	"Arch":               {"zh": "系统架构", "en": "Arch"},
	"Kernel":             {"zh": "内核版本", "en": "Kernel"},
	"Platform":           {"zh": "系统类型", "en": "Platform"},
	"Hostname":           {"zh": "主机名称", "en": "Hostname"},
	"TimeZone":           {"zh": "时区", "en": "Time zone"},
	"Load1":              {"zh": "1分钟平均负载", "en": "Load average (1 min)"},
	"Load5":              {"zh": "5分钟平均负载", "en": "Load average (5 min)"},
	"Load15":             {"zh": "15分钟平均负载", "en": "Load average (15 min)"},
	"NicName":            {"zh": "网卡名称", "en": "Name"},
	"NicPCIAddress":      {"zh": "PCI 地址", "en": "PCI Address"},
	"NicMacAddress":      {"zh": "MAC 地址", "en": "MAC Address"},
	"NicSpeed":           {"zh": "网卡速率", "en": "Speed"},
	"NicDuplex":          {"zh": "工作模式", "en": "Duplex"},
	"NicDriver":          {"zh": "网卡驱动", "en": "Driver"},
	"NicProduct":         {"zh": "网卡型号", "en": "Product"},
	"NicVendor":          {"zh": "网卡厂商", "en": "Vendor"},
	"MemoryTotal":        {"zh": "内存大小", "en": "Total"},
	"MemoryUsed":         {"zh": "已用内存", "en": "Used"},
	"MemoryUsedPercent":  {"zh": "内存占用", "en": "Used Percent"},
	"MemoryFree":         {"zh": "空闲内存", "en": "Free"},
	"MemoryShared":       {"zh": "共享内存", "en": "Shared"},
	"MemoryBuffCache":    {"zh": "缓冲内存", "en": "Buff Cache"},
	"MemoryAvail":        {"zh": "可用内存", "en": "Avail"},
	"SwapDisabled":       {"zh": "交换空间未启用", "en": "Swap Disabled"},
	"SwapTotal":          {"zh": "交换空间大小", "en": "Total"},
	"SwapFree":           {"zh": "空闲交换空间", "en": "Free"},
	"Process":            {"zh": "进程数", "en": "Process"},
	"ProductVendor":      {"zh": "设备厂商", "en": "Vendor"},
	"ProductName":        {"zh": "设备名称", "en": "Name"},
	"StorageName":        {"zh": "磁盘名称", "en": "Name"},
	"StorageType":        {"zh": "磁盘类型", "en": "Type"},
	"StorageDriver":      {"zh": "磁盘驱动", "en": "Driver"},
	"StorageVendor":      {"zh": "磁盘厂商", "en": "Vendor"},
	"StorageModel":       {"zh": "磁盘型号", "en": "Model"},
	"StorageSerial":      {"zh": "磁盘序列号", "en": "Serial"},
	"StorageRemovable":   {"zh": "磁盘可移除", "en": "Removable"},
	"StorageSize":        {"zh": "磁盘容量", "en": "Size"},
	"BootTime":           {"zh": "系统启动时间", "en": "Boot Time"},
	"Uptime":             {"zh": "系统运行时长", "en": "Uptime"},
	"StartTime":          {"zh": "系统启动用时", "en": "Startup Time"},
	"User":               {"zh": "用户名称", "en": "User"},
	"UserName":           {"zh": "用户昵称", "en": "Username"},
	"UserUid":            {"zh": "用户标识", "en": "UID"},
	"UserGid":            {"zh": "用户组标识", "en": "GID"},
	"UserHomeDir":        {"zh": "用户主目录", "en": "Home Dir"},
	"UpdateList":         {"zh": "更新列表", "en": "Update List"},
	"UpdateDaemonStatus": {"zh": "更新服务", "en": "Update Daemon"},
}

各部分.条目的名称

View Source
var Language = GetLanguage() // 系统语言
View Source
var PartName = map[string]map[string]string{
	"Product": {"zh": "设备", "en": "Product"},
	"Board":   {"zh": "主板", "en": "Board"},
	"BIOS":    {"zh": "BIOS", "en": "BIOS"},
	"CPU":     {"zh": "处理器", "en": "CPU"},
	"GPU":     {"zh": "显卡", "en": "GPU"},
	"Memory":  {"zh": "内存", "en": "Memory"},
	"Swap":    {"zh": "交换分区", "en": "Swap"},
	"Disk":    {"zh": "磁盘", "en": "Disk"},
	"NIC":     {"zh": "网卡", "en": "NIC"},
	"OS":      {"zh": "系统", "en": "OS"},
	"Load":    {"zh": "负载", "en": "Load"},
	"Time":    {"zh": "时间", "en": "Time"},
	"User":    {"zh": "用户", "en": "User"},
	"Update":  {"zh": "更新", "en": "Update"},
}

各部分的名称

View Source
var Platform = runtime.GOOS // 操作系统
View Source
var UserInfo, _ = GetUserInfoByName(UserName) // 用户信息
View Source
var UserName = func() string {
	if GetVariable("SUDO_USER") != "" {
		return GetVariable("SUDO_USER")
	}
	return GetVariable("USER")
}()

用户名,当程序提权运行时,使用 SUDO_USER 变量获取提权前的用户名

Functions

func BubbleSort

func BubbleSort(arr []float64)

BubbleSort 冒泡排序

参数:

  • arr: 需要排序的切片

func CompareFile added in v1.1.6

func CompareFile(file1Path string, file2Path string) (bool, error)

CompareFile 并发比较两个文件是否相同

参数:

  • file1Path: 文件1路径
  • file2Path: 文件2路径

返回:

  • 文件相同返回 true,出错或不同返回 false

func CreateDir added in v1.1.6

func CreateDir(dirPath string) error

CreateDir 创建文件夹

参数:

  • dirPath: 文件夹路径

返回:

  • 错误信息

func CreateFile

func CreateFile(filePath string) error

CreateFile 创建文件,包括其父目录

参数:

  • filePath: 文件路径

返回:

  • 错误信息

func DeleteFile

func DeleteFile(filePath string) error

DeleteFile 删除文件

参数:

  • filePath: 文件路径

返回:

  • 错误信息

func FileEmpty added in v1.1.6

func FileEmpty(filePath string) bool

FileEmpty 判断文件是否为空

  • 无法判断文件夹

参数:

  • filePath: 文件路径

返回:

  • 文件为空返回 true,否则返回 false

func FileExist

func FileExist(filePath string) bool

FileExist 判断文件是否存在

参数:

  • filePath: 文件路径

返回:

  • 文件存在返回 true,否则返回 false

func FolderEmpty added in v1.1.6

func FolderEmpty(dirPath string) bool

FolderEmpty 判断文件夹是否为空

  • 包括隐藏文件

参数:

  • dirPath: 文件夹路径

返回:

  • 文件夹为空返回 true,否则返回 false

func FormatFloat

func FormatFloat(value float64, precision int) string

FormatFloat 动态计算浮点数长度并输出合适的格式字符串

参数:

  • value: 需要处理的浮点数
  • precision: 期望的小数位数

返回:

  • 格式化后的字符串

func GetAbsPath added in v1.1.6

func GetAbsPath(filePath string) string

GetAbsPath 获取指定文件的绝对路径

参数:

  • filePath: 文件路径

返回:

  • 文件的绝对路径

func GetBIOSInfo added in v1.3.9

func GetBIOSInfo(sysInfo sysinfo.SysInfo) map[string]interface{}

GetBIOSInfo 获取 BIOS 信息

参数:

  • sysInfo: 总的系统信息

返回:

  • BIOS 信息

func GetBoardInfo added in v1.3.9

func GetBoardInfo(sysInfo sysinfo.SysInfo) map[string]interface{}

GetBoardInfo 获取主板信息

参数:

  • sysInfo: 总的系统信息

返回:

  • 主板信息

func GetCPUInfo added in v1.3.9

func GetCPUInfo(sysInfo sysinfo.SysInfo, dataUnit string) map[string]interface{}

GetCPUInfo 获取 CPU 信息

参数:

  • sysInfo: 总的系统信息
  • dataUnit: 存储数据单位

返回:

  • CPU 信息

func GetColor added in v1.2.1

func GetColor() int

GetColor 随机获取一个颜色

返回:

  • 颜色代码

func GetCurrentUserInfo

func GetCurrentUserInfo() (*user.User, error)

GetCurrentUserInfo 获取当前用户信息

返回:

  • 用户信息
  • 错误信息

func GetGPUInfo added in v1.3.9

func GetGPUInfo() map[string]interface{}

GetGPUInfo 获取显卡信息

返回:

  • 显卡信息

func GetHostname

func GetHostname() string

GetHostname 获取系统 HOSTNAME

返回:

  • HOSTNAME 或空字符串

func GetLanguage added in v1.2.5

func GetLanguage() string

GetLanguage 获取系统语言

返回:

  • 系统语言,目前仅支持 zh 或 en

func GetLoadInfo added in v1.3.9

func GetLoadInfo() map[string]interface{}

GetLoadInfo 获取负载信息

返回:

  • 系统负载信息

func GetMemoryInfo added in v1.3.9

func GetMemoryInfo(dataUnit string, percentUnit string) map[string]interface{}

GetMemoryInfo 获取内存信息

参数:

  • dataUnit: 存储数据单位
  • percentUnit: 百分比数据单位

返回:

  • 内存信息

func GetNicInfo added in v1.3.9

func GetNicInfo() map[string]interface{}

GetNicInfo 获取网卡信息

返回:

  • 网卡信息

func GetOSInfo added in v1.3.9

func GetOSInfo(sysInfo sysinfo.SysInfo) map[string]interface{}

GetOSInfo 获取系统信息

参数:

  • sysInfo: 总的系统信息 (System Info)

返回:

  • 系统信息 (OS Info)

func GetProductInfo added in v1.3.9

func GetProductInfo(sysInfo sysinfo.SysInfo) map[string]interface{}

GetProductInfo 获取产品信息

参数:

  • sysInfo: 总的系统信息

返回:

  • 产品信息

func GetStorageInfo added in v1.3.9

func GetStorageInfo() map[string]interface{}

GetStorageInfo 获取存储设备信息

返回:

  • 存储设备信息

func GetSwapInfo added in v1.3.9

func GetSwapInfo(dataUnit string) map[string]interface{}

GetSwapInfo 获取交换分区信息

参数:

  • dataUnit: 存储数据单位

返回:

  • 交换分区信息

func GetTimeInfo added in v1.3.9

func GetTimeInfo() (map[string]interface{}, error)

GetTimeInfo 获取时间信息

返回:

  • 时间信息
  • 错误信息

func GetTomlConfig added in v1.3.9

func GetTomlConfig(filePath string) (*toml.Tree, error)

GetTomlConfig 读取 toml 配置文件

参数:

  • filePath: toml 配置文件路径

返回:

  • toml 配置树
  • 错误信息

func GetUpdateDaemonInfo added in v1.3.9

func GetUpdateDaemonInfo() (map[string]interface{}, error)

GetUpdateDaemonInfo 获取更新检测服务的信息

返回:

  • 更新检测服务的信息
  • 错误信息

func GetUpdateInfo added in v1.3.9

func GetUpdateInfo(filePath string, line int) ([]string, error)

GetUpdateInfo 读取更新信息记录文件

  • 参数 line=0 时读取全部行

参数:

  • filePath: 更新信息记录文件路径
  • line: 读取指定行

返回:

  • 更新信息
  • 错误信息

func GetUserInfo added in v1.3.9

func GetUserInfo() map[string]interface{}

GetUserInfo 获取用户信息

返回:

  • 用户信息

func GetUserInfoById

func GetUserInfoById(userId int) (*user.User, error)

GetUserInfoById 根据 ID 获取用户信息

参数:

  • userId: 用户 ID

返回:

  • 用户信息
  • 错误信息

func GetUserInfoByName

func GetUserInfoByName(userName string) (*user.User, error)

GetUserInfoByName 根据用户名获取用户信息

参数:

  • userName: 用户名

返回:

  • 用户信息
  • 错误信息

func GetVariable

func GetVariable(key string) string

GetVariable 获取环境变量

参数:

  • key: 变量名

返回:

  • 变量值

func GoToDir added in v1.1.6

func GoToDir(dirPath string) error

GoToDir 进到指定文件夹

参数:

  • dirPath: 文件夹路径

返回:

  • 错误信息

func Human added in v1.1.7

func Human(size float64, initialUnit string) (float64, string)

Human 存储数据转换为人类可读的格式

参数:

  • size: 需要转换的存储数据
  • initialUnit: 初始单位

返回:

  • 转换后的数据
  • 转换后的单位

func ProgramInfo

func ProgramInfo() map[string]string

ProgramInfo 返回程序信息

返回:

  • 程序信息

func ReadFileCount added in v1.1.6

func ReadFileCount(file, key string) int

ReadFileCount 获取文件包含关键字的行的计数

参数:

  • file: 文件路径
  • key: 关键字

返回:

  • 包含关键字的行的数量

func ReadFileKey added in v1.1.6

func ReadFileKey(file, key string) string

ReadFileKey 读取文件包含关键字的行

参数:

  • file: 文件路径
  • key: 关键字

返回:

  • 包含关键字的行的内容

func ReadFileLine added in v1.1.6

func ReadFileLine(file string, line int) string

ReadFileLine 读取文件指定行

参数:

  • file: 文件路径
  • line: 行号

返回:

  • 指定行的内容

func RunCommand

func RunCommand(command string, args []string) error

RunCommand 运行命令不返回命令的输出

参数:

  • command: 命令
  • args: 命令参数

返回:

  • 错误信息

func RunCommandGetResult

func RunCommandGetResult(command string, args []string) (string, error)

RunCommandGetResult 运行命令并返回命令的输出

参数:

  • command: 命令
  • args: 命令参数

返回:

  • 命令的输出
  • 错误信息

func SetVariable

func SetVariable(key, value string) error

SetVariable 设置环境变量

参数:

  • key: 变量名
  • value: 变量值

返回:

  • 错误信息

func UnixTime2DayHourMinuteSecond added in v1.2.3

func UnixTime2DayHourMinuteSecond(unixTime uint64) (day, hour, minute, second uint64)

UnixTime2DayHourMinuteSecond Unix 时间戳转换为天、小时、分钟、秒

参数:

  • totalSeconds: Unix 时间戳

返回:

  • day: 天
  • hour: 小时
  • minute: 分钟
  • second: 秒

func UnixTime2TimeString added in v1.2.3

func UnixTime2TimeString(unixTime uint64) string

UnixTime2TimeString uint64 格式的 Unix 时间戳转换为字符串格式

参数:

  • timeStamp: Unix 时间戳

返回:

  • 格式化的 Unix 时间戳字符串

func UpperStringFirstChar

func UpperStringFirstChar(str string) string

UpperStringFirstChar 最大化字符串的第一个字母

参数:

  • str: 需要处理的字符串

返回:

  • 处理后的字符串

func WriteFile added in v1.1.6

func WriteFile(filePath string, content string) error

WriteFile 写入内容到文件

参数:

  • filePath: 文件路径
  • content: 内容

返回:

  • 错误信息

func WriteTomlConfig added in v1.3.9

func WriteTomlConfig(filePath string) (int64, error)

WriteTomlConfig 写入 toml 配置文件

参数:

  • filePath: toml 配置文件路径

返回:

  • 写入的字节数
  • 错误信息

Types

type Config added in v1.3.9

type Config struct {
	Genealogy GenealogyConfig `toml:"genealogy"`
	Main      MainConfig      `toml:"main"`
}

用于转换 Toml 配置树的结构体

func LoadConfigToStruct added in v1.3.9

func LoadConfigToStruct(configTree *toml.Tree) (*Config, error)

LoadConfigToStruct 将 Toml 配置树加载到结构体

参数:

  • configTree: 解析 toml 配置文件得到的配置树

返回:

  • 结构体
  • 错误信息

type CpuConfig added in v1.3.9

type CpuConfig struct {
	CacheUnit string `toml:"cache_unit"`
}

type GenealogyConfig added in v1.3.9

type GenealogyConfig struct {
	Cpu    CpuConfig    `toml:"cpu"`
	Memory MemoryConfig `toml:"memory"`
	Update UpdateConfig `toml:"update"`
}

type MainConfig added in v1.3.9

type MainConfig struct {
	Colorful bool `toml:"colorful"`
}

type MemoryConfig added in v1.3.9

type MemoryConfig struct {
	DataUnit    string `toml:"data_unit"`
	PercentUnit string `toml:"percent_unit"`
}

type UpdateConfig added in v1.3.9

type UpdateConfig struct {
	RecordFile string `toml:"record_file"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL