general

package
v1.0.1 Latest Latest
Warning

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

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

Documentation

Index

Constants

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

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 (
	RunFlag     = "🐙"  // 运行状态符号 - 运行中
	LatestFlag  = "🌟"  // 运行状态符号 - 已是最新
	SuccessFlag = "✅"  // 运行状态符号 - 成功
	WarningFlag = "⚠️" // 运行状态符号 - 警告
	ErrorFlag   = "❌"  // 运行状态符号 - 失败
)
View Source
var (
	CursorOnFlag   = "👉"  // 选择器符号 - 光标在
	CursorOffFlag  = "  " // 选择器符号 - 光标不在
	SelectedFlag   = "•"  // 选择器符号 - 已选中
	UnselectedFlag = " "  // 选择器符号 - 已选中
	SelectAllFlag  = "⭐️" // 选择器符号 - 全选
)
View Source
var (
	Separator1st = "=" // 分隔符 - 1级
	Separator2st = "-" // 分隔符 - 2级
	Separator3st = "·" // 分隔符 - 3级
)
View Source
var (
	MultiSelectTips  = "Please select from the %s below (multi-select)\n"  // 提示词 - 多选
	SingleSelectTips = "Please select from the %s below (single-select)\n" // 提示词 - 单选
	QuietTips        = "Press '%s' to quit\n"                              // 提示词 - 退出
	SelectAllTips    = "Select All"                                        // 提示词 - 全选
)
View Source
var (
	BranchFlag    = "🌿" // Git 符号 - 分支
	SubmoduleFlag = "📦" // Git 符号 - 子模块
)
View Source
var (
	JoinerIng    = "├──" // 条目连接符号 - 中间条目
	JoinerFinish = "└──" // 条目连接符号 - 最后条目
)
View Source
var (
	GitCommitHash string = "Unknown" // Git 提交 Hash
	BuildTime     string = "Unknown" // 编译时间
	BuildBy       string = "Unknown" // 编译者
)
View Source
var Arch = runtime.GOARCH // 系统架构
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 CloneRepoViaSSH

func CloneRepoViaSSH(repoPath, URL, username, repoName string, publicKeys *ssh.PublicKeys) (*git.Repository, error)

CloneRepoViaSSH 使用 SSH 协议将远端仓库克隆到本地

参数:

  • repoPath: 本地仓库路径
  • URL: 远端仓库地址(仅包括主地址,例如:github.com)
  • username: 远端仓库用户名
  • repoName: 远端仓库名称
  • publicKeys: ssh 公钥

返回:

  • 本地仓库对象
  • 错误信息

func CompareFile

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

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

参数:

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

返回:

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

func CreateDir

func CreateDir(dirPath string) error

CreateDir 创建文件夹

参数:

  • dirPath: 文件夹路径

返回:

  • 错误信息

func CreateFile

func CreateFile(filePath string) error

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

参数:

  • filePath: 文件路径

返回:

  • 错误信息

func CreateLocalBranch

func CreateLocalBranch(repo *git.Repository, branchs []fs.FileInfo) []string

CreateLocalBranch 本地仓库根据远程分支创建本地分支

  • 远程分支 refs/remotes/${remote}/<remoteBranchName>
  • 本地分支 refs/heads/<localBranchName>

参数:

  • repo: 本地仓库对象
  • branchs: 远程分支信息

返回:

  • 错误信息切片

func Delay

func Delay(second float32)

Delay 延时

参数:

  • second: 延时秒数

func DeleteFile

func DeleteFile(filePath string) error

DeleteFile 删除文件

参数:

  • filePath: 文件路径

返回:

  • 错误信息

func FileEmpty

func FileEmpty(filePath string) bool

FileEmpty 判断文件是否为空

  • 无法判断文件夹

参数:

  • filePath: 文件路径

返回:

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

func FileExist

func FileExist(filePath string) bool

FileExist 判断文件是否存在

参数:

  • filePath: 文件路径

返回:

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

func FolderEmpty

func FolderEmpty(dirPath string) bool

FolderEmpty 判断文件夹是否为空

  • 包括隐藏文件

参数:

  • dirPath: 文件夹路径

返回:

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

func GetAbsPath

func GetAbsPath(filePath string) string

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

参数:

  • filePath: 文件路径

返回:

  • 文件的绝对路径

func GetCurrentUserInfo

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

GetCurrentUserInfo 获取当前用户信息

返回:

  • 用户信息
  • 错误信息

func GetHostname

func GetHostname() string

GetHostname 获取系统 HOSTNAME

返回:

  • HOSTNAME 或空字符串

func GetLocalRepoSubmoduleInfo

func GetLocalRepoSubmoduleInfo(worktree *git.Worktree) (git.Submodules, error)

GetLocalRepoSubmoduleInfo 获取本地仓库子模块信息

参数:

  • worktree: 仓库的 git 工作树对象

返回:

  • 子模块信息
  • 错误信息

func GetPublicKeysByGit

func GetPublicKeysByGit(pemFile string) (*ssh.PublicKeys, error)

GetPublicKeysByGit 使用 go-git 自带的方法获取 ssh 公钥

参数:

  • pemFile: 私钥文件路径

返回:

  • ssh 公钥
  • 错误信息

func GetPublicKeysBySSH

func GetPublicKeysBySSH(pemFile string) (transport.AuthMethod, error)

GetPublicKeysBySSH 使用 crypto/ssh 模块获取 ssh 公钥

参数:

  • pemFile: 私钥文件路径

返回:

  • ssh 公钥
  • 错误信息

func GetRepoBranchInfo

func GetRepoBranchInfo(worktree *git.Worktree, which string) ([]fs.FileInfo, error)

GetRepoBranchInfo 获取本地仓库的[本地|远程]分支信息

参数:

  • worktree: 仓库的 git 工作树对象
  • which: 'local' or 'remote',指定要获取的是本地分支还是远程分支

返回:

  • 分支信息
  • 错误信息

func GetRepoHeadRef added in v1.0.1

func GetRepoHeadRef(repo *git.Repository) *plumbing.Reference

GetRepoHeadRef 获取本地仓库对象 HEAD 指向的引用

参数:

  • repo: 本地仓库对象

返回:

  • HEAD 引用

func GetTomlConfig added in v0.13.1

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

GetTomlConfig 读取 toml 配置文件

参数:

  • filePath: toml 配置文件路径

返回:

  • toml 配置树
  • 错误信息

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

func GoToDir(dirPath string) error

GoToDir 进到指定文件夹

参数:

  • dirPath: 文件夹路径

返回:

  • 错误信息

func IsLocalRepo

func IsLocalRepo(path string) (bool, *git.Repository, *plumbing.Reference)

IsLocalRepo 检测是不是本地仓库,是的话返回本地仓库对象及其 HEAD 指向的引用

参数:

  • path: 本地仓库路径

返回:

  • 是否本地仓库
  • 本地仓库对象
  • HEAD 引用

func ModifyGitConfig added in v0.13.1

func ModifyGitConfig(configFile, originalLink, newLink string) error

ModifyGitConfig 修改 .git/config 文件,确保 [remote "origin"] 的 url 字段是以 'git@' 开头,并添加两行 pushurl

参数:

  • configFile: .git/config 文件路径
  • originalLink: 需要替换的原始链接
  • newLink: 替换上去的新链接

返回:

  • 错误信息

func MultipleSelectionFilter added in v0.12.7

func MultipleSelectionFilter(choices []string) ([]string, error)

MultipleSelectionFilter 多选筛选器,接受一个可选项切片,返回一个已选项切片,允许全选

参数:

  • choices: 可选项

返回:

  • 已选项
  • 错误信息

func ProgramInfo

func ProgramInfo() map[string]string

ProgramInfo 返回程序信息

返回:

  • 程序信息

func PullRepo added in v0.11.0

func PullRepo(repo *git.Repository, publicKeys *ssh.PublicKeys) (worktree *git.Worktree, leftCommit, rightCommit *object.Commit, err error)

PullRepo 拉取远端仓库的更改到本地

参数:

  • repo: 本地仓库对象

返回:

  • 拉取到的更改的提交信息
  • 错误信息

func ReadFileCount

func ReadFileCount(file, key string) int

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

参数:

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

返回:

  • 包含关键字的行的数量

func ReadFileKey

func ReadFileKey(file, key string) string

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

参数:

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

返回:

  • 包含关键字的行的内容

func ReadFileLine

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 RunScript added in v0.13.1

func RunScript(filePath, scriptName string) error

RunScript 运行 shell 脚本

参数:

  • filePath: 脚本所在目录
  • scriptName: 脚本名

返回:

  • 错误信息

func SetVariable

func SetVariable(key, value string) error

SetVariable 设置环境变量

参数:

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

返回:

  • 错误信息

func WriteFile

func WriteFile(filePath string, content string) error

WriteFile 写入内容到文件

参数:

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

返回:

  • 错误信息

func WriteTomlConfig added in v0.13.1

func WriteTomlConfig(filePath string) (int64, error)

WriteTomlConfig 写入 toml 配置文件

参数:

  • filePath: toml 配置文件路径

返回:

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

Types

type Config added in v0.13.1

type Config struct {
	Git     GitConfig     `toml:"git"`
	Script  ScriptConfig  `toml:"script"`
	SSH     SSHConfig     `toml:"ssh"`
	Storage StorageConfig `toml:"storage"`
}

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

func LoadConfigToStruct added in v0.13.1

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

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

参数:

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

返回:

  • 结构体
  • 错误信息

type GitConfig added in v0.13.1

type GitConfig struct {
	GithubUrl      string   `toml:"github_url"`
	GithubUsername string   `toml:"github_username"`
	GiteaUrl       string   `toml:"gitea_url"`
	GiteaUsername  string   `toml:"gitea_username"`
	Repos          []string `toml:"repos"`
}

type SSHConfig added in v0.13.1

type SSHConfig struct {
	RsaFile string `toml:"rsa_file"`
}

type ScriptConfig added in v0.13.1

type ScriptConfig struct {
	NameList []string `toml:"name_list"`
}

type StorageConfig added in v0.13.1

type StorageConfig struct {
	Path string `toml:"path"`
}

Jump to

Keyboard shortcuts

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