From 0f737973beab4009b68d999ca4364abe0e73d87d Mon Sep 17 00:00:00 2001 From: ph4nt0mer Date: Tue, 6 Sep 2022 11:30:47 +0800 Subject: [PATCH] v1.1 --- .gitignore | 3 +- README.md | 24 ++++++------- go.mod | 2 ++ go.sum | 3 ++ main.go | 78 +++++++++++++++++------------------------ src/utils/YamlAction.go | 55 +++++++++++++++++++++++++++++ 6 files changed, 105 insertions(+), 60 deletions(-) create mode 100644 go.sum create mode 100644 src/utils/YamlAction.go diff --git a/.gitignore b/.gitignore index 20375aa..ea9c1e1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .vscode/ quake_go -quake \ No newline at end of file +quake +config.yaml \ No newline at end of file diff --git a/README.md b/README.md index 43c9246..66acca3 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ * @Author: ph4nt0mer * @Date: 2022-09-01 18:39:52 * @LastEditors: ph4nt0mer - * @LastEditTime: 2022-09-02 15:51:29 + * @LastEditTime: 2022-09-06 11:29:48 * @FilePath: /quake_go/README.md * @Description: * @@ -29,7 +29,13 @@ rust 项目代码 移步这里——https://github.com/360quake/quake_rs/ ## 更新日志 +- 2022-09-06 v1.1: + + - 简化参数传参格式 + - 将 token 固化在当前目录 + - 2022-09-01 v1.0: + - 新增 info 接口功能 - 新增 service 接口功能 @@ -38,17 +44,7 @@ rust 项目代码 移步这里——https://github.com/360quake/quake_rs/ ```bash Starting Quake Cli... example: - ./quake -token= -model= -query= - -Usage of ./quake_go: - -model string - model String value - -query string - query String value - -size string - size String value (default "10") - -start string - start String value (default "0") - -token string - token Sting value +./quake init +./quake info +./quake query port:443 ``` diff --git a/go.mod b/go.mod index f5f90a7..ec93555 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module quake go 1.19 + +require gopkg.in/yaml.v2 v2.4.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..7534661 --- /dev/null +++ b/go.sum @@ -0,0 +1,3 @@ +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= diff --git a/main.go b/main.go index 8d37baf..2c8214d 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,7 @@ * @Author: ph4nt0mer * @Date: 2022-08-31 17:03:03 * @LastEditors: ph4nt0mer - * @LastEditTime: 2022-09-02 16:51:22 + * @LastEditTime: 2022-09-06 11:26:22 * @FilePath: /quake_go/main.go * @Description: * @@ -11,57 +11,54 @@ package main import ( - "encoding/json" "flag" "fmt" "os" "quake/src/apis" + "quake/src/utils" "strings" ) func main() { - test() - + Init() } func Init() { fmt.Println("Starting Quake Cli...") - var model, token, query, start, size string - flag.StringVar(&size, "size", "10", "size String value") - flag.StringVar(&start, "start", "0", "start String value") - flag.StringVar(&query, "query", "", "query String value") - flag.StringVar(&model, "model", "", "model String value,example: server,info") - flag.StringVar(&token, "token", "", "token Sting value") - - flag.Parse() - - if len(os.Args) == 1 { - fmt.Println(`example: - ./quake -token= -model= -query=`) + num := len(os.Args) + if num < 2 { fmt.Println(` Usage of ./quake_go: - -model string - model String value (default "info") - -query string - query String value -size string size String value (default "10") -start string - start String value (default "0") - -token string - token Sting value`) + start String value (default "0")`) return } - if token == "" { - fmt.Println("!!!!token is empty!!!!") + path := "./config.yaml" + if strings.ToLower(os.Args[1]) == "init" { + if num < 3 { + fmt.Println("!!!!token is empty !!!!") + return + } + utils.WriteYaml(path, os.Args[2]) return } - - switch strings.ToLower(model) { + token, status := utils.ReadYaml(path) + if status { + fmt.Println("!!!!please ./quake init token!!!!") + return + } + start, size := flaginit() + switch strings.ToLower(os.Args[1]) { case "info": - apis.InfoGet(token) - case "server": - apis.SearchServicePost(query, start, size, token) + apis.InfoGet(token.Token) + case "query": + if num < 3 { + fmt.Println("!!!!query is empty !!!!") + return + } + apis.SearchServicePost(os.Args[2], start, size, token.Token) case "host": fmt.Println("主机数据接口待完成。。。") case "favicon": @@ -71,20 +68,11 @@ Usage of ./quake_go: } } -type Server struct { - ServerName string // 首字母大写,首字母不敏感,其他字母敏感 - ServerIP string // 首字母大写 -} - -type Serverslice struct { - Servers []Server -} - -func test() { - var s Serverslice - str := `{"servers":[{"serverName":"Local_Web","serverIP":"127.0.0.1"},{"serverName":"Local_DB","serverIP":"127.0.0.1"}]}` - json.Unmarshal([]byte(str), &s) +func flaginit() (string, string) { + var start, size string + flag.StringVar(&size, "size", "10", "size String value") + flag.StringVar(&start, "start", "0", "start String value") - // 输出转换后第一条 - fmt.Println(s.Servers[0]) + flag.Parse() + return start, size } diff --git a/src/utils/YamlAction.go b/src/utils/YamlAction.go new file mode 100644 index 0000000..94e6907 --- /dev/null +++ b/src/utils/YamlAction.go @@ -0,0 +1,55 @@ +/* + * @Author: ph4nt0mer rootphantomy@hotmail.com + * @Date: 2022-09-06 10:04:23 + * @LastEditors: ph4nt0mer + * @LastEditTime: 2022-09-06 11:17:07 + * @FilePath: /quake_go/src/utils/YamlAction.go + * @Description:读写yaml + * + * Copyright (c) 2022 by ph4nt0mer, All Rights Reserved. + */ +package utils + +import ( + "fmt" + "io/ioutil" + + "gopkg.in/yaml.v2" +) + +type TokenInfo struct { + Token string `yaml:"token"` +} + +func WriteYaml(path string, token string) { + // 进行写yaml操作 + var tokeninfo TokenInfo + tokeninfo.Token = token + data, err := yaml.Marshal(tokeninfo) + //config赋值777操作 + err = ioutil.WriteFile(path, data, 0777) + if err = ioutil.WriteFile(path, data, 0777); err != nil { + fmt.Printf("token init fail!") + } else { + fmt.Printf("token init success!") + } + +} + +func checkError(err error) bool { + // 判断是否有错误,有错误nil,返回false,无返回true + if err != nil { + return true + } + return false +} + +func ReadYaml(path string) (TokenInfo, bool) { + // 读取yaml里面的内容 + content, err := ioutil.ReadFile(path) + status := checkError(err) + var token TokenInfo + err = yaml.Unmarshal(content, &token) + status = checkError(err) + return token, status +}