Skip to content

Commit

Permalink
添加特殊模式,省略 -a 选项
Browse files Browse the repository at this point in the history
添加type字段 描述字典类型
添加-fresh参数
  • Loading branch information
ttstormxx committed Aug 19, 2023
1 parent 9f31e8f commit 5dbed6d
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 15 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ go build lineadd.go
-config 重新初始化(遍历字典根目录初始化配置文件)
-base 字典根目录(用于在-config时设置BaseDir)
-write 依据配置文件初始化字典根目录(-config时使用)
-fresh 读取配置文件, 写入配置文件, 什么都没变, 适配新增的字典Type(-config时使用)
-silent 安静模式 一个挂件
```
## 快速使用
Expand All @@ -45,21 +46,29 @@ go build lineadd.go
配置文件可以自定义,即使所有字典txt文件都在同一目录下,也可以自由分类
![Alt text](pics/example.png)

#### 向某类字典新增行
#### 向web类字典新增行
`echo 123123123|lineadd -a web`

![Alt text](pics/add.png)

#### 特殊模式 省略 -a
`echo 123123123|lineadd web`

![Alt text](pics/special.png)

#### 路径类字典(在配置文件中将type设置为path) 自动处理新行开头的/
![Alt text](pics/leadingslash.png)

#### 查看字典状态和别名
`lineadd -t`
![Alt text](pics/stat.png)

## trick
#### 数据输入模式
支持文件读取、cmd输入、管道符输入、标准输入4种模式
支持文件读取、cmd参数输入、管道符输入、cmd输入4种模式
![Alt text](pics/input.png)

当文件读取、cmd输入、管道符输入均不存在时,启用标准输入
当文件读取、cmd参数输入、管道符输入均不存在时,启用cmd输入
![Alt text](pics/stdin.png)


Expand All @@ -72,6 +81,7 @@ captcha: #类名
path: #相对路径
alias: #别名 列表 与字典名对应顺序
- cap
type: #可使用 path 值将字典标记为路径类字典或置空
```

#### 通过根目录自动设置配置文件
Expand Down
13 changes: 11 additions & 2 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ go build lineadd.go
-config Reinitialize (traverse dictionary root directory initialization configuration file)
-base Dictionary root directory (used to set BaseDir when -config)
-write Initialize the dictionary root directory according to the configuration file (used when -config)
-fresh Read configuration file, write configuration file, nothing has changed, adapt to the newly added dictionary type (used with -config)
-silent quiet mode literaly no use
```
## Quick start
Expand All @@ -51,16 +52,23 @@ The configuration file can be customized, even if all the dictionary text files

![Alt text](pics/add.png)

#### special mode omit -a
`echo 123123123|lineadd web`
![Alt text](pics/special.png)

#### Path-based dictionary (set type as "path" in the configuration file), automatically handle leading "/" in new lines.
![Alt text](pics/leadingslash.png)

#### View the status and aliases of the dictionary.
`lineadd -t`
![Alt text](pics/stat.png)

## trick
#### Data Input Modes
Supports four modes of data input: file reading, command line input, pipe input, and standard input.
Supports four modes of data input: file reading, command line option input, pipe input, and command line input.
![Alt text](pics/input.png)

If file reading, command line input, and pipe input are not available, enable standard input.
If file reading, command line option input, and pipe input are not available, enable command line input.
![Alt text](pics/stdin.png)


Expand All @@ -73,6 +81,7 @@ captcha: #category
path: #Relative Path
alias: #Alias: List Corresponding to Dictionary Names in Order
- cap
type: #Use the "path" value to designate the dictionary as a path-based dictionary or leave it empty.
```

#### Automatically set the configuration file using the root directory.
Expand Down
116 changes: 106 additions & 10 deletions lineadd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"gopkg.in/yaml.v3"
)

var version = "1.0"
var version = "1.1"

var banner = `
_ _ _ _
Expand All @@ -34,12 +34,23 @@ func showBanner() {
}

// * 配置文件解析
// type Config struct {
// BaseDir string `yaml:"baseDir"`
// Items map[string]struct {
// Dicts []string `yaml:"dicts"`
// Path string `yaml:"path"`
// Alias []string `yaml:"alias"`
// } `yaml:",inline"`
// }

// 增加字典类型type 专用于path类型处理
type Config struct {
BaseDir string `yaml:"baseDir"`
Items map[string]struct {
Dicts []string `yaml:"dicts"`
Path string `yaml:"path"`
Alias []string `yaml:"alias"`
Type string `yaml:"type"`
} `yaml:",inline"`
}

Expand Down Expand Up @@ -84,6 +95,7 @@ var (
reconfig bool //重新初始化
firstrun bool //首次运行
write bool //依据配置文件想字典根目录写入配置的字典
fresh bool //读取配置文件,再将配置重新写入,适配新增的type

BaseDir string //字典根目录
BaseDirFromUser string //用户-base输入的根目录
Expand All @@ -101,7 +113,7 @@ func ParseImplement() {
}

// 判断是否存在必备参数 模式是否唯一
func ValidMode() {
func ValidMode(config Config) {

num := 0
if len(add) > 0 {
Expand Down Expand Up @@ -141,15 +153,31 @@ func ValidMode() {
if num > 0 {
if len(BaseDirFromUser) > 0 {
fmt.Println("-base选项仅在-config时有效")
os.Exit(1)
}
if write {
fmt.Println("-write选项仅在-config时有效")
os.Exit(1)
}
if fresh {
fmt.Println("-fresh选项仅在-config时有效")
os.Exit(1)
}
}
if reconfig {
num++
if len(BaseDirFromUser) > 0 && write {
fmt.Println("-base选项和-write选项只能选一种")
count := 0
if len(BaseDirFromUser) > 0 {
count++
}
if write {
count++
}
if fresh {
count++
}
if count > 1 {
fmt.Println("-base选项和-write和-fresh选项只能选一种")
os.Exit(1)
}
}
Expand All @@ -158,9 +186,30 @@ func ValidMode() {
return
} else {
if num == 0 {
fmt.Println("请选择处理模式: add del count read backup stat query config")
flag.Usage()
os.Exit(1)
// 特殊模式 判断字典类是否存在,若存在则为加行模式
var configtypekeys []string
for k := range config.Items {
configtypekeys = append(configtypekeys, k)
}
for _, arg := range os.Args {
if contains(configtypekeys, arg) {
add = arg
num += 1
break
}
}
if num == 0 { //再次判断

fmt.Println("请选择处理模式: add del count read backup stat query config")
flag.Usage()
os.Exit(1)
} else {
// 特殊模式 需要自主解析输入的新行
ParamParseImp()
}
// fmt.Println("请选择处理模式: add del count read backup stat query config")
// flag.Usage()
// os.Exit(1)
} else if num > 1 {
fmt.Println("只能选择1种模式: add del count read backup stat query config")
os.Exit(1)
Expand Down Expand Up @@ -255,9 +304,39 @@ func usage() {
-config 重新初始化(遍历字典根目录初始化配置文件)
-base 字典根目录(用于在-config时设置BaseDir)
-write 依据配置文件初始化字典根目录(-config时使用)
-fresh 读取配置文件, 写入配置文件, 什么都没变, 适配新增的字典Type(-config时使用)
-silent 安静模式 一个挂件`
fmt.Println(usagetips)
}

func ParamParseImp() {
for i := 0; i < len(os.Args); i++ {
if os.Args[i] == "-l" {
if i+1 < len(os.Args) {
line = os.Args[i+1]
} else {
fmt.Println("flag needs an argument: -l")
os.Exit(1)
}
if len(line) > 0 {
lines = strings.Split(line, ",")
loginfo(strings.Join(lines, " "))
}
} else if os.Args[i] == "-f" {
if i+1 < len(os.Args) {
file = os.Args[i+1]
} else {
fmt.Println("flag needs an argument: -f")
os.Exit(1)
}

}
}
if len(os.Args) > 2 && len(line) == 0 && len(file) == 0 {
fmt.Println("特殊模式(add)只能使用 -l -f 选项")
os.Exit(1)
}
}
func FlagParse(config Config) {
flag.Usage = usage
flag.StringVar(&add, "a", "", "添加模式, 指定字典")
Expand All @@ -273,15 +352,17 @@ func FlagParse(config Config) {
flag.BoolVar(&query, "q", false, "查询某行(单行数据)是否在字典中")
flag.BoolVar(&reconfig, "config", false, "重新初始化(遍历字典根目录初始化配置文件)")
flag.BoolVar(&write, "write", false, "依据配置文件初始化字典根目录(-config时使用)")
flag.BoolVar(&fresh, "fresh", false, "读取配置文件, 写入配置文件,什么都没变(-config时使用)")
flag.StringVar(&BaseDirFromUser, "base", "", "字典根目录(用于在-config时设置BaseDir)")
flag.Parse()

//有效性判断
ValidMode()
ValidMode(config)
ParamValid(config)

if len(line) > 0 {
lines = strings.Split(line, ",")
loginfo(strings.Join(lines, " "))
}

}
Expand Down Expand Up @@ -569,7 +650,7 @@ func findIndex(arr []string, target string) *int {
}

// 输入数据处理
func InputManage() []string {
func InputManage(config Config) []string {
// 读取待处理行
var newlines []string
var newlinesfromfile []string
Expand Down Expand Up @@ -603,6 +684,15 @@ func InputManage() []string {
newlines = append(newlines, newlinesfromuserinput...)
// 去重
uniqlines = removeDuplicates(newlines)
if config.Items[category].Type == "path" {
// 对于路径类字典,去除新行开始的/
var tmplines []string
for _, x := range uniqlines {

tmplines = append(tmplines, strings.TrimPrefix(x, "/"))
}
uniqlines = tmplines
}
return uniqlines
}

Expand Down Expand Up @@ -968,6 +1058,7 @@ func SetConfig(baseDir string) (*Config, error) {
Dicts []string `yaml:"dicts"`
Path string `yaml:"path"`
Alias []string `yaml:"alias"`
Type string `yaml:"type"`
}),
}

Expand Down Expand Up @@ -1002,6 +1093,7 @@ func SetConfig(baseDir string) (*Config, error) {
Dicts []string `yaml:"dicts"`
Path string `yaml:"path"`
Alias []string `yaml:"alias"`
Type string `yaml:"type"`
}{
Path: relPath,
}
Expand Down Expand Up @@ -1064,6 +1156,10 @@ func RECONFIGMODE(config Config) {
loginfo(fmt.Sprintf("即将使用输入的根目录初始化配置文件: %s", BaseDirFromUser))
BaseDir = BaseDirFromUser

} else if fresh {
loginfo("标准化配置文件, 适配字典Type值, 不要担心, 任何东西都没变")
writeConfig(&config)
return
} else {

loginfo(fmt.Sprintf("即将使用配置文件中的根目录初始化配置文件: %s", BaseDir))
Expand Down Expand Up @@ -1359,7 +1455,7 @@ func main() {
} else { //加减行模式
//待处理数据输入
loginfo("mode: " + optype)
newlines = InputManage()
newlines = InputManage(config)
if optype == "add" {
// loginfo("mode: " + optype)
loginfo("添加处理中")
Expand Down
Binary file added pics/leadingslash.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pics/special.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5dbed6d

Please sign in to comment.