commit 32d1cf26d66c427640edd42c578a9b27378c7081 Author: gari Date: Tue Nov 30 14:58:33 2021 +0100 first commit diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..17d6464 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use_nix \ No newline at end of file diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..8feef40 --- /dev/null +++ b/default.nix @@ -0,0 +1,6 @@ +with import {}; +mkShell { + nativeBuildInputs = [ + go_1_17 + ]; +} \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..8aa5a15 --- /dev/null +++ b/go.mod @@ -0,0 +1,9 @@ +module git.entr0py.de/garionion/murphy + +go 1.16 + +require ( + github.com/karrick/godirwalk v1.16.1 // indirect + github.com/mehdioa/nlog v0.0.0-20210327090009-d60bf476a16a + github.com/oriser/regroup v0.0.0-20210730155327-fca8d7531263 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..66fb477 --- /dev/null +++ b/go.sum @@ -0,0 +1,18 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/karrick/godirwalk v1.16.1 h1:DynhcF+bztK8gooS0+NDJFrdNZjJ3gzVzC545UNA9iw= +github.com/karrick/godirwalk v1.16.1/go.mod h1:j4mkqPuvaLI8mp1DroR3P6ad7cyYd4c1qeJ3RV7ULlk= +github.com/mehdioa/nlog v0.0.0-20210327090009-d60bf476a16a h1:p/otnVjZSfec48nsELzyGDwoAMtD+xoG8a6c5TW8Fuw= +github.com/mehdioa/nlog v0.0.0-20210327090009-d60bf476a16a/go.mod h1:Co+vI+QcF2CG+gvNdG+kvzh2f9EE+YiF0OSdWoAmlOU= +github.com/oriser/regroup v0.0.0-20210730155327-fca8d7531263 h1:Qd1Ml+uEhpesT8Og0ysEhu5+DGhbhW+qxjapH8t1Kvs= +github.com/oriser/regroup v0.0.0-20210730155327-fca8d7531263/go.mod h1:odkMeLkWS8G6+WP2z3Pn2vkzhPSvBtFhAUYTKXAtZMQ= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go new file mode 100644 index 0000000..f7503a1 --- /dev/null +++ b/main.go @@ -0,0 +1,21 @@ +package main + +import ( + "git.entr0py.de/garionion/murphy/scanner" + "git.entr0py.de/garionion/murphy/store" + "github.com/mehdioa/nlog" +) + +var log *nlog.Logger + +const source = "/mnt/clarkson" + + +func main() { + formatter := nlog.NewTextFormatter(true, true) + log = nlog.NewLogger(nlog.DebugLevel, formatter) + + s := store.NewStore(log) + + scanner.InitScanner(s, source) +} diff --git a/scanner/scanner.go b/scanner/scanner.go new file mode 100644 index 0000000..4428ddf --- /dev/null +++ b/scanner/scanner.go @@ -0,0 +1,100 @@ +package scanner + +import ( + "fmt" + store "git.entr0py.de/garionion/murphy/store" + "github.com/karrick/godirwalk" + "github.com/mehdioa/nlog" + "github.com/oriser/regroup" + "io/ioutil" + "path" + "strings" + "time" +) + +type Scanner struct { + TargetPath string + LastChange time.Time + Created time.Time + ProjectName string + Logger *nlog.Logger +} + +func NewScanner(target, projectName string, createdAt time.Time, logger *nlog.Logger) *Scanner { + projectLogger := logger.New("Scanner", nlog.Data{"Project": projectName,}) + scanner := &Scanner{TargetPath: target, Created: createdAt, ProjectName: projectName, Logger: logger} + projectLogger.Infof("Scanning %s", projectName) + err := godirwalk.Walk(target, &godirwalk.Options{ + Callback: func(osPathname string, de *godirwalk.Dirent) error { + // Following string operation is not most performant way + // of doing this, but common enough to warrant a simple + // example here: + if strings.Contains(osPathname, ".git") { + return godirwalk.SkipThis + } + fmt.Printf("%s %s\n", de.ModeType(), osPathname) + return nil + }, + Unsorted: true, // (optional) set true for faster yet non-deterministic enumeration (see godoc) + }) + if err != nil { + projectLogger.Errorf("Error scanning %s: %s", projectName, err) + } + return scanner +} + + +func InitScanner(s *store.Store, sourcePath string) { + s.Logger.Infof("Initializing scanner") + scanners := map[string]*Scanner{} + RootScanner(sourcePath, scanners, s.Logger) +} + +func RootScanner(rootPath string, scanners map[string]*Scanner, logger *nlog.Logger) { + resorts, err := ioutil.ReadDir(rootPath) + if err != nil { + logger.Errorf("Error reading directory %s: %v", rootPath, err) + return + } + re := regroup.MustCompile(`(?P\d{4}-\d{2}-\d{2})_(?P.*$)`) + for _, r := range resorts { + if r.IsDir() && r.Name()[0] != '.' { + resort := path.Join(rootPath, r.Name()) + logger.Debugf("Found directory: %s", resort) + projects, err := ioutil.ReadDir(resort) + if err != nil { + logger.Errorf("Error reading directory %s: %v", projects, err) + return + } + for _, p := range projects { + if p.IsDir() { + _, mapContainsProject := scanners[p.Name()] + if mapContainsProject { + continue + } + projectPath := path.Join(resort, p.Name()) + matches, err := re.Groups(p.Name()) + if err != nil { + logger.Debugf("Failed to Match %s: %v", p.Name(), err) + continue + } + logger.Debugf("Matches: %+v", matches) + if len(matches) != 0 { + createdAtString := matches["date"] + projectName := matches["name"] + logger.Infof("Found project: %s", projectName) + + createdAt, err := time.Parse("2006-01-02", createdAtString) + if err != nil { + logger.Errorf("Failed to parse date %s: %v, skipping %s", createdAtString, err, p.Name()) + } + scanner := NewScanner(projectPath, projectName, createdAt, logger) + scanners[p.Name()] = scanner + } else { + logger.Debugf("Ignoring Directory %s", projectPath) + } + } + } + } + } +} diff --git a/store/store.go b/store/store.go new file mode 100644 index 0000000..6f43305 --- /dev/null +++ b/store/store.go @@ -0,0 +1,19 @@ +package store + +import ( + "github.com/mehdioa/nlog" + "time" +) + +type Store struct { + TargetPath []string + SourcePath []string + GracePeriod time.Duration + Logger *nlog.Logger +} + +func NewStore(Logger *nlog.Logger) *Store{ + store := &Store{Logger: Logger} + + return store +}