Skip to content

Commit

Permalink
pause
Browse files Browse the repository at this point in the history
  • Loading branch information
hi20160616 committed Sep 2, 2021
1 parent 591e060 commit 196845c
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 114 deletions.
218 changes: 112 additions & 106 deletions configs/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,23 @@ import (
"log"
"os"
"path/filepath"
"runtime"
"strings"
"time"

"github.com/pkg/errors"

"golang.org/x/sys/windows/registry"
)

var (
ProjectName = "spamkiller"
V = &configuration{}
LogWriter = &os.File{}
LogWriter = &os.File{}
)

type configuration struct {
type Config struct {
ProjectName string
RootPath string
Raw []byte
Debug bool
Verbose bool // if true, prompt enter to exit.
LogName string
Folder string
Result string // to copy treated emls
DropDaysAgo int `json:"DropDaysAgo"`
Expand All @@ -32,123 +30,131 @@ type configuration struct {
Spams []string
Focuses []string
} `json:"filter"`
Err error
}

func setRootPath() error {
if runtime.GOOS == "Linux" {
root, err := os.Getwd()
if err != nil {
return err
}
V.RootPath = root
}
if runtime.GOOS == "windows" {
s, err := readKey()
if err != nil {
return err
}
V.RootPath = filepath.Dir(strings.ReplaceAll(s, "\"", ""))
}
if strings.Contains(os.Args[0], ".test") {
return rootPath4Test()
}
return nil
}

func readKey() (string, error) {
k, err := registry.OpenKey(registry.CLASSES_ROOT, `Directory\shell\SpamKiller\command`, registry.QUERY_VALUE)
if err != nil {
return "", err
func NewConfig(projectName string) (*Config, error) {
cfg := &Config{ProjectName: projectName}
cfg = setRootPath(cfg).load()
if cfg.Err != nil {
// log.Printf("configs load error: %v\n", cfg.Err)
return nil, cfg.Err
}
defer k.Close()

s, _, err := k.GetStringValue("")
if err != nil {
return "", err
if err := initLog(cfg); err != nil {
return nil, err
}
return strings.Split(s, " ")[0], nil
return cfg, nil
}

func load() error {
cf := filepath.Join(V.RootPath, "configs", "configs.json")
f, err := os.ReadFile(cf)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return errors.WithMessage(err, "ReadFile error: no configs.json")
} else {
return err
}
}
if err = json.Unmarshal(f, V); err != nil {
return errors.WithMessage(err, "Unmarshal configs.json error")
}

// Drop
V.Drop = time.Now().AddDate(0, 0, -V.DropDaysAgo)

// load focuses.json
fp := filepath.Join(V.RootPath, "configs", "focuses.json")
fJson, err := os.ReadFile(fp)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
log.Println("warning: no focuses.json")
} else {
return err
}
}
if err = json.Unmarshal(fJson, &V.Filter.Focuses); err != nil {
return errors.WithMessage(err, "Unmarshal focuses.json error")
}

// load spams.json
sp := filepath.Join(V.RootPath, "configs", "spams.json")
sJson, err := os.ReadFile(sp)
func initLog(cfg *Config) error {
LogWriter, err := os.OpenFile(
filepath.Join(cfg.RootPath, cfg.LogName),
os.O_CREATE|os.O_APPEND|os.O_RDWR, 0755)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
log.Println("warning: no spams.json")
} else {
return errors.WithMessage(err, "Unmarshal spams.json error")
}
}
if err = json.Unmarshal(sJson, &V.Filter.Spams); err != nil {
return err
}

// // write to configs.json
// data, err := json.MarshalIndent(V, "", " ")
// if err != nil {
// return err
// }
// return os.WriteFile(cf, data, 0755)
log.SetOutput(LogWriter)
return nil
}

func init() {
if err := setRootPath(); err != nil {
log.Printf("configs init error: %v\n", err)
}
if err := load(); err != nil {
log.Printf("configs load error: %v\n", err)
}
LogWriter, err := os.OpenFile(filepath.Join(V.RootPath, "log.txt"), os.O_CREATE|os.O_APPEND|os.O_RDWR, 0755)
if err != nil {
log.Println(err)
}
log.SetOutput(LogWriter)
}
// func setRootPath(cfg *Config) *Config {
// if runtime.GOOS == "Linux" {
// cfg.RootPath, cfg.Err = os.Getwd()
// if cfg.Err != nil {
// return cfg
// }
// }
// if runtime.GOOS == "windows" {
// cfg.RootPath, cfg.Err = readKey()
// if cfg.Err != nil {
// return cfg
// }
// cfg.RootPath = filepath.Dir(cfg.RootPath)
// }
// if strings.Contains(os.Args[0], ".test") {
// return rootPath4Test(cfg)
// }
// return cfg
// }
//
// func readKey() (string, error) {
// k, err := registry.OpenKey(registry.CLASSES_ROOT, `Directory\shell\SpamKiller\command`, registry.QUERY_VALUE)
// if err != nil {
// return "", err
// }
// defer k.Close()
//
// s, _, err := k.GetStringValue("")
// if err != nil {
// return "", err
// }
// s = strings.Split(s, " ")[0]
// return strings.ReplaceAll(s, "\"", ""), nil
// }

func rootPath4Test() error {
root, err := os.Getwd()
if err != nil {
return err
func rootPath4Test(cfg *Config) *Config {
cfg.RootPath, cfg.Err = os.Getwd()
if cfg.Err != nil {
return cfg
}
ps := strings.Split(root, ProjectName)
ps := strings.Split(cfg.RootPath, cfg.ProjectName)
n := 0
if len(ps) > 1 {
n = strings.Count(ps[1], string(os.PathSeparator))
}
for i := 0; i < n; i++ {
V.RootPath = filepath.Join("../", "./")
cfg.RootPath = filepath.Join("../", "./")
}
return nil
return cfg
}

func (c *Config) load() *Config {
if c.Err != nil {
return c
}
cfgFile := filepath.Join(c.RootPath, "configs", "configs.json")
c.Raw, c.Err = os.ReadFile(cfgFile)
if c.Err != nil {
if errors.Is(c.Err, os.ErrNotExist) {
c.Err = errors.WithMessage(c.Err, "ReadFile error: no configs.json")
}
return c
}
cfgTemp := &Config{}
if c.Err = json.Unmarshal(c.Raw, cfgTemp); c.Err != nil {
c.Err = errors.WithMessage(c.Err, "Unmarshal configs.json error")
return c
}
c.Debug = cfgTemp.Debug
c.Verbose = cfgTemp.Verbose
c.Folder = cfgTemp.Folder
c.Result = cfgTemp.Result
c.DropDaysAgo = cfgTemp.DropDaysAgo

// Drop
c.Drop = time.Now().AddDate(0, 0, -c.DropDaysAgo)

// load *.json
loadJson := func(filename string) ([]string, error) {
fp := filepath.Join(c.RootPath, "configs", filename)
fJson, err := os.ReadFile(fp)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
log.Println("warning: no ", filename)
} else {
return nil, err
}
}
keywords := []string{}
if err = json.Unmarshal(fJson, &keywords); err != nil {
return nil, errors.WithMessagef(err, "Unmarshal %s error", filename)
}
return keywords, nil
}

// load focuses.json
c.Filter.Focuses, c.Err = loadJson("focuses.json")
// load spams.json
c.Filter.Spams, c.Err = loadJson("spams.json")
return c
}
1 change: 1 addition & 0 deletions configs/configs.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"Debug": false,
"Verbose": false,
"LogName":"log.txt",
"Folder": "./",
"Result": "./",
"DropDaysAgo": 2
Expand Down
16 changes: 9 additions & 7 deletions configs/configs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@ import (
)

func TestLoad(t *testing.T) {
if err := load(); err != nil {
cfg := &Config{}
if err := setRootPath(cfg).load(); err != nil {
t.Error(err)
}
fmt.Println(V.Drop)
for _, e := range V.Filter.Spams {
fmt.Println(cfg.Drop)
for _, e := range cfg.Filter.Spams {
fmt.Println(e)
}
for _, e := range V.Filter.Focuses {
for _, e := range cfg.Filter.Focuses {
fmt.Println(e)
}
}

func TestRootPath4Test(t *testing.T) {
if err := rootPath4Test(); err != nil {
t.Error(err)
cfg := rootPath4Test(&Config{})
if cfg.Err != nil {
t.Error(cfg.Err)
}
fmt.Println(V.RootPath)
fmt.Println(cfg.RootPath)
}
17 changes: 17 additions & 0 deletions configs/linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package configs

import (
"os"
"strings"
)

func setRootPath(cfg *Config) *Config {
cfg.RootPath, cfg.Err = os.Getwd()
if cfg.Err != nil {
return cfg
}
if strings.Contains(os.Args[0], ".test") {
return rootPath4Test(cfg)
}
return cfg
}
38 changes: 38 additions & 0 deletions configs/windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//go:build windows

package configs

import (
"os"
"path/filepath"
"strings"

"golang.org/x/sys/windows/registry"
)

func setRootPath(cfg *Config) *Config {
cfg.RootPath, cfg.Err = readKey()
if cfg.Err != nil {
return cfg
}
cfg.RootPath = filepath.Dir(cfg.RootPath)
if strings.Contains(os.Args[0], ".test") {
return rootPath4Test(cfg)
}
return cfg
}

func readKey() (string, error) {
k, err := registry.OpenKey(registry.CLASSES_ROOT, `Directory\shell\SpamKiller\command`, registry.QUERY_VALUE)
if err != nil {
return "", err
}
defer k.Close()

s, _, err := k.GetStringValue("")
if err != nil {
return "", err
}
s = strings.Split(s, " ")[0]
return strings.ReplaceAll(s, "\"", ""), nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ go 1.16

require (
github.com/pkg/errors v0.9.1
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf
golang.org/x/sys v0.0.0-20210831042530-f4d43177bf5e
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf h1:2ucpDCmfkl8Bd/FsLtiD653Wf96cW37s+iGx93zsu4k=
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210831042530-f4d43177bf5e h1:XMgFehsDnnLGtjvjOfqWSUzt0alpTR1RSEuznObga2c=
golang.org/x/sys v0.0.0-20210831042530-f4d43177bf5e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

0 comments on commit 196845c

Please sign in to comment.