Skip to content

Commit 32c3042

Browse files
committed
feat: run once
1 parent 83f0fcb commit 32c3042

File tree

5 files changed

+55
-8
lines changed

5 files changed

+55
-8
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ vendor/
2626
# Dependency directories
2727
Godeps/
2828

29-
# Test output
29+
# output
3030
convert/test_output/
31+
build/

cmd/root.go

-5
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,8 @@ var rootCmd = &cobra.Command{
1212
Use: "provider2domainset",
1313
Short: "p2d, convert domain list form clash provider to mosdns domainset.",
1414
Long: `p2d, convert domain list form clash provider to mosdns domainset.`,
15-
// Uncomment the following line if your bare application
16-
// has an action associated with it:
17-
// Run: func(cmd *cobra.Command, args []string) { },
1815
}
1916

20-
// Execute adds all child commands to the root command and sets flags appropriately.
21-
// This is called by main.main(). It only needs to happen once to the rootCmd.
2217
func Execute() {
2318
err := rootCmd.Execute()
2419
if err != nil {

cmd/run.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package cmd
2+
3+
import (
4+
"github.com/dn-11/provider2domainset/conf"
5+
"github.com/dn-11/provider2domainset/convert"
6+
"github.com/spf13/cobra"
7+
)
8+
9+
var (
10+
config string
11+
)
12+
var runCmd = &cobra.Command{
13+
Use: "run",
14+
Short: "run convert once",
15+
Run: func(cmd *cobra.Command, args []string) {
16+
conf.Init(config)
17+
convert.RunOnce()
18+
},
19+
}
20+
21+
func init() {
22+
runCmd.PersistentFlags().StringVarP(&config, "config", "c", "config.yaml", "config file path")
23+
rootCmd.AddCommand(runCmd)
24+
}

conf/config.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,15 @@ func Init(file string) {
3333
if !os.IsNotExist(err) {
3434
log.L().Sugar().With(err).Errorf("get stat of %s failed", file)
3535
}
36-
log.L().Sugar().Fatalf("config not existed, creating at %s", file)
36+
log.L().Sugar().Errorf("config not existed, creating at %s", file)
3737
created, err := os.Create(file)
3838
if err != nil {
3939
log.L().Sugar().With(err).Errorf("create config at %s failed", file)
4040
}
4141
if _, err := created.Write(configSample); err != nil {
4242
log.L().Sugar().With(err).Errorf("write config at %s failed", file)
4343
}
44+
log.L().Fatal("config created, please edit it.")
4445
}
4546

4647
viper.SetConfigFile(file)
@@ -90,6 +91,9 @@ func update() {
9091
Service.Delay = viper.GetInt("service.delay")
9192

9293
if level, err := zapcore.ParseLevel(viper.GetString("log.level")); err == nil {
93-
Log.Level = level
94+
if log.L().Level.Level() == zapcore.InfoLevel {
95+
Log.Level = level
96+
log.L().SetLogLevel(level)
97+
}
9498
}
9599
}

convert/run.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package convert
2+
3+
import (
4+
"github.com/dn-11/provider2domainset/conf"
5+
"github.com/dn-11/provider2domainset/log"
6+
)
7+
8+
func RunOnce() {
9+
log.L().Info("start convert now")
10+
11+
for _, f := range conf.Convert.ProcessFiles {
12+
err := Convert(Task{
13+
Source: conf.Convert.Source,
14+
Target: conf.Convert.Target,
15+
File: f,
16+
})
17+
if err != nil {
18+
log.L().Sugar().With(err).Error("convert failed")
19+
}
20+
}
21+
22+
log.L().Info("convert done")
23+
}

0 commit comments

Comments
 (0)