Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
reugn committed Jun 12, 2020
0 parents commit 988d50e
Show file tree
Hide file tree
Showing 10 changed files with 574 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea/
.vscode/
fsweeper
conf.yaml
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

The MIT License (MIT)

Copyright (c) 2020 reugn

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
60 changes: 60 additions & 0 deletions http/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package http

import (
"fmt"
"io/ioutil"
"net/http"
"strconv"

"github.com/reugn/fsweeper/rules"
)

// StartHTTPServer starts HTTP listener
func StartHTTPServer(host string, port int) {
addr := host + ":" + strconv.Itoa(port)

http.HandleFunc("/", rootActionHandler)

// executes configuration
http.HandleFunc("/execute", executeActionHandler)

// writes rules to the default configuration file
http.HandleFunc("/config", configActionHandler)

http.ListenAndServe(addr, nil)
}

func rootActionHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
w.WriteHeader(http.StatusNotFound)
}
fmt.Fprintf(w, "")
}

func executeActionHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
config := rules.ReadConfig()
config.Execute()
case "POST":
body, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
}
config := rules.ReadConfigFromByteArray(body)
config.Execute()
default:
w.WriteHeader(http.StatusBadRequest)
}
}

func configActionHandler(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
}
err = ioutil.WriteFile(rules.GetDefaultConfigFile(), body, 0644)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
}
102 changes: 102 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package main

import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"time"

"github.com/reugn/fsweeper/http"
"github.com/reugn/fsweeper/rules"
)

var version = "0.1.0"

var (
configFile = flag.String("conf", rules.DefaultConfigFile, "Configuration file path")
httpParam = flag.Bool("http", false, "Run in a HTTP mode")
serverHostParam = flag.String("host", "0.0.0.0", "HTTP Server host")
serverPortParam = flag.Int("port", 8081, "HTTP Server port")

configureParam = flag.Bool("configure", false, "Open default configuration file in $EDITOR")
versionParam = flag.Bool("version", false, "Show version")
filtersParam = flag.Bool("filters", false, "Show supported filters")
actionsParam = flag.Bool("actions", false, "Show supported actions")
)

func main() {
flag.Parse()

if *configureParam {
openFileInEditor(rules.GetDefaultConfigFile())
return
}

if rt := handleInfoFlags(); rt {
return
}

if *httpParam {
log.Printf("Starting HTTP server on port %d...\n", *serverPortParam)
http.StartHTTPServer(*serverHostParam, *serverPortParam)
} else {
// read configuration file
config := rules.ReadConfigFromFile(*configFile)

// execute rules
log.Println("Starting execute rules...")
start := time.Now()
config.Execute()

log.Printf("Done in %v.\n", time.Since(start))
}
}

func handleInfoFlags() bool {
var rt bool

if *versionParam {
fmt.Println("Version: " + version)
rt = true
}

if *filtersParam {
fmt.Println("Filters:")
for _, filter := range rules.Filters {
fmt.Printf("\t- %s\n", filter)
}
rt = true
}

if *actionsParam {
fmt.Println("Actions:")
for _, action := range rules.Actions {
fmt.Printf("\t- %s\n", action)
}
rt = true
}

return rt
}

func openFileInEditor(filename string) error {
editor := os.Getenv("EDITOR")
if editor == "" {
editor = "vi"
}

// get the full executable path for the editor
executable, err := exec.LookPath(editor)
if err != nil {
return err
}

cmd := exec.Command(executable, filename)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

return cmd.Run()
}
6 changes: 6 additions & 0 deletions ospkg/linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// +build !windows

package ospkg

// PathSeparator for a Unix-like os
const PathSeparator = "/"
6 changes: 6 additions & 0 deletions ospkg/windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// +build windows

package ospkg

// PathSeparator for Windows
const PathSeparator = "\\"
47 changes: 47 additions & 0 deletions rules/actions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package rules

import (
"log"
"os"
"path"
"time"
)

// Action to perform on a file
type Action struct {
Action string `yaml:"action"`
Payload string `yaml:"payload"`
}

func (action *Action) echoAction(filePath string) {
log.Printf("[%s] %s\n", filePath, action.Payload)
}

func (action *Action) touchAction(filePath string) {
now := time.Now().Local()
err := os.Chtimes(filePath, now, now)
if err != nil {
log.Fatalf("Failed to touch file: %s", filePath)
}
}

func (action *Action) moveAction(filePath string) {
err := os.Rename(filePath, action.Payload)
if err != nil {
log.Fatalf("Failed to move file: %s to: %s", filePath, action.Payload)
}
}

func (action *Action) renameAction(filePath string) {
err := os.Rename(filePath, validatePath(path.Dir(filePath))+action.Payload)
if err != nil {
log.Fatalf("Failed to rename file: %s to: %s", filePath, action.Payload)
}
}

func (action *Action) deleteAction(filePath string) {
err := os.Remove(filePath)
if err != nil {
log.Fatalf("Failed to remove file: %s", filePath)
}
}
Loading

0 comments on commit 988d50e

Please sign in to comment.