Newer
Older
sisyphe-go / main.go
@Nacim Nacim on 6 Feb 2022 1 KB add log
package main

import (
	"fmt"
	"log"
	"os"
	"path/filepath"
	"sync"
	"time"

	"github.com/gabriel-vasile/mimetype"
	"github.com/sirupsen/logrus"
)

type Message struct {
	corpusname string
	startAt    string
	extension  string
	path       string
	mimetype   string
	size       int64
}

var wg sync.WaitGroup // instanciation de notre structure WaitGroup
var numberFiles int = 0

func logger(message *Message) {
	logrus.WithFields(logrus.Fields{
		"corpusname": message.corpusname,
		"startAt":    message.startAt,
		"extension":  message.extension,
		"path":       message.path,
		"mimetype":   message.mimetype,
		"size":       message.size,
	}).Info("")
}

func getAllFiles(dir string) {
	defer wg.Done()

	visit := func(path string, file os.FileInfo, err error) error {
		if file.IsDir() && path != dir {
			wg.Add(1)
			go getAllFiles(path)
			return filepath.SkipDir
		}
		if file.Mode().IsRegular() {
			numberFiles++
			fmt.Printf("\rFiles processed: %d", numberFiles)
			mtype, err := mimetype.DetectFile(path)
			absolutePath, err2 := filepath.Abs(path)
			if err == nil && err2 == nil {
				logger(&Message{
					corpusname: file.Name(),
					startAt:    file.ModTime().String(),
					extension:  mtype.Extension(),
					path:       absolutePath,
					mimetype:   mtype.String(),
					size:       file.Size(),
				})
			}

		}

		return nil
	}

	filepath.Walk(dir, visit)
}

func initProcess() {
	// init logger
	f, err := os.OpenFile("./file.json", os.O_WRONLY|os.O_CREATE, 0755)
	if err != nil {
		panic(err)
	}
	logrus.SetFormatter(&logrus.JSONFormatter{})
	logrus.SetOutput(f)
}

func main() {
	start := time.Now()
	
	initProcess()
	// read all files
	log.Println("Read corpus in", os.Args[1])
	root := os.Args[1]
	wg.Add(1)
	getAllFiles(root)
	wg.Wait()
	elapsed := time.Since(start)
	fmt.Println("")
	log.Printf("Total time %s", elapsed)
}