21 Commits
v0.1 ... v1.3

Author SHA1 Message Date
bel
807072a77f Unittest and exec file proof on search 2019-12-01 14:16:45 -07:00
bel
081d50328f Fix no configured user 2019-12-01 13:25:49 -07:00
bel
4e2b7b3c85 Dont list hidden files and hopefully search files by ext only .md 2019-11-24 05:09:29 +00:00
Bel LaPointe
e4632d36ba Manual test versioning looks good 2019-11-21 14:17:45 -07:00
Bel LaPointe
fecd343f1b Impl versions with shell commands 2019-11-21 14:09:46 -07:00
Bel LaPointe
217a221cf4 2019-11-21 13:53:04.526575 -0700 MST m=+0.013733497 2019-11-21 13:53:04 -07:00
Bel LaPointe
b73a962556 Test notes 2019-11-21 13:39:03 -07:00
Bel LaPointe
c88fed0929 testing filetree 2019-11-21 13:26:07 -07:00
Bel LaPointe
a140d0eade Impl complete testing needed 2019-11-21 13:12:30 -07:00
Bel LaPointe
3079cd163f Fix absolute and relative without parent dir paths work 2019-11-21 12:28:30 -07:00
Bel LaPointe
9fc4e63a34 Crap absolute paths to public dont work 2019-11-14 15:01:23 -07:00
Bel LaPointe
1b9067a72d permissions 2019-11-14 14:57:30 -07:00
Bel LaPointe
e6f63a578f Fix pathing with relative non-single-depth root 2019-11-14 11:14:57 -07:00
Bel LaPointe
7964518d36 Testing versioning with git in go in versions 2019-11-12 14:48:28 -07:00
Bel LaPointe
259a8efc70 Impl FTS and some main tests 2019-11-10 07:54:42 -07:00
Bel LaPointe
60dc6bc876 Split into packages only manually tested 2019-11-09 18:51:03 -07:00
bel
0d497f4fa8 Fix nonroot path 2019-11-06 17:44:04 -07:00
bel
c6d43699ef TODO 2019-11-06 17:19:57 -07:00
Bel LaPointe
618b9ed513 Fix serving . but absolute and parents still cause bugs 2019-10-21 09:52:05 -06:00
bel
0595c49fc2 todo 2019-10-20 17:34:02 -06:00
bel
da6eaca26f Pass things around via query params 2019-10-20 16:39:26 -06:00
73 changed files with 1520 additions and 164 deletions

1
.gitignore vendored
View File

@@ -1,4 +1,5 @@
gollum gollum
public
**.sw* **.sw*
**/**.sw* **/**.sw*
*.sw* *.sw*

View File

@@ -1,11 +1,8 @@
FROM frolvlad/alpine-glibc:alpine-3.9_glibc-2.29
RUN apk update && apk add --no-cache ca-certificates git
FROM golang:1.13-alpine as certs
RUN apk update && apk add --no-cache ca-certificates
FROM busybox:glibc
RUN mkdir -p /var/log RUN mkdir -p /var/log
WORKDIR /main WORKDIR /main
COPY --from=certs /etc/ssl/certs /etc/ssl/certs
COPY . . COPY . .
@@ -13,4 +10,3 @@ ENV GOPATH=""
ENV MNT="/mnt/" ENV MNT="/mnt/"
ENTRYPOINT ["/main/exec-notes-server"] ENTRYPOINT ["/main/exec-notes-server"]
CMD [] CMD []

38
TODO
View File

@@ -1,7 +1,39 @@
x edit page x edit page
x create page x create page
x create dir x create dir
x main test -
- create,
- header
- text box
- submit
- submit target
- edit,
- header
- text box
- submit
- submit target
- dir,
- header
- create
- create target
- list
- note,
- header
- edit
- edit target
- content
- root-> dir,
- root->file,
- dir->dir,
- dir->file
TOC levels x TOC levels
delete pages x delete pages
search x search
FTS
https://stackoverflow.com/questions/26709971/could-this-be-more-efficient-in-go
x move auth as flag in router
x . and ../** as roots cause bugs in listing and loading and linking
x `create` at root is a 400, base= in URL (when `create` input is empty)
delete top-level pages
versioning

View File

@@ -8,6 +8,7 @@ import (
"log" "log"
"os" "os"
"strings" "strings"
"time"
) )
var ( var (
@@ -15,6 +16,8 @@ var (
Port string Port string
Head string Head string
Foot string Foot string
OAuthServer string
VersionInterval time.Duration
) )
func init() { func init() {
@@ -28,8 +31,10 @@ func Refresh() {
as := args.NewArgSet() as := args.NewArgSet()
as.Append(args.STRING, "root", "root dir path", "./public") as.Append(args.STRING, "root", "root dir path", "./public")
as.Append(args.STRING, "port", "port to listen on", "39909") as.Append(args.STRING, "port", "port to listen on", "49909")
as.Append(args.STRING, "wrap", "file with http header/footer", "./wrapper.html") as.Append(args.STRING, "wrap", "file with http header/footer", "./wrapper.html")
as.Append(args.STRING, "oauth", "oauth URL", "")
as.Append(args.DURATION, "version", "duration to mark versions", "0s")
if err := as.Parse(); err != nil { if err := as.Parse(); err != nil {
panic(err) panic(err)
} }
@@ -49,4 +54,6 @@ func Refresh() {
Port = ":" + strings.TrimPrefix(as.Get("port").GetString(), ":") Port = ":" + strings.TrimPrefix(as.Get("port").GetString(), ":")
Head = string(bs[0]) Head = string(bs[0])
Foot = string(bs[1]) Foot = string(bs[1])
OAuthServer = as.Get("oauth").GetString()
VersionInterval = as.Get("version").GetDuration()
} }

View File

@@ -1,4 +1,4 @@
package server package filetree
import ( import (
"os" "os"
@@ -7,7 +7,7 @@ import (
type Dirs []Path type Dirs []Path
func newDirs() *Dirs { func NewDirs() *Dirs {
d := Dirs([]Path{}) d := Dirs([]Path{})
return &d return &d
} }

28
filetree/dirs_test.go Executable file
View File

@@ -0,0 +1,28 @@
package filetree
import (
"local/notes-server/config"
"os"
"testing"
)
func TestDirs(t *testing.T) {
config.Root = "/"
dirs := NewDirs()
info, err := os.Stat("/usr/local")
if err != nil {
t.Fatal(err)
}
p := NewPathFromLocal("/usr")
dirs.Push(p, info)
if len([]Path(*dirs)) != 1 {
t.Error(dirs)
}
first := []Path(*dirs)[0]
if first.Base != "local" {
t.Error(first)
}
if first.Local != "/usr/local" {
t.Error(first)
}
}

View File

@@ -1,4 +1,4 @@
package server package filetree
import ( import (
"os" "os"
@@ -7,7 +7,7 @@ import (
type Files []Path type Files []Path
func newFiles() *Files { func NewFiles() *Files {
d := Files([]Path{}) d := Files([]Path{})
return &d return &d
} }

28
filetree/files_test.go Executable file
View File

@@ -0,0 +1,28 @@
package filetree
import (
"local/notes-server/config"
"os"
"testing"
)
func TestFiles(t *testing.T) {
config.Root = "/"
files := NewFiles()
info, err := os.Stat("/etc/hosts")
if err != nil {
t.Fatal(err)
}
p := NewPathFromLocal("/etc")
files.Push(p, info)
if len([]Path(*files)) != 1 {
t.Error(files)
}
first := []Path(*files)[0]
if first.Base != "hosts" {
t.Error(first)
}
if first.Local != "/etc/hosts" {
t.Error(first)
}
}

View File

@@ -1,4 +1,4 @@
package server package filetree
import ( import (
"fmt" "fmt"
@@ -16,9 +16,13 @@ type Path struct {
} }
func NewPathFromLocal(p string) Path { func NewPathFromLocal(p string) Path {
splits := strings.SplitN(p, path.Base(config.Root), 2) root := config.Root + "/"
if strings.HasPrefix(root, "./") {
root = root[2:]
}
splits := strings.SplitN(p, root, 2)
href := splits[0] href := splits[0]
if len(splits) > 1 { if len(splits) > 1 && (splits[0] == "" || splits[0] == "/") {
href = splits[1] href = splits[1]
} }
href = path.Join("/notes", href) href = path.Join("/notes", href)
@@ -67,6 +71,10 @@ func (p Path) MultiLink() string {
return full return full
} }
func (p Path) FullLI() string {
return fmt.Sprintf(`<li><a href=%q>%s</a></li>`, p.HREF, p.HREF)
}
func (p Path) LI() string { func (p Path) LI() string {
return fmt.Sprintf(`<li><a href=%q>%s</a></li>`, p.HREF, p.Base) return fmt.Sprintf(`<li><a href=%q>%s</a></li>`, p.HREF, p.Base)
} }

77
filetree/path_test.go Executable file
View File

@@ -0,0 +1,77 @@
package filetree
import (
"local/notes-server/config"
"testing"
)
func TestPathIs(t *testing.T) {
p := Path{Local: "/dev/null"}
if ok := p.IsDir(); ok {
t.Fatal(ok, p)
}
if ok := p.IsFile(); !ok {
t.Fatal(ok, p)
}
p = Path{Local: "/tmp"}
if ok := p.IsDir(); !ok {
t.Fatal(ok, p)
}
if ok := p.IsFile(); ok {
t.Fatal(ok, p)
}
}
func TestNewPathFromLocal(t *testing.T) {
cases := []struct {
in string
root string
href string
local string
}{
{
in: "/wiki/wiki/b/a.md",
root: "/wiki/wiki",
href: "/notes/b/a.md",
local: "/wiki/wiki/b/a.md",
},
{
in: "/wiki/b/a.md",
root: "/wiki",
href: "/notes/b/a.md",
local: "/wiki/b/a.md",
},
{
in: "/wiki/a.md",
root: "/wiki",
href: "/notes/a.md",
local: "/wiki/a.md",
},
{
in: "/b/a.md",
root: "/",
href: "/notes/b/a.md",
local: "/b/a.md",
},
{
in: "/a.md",
root: "/",
href: "/notes/a.md",
local: "/a.md",
},
}
for i, c := range cases {
config.Root = c.root
p := NewPathFromLocal(c.in)
if p.HREF != c.href {
t.Fatal(i, "href", p.HREF, c.href, c, p)
}
if p.Local != c.local {
t.Fatal(i, "local", p.Local, c.local, c, p)
}
}
}

19
filetree/paths.go Executable file
View File

@@ -0,0 +1,19 @@
package filetree
type Paths []Path
func (p Paths) List(full ...bool) string {
content := "<ul>\n"
for _, path := range p {
if len(path.Base) > 0 && path.Base[0] == '.' {
continue
}
if len(full) > 0 && full[0] {
content += path.FullLI() + "\n"
} else {
content += path.LI() + "\n"
}
}
content += "</ul>\n"
return content
}

30
filetree/paths_test.go Executable file
View File

@@ -0,0 +1,30 @@
package filetree
import (
"strings"
"testing"
)
func TestPaths(t *testing.T) {
paths := Paths([]Path{
NewPathFromURL("/notes/a/b"),
NewPathFromURL("/notes/c/d"),
NewPathFromURL("/notes/e/f"),
})
list := paths.List()
if strings.Count(list, "<li>") != 3 {
t.Error(list)
}
if !strings.Contains(list, ">f") {
t.Error(list)
}
list = paths.List(true)
if strings.Count(list, "<li>") != 3 {
t.Error(list)
}
if !strings.Contains(list, ">/notes/a") {
t.Error(list)
}
}

16
main.go
View File

@@ -3,10 +3,12 @@ package main
import ( import (
"local/notes-server/config" "local/notes-server/config"
"local/notes-server/server" "local/notes-server/server"
"local/notes-server/versions"
"log" "log"
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
"time"
) )
func main() { func main() {
@@ -15,6 +17,20 @@ func main() {
panic(err) panic(err)
} }
go func() {
if config.VersionInterval == 0 {
log.Println("versions disabled")
return
}
versions, err := versions.New()
if err != nil {
panic(err)
}
for _ = range time.NewTicker(config.VersionInterval).C {
log.Println(versions.Gitmmit())
}
}()
go func() { go func() {
log.Printf("Serving %q on %q", config.Root, config.Port) log.Printf("Serving %q on %q", config.Root, config.Port)
if err := http.ListenAndServe(config.Port, server); err != nil { if err := http.ListenAndServe(config.Port, server); err != nil {

245
main_test.go Executable file
View File

@@ -0,0 +1,245 @@
package main
import (
"fmt"
"io/ioutil"
"local/notes-server/config"
"local/notes-server/server"
"log"
"net/http"
"net/http/httptest"
"os"
"path"
"regexp"
"strings"
"testing"
)
func TestAll(t *testing.T) {
for _, basedir := range []string{os.TempDir(), "./tempDir"} {
os.MkdirAll(basedir, os.ModePerm)
makeFiles(t, basedir)
if basedir[0] == '.' && config.Root[0] != '.' {
config.Root = "./" + config.Root
}
defer os.RemoveAll(config.Root)
log.Println(config.Root)
t.Log("trying with root", config.Root)
s := makeServer(t)
defer s.Close()
testServer(t, s.URL)
if basedir[0] == '.' {
os.RemoveAll(basedir)
}
}
}
func makeFiles(t *testing.T, basedir string) {
d, err := ioutil.TempDir(basedir, "pattern*")
if err != nil {
t.Fatal(err)
}
config.Root = d
for _, dir := range []string{"dirA", "dirB", "."} {
if err := os.MkdirAll(path.Join(d, dir), os.ModePerm); err != nil {
t.Fatal(err)
}
for _, file := range []string{"fileA", "fileB"} {
content := fmt.Sprintf("hello from %s/%s/%s", d, dir, file)
err := ioutil.WriteFile(
path.Join(d, dir, file),
[]byte(content),
os.ModePerm,
)
if err != nil {
t.Fatal(err)
}
}
}
}
func makeServer(t *testing.T) *httptest.Server {
s := server.New()
if err := s.Routes(); err != nil {
t.Fatal(err)
}
return httptest.NewServer(s)
}
func testServer(t *testing.T, url string) {
testCreate(t, url)
testEdit(t, url)
testDir(t, url)
testFile(t, url)
testNavRootDir(t, url)
testNavRootFile(t, url)
testNavDirFile(t, url)
}
func testCreate(t *testing.T, url string) {
for _, path := range []string{"dirX/fileX", "fileX"} {
resp, err := http.Get(url + "/create/" + path)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatal(resp.StatusCode)
}
b, _ := ioutil.ReadAll(resp.Body)
s := string(b)
if ok := assertHasMultilink(s, path); !ok {
t.Error(ok)
}
if ok := assertHasForm(s, "/submit/"+path); !ok {
t.Error(ok)
}
if ok := assertHasTextArea(s); !ok {
t.Error(ok)
}
if ok := assertHasSubmit(s); !ok {
t.Error(ok)
}
}
}
func testEdit(t *testing.T, url string) {
for _, path := range []string{"dirX/fileX", "fileX"} {
resp, err := http.Get(url + "/edit/" + path)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatal(resp.StatusCode)
}
b, _ := ioutil.ReadAll(resp.Body)
s := string(b)
if ok := assertHasMultilink(s, path); !ok {
t.Error(ok)
}
if ok := assertHasForm(s); !ok {
t.Error(ok)
}
if ok := assertHasTextArea(s); !ok {
t.Error(ok)
}
if ok := assertHasSubmit(s); !ok {
t.Error(ok)
}
}
}
func testDir(t *testing.T, url string) {
path := url + "/notes/dirA"
resp, err := http.Get(path)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatal(resp.StatusCode)
}
b, _ := ioutil.ReadAll(resp.Body)
s := string(b)
if ok := assertHasMultilink(s, "/notes/dirA"); !ok {
t.Error(ok)
}
if ok := assertHasForm(s, "/create/dirA"); !ok {
t.Error(ok)
}
if ok := assertHasSubmit(s); !ok {
t.Error(ok)
}
}
func testFile(t *testing.T, url string) {
path := url + "/notes/dirA/fileA"
resp, err := http.Get(path)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatal(resp.StatusCode)
}
b, _ := ioutil.ReadAll(resp.Body)
s := string(b)
if ok := assertHasMultilink(s, "/notes/dirA", "/notes/dirA/fileA"); !ok {
t.Error(ok)
}
if ok := assertHasHref(s, "/edit/dirA/fileA"); !ok {
t.Error(ok)
}
}
func testNavRootDir(t *testing.T, url string) {
resp, err := http.Get(url + "/notes")
if err != nil {
t.Fatal(err)
}
b, _ := ioutil.ReadAll(resp.Body)
s := string(b)
if ok := assertHasHref(s, "/notes/dirA"); !ok {
t.Fatal(s)
}
}
func testNavRootFile(t *testing.T, url string) {
resp, err := http.Get(url + "/notes")
if err != nil {
t.Fatal(err)
}
b, _ := ioutil.ReadAll(resp.Body)
s := string(b)
if ok := assertHasHref(s, "/notes/fileA"); !ok {
t.Fatal(s)
}
}
func testNavDirFile(t *testing.T, url string) {
resp, err := http.Get(url + "/notes/dirA")
if err != nil {
t.Fatal(err)
}
b, _ := ioutil.ReadAll(resp.Body)
s := string(b)
if ok := assertHasHref(s, "/notes/dirA/fileA"); !ok {
t.Fatal(s)
}
}
func assertHasMultilink(body string, segments ...string) bool {
if !strings.Contains(body, `/<a href="/notes">notes</a>/`) {
return false
}
for i := range segments {
segments[i] = "/notes/" + strings.TrimPrefix(segments[i], "/notes/")
}
return assertHasHref(body, segments...)
}
func assertHasForm(body string, action ...string) bool {
return strings.Contains(body, `<form`) && (len(action) == 0 || strings.Contains(body, `action="`))
}
func assertHasTextArea(body string) bool {
return strings.Contains(body, `<textarea`)
}
func assertHasSubmit(body string) bool {
return strings.Contains(body, `<button`) && strings.Contains(body, `type="submit"`)
}
func assertHasHref(body string, segments ...string) bool {
if !strings.Contains(body, `href="`) {
return false
}
for _, segment := range segments {
re := regexp.MustCompile(`a[^>]*href="` + segment + `"`)
if !re.MatchString(body) {
return false
}
}
return true
}

15
notes/create.go Executable file
View File

@@ -0,0 +1,15 @@
package notes
import (
"errors"
"local/notes-server/filetree"
"path"
)
func (n *Notes) Create(urlPath string) (string, error) {
p := filetree.NewPathFromURL(urlPath)
if p.IsDir() {
return "", errors.New("directory exists")
}
return path.Join("/edit/", p.BaseHREF), nil
}

18
notes/create_test.go Executable file
View File

@@ -0,0 +1,18 @@
package notes
import (
"local/notes-server/config"
"testing"
)
func TestCreate(t *testing.T) {
config.Root = "/tmp"
n := &Notes{}
resp, err := n.Create("/create/a")
if err != nil {
t.Error(err)
}
if resp != "/edit/a" {
t.Error(resp)
}
}

15
notes/delete.go Executable file
View File

@@ -0,0 +1,15 @@
package notes
import (
"errors"
"local/notes-server/filetree"
"os"
)
func (n *Notes) Delete(urlPath string) error {
p := filetree.NewPathFromURL(urlPath)
if p.IsDir() {
return errors.New("path is dir")
}
return os.Remove(p.Local)
}

20
notes/delete_test.go Executable file
View File

@@ -0,0 +1,20 @@
package notes
import (
"io/ioutil"
"local/notes-server/config"
"os"
"testing"
)
func TestDelete(t *testing.T) {
config.Root = "/tmp"
ioutil.WriteFile("/tmp/a", []byte("hi"), os.ModePerm)
n := &Notes{}
if err := n.Delete("/notes/a"); err != nil {
t.Error(err)
}
if _, err := os.Stat("/tmp/a"); err == nil {
t.Error(err)
}
}

28
notes/dir.go Executable file
View File

@@ -0,0 +1,28 @@
package notes
import (
"errors"
"io/ioutil"
"local/notes-server/filetree"
)
func (n *Notes) Dir(urlPath string) (string, string, error) {
p := filetree.NewPathFromURL(urlPath)
if !p.IsDir() {
return "", "", errors.New("not a dir")
}
dirs, files := n.lsDir(p)
return dirs.List(), files.List(), nil
}
func (n *Notes) lsDir(path filetree.Path) (filetree.Paths, filetree.Paths) {
dirs := filetree.NewDirs()
files := filetree.NewFiles()
found, _ := ioutil.ReadDir(path.Local)
for _, f := range found {
dirs.Push(path, f)
files.Push(path, f)
}
return filetree.Paths(*dirs), filetree.Paths(*files)
}

36
notes/dir_test.go Executable file
View File

@@ -0,0 +1,36 @@
package notes
import (
"local/notes-server/config"
"testing"
)
func TestDir(t *testing.T) {
n := &Notes{}
config.Root = "/"
dirs, files, err := n.Dir("/notes/usr/local")
if err != nil {
t.Fatal(err)
}
if len(dirs) == 0 {
t.Fatal(len(dirs))
}
if len(files) == 0 {
t.Fatal(len(files))
}
t.Log(dirs)
t.Log(files)
}
func TestNotesDir(t *testing.T) {
n := &Notes{}
body, body2, err := n.Dir("/notes/usr/local")
if err != nil {
t.Fatal(err)
}
if body == "" || body2 == "" {
t.Fatal(body, body2)
}
t.Logf("%s", body)
t.Logf("%s", body2)
}

36
notes/edit.go Executable file
View File

@@ -0,0 +1,36 @@
package notes
import (
"errors"
"fmt"
"io/ioutil"
"local/notes-server/filetree"
"strings"
)
func (n *Notes) Edit(urlPath string) (string, error) {
p := filetree.NewPathFromURL(urlPath)
if p.IsDir() {
return "", errors.New("path is dir")
}
return editFile(p), nil
}
func editFile(p filetree.Path) string {
href := p.HREF
href = strings.TrimPrefix(href, "/")
hrefs := strings.SplitN(href, "/", 2)
href = hrefs[0]
if len(hrefs) > 1 {
href = hrefs[1]
}
b, _ := ioutil.ReadFile(p.Local)
return fmt.Sprintf(`
<form action="/submit/%s" method="post" style="width:100%%; height: 90%%">
<table style="width:100%%; height: 90%%">
<textarea name="content" style="width:100%%; min-height:90%%">%s</textarea>
</table>
<button type="submit">Submit</button>
</form>
`, href, b)
}

21
notes/edit_test.go Executable file
View File

@@ -0,0 +1,21 @@
package notes
import (
"local/notes-server/config"
"strings"
"testing"
)
func TestEdit(t *testing.T) {
config.Root = "/tmp"
n := &Notes{}
if body, err := n.Edit("/notes/a"); err != nil {
t.Error(err)
} else if !strings.Contains(body, "/submit/a") {
t.Error(body)
}
config.Root = "/usr"
if _, err := n.Edit("/notes/local"); err == nil {
t.Error(err)
}
}

25
notes/file.go Executable file
View File

@@ -0,0 +1,25 @@
package notes
import (
"errors"
"io/ioutil"
"local/notes-server/filetree"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser"
)
func (n *Notes) File(urlPath string) (string, error) {
p := filetree.NewPathFromURL(urlPath)
if p.IsDir() {
return "", errors.New("path is dir")
}
b, _ := ioutil.ReadFile(p.Local)
renderer := html.NewRenderer(html.RendererOptions{
Flags: html.CommonFlags | html.TOC,
})
parser := parser.NewWithExtensions(parser.CommonExtensions | parser.HeadingIDs | parser.AutoHeadingIDs | parser.Titleblock)
content := markdown.ToHTML(b, parser, renderer)
return string(content) + "\n", nil
}

49
notes/file_test.go Executable file
View File

@@ -0,0 +1,49 @@
package notes
import (
"fmt"
"io/ioutil"
"local/notes-server/config"
"os"
"path"
"strings"
"testing"
)
func TestFile(t *testing.T) {
f, err := ioutil.TempFile(os.TempDir(), "until*")
if err != nil {
t.Fatal(err)
}
defer os.Remove(f.Name())
fmt.Fprintln(f, `
# Hello
## World
* This
* is
* bullets
| My | table | goes |
|----|-------|------|
| h | e | n |
`)
f.Close()
n := &Notes{}
config.Root = "/"
s, err := n.File(path.Join("notes", f.Name()))
if err != nil {
t.Fatal(err)
}
shouldContain := []string{
"tbody",
"h1",
"h2",
}
for _, should := range shouldContain {
if !strings.Contains(s, should) {
t.Fatalf("%s: %s", should, s)
}
}
t.Logf("%s", s)
}

13
notes/notes.go Executable file
View File

@@ -0,0 +1,13 @@
package notes
import "local/notes-server/config"
type Notes struct {
root string
}
func New() *Notes {
return &Notes{
root: config.Root,
}
}

1
notes/notes_test.go Executable file
View File

@@ -0,0 +1 @@
package notes

56
notes/search.go Executable file
View File

@@ -0,0 +1,56 @@
package notes
import (
"bufio"
"bytes"
"local/notes-server/filetree"
"log"
"os"
"path"
"path/filepath"
)
func (n *Notes) Search(phrase string) (string, error) {
files := filetree.NewFiles()
err := filepath.Walk(n.root,
func(walked string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if size := info.Size(); size < 1 || size > (5*1024*1024) {
return nil
}
ok, err := grepFile(walked, []byte(phrase))
if err != nil && err.Error() == "bufio.Scanner: token too long" {
err = nil
}
if err == nil && ok {
p := filetree.NewPathFromLocal(path.Dir(walked))
files.Push(p, info)
}
if err != nil {
log.Printf("failed to scan %v: %v", walked, err)
}
return err
},
)
return filetree.Paths(*files).List(true), err
}
func grepFile(file string, phrase []byte) (bool, error) {
f, err := os.Open(file)
if err != nil {
return false, err
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
if bytes.Contains(scanner.Bytes(), phrase) {
return true, scanner.Err()
}
}
return false, scanner.Err()
}

54
notes/search_test.go Executable file
View File

@@ -0,0 +1,54 @@
package notes
import (
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
)
func TestSearch(t *testing.T) {
d, err := ioutil.TempDir(os.TempDir(), "pattern*")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(d)
for i := 0; i < 5; i++ {
f, err := ioutil.TempFile(d, fmt.Sprintf("file_%d", i))
if err != nil {
t.Fatal(err)
}
fmt.Fprintf(f, "this file is number %d", i)
f.Close()
}
n := New()
n.root = d
result, err := n.Search("this file")
if err != nil {
t.Fatal(err)
}
if v := len(strings.Split(result, "\n")); v < 7 {
t.Fatal(v, result)
}
result, err = n.Search("4")
if err != nil {
t.Fatal(err)
}
if v := len(strings.Split(result, "\n")); v > 4 {
t.Fatal(v, result)
}
}
func TestSearchBigFiles(t *testing.T) {
n := New()
n.root = "/usr/local/bin"
_, err := n.Search("this file")
if err != nil {
t.Fatal(err)
}
}

14
notes/submit.go Executable file
View File

@@ -0,0 +1,14 @@
package notes
import (
"io/ioutil"
"local/notes-server/filetree"
"os"
"path"
)
func (n *Notes) Submit(urlPath, content string) error {
p := filetree.NewPathFromURL(urlPath)
os.MkdirAll(path.Dir(p.Local), os.ModePerm)
return ioutil.WriteFile(p.Local, []byte(content), os.ModePerm)
}

22
notes/submit_test.go Executable file
View File

@@ -0,0 +1,22 @@
package notes
import (
"io/ioutil"
"local/notes-server/config"
"os"
"testing"
)
func TestSubmit(t *testing.T) {
config.Root = "/tmp"
n := &Notes{}
if err := n.Submit("/submit/a", "a"); err != nil {
t.Error(err)
} else if b, err := ioutil.ReadFile("/tmp/a"); err != nil {
t.Error(err)
} else if string(b) != "a" {
t.Error(string(b))
} else if err := os.Remove("/tmp/a"); err != nil {
t.Error(err)
}
}

3
public/A/D/G/A/b Executable file
View File

@@ -0,0 +1,3 @@
asdf
this contains my search string

5
public/A/asdf Executable file
View File

@@ -0,0 +1,5 @@
asdf
searchString
here is my new line

5
public/B/y Executable file
View File

@@ -0,0 +1,5 @@
## B.y
| hello | world |
|-------|-------|
| cont | ent. |

8
public/B/z Executable file
View File

@@ -0,0 +1,8 @@
## B.z
| hello | world |
|-------|-------|
| cont | ent. |
HI

1
public/D/E/F/g Executable file
View File

@@ -0,0 +1 @@
hi

24
server/.notes/create.go Executable file
View File

@@ -0,0 +1,24 @@
package notes
import (
"html"
"local/notes-server/filetree"
"net/http"
"path"
"strings"
)
func (n *Notes) Create(w http.ResponseWriter, r *http.Request) {
content := r.FormValue("base")
content = html.UnescapeString(content)
content = strings.ReplaceAll(content, "\r", "")
urlPath := path.Join(r.URL.Path, content)
p := filetree.NewPathFromURL(urlPath)
if p.IsDir() {
w.WriteHeader(http.StatusBadRequest)
return
}
url := *r.URL
url.Path = path.Join("/edit/", p.BaseHREF)
http.Redirect(w, r, url.String(), http.StatusSeeOther)
}

1
server/.notes/create_test.go Executable file
View File

@@ -0,0 +1 @@
package notes

View File

@@ -1,4 +1,4 @@
package server package notes
import ( import (
"fmt" "fmt"

View File

@@ -1,4 +1,4 @@
package server package notes
import ( import (
"net/http/httptest" "net/http/httptest"

43
server/.notes/edit.go Executable file
View File

@@ -0,0 +1,43 @@
package notes
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
)
func (s *Server) edit(w http.ResponseWriter, r *http.Request) {
p := NewPathFromURL(r.URL.Path)
if p.IsDir() {
http.NotFound(w, r)
return
}
head(w, r)
editHead(w, p)
editFile(w, p)
foot(w, r)
}
func editHead(w http.ResponseWriter, p Path) {
fmt.Fprintln(w, h2(p.MultiLink()))
}
func editFile(w http.ResponseWriter, p Path) {
href := p.HREF
href = strings.TrimPrefix(href, "/")
hrefs := strings.SplitN(href, "/", 2)
href = hrefs[0]
if len(hrefs) > 1 {
href = hrefs[1]
}
b, _ := ioutil.ReadFile(p.Local)
fmt.Fprintf(w, `
<form action="/submit/%s" method="post" style="width:100%%; height: 90%%">
<table style="width:100%%; height: 90%%">
<textarea name="content" style="width:100%%; min-height:90%%">%s</textarea>
</table>
<button type="submit">Submit</button>
</form>
`, href, b)
}

1
server/.notes/edit_test.go Executable file
View File

@@ -0,0 +1 @@
package notes

View File

@@ -1,4 +1,4 @@
package server package notes
import ( import (
"fmt" "fmt"

View File

@@ -1,4 +1,4 @@
package server package notes
import ( import (
"fmt" "fmt"

13
server/.notes/notes.go Executable file
View File

@@ -0,0 +1,13 @@
package notes
import "local/notes-server/config"
type Notes struct {
root string
}
func New() *Notes {
return &Notes{
root: config.Root,
}
}

27
server/.notes/rnotes.go Executable file
View File

@@ -0,0 +1,27 @@
package notes
import (
"fmt"
"net/http"
)
func (s *Server) notes(w http.ResponseWriter, r *http.Request) {
p := NewPathFromURL(r.URL.Path)
if p.IsDir() {
head(w, r)
notesHead(w, p)
notesDir(p, w, r)
foot(w, r)
} else if p.IsFile() {
head(w, r)
notesHead(w, p)
notesFile(p, w, r)
foot(w, r)
} else {
http.NotFound(w, r)
}
}
func notesHead(w http.ResponseWriter, p Path) {
fmt.Fprintln(w, h2(p.MultiLink()))
}

1
server/.notes/rnotes_test.go Executable file
View File

@@ -0,0 +1 @@
package notes

31
server/.notes/submit.go Executable file
View File

@@ -0,0 +1,31 @@
package notes
import (
"fmt"
"html"
"io/ioutil"
"net/http"
"os"
"path"
"strings"
)
func (s *Server) submit(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.NotFound(w, r)
return
}
content := r.FormValue("content")
content = html.UnescapeString(content)
content = strings.ReplaceAll(content, "\r", "")
p := NewPathFromURL(r.URL.Path)
os.MkdirAll(path.Dir(p.Local), os.ModePerm)
if err := ioutil.WriteFile(p.Local, []byte(content), os.ModePerm); err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintln(w, err)
} else {
url := *r.URL
url.Path = path.Join("/notes/", p.BaseHREF)
http.Redirect(w, r, url.String(), http.StatusSeeOther)
}
}

1
server/.notes/submit_test.go Executable file
View File

@@ -0,0 +1 @@
package notes

View File

@@ -12,12 +12,12 @@ func (s *Server) create(w http.ResponseWriter, r *http.Request) {
content = html.UnescapeString(content) content = html.UnescapeString(content)
content = strings.ReplaceAll(content, "\r", "") content = strings.ReplaceAll(content, "\r", "")
urlPath := path.Join(r.URL.Path, content) urlPath := path.Join(r.URL.Path, content)
p := NewPathFromURL(urlPath) url := *r.URL
if p.IsDir() { path, err := s.Notes.Create(urlPath)
w.WriteHeader(http.StatusBadRequest) if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return return
} }
url := *r.URL url.Path = path
url.Path = path.Join("/edit/", p.BaseHREF)
http.Redirect(w, r, url.String(), http.StatusSeeOther) http.Redirect(w, r, url.String(), http.StatusSeeOther)
} }

View File

@@ -1 +0,0 @@
package server

16
server/delete.go Executable file
View File

@@ -0,0 +1,16 @@
package server
import (
"net/http"
"path"
"strings"
)
func (s *Server) delete(w http.ResponseWriter, r *http.Request) {
if err := s.Notes.Delete(r.URL.Path); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
r.URL.Path = strings.Replace(path.Dir(r.URL.Path), "delete", "notes", 1)
http.Redirect(w, r, r.URL.String(), http.StatusPermanentRedirect)
}

View File

@@ -1 +0,0 @@
package server

View File

@@ -2,42 +2,22 @@ package server
import ( import (
"fmt" "fmt"
"io/ioutil" "local/notes-server/filetree"
"net/http" "net/http"
"strings"
) )
func (s *Server) edit(w http.ResponseWriter, r *http.Request) { func (s *Server) edit(w http.ResponseWriter, r *http.Request) {
p := NewPathFromURL(r.URL.Path) head(w, r)
if p.IsDir() { editHead(w, filetree.NewPathFromURL(r.URL.Path))
http.NotFound(w, r) edit, err := s.Notes.Edit(r.URL.Path)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return return
} }
head(w, r) fmt.Fprintln(w, edit)
editHead(w, p)
editFile(w, p)
foot(w, r) foot(w, r)
} }
func editHead(w http.ResponseWriter, p Path) { func editHead(w http.ResponseWriter, p filetree.Path) {
fmt.Fprintln(w, h2(p.MultiLink())) fmt.Fprintln(w, h2(p.MultiLink()))
} }
func editFile(w http.ResponseWriter, p Path) {
href := p.HREF
href = strings.TrimPrefix(href, "/")
hrefs := strings.SplitN(href, "/", 2)
href = hrefs[0]
if len(hrefs) > 1 {
href = hrefs[1]
}
b, _ := ioutil.ReadFile(p.Local)
fmt.Fprintf(w, `
<form action="/submit/%s" method="post" style="width:100%%; height: 90%%">
<table style="width:100%%; height: 90%%">
<textarea name="content" style="width:100%%; min-height:90%%">%s</textarea>
</table>
<button type="submit">Submit</button>
</form>
`, href, b)
}

View File

@@ -1 +0,0 @@
package server

View File

@@ -1 +0,0 @@
package server

43
server/html.go Executable file
View File

@@ -0,0 +1,43 @@
package server
import (
"fmt"
"local/notes-server/config"
"net/http"
)
func head(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(config.Head + "\n"))
}
func foot(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(config.Foot + "\n"))
}
func block(w http.ResponseWriter, content string) {
fmt.Fprintf(w, "\n<div>\n%s\n</div>\n", content)
}
func h1(content string) string {
return h("1", content)
}
func h2(content string) string {
return h("2", content)
}
func h3(content string) string {
return h("3", content)
}
func h4(content string) string {
return h("4", content)
}
func h5(content string) string {
return h("5", content)
}
func h(level, content string) string {
return fmt.Sprintf("\n<h%s>\n%s\n</h%s>\n", level, content, level)
}

View File

@@ -28,7 +28,7 @@ func TestFoot(t *testing.T) {
func TestBlock(t *testing.T) { func TestBlock(t *testing.T) {
w := httptest.NewRecorder() w := httptest.NewRecorder()
block("hi", w) block(w, "hi")
s := strings.ReplaceAll(strings.TrimSpace(string(w.Body.Bytes())), "\n", ".") s := strings.ReplaceAll(strings.TrimSpace(string(w.Body.Bytes())), "\n", ".")
if ok, err := regexp.MatchString("<div>.*hi.*<.div>", s); err != nil { if ok, err := regexp.MatchString("<div>.*hi.*<.div>", s); err != nil {
t.Fatal(err, s) t.Fatal(err, s)

View File

@@ -2,26 +2,70 @@ package server
import ( import (
"fmt" "fmt"
"local/notes-server/filetree"
"net/http" "net/http"
"path"
) )
func (s *Server) notes(w http.ResponseWriter, r *http.Request) { func (s *Server) notes(w http.ResponseWriter, r *http.Request) {
p := NewPathFromURL(r.URL.Path) p := filetree.NewPathFromURL(r.URL.Path)
head(w, r)
notesHead(w, p)
defer foot(w, r)
if p.IsDir() { if p.IsDir() {
head(w, r) s.dir(w, r)
notesHead(w, p)
notesDir(p, w, r)
foot(w, r)
} else if p.IsFile() { } else if p.IsFile() {
head(w, r) s.file(w, r)
notesHead(w, p)
notesFile(p, w, r)
foot(w, r)
} else { } else {
http.NotFound(w, r) http.NotFound(w, r)
} }
} }
func notesHead(w http.ResponseWriter, p Path) { func notesHead(w http.ResponseWriter, p filetree.Path) {
fmt.Fprintln(w, h2(p.MultiLink())) fmt.Fprintln(w, h2(p.MultiLink()))
fmt.Fprintf(w, `
<form action=%q method="post">
<input type="text" name="keywords"></input>
<button type="submit">Search</button>
</form>
`, "/search")
}
func (s *Server) dir(w http.ResponseWriter, r *http.Request) {
dirs, files, err := s.Notes.Dir(r.URL.Path)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
dirHead(w, filetree.NewPathFromURL(r.URL.Path).BaseHREF)
block(w, dirs)
block(w, files)
}
func dirHead(w http.ResponseWriter, baseHREF string) {
fmt.Fprintf(w, `
<form action=%q method="get">
<input type="text" name="base"></input>
<button type="submit">Create</button>
</form>
`, path.Join("/create/", baseHREF))
}
func (s *Server) file(w http.ResponseWriter, r *http.Request) {
file, err := s.Notes.File(r.URL.Path)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
fileHead(w, filetree.NewPathFromURL(r.URL.Path).BaseHREF)
fmt.Fprintln(w, file)
}
func fileHead(w http.ResponseWriter, baseHREF string) {
fmt.Fprintf(w, `
<a href=%q><input type="button" value="Edit"></input></a>
`, path.Join("/edit/", baseHREF))
fmt.Fprintf(w, `
<a href=%q><input type="button" value="Delete"></input></a>
`, path.Join("/delete/", baseHREF))
} }

View File

@@ -1 +0,0 @@
package server

View File

@@ -1,23 +0,0 @@
package server
import "testing"
func TestPathIs(t *testing.T) {
p := Path{Local: "/dev/null"}
if ok := p.IsDir(); ok {
t.Fatal(ok, p)
}
if ok := p.IsFile(); !ok {
t.Fatal(ok, p)
}
p = Path{Local: "/tmp"}
if ok := p.IsDir(); !ok {
t.Fatal(ok, p)
}
if ok := p.IsFile(); ok {
t.Fatal(ok, p)
}
}

View File

@@ -1,12 +0,0 @@
package server
type Paths []Path
func (p Paths) List() string {
content := "<ul>\n"
for _, path := range p {
content += path.LI() + "\n"
}
content += "</ul>\n"
return content
}

View File

@@ -1 +0,0 @@
package server

View File

@@ -12,21 +12,33 @@ func (s *Server) Routes() error {
path string path string
handler http.HandlerFunc handler http.HandlerFunc
}{ }{
{
path: "/",
handler: s.root,
},
{ {
path: fmt.Sprintf("notes/%s%s", wildcard, wildcard), path: fmt.Sprintf("notes/%s%s", wildcard, wildcard),
handler: s.notes, handler: s.authenticate(s.notes),
}, },
{ {
path: fmt.Sprintf("edit/%s%s", wildcard, wildcard), path: fmt.Sprintf("edit/%s%s", wildcard, wildcard),
handler: s.edit, handler: s.authenticate(s.edit),
},
{
path: fmt.Sprintf("delete/%s%s", wildcard, wildcard),
handler: s.authenticate(s.delete),
}, },
{ {
path: fmt.Sprintf("submit/%s%s", wildcard, wildcard), path: fmt.Sprintf("submit/%s%s", wildcard, wildcard),
handler: s.submit, handler: s.authenticate(s.submit),
}, },
{ {
path: fmt.Sprintf("create/%s%s", wildcard, wildcard), path: fmt.Sprintf("create/%s%s", wildcard, wildcard),
handler: s.create, handler: s.authenticate(s.create),
},
{
path: fmt.Sprintf("search"),
handler: s.authenticate(s.search),
}, },
} }
@@ -37,3 +49,8 @@ func (s *Server) Routes() error {
} }
return nil return nil
} }
func (s *Server) root(w http.ResponseWriter, r *http.Request) {
r.URL.Path = "/notes"
http.Redirect(w, r, r.URL.String(), http.StatusPermanentRedirect)
}

View File

@@ -37,7 +37,7 @@ func TestServerNotes(t *testing.T) {
t.Logf("serve %s: %s", r.URL.Path, w.Body.Bytes()) t.Logf("serve %s: %s", r.URL.Path, w.Body.Bytes())
} }
func TestServerNotes(t *testing.T) { func TestServerNotesB(t *testing.T) {
s := New() s := New()
w := httptest.NewRecorder() w := httptest.NewRecorder()
r := &http.Request{ r := &http.Request{

26
server/search.go Executable file
View File

@@ -0,0 +1,26 @@
package server
import (
"fmt"
"html"
"local/notes-server/filetree"
"net/http"
)
func (s *Server) search(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
keywords := r.FormValue("keywords")
keywords = html.UnescapeString(keywords)
results, err := s.Notes.Search(keywords)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
head(w, r)
fmt.Fprintln(w, h2(filetree.NewPathFromURL("/notes").MultiLink()))
fmt.Fprintln(w, h1(keywords), results)
foot(w, r)
}

View File

@@ -1,54 +1,35 @@
package server package server
import ( import (
"fmt"
"local/notes-server/config" "local/notes-server/config"
"local/notes-server/notes"
"local/oauth2/oauth2client"
"local/router" "local/router"
"log"
"net/http" "net/http"
) )
type Server struct { type Server struct {
*router.Router *router.Router
Notes *notes.Notes
} }
func New() *Server { func New() *Server {
return &Server{ return &Server{
Router: router.New(), Router: router.New(),
Notes: notes.New(),
} }
} }
func head(w http.ResponseWriter, r *http.Request) { func (s *Server) authenticate(foo http.HandlerFunc) http.HandlerFunc {
w.Write([]byte(config.Head + "\n")) return func(w http.ResponseWriter, r *http.Request) {
} if config.OAuthServer != "" {
err := oauth2client.Authenticate(config.OAuthServer, "notes-server", w, r)
func foot(w http.ResponseWriter, r *http.Request) { if err != nil {
w.Write([]byte(config.Foot + "\n")) log.Println(err)
} return
}
func block(content string, w http.ResponseWriter) { }
fmt.Fprintf(w, "\n<div>\n%s\n</div>\n", content) foo(w, r)
} }
func h1(content string) string {
return h("1", content)
}
func h2(content string) string {
return h("2", content)
}
func h3(content string) string {
return h("3", content)
}
func h4(content string) string {
return h("4", content)
}
func h5(content string) string {
return h("5", content)
}
func h(level, content string) string {
return fmt.Sprintf("\n<h%s>\n%s\n</h%s>\n", level, content, level)
} }

View File

@@ -1,11 +1,9 @@
package server package server
import ( import (
"fmt"
"html" "html"
"io/ioutil" "local/notes-server/filetree"
"net/http" "net/http"
"os"
"path" "path"
"strings" "strings"
) )
@@ -18,14 +16,12 @@ func (s *Server) submit(w http.ResponseWriter, r *http.Request) {
content := r.FormValue("content") content := r.FormValue("content")
content = html.UnescapeString(content) content = html.UnescapeString(content)
content = strings.ReplaceAll(content, "\r", "") content = strings.ReplaceAll(content, "\r", "")
p := NewPathFromURL(r.URL.Path) err := s.Notes.Submit(r.URL.Path, content)
os.MkdirAll(path.Dir(p.Local), os.ModePerm) if err != nil {
if err := ioutil.WriteFile(p.Local, []byte(content), os.ModePerm); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError)
w.WriteHeader(http.StatusInternalServerError) return
fmt.Fprintln(w, err)
} else {
url := *r.URL
url.Path = path.Join("/notes/", p.BaseHREF)
http.Redirect(w, r, url.String(), http.StatusSeeOther)
} }
url := *r.URL
url.Path = path.Join("/notes/", filetree.NewPathFromURL(r.URL.Path).BaseHREF)
http.Redirect(w, r, url.String(), http.StatusSeeOther)
} }

View File

@@ -1 +0,0 @@
package server

67
versions/.versions.go Executable file
View File

@@ -0,0 +1,67 @@
package versions
import (
"local/notes-server/config"
"time"
git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/object"
)
type Versions struct {
repo *git.Repository
}
func New() (*Versions, error) {
repo, err := git.PlainInit(config.Root, false)
if err != nil {
repo, err = git.PlainOpen(config.Root)
}
return &Versions{
repo: repo,
}, err
}
func (v *Versions) Gitmmit() error {
if err := v.AddAll(); err != nil {
return err
}
if err := v.Commit(); err != nil {
return err
}
return nil
}
func (v *Versions) AddAll() error {
worktree, err := v.worktree()
if err != nil {
return err
}
for _, path := range []string{".", "./*", "./**", "/", "/**", "/*"} {
if err := worktree.AddGlob(path); err != nil {
return err
}
}
return nil
}
func (v *Versions) Commit() error {
worktree, err := v.worktree()
if err != nil {
return err
}
opts := &git.CommitOptions{Author: &object.Signature{}}
_, err = worktree.Commit(time.Now().String(), opts)
return err
}
func (v *Versions) worktree() (*git.Worktree, error) {
worktree, err := v.repo.Worktree()
if err != nil {
return nil, err
}
return worktree, nil
}

65
versions/.versions_test.go Executable file
View File

@@ -0,0 +1,65 @@
package versions
import (
"io/ioutil"
"local/notes-server/config"
"os"
"path"
"testing"
)
func TestVersionsHappy(t *testing.T) {
d, err := ioutil.TempDir(os.TempDir(), "prefix")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(d)
if err := ioutil.WriteFile(path.Join(d, "a.md"), []byte("# Hello"), os.ModePerm); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(path.Join(d, "b.md"), []byte("# World"), os.ModePerm); err != nil {
t.Fatal(err)
}
config.Root = d
v, err := New()
if err != nil {
t.Error(err)
}
if err := v.AddAll(); err != nil {
t.Error("failed add", err)
}
if err := v.Commit(); err != nil {
t.Error("failed commit", err)
}
if err := v.Gitmmit(); err != nil {
t.Error("failed gitmmit", err)
}
}
func TestVersionsBad(t *testing.T) {
config.Root = "/not/a/real/path"
if _, err := New(); err == nil {
t.Error("passed new from nil path")
}
}
func TestVersionsDirty(t *testing.T) {
if os.Getenv("DIRTY") == "" {
return
}
config.Root = "/tmp/foo"
v, err := New()
if err != nil {
t.Fatal(err)
}
ioutil.WriteFile(path.Join(config.Root, "file.md"), []byte(`
# Hello
## World
I'm a doc
`), os.ModePerm)
if err := v.Gitmmit(); err != nil {
t.Fatal(err)
}
t.Log(v)
}

44
versions/versions.go Executable file
View File

@@ -0,0 +1,44 @@
package versions
import (
"fmt"
"local/notes-server/config"
"os/exec"
"time"
)
type Versions struct {
}
func New() (*Versions, error) {
v := &Versions{}
v.cmd("git", "init")
v.cmd("git", "config", "user.email", "user@user.user")
v.cmd("git", "config", "user.name", "user")
return v, nil
}
func (v *Versions) Gitmmit() error {
if err := v.AddAll(); err != nil {
return fmt.Errorf("cannot add all: %v", err)
}
if err := v.Commit(); err != nil {
return fmt.Errorf("cannot commit: %v", err)
}
return nil
}
func (v *Versions) AddAll() error {
return v.cmd("git", "add", "-A", ":/")
}
func (v *Versions) Commit() error {
return v.cmd("git", "commit", "-m", time.Now().String())
}
func (v *Versions) cmd(cmd string, args ...string) error {
command := exec.Command(cmd, args...)
command.Dir = config.Root
_, err := command.CombinedOutput()
return err
}

42
versions/versions_test.go Executable file
View File

@@ -0,0 +1,42 @@
package versions
import (
"io/ioutil"
"local/notes-server/config"
"os"
"path"
"testing"
)
func TestVersions(t *testing.T) {
d, err := ioutil.TempDir(os.TempDir(), "prefix")
config.Root = d
if err != nil {
t.Fatal(err)
}
for _, f := range []string{"a", "b"} {
ioutil.WriteFile(path.Join(d, f+".md"), []byte(f), os.ModePerm)
}
v, err := New()
if err != nil {
t.Fatal(err)
}
if err := v.AddAll(); err != nil {
t.Error(err)
}
if err := v.Commit(); err != nil {
t.Error(err)
}
if err := v.Gitmmit(); err == nil {
t.Error(err)
}
for _, f := range []string{"c", "b"} {
ioutil.WriteFile(path.Join(d, f+".md"), []byte("d"), os.ModePerm)
}
if err := v.Gitmmit(); err != nil {
t.Error(config.Root, err)
}
}

View File

@@ -12,6 +12,9 @@
padding: .5pt; padding: .5pt;
border-radius: 6px; border-radius: 6px;
} }
nav li li li li {
display: none;
}
img { img {
max-height: 400px; max-height: 400px;
} }