|
@ -8,6 +8,7 @@ import ( |
|
|
"encoding/base64" |
|
|
"encoding/base64" |
|
|
"fmt" |
|
|
"fmt" |
|
|
"image/png" |
|
|
"image/png" |
|
|
|
|
|
"io" |
|
|
"io/fs" |
|
|
"io/fs" |
|
|
"io/ioutil" |
|
|
"io/ioutil" |
|
|
"net/http" |
|
|
"net/http" |
|
@ -17,6 +18,7 @@ import ( |
|
|
"path/filepath" |
|
|
"path/filepath" |
|
|
"sort" |
|
|
"sort" |
|
|
"strings" |
|
|
"strings" |
|
|
|
|
|
"time" |
|
|
|
|
|
|
|
|
logrusStack "github.com/Gurpartap/logrus-stack" |
|
|
logrusStack "github.com/Gurpartap/logrus-stack" |
|
|
"github.com/barasher/go-exiftool" |
|
|
"github.com/barasher/go-exiftool" |
|
@ -178,7 +180,19 @@ Example: |
|
|
if c.Bool("debug") { |
|
|
if c.Bool("debug") { |
|
|
logrus.SetLevel(logrus.DebugLevel) |
|
|
logrus.SetLevel(logrus.DebugLevel) |
|
|
logrus.SetFormatter(&logrus.TextFormatter{}) |
|
|
logrus.SetFormatter(&logrus.TextFormatter{}) |
|
|
logrus.SetOutput(os.Stderr) |
|
|
|
|
|
|
|
|
logFile, err := getLogFile() |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
logrus.Fatalf("unable to set up a log file, saw: %s", err) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
logrus.SetOutput(io.MultiWriter(os.Stderr, logFile)) |
|
|
|
|
|
logrus.RegisterExitHandler(func() { |
|
|
|
|
|
if logFile == nil { |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
logFile.Close() |
|
|
|
|
|
}) |
|
|
logrus.AddHook(logrusStack.StandardHook()) |
|
|
logrus.AddHook(logrusStack.StandardHook()) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -190,6 +204,8 @@ Example: |
|
|
logrus.Fatal(err) |
|
|
logrus.Fatal(err) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
logrus.Debugf("selecting %s as distribusi root", root) |
|
|
|
|
|
|
|
|
var ignore []string |
|
|
var ignore []string |
|
|
if c.String("ignore") != "" { |
|
|
if c.String("ignore") != "" { |
|
|
ignore = strings.Split(c.String("ignore"), ",") |
|
|
ignore = strings.Split(c.String("ignore"), ",") |
|
@ -217,21 +233,25 @@ Example: |
|
|
exiftoolInstalled = false |
|
|
exiftoolInstalled = false |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
logrus.Debugf("selecting %s as distribusi root", root) |
|
|
|
|
|
|
|
|
|
|
|
ch := make(chan error, 2) |
|
|
ch := make(chan error, 2) |
|
|
go func() { |
|
|
go func() { |
|
|
|
|
|
logrus.Debug("distribuis commencing generation...") |
|
|
|
|
|
|
|
|
if err := distribusify(c, root, ignore); err != nil { |
|
|
if err := distribusify(c, root, ignore); err != nil { |
|
|
ch <- err |
|
|
ch <- err |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
logrus.Debug("distribusi finished generation!") |
|
|
|
|
|
|
|
|
ch <- nil |
|
|
ch <- nil |
|
|
return |
|
|
return |
|
|
}() |
|
|
}() |
|
|
|
|
|
|
|
|
if c.Bool("serve") { |
|
|
if c.Bool("serve") { |
|
|
go func() { |
|
|
go func() { |
|
|
|
|
|
logrus.Debug("starting up web server") |
|
|
|
|
|
|
|
|
if err := serveHTTP(root); err != nil { |
|
|
if err := serveHTTP(root); err != nil { |
|
|
ch <- err |
|
|
ch <- err |
|
|
return |
|
|
return |
|
@ -283,7 +303,8 @@ func distribusify(c *cli.Context, root string, ignore []string) error { |
|
|
|
|
|
|
|
|
absPath, err := filepath.Abs(fpath) |
|
|
absPath, err := filepath.Abs(fpath) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return err |
|
|
logrus.Debugf("unable to read %s", absPath) |
|
|
|
|
|
return nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
contents, err := ioutil.ReadDir(absPath) |
|
|
contents, err := ioutil.ReadDir(absPath) |
|
@ -297,15 +318,19 @@ func distribusify(c *cli.Context, root string, ignore []string) error { |
|
|
dirs = append(dirs, path.Join(absPath, content.Name())) |
|
|
dirs = append(dirs, path.Join(absPath, content.Name())) |
|
|
} else { |
|
|
} else { |
|
|
if content.Name() == "index.html" { |
|
|
if content.Name() == "index.html" { |
|
|
file, err := os.ReadFile(path.Join(absPath, content.Name())) |
|
|
indexPath := path.Join(absPath, content.Name()) |
|
|
|
|
|
file, err := os.ReadFile(indexPath) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
logrus.Debugf("unable to read %s, skipping", content.Name()) |
|
|
logrus.Debugf("unable to read %s, skipping", content.Name()) |
|
|
continue |
|
|
continue |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if strings.Contains(string(file), generatedInDistribusi) { |
|
|
if strings.Contains(string(file), generatedInDistribusi) { |
|
|
|
|
|
logrus.Debugf("%s was not generated by distribusi, skipping", indexPath) |
|
|
continue |
|
|
continue |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
files = append(files, path.Join(absPath, content.Name())) |
|
|
files = append(files, path.Join(absPath, content.Name())) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -477,6 +502,12 @@ func getCaption(c *cli.Context, fpath string) (string, error) { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if caption != "" { |
|
|
|
|
|
logrus.Debug("retrieved caption %s from %s", caption, fpath) |
|
|
|
|
|
} else { |
|
|
|
|
|
logrus.Debug("no comment retrieved for %s", fpath) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return caption, nil |
|
|
return caption, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -569,6 +600,8 @@ func getHref(c *cli.Context, fpath string, mtype string) (bool, string, error) { |
|
|
href = fmt.Sprintf("<a class='%s' href=\"%s\">%s</a>", stype, fname, fname) |
|
|
href = fmt.Sprintf("<a class='%s' href=\"%s\">%s</a>", stype, fname, fname) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
logrus.Debugf("generated href for %s: %s", fname, href) |
|
|
|
|
|
|
|
|
return unknown, href, nil |
|
|
return unknown, href, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -594,6 +627,8 @@ func mkDiv(c *cli.Context, mtype string, href, fname string, unknown bool) (stri |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
logrus.Debugf("generated div wrapper for %s: %s", fname, div) |
|
|
|
|
|
|
|
|
return div, nil |
|
|
return div, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -613,6 +648,8 @@ func writeIndex(fpath string, html []string, styles string) error { |
|
|
return nil |
|
|
return nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
logrus.Debugf("loading custom styles from %s", absPath) |
|
|
|
|
|
|
|
|
body = fmt.Sprintf(htmlBody, generatedInDistribusi, contents, strings.Join(html, "\n")) |
|
|
body = fmt.Sprintf(htmlBody, generatedInDistribusi, contents, strings.Join(html, "\n")) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -670,6 +707,7 @@ func wipeGeneratedFiles(dir string) error { |
|
|
if err := removeIndex(fpath); err != nil { |
|
|
if err := removeIndex(fpath); err != nil { |
|
|
return err |
|
|
return err |
|
|
} |
|
|
} |
|
|
|
|
|
logrus.Debugf("wiping %s as requested", fpath) |
|
|
} |
|
|
} |
|
|
return nil |
|
|
return nil |
|
|
}); err != nil { |
|
|
}); err != nil { |
|
@ -704,3 +742,19 @@ func shouldSkip(c *cli.Context, fpath string, ignore []string) (bool, error) { |
|
|
|
|
|
|
|
|
return false, nil |
|
|
return false, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// getLogFile creates a new log file for debug output.
|
|
|
|
|
|
func getLogFile() (*os.File, error) { |
|
|
|
|
|
cTime := time.Now() |
|
|
|
|
|
timeNow := fmt.Sprintf("%v-%v-%v", cTime.Hour(), cTime.Minute(), cTime.Second()) |
|
|
|
|
|
prefix := fmt.Sprintf("distribusi-go-%s", timeNow) |
|
|
|
|
|
|
|
|
|
|
|
file, err := ioutil.TempFile("/tmp", prefix) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return nil, err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
logrus.Debugf("creating %s as debug log file", file.Name()) |
|
|
|
|
|
|
|
|
|
|
|
return file, nil |
|
|
|
|
|
} |
|
|