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 a602285..c5dc733 100644 --- a/main.go +++ b/main.go @@ -15,47 +15,50 @@ import ( "fmt" "os" "quake/src/apis" + "quake/src/utils" "strings" ) func main() { Init() - 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=`) +func Init() { + fmt.Println("Starting Quake Cli...") + 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": @@ -65,6 +68,11 @@ Usage of ./quake_go: } } -func Init() { - fmt.Println("Starting Quake Cli...") +func flaginit() (string, string) { + var start, size string + flag.StringVar(&size, "size", "10", "size String value") + flag.StringVar(&start, "start", "0", "start String value") + + 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 +}