From 38b7385331f23f1a664f1b35fbe5d6d92b6129eb Mon Sep 17 00:00:00 2001 From: wuqinqiang <1185079673@qq.com> Date: Thu, 2 Mar 2023 21:54:29 +0800 Subject: [PATCH] Add conf flag for daemon --- Makefile | 6 +++++- README.md | 4 ++-- build.sh | 3 ++- cmd/daemon.go | 6 +++++- cmd/phrase.go | 8 +++++++- collector/file/file.go | 11 ++++------- conf/conf.go | 26 ++++++++++++++++++++++---- core/core.go | 2 +- go.mod | 7 +++---- go.sum | 6 ++---- 10 files changed, 53 insertions(+), 26 deletions(-) diff --git a/Makefile b/Makefile index b6d5968..8df9472 100644 --- a/Makefile +++ b/Makefile @@ -19,4 +19,8 @@ linux: macos-arm64: GOOS=darwin GOARCH=arm64 go build -o helloword-macos-arm64 main.go -build: win macos-adm64 linux macos-arm64 \ No newline at end of file +build: win macos-adm64 linux macos-arm64 + + +all: + $(shell sh build.sh) \ No newline at end of file diff --git a/README.md b/README.md index c378dc2..8f6628f 100644 --- a/README.md +++ b/README.md @@ -43,17 +43,17 @@ notify: # 通知配置,目前支持telegram,dingtalk,lark可以全配, 指定单词数量,随机选择单词,生成一段小短文,推送到用户指定平台。 ```shell -./helloword daemon --files="CET4.txt,CET6.txt" --spec="@every 10s" --word-number=8 +./helloword daemon --files="CET4.txt,CET6.txt" --spec="@every 10s" --word-number=8 --c=conf.yml ``` **参数说明** 这个程序有以下可选项: - - files:默认导入 CET4.txt 单词文件,你可以通过逗号同时导入多个单词文件,它们都存储在 library 文件夹下。 - spec:表示推送频率设置,默认为每小时生成一个新的短语,具体时间规则使用的是 [robif/cron](https://github.com/robfig/cron) 库,请参考该库的文档自行设置。 - word-number:表示生成一次短语使用的单词数量,默认为 5 个,最多不超过 10 个 - strategy: 单词选择策略,默认随机random,还可选择 leastRecentlyUsed,最近最少被使用的单词 +- conf: 可选,配置文件。具体配置信息如上 ![example](./library/example.png) diff --git a/build.sh b/build.sh index 17ec4ed..98059d2 100755 --- a/build.sh +++ b/build.sh @@ -29,7 +29,8 @@ for os in $os_all; do mv ./helloword_${os}_${arch} ${hello_path}/helloword fi cp ../LICENSE ${hello_path} - cp -rf ../library/* ${hello_path} + cp ../library/* ${hello_path} + cp ../conf/conf.example.yml ${hello_path}/conf.yml # packages cd ./packages diff --git a/cmd/daemon.go b/cmd/daemon.go index 3bc60e4..b4f7bed 100644 --- a/cmd/daemon.go +++ b/cmd/daemon.go @@ -40,10 +40,14 @@ var DaemonCmd = &cli.Command{ Usage: "单词选择策略,默认随机random,还可选择 leastRecentlyUsed,最近最少被使用的单词", Value: "random", }, + &cli.StringFlag{ + Name: "conf", + Aliases: []string{"c"}, + }, }, Action: func(cctx *cli.Context) error { - settings, err := conf.GetConf() + settings, err := conf.GetConf(cctx.String("conf")) if err != nil { return err } diff --git a/cmd/phrase.go b/cmd/phrase.go index c23d5e6..991a35b 100644 --- a/cmd/phrase.go +++ b/cmd/phrase.go @@ -18,12 +18,18 @@ import ( var PhraseCmd = &cli.Command{ Name: "phrase", Usage: "Generate phrases directly", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "conf", + Aliases: []string{"c"}, + }, + }, Action: func(cctx *cli.Context) error { req := cctx.Args().Get(0) if req == "" { return errors.New("please input your words") } - conf, err := conf.GetConf() + conf, err := conf.GetConf(cctx.String("c")) if err != nil { return err } diff --git a/collector/file/file.go b/collector/file/file.go index a7c4cb0..a9950bb 100644 --- a/collector/file/file.go +++ b/collector/file/file.go @@ -14,14 +14,12 @@ import ( ) type File struct { - fileList []string + list []string } -func New(fileNames string) *File { +func New(files string) *File { file := new(File) - for _, fileName := range strings.Split(fileNames, ",") { - file.fileList = append(file.fileList, fileName) - } + file.list = append(file.list, strings.Split(files, ",")...) return file } @@ -30,11 +28,10 @@ func (f *File) Name() string { } func (f *File) Collect(ctx context.Context) (model.Words, error) { - var words model.Words fx.From(func(source chan<- interface{}) { - for _, file := range f.fileList { + for _, file := range f.list { source <- file } diff --git a/conf/conf.go b/conf/conf.go index d8fe311..af2a45f 100644 --- a/conf/conf.go +++ b/conf/conf.go @@ -2,6 +2,8 @@ package conf import ( _ "embed" + "io" + "os" "gopkg.in/yaml.v3" @@ -12,11 +14,27 @@ import ( ) //go:embed conf.yml -var conf []byte +var base []byte -func GetConf() (*Settings, error) { - var settings Settings - err := yaml.Unmarshal(conf, &settings) +func GetConf(filePath string) (*Settings, error) { + var ( + settings Settings + b = base + ) + + if filePath != "" { + file, err := os.Open(filePath) + if err != nil { + return nil, err + } + defer file.Close() //nolint + + if b, err = io.ReadAll(file); err != nil { + return nil, err + } + } + + err := yaml.Unmarshal(b, &settings) if err != nil { return nil, err } diff --git a/core/core.go b/core/core.go index f705015..9bdafb5 100644 --- a/core/core.go +++ b/core/core.go @@ -72,7 +72,7 @@ func (core *Core) Run() error { return err } core.cron.Start() - + logging.Infof("Hello Word") ch := make(chan os.Signal, 1) signal.Notify(ch, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGINT) <-ch diff --git a/go.mod b/go.mod index 241ec2e..39a3b19 100644 --- a/go.mod +++ b/go.mod @@ -4,13 +4,15 @@ go 1.19 require ( github.com/go-resty/resty/v2 v2.7.0 + github.com/google/uuid v1.3.0 github.com/mitchellh/go-homedir v1.1.0 github.com/pkg/errors v0.9.1 github.com/robfig/cron/v3 v3.0.0 - github.com/sashabaranov/go-gpt3 v1.1.0 + github.com/sashabaranov/go-gpt3 v1.3.1 github.com/stretchr/testify v1.8.0 github.com/urfave/cli/v2 v2.24.4 go.uber.org/zap v1.24.0 + gopkg.in/yaml.v3 v3.0.1 gorm.io/driver/sqlite v1.4.3 gorm.io/gen v0.3.21 gorm.io/gorm v1.24.5 @@ -21,7 +23,6 @@ require ( github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/go-sql-driver/mysql v1.7.0 // indirect - github.com/google/uuid v1.3.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect github.com/mattn/go-sqlite3 v1.14.15 // indirect @@ -34,8 +35,6 @@ require ( golang.org/x/net v0.6.0 // indirect golang.org/x/sys v0.5.0 // indirect golang.org/x/tools v0.6.0 // indirect - gopkg.in/yaml.v2 v2.4.0 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect gorm.io/datatypes v1.1.0 // indirect gorm.io/driver/mysql v1.4.6 // indirect gorm.io/hints v1.1.1 // indirect diff --git a/go.sum b/go.sum index 6e61d0f..79d4007 100644 --- a/go.sum +++ b/go.sum @@ -39,8 +39,8 @@ github.com/robfig/cron/v3 v3.0.0 h1:kQ6Cb7aHOHTSzNVNEhmp8EcWKLb4CbiMW9h9VyIhO4E= github.com/robfig/cron/v3 v3.0.0/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/sashabaranov/go-gpt3 v1.1.0 h1:94w3N8Sx5VBSLAXFm4+dWrxWR4h1ID1wUftChJ0Ji5A= -github.com/sashabaranov/go-gpt3 v1.1.0/go.mod h1:BIZdbwdzxZbCrcKGMGH6u2eyGe1xFuX9Anmh3tCP8lQ= +github.com/sashabaranov/go-gpt3 v1.3.1 h1:ACQOAVX5CAV5rHt0oJOBMKo9BNcqVnmxEdjVxcjVAzw= +github.com/sashabaranov/go-gpt3 v1.3.1/go.mod h1:BIZdbwdzxZbCrcKGMGH6u2eyGe1xFuX9Anmh3tCP8lQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= @@ -77,8 +77,6 @@ golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= 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= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=