make cmd buildable by exporting more functions in the package

master
Carlo Strub 7 years ago
parent 95eae62da6
commit 632f0d7ea3

@ -9,15 +9,11 @@ import (
"syscall"
"github.com/boltdb/bolt"
"github.com/carlostrub/sisyphus"
"github.com/fsnotify/fsnotify"
"github.com/urfave/cli"
)
const (
good = "0"
junk = "1"
)
func main() {
// Define App
@ -116,15 +112,15 @@ func main() {
log.Fatal("Sorry... only one Maildir supported as of today.")
}
CreateDirs(maildirPaths[0])
sisyphus.CreateDirs(maildirPaths[0])
mails, err := Index(maildirPaths[0])
mails, err := sisyphus.Index(maildirPaths[0])
if err != nil {
log.Fatal("Wrong path to Maildir")
}
// Open the database
db, err := openDB(maildirPaths[0])
db, err := sisyphus.OpenDB(maildirPaths[0])
if err != nil {
log.Fatal(err)
}
@ -146,13 +142,13 @@ func main() {
log.Print(err)
}
}
if string(v) == good && mails[i].Junk == true {
if string(v) == sisyphus.Good && mails[i].Junk == true {
err = mails[i].Learn(db)
if err != nil {
log.Print(err)
}
}
if string(v) == junk && mails[i].Junk == false {
if string(v) == sisyphus.Junk && mails[i].Junk == false {
err = mails[i].Learn(db)
if err != nil {
log.Print(err)
@ -176,7 +172,7 @@ func main() {
case event := <-watcher.Events:
if event.Op&fsnotify.Create == fsnotify.Create {
mailName := strings.Split(event.Name, "/")
m := Mail{
m := sisyphus.Mail{
Key: mailName[len(mailName)-1],
}
@ -223,7 +219,7 @@ func main() {
Usage: "start sisyphus daemon in the background",
Action: func(c *cli.Context) error {
err := daemonStart(*pidfile)
err := sisyphus.DaemonStart(*pidfile)
if err != nil {
log.Fatal(err)
}
@ -237,7 +233,7 @@ func main() {
Usage: "stop sisyphus daemon",
Action: func(c *cli.Context) error {
err := daemonStop(*pidfile)
err := sisyphus.DaemonStop(*pidfile)
if err != nil {
log.Fatal(err)
}
@ -251,7 +247,7 @@ func main() {
Usage: "restart sisyphus daemon",
Action: func(c *cli.Context) error {
err := daemonRestart(*pidfile)
err := sisyphus.DaemonRestart(*pidfile)
if err != nil {
log.Fatal(err)
}

@ -32,7 +32,8 @@ func savePID(pidfile string, p int) error {
return nil
}
func daemonStart(pidfile string) error {
// DaemonStart starts sisyphus as a backgound process
func DaemonStart(pidfile string) error {
// check if daemon already running.
if _, err := os.Stat(pidfile); err == nil {
return errors.New("sisyphus running or " + pidfile + " file exists.")
@ -50,7 +51,8 @@ func daemonStart(pidfile string) error {
return nil
}
func daemonStop(pidfile string) error {
// DaemonStop stops a running sisyphus background process
func DaemonStop(pidfile string) error {
_, err := os.Stat(pidfile)
if err != nil {
@ -92,7 +94,8 @@ func daemonStop(pidfile string) error {
return nil
}
func daemonRestart(pidfile string) error {
// DaemonRestart restarts a running sisyphus background process
func DaemonRestart(pidfile string) error {
_, err := os.Stat(pidfile)
if err != nil {
return errors.New("sisyphus is not running")

@ -6,8 +6,8 @@ import (
"github.com/boltdb/bolt"
)
// openDB creates and opens a new database and its respective buckets (if required)
func openDB(maildir string) (db *bolt.DB, err error) {
// OpenDB creates and opens a new database and its respective buckets (if required)
func OpenDB(maildir string) (db *bolt.DB, err error) {
log.Println("loading database")
// Open the sisyphus.db data file in your current directory.

@ -16,6 +16,13 @@ import (
"github.com/luksen/maildir"
)
const (
// Good holds a placeholder string for the database
Good = "0"
// Junk holds a placeholder string for the database
Junk = "1"
)
// Mail includes the key of a mail in Maildir
type Mail struct {
Key string
@ -251,12 +258,12 @@ func (m *Mail) Classify(db *bolt.DB) error {
b := tx.Bucket([]byte("Processed"))
bMails := b.Bucket([]byte("Mails"))
if ju {
err := bMails.Put([]byte(m.Key), []byte(junk))
err := bMails.Put([]byte(m.Key), []byte(Junk))
if err != nil {
return err
}
} else {
err := bMails.Put([]byte(m.Key), []byte(good))
err := bMails.Put([]byte(m.Key), []byte(Good))
if err != nil {
return err
}

Loading…
Cancel
Save