Author: LakeFox
Email: [email protected]
Date: Sun, 20 Apr 2025 21:14:43 -0600
main.go
Delete
Commits
Diff
package main

import (
	"bytes"
	"flag"
	"fmt"
	"logc/email"
	"logc/flagtype"
	"logc/git"
	"logc/dir"
	"logc/output"
	"logc/search"
	"logc/tagc"
	"net/http"
	"os"
	"path/filepath"
	"strings"
	"text/template"
)

// !NOTE: The idea is source -> filter -> encode -> output
// + ./ (directory) -> only include issues -> parse the comments -> output text to the console
// + .git -> get the first five commits -> parse the git repo -> output as html and save the files as user/commitid/filepath
// + take all the files in a directory -> no filter -> add a template -> save as dirname.html
// + take directory and output as html folder structure or json or template

// !FUTURE: Add things like spreadsheets, rss, and databases (ip connections too) (try to replace retool) also email output
// + add sorting

func main() {

	// -src mail.privateemail.com:993 -username [email protected] -password 123qweASD
	path := flag.String("src", ".", "Source directory or file")
	username := flag.String("username", "", "Email login username")
	password := flag.String("password", "", "Email login password")
	mode := flag.String("mode", "tagc", "Set Content mode (\"git\", \"tagc\", \"email\", \"directory\")")

	var filters flagType.FlagType
	flag.Var(&filters, "filter", `Include entrees only matching provided the RegExp (--filter tags=/ISSUE/ --filter commit.email=/(\w)(\w*)(\w)@([^\s]+)/`)
	var removes flagType.FlagType
	var replaces flagType.FlagType
	flag.Var(&removes, "remove", "Remove fields from the output")
	flag.Var(&replaces, "replace", `RegExp to replace text within a field. Example: --replace commit.email=/(\w)(\w*)(\w)@([^\s]+)/${1}*${3}@${4}/ (censors emails it git commits) Optional: Import a replace file with a .rf extention Example: --replace commit.email=/path/to/email/regex.rf`)

	encoding := flag.String("encoding", "text", "Output encoding (json/xml/text/template file path)")

	outputPath := flag.String("output", "stdout", "Output path (must be templated when using encoding template) default stdout")
	cwd := flag.String("dir", ".", "Set the current directory")

	address := flag.String("address", ":6753", "HTTP Address to listen on")
	serve := flag.Bool("serve", false, "Start HTTP Server")

	flag.Parse()

	op := filepath.Join(*cwd, *outputPath)
	enc := strings.ToLower(*encoding)

	var tmpl *template.Template
	var err error
	if enc != "json" && enc != "text" && enc != "xml" {
		templatePath := filepath.Join(*cwd, enc)
		tmpl, err = template.ParseFiles(templatePath)

		if err != nil {
			panic(err)
		}
	}

	// !TODO: Make async so you don't need to store soo much in memory
	// add checks for errors
	

	handle := (func (data interface{}) {
		result := search.One(data, filters.Array(), removes.Array(), replaces.Array(), *cwd)

		var out string
		if enc == "json" {
			out = output.JSON(result)
		} else if enc == "text" {
			out = output.Text(result)
		} else if enc == "xml" {
			out = output.XML(result)
		} else {
			var f bytes.Buffer
			err := tmpl.Execute(&f, result)
			if err != nil {
				fmt.Println("Template path not found: ", filepath.Join(*cwd, enc))
				panic(err)
			}
			out = f.String()
		}

		if *outputPath == "stdout" {
			fmt.Println(out)
		} else {
			p := output.FormatPath(op, result)
			if _, err := os.Stat(filepath.Dir(p)); os.IsNotExist(err) {
				os.MkdirAll(filepath.Dir(p), 0700)
			}
			err := os.WriteFile(p, []byte(out), 0666)
			if err != nil {
				panic(err)
			}
		}
})



	if *mode == "tagc" {
		err := tagc.Read(*path, handle)
		if err != nil {
			panic(err)
		}


	} else if *mode == "git" {
		err := git.Read(*path, handle)
		if err != nil {
			panic(err)
		}

	} else if *mode == "email" {
		inbox := email.Connect(*path, *username, *password)
		inbox.GetMessages("INBOX")

		for _, v := range inbox.Threads {
			handle(v)
		}
		
		inbox.Client.Logout()
	} else if *mode == "directory" {
		err := dir.Read(*path, handle)
		if err != nil {
			panic(err)
		}
	} 

	
	if *serve {
		fs := http.FileServer(http.Dir(*cwd))
		http.Handle("/", fs)

		fmt.Println("Listening on:" + *address)
		fmt.Println("Serving: " + *cwd)
		err := http.ListenAndServe(*address, nil)
		if err != nil {
			panic(err)
		}
	}

	return
}