Skip to content

Commit

Permalink
Add conf flag for daemon
Browse files Browse the repository at this point in the history
  • Loading branch information
wuqinqiang committed Mar 2, 2023
1 parent 0eb15e1 commit 38b7385
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 26 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
build: win macos-adm64 linux macos-arm64


all:
$(shell sh build.sh)
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 2 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
8 changes: 7 additions & 1 deletion cmd/phrase.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
11 changes: 4 additions & 7 deletions collector/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down
26 changes: 22 additions & 4 deletions conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package conf

import (
_ "embed"
"io"
"os"

"gopkg.in/yaml.v3"

Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down

0 comments on commit 38b7385

Please sign in to comment.