~kornellapacz/gmnigit

56adc53ede4856ebb365720d24d15d807833a253 — Korneliusz Ɓapacz 3 years ago 38fe8bd
optional show files permissions in tree
3 files changed, 38 insertions(+), 6 deletions(-)

M README.md
M files.go
M main.go
M README.md => README.md +2 -0
@@ 22,6 22,8 @@ place `gmnigit` binary somewhere in your `$PATH`.
Usage of gmnigit:
  -dist string
        destination path (if exists will be overwritten)
  -perms
        show files permissions in browsable tree (default false)
  -repo string
        path to repository
```

M files.go => files.go +32 -4
@@ 2,6 2,7 @@ package main

import (
	"fmt"
	"io/fs"
	"io/ioutil"
	"log"
	"os"


@@ 13,6 14,33 @@ import (
	"github.com/go-git/go-git/v5"
)

type fileType int

const (
	dir fileType = iota
	txt
	bin
)

func createLink(file fs.FileInfo, t fileType) string {
	var link, perm string

	switch t {
	case txt:
		link = "%s.txt%s %s"
	case dir:
		link = "%s/%s %s/"
	case bin:
		link = "%s%s %s"
	}

	if showPermissions {
		perm = " " + file.Mode().String()
	}

	return fmt.Sprintf("=> "+link+"\n", file.Name(), perm, file.Name())
}

func isBinary(detectedMIME *mimetype.MIME) bool {
	for mime := detectedMIME; mime != nil; mime = mime.Parent() {
		if mime.Is("text/plain") {


@@ 33,7 61,7 @@ func arrangeDirectory(root, path string) {

	for _, file := range files {
		if file.IsDir() {
			indexContent += fmt.Sprintf("=> %s/\n", file.Name())
			indexContent += createLink(file, dir)
			arrangeDirectory(root, filepath.Join(path, file.Name()))
			continue
		}


@@ 49,7 77,7 @@ func arrangeDirectory(root, path string) {
			symlink.WriteString("symlink to: " + dest)

			check(symlink.Close())
			indexContent += fmt.Sprintf("=> %s.txt %s\n", file.Name(), file.Name())
			indexContent += createLink(file, txt)
			continue
		}



@@ 57,12 85,12 @@ func arrangeDirectory(root, path string) {
		check(err)

		if isBinary(detectedMIME) {
			indexContent += fmt.Sprintf("=> %s\n", file.Name())
			indexContent += createLink(file, bin)
			continue
		}

		check(os.Rename(filePath, filePath+".txt"))
		indexContent += fmt.Sprintf("=> %s.txt %s\n", file.Name(), file.Name())
		indexContent += createLink(file, txt)
	}

	index, err := os.Create(filepath.Join(root, path, "index.gmi"))

M main.go => main.go +4 -2
@@ 17,13 17,15 @@ const (
)

var (
	repositoryPath string
	distPath       string
	repositoryPath  string
	distPath        string
	showPermissions bool
)

func main() {
	flag.StringVar(&repositoryPath, "repo", "", "path to repository")
	flag.StringVar(&distPath, "dist", "", "destination path (if exists will be overwritten)")
	flag.BoolVar(&showPermissions, "perms", false, "show files permissions in browsable tree")

	flag.Parse()