initial commit
This commit is contained in:
commit
18995db757
871 changed files with 492725 additions and 0 deletions
14
pkg/core/const.go
Normal file
14
pkg/core/const.go
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
// Copyright (C) 2019 Marius Schellenberger
|
||||
|
||||
package core
|
||||
|
||||
var (
|
||||
IndexURI = "/"
|
||||
TotpURI = "/totp"
|
||||
LoginURI = "/login"
|
||||
LogoutURI = "/logout"
|
||||
|
||||
AutoImport = "auto_import"
|
||||
ManualImport = "manual_import"
|
||||
Imported = "imported"
|
||||
)
|
||||
26
pkg/core/helper.go
Normal file
26
pkg/core/helper.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package core
|
||||
|
||||
import "time"
|
||||
|
||||
// Contains checks if slice a contains string str
|
||||
func Contains(str string, a []string) (int, bool) {
|
||||
for i, s := range a {
|
||||
if s == str {
|
||||
return i, true
|
||||
}
|
||||
}
|
||||
return -1, false
|
||||
}
|
||||
|
||||
const (
|
||||
timeFmt = "2006-01-02 15:04:05"
|
||||
fileFmt = "20060102_150405"
|
||||
)
|
||||
|
||||
func Now() string {
|
||||
return time.Now().Format(timeFmt)
|
||||
}
|
||||
|
||||
func FileName() string {
|
||||
return time.Now().Format(fileFmt)
|
||||
}
|
||||
41
pkg/core/permission.go
Normal file
41
pkg/core/permission.go
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
// Copyright (C) 2019 Marius Schellenberger
|
||||
|
||||
package core
|
||||
|
||||
type Permission int
|
||||
|
||||
const (
|
||||
Invalid Permission = iota
|
||||
Public
|
||||
Private
|
||||
)
|
||||
|
||||
func ParsePermString(p string) Permission {
|
||||
switch p {
|
||||
case "1":
|
||||
return Public
|
||||
case "2":
|
||||
return Private
|
||||
}
|
||||
return Invalid
|
||||
}
|
||||
|
||||
func ReadPerm(username string, p Permission, s Section) bool {
|
||||
switch p {
|
||||
case Public:
|
||||
return true
|
||||
case Private:
|
||||
_, c := Contains(username, s.Members)
|
||||
return c
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func WritePerm(username string, p Permission, s Section) bool {
|
||||
switch p {
|
||||
case Public, Private:
|
||||
_, c := Contains(username, s.Members)
|
||||
return c
|
||||
}
|
||||
return false
|
||||
}
|
||||
77
pkg/core/permission_test.go
Normal file
77
pkg/core/permission_test.go
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
// Copyright (C) 2019 Marius Schellenberger
|
||||
|
||||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParsePermString(t *testing.T) {
|
||||
data := map[string]Permission{
|
||||
"": Invalid,
|
||||
"1": Public,
|
||||
"2": Private,
|
||||
"123": Invalid,
|
||||
"test": Invalid,
|
||||
}
|
||||
for k, v := range data {
|
||||
t.Run(k, func(t *testing.T) {
|
||||
if ParsePermString(k) != v {
|
||||
t.Fail()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadPerm(t *testing.T) {
|
||||
data := []struct {
|
||||
Username string
|
||||
Perm Permission
|
||||
Section Section
|
||||
OK bool
|
||||
}{
|
||||
{"", Invalid, Section{Members: []string{"admin"}}, false},
|
||||
{"", Public, Section{Members: []string{"admin"}}, true},
|
||||
{"user", Public, Section{Members: []string{"admin"}}, true},
|
||||
{"", Private, Section{Members: []string{"admin"}}, false},
|
||||
{"user", Private, Section{Members: []string{"admin"}}, false},
|
||||
{"", Private, Section{Members: []string{"user"}}, false},
|
||||
{"user", Private, Section{Members: []string{"user"}}, true},
|
||||
{"user", Private, Section{Members: []string{"user", "admin"}}, true},
|
||||
{"admin", Private, Section{Members: []string{"user"}}, false},
|
||||
}
|
||||
for i, v := range data {
|
||||
t.Run(fmt.Sprintf("Read:%d", i), func(t *testing.T) {
|
||||
if ReadPerm(v.Username, v.Perm, v.Section) != v.OK {
|
||||
t.Fail()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestWritePerm(t *testing.T) {
|
||||
data := []struct {
|
||||
Username string
|
||||
Perm Permission
|
||||
Section Section
|
||||
OK bool
|
||||
}{
|
||||
{"", Invalid, Section{Members: []string{"user"}}, false},
|
||||
{"", Public, Section{Members: []string{"user"}}, false},
|
||||
{"user", Public, Section{Members: []string{"user"}}, true},
|
||||
{"user", Public, Section{Members: []string{"admin"}}, false},
|
||||
{"user", Private, Section{Members: []string{"user"}}, true},
|
||||
{"", Private, Section{Members: []string{"user"}}, false},
|
||||
{"", Private, Section{Members: []string{"user", "admin"}}, false},
|
||||
{"user", Private, Section{Members: []string{"user", "admin"}}, true},
|
||||
{"admin", Private, Section{Members: []string{"user", "admin"}}, true},
|
||||
}
|
||||
for i, v := range data {
|
||||
t.Run(fmt.Sprintf("Write:%d", i), func(t *testing.T) {
|
||||
if WritePerm(v.Username, v.Perm, v.Section) != v.OK {
|
||||
t.Fail()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
31
pkg/core/timeout.go
Normal file
31
pkg/core/timeout.go
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
package core
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Timeout(cmd *exec.Cmd, t time.Duration) (output []byte, err error) {
|
||||
done := make(chan error)
|
||||
out := make(chan []byte)
|
||||
go func() {
|
||||
o, e := cmd.Output()
|
||||
done <- e
|
||||
out <- o
|
||||
}()
|
||||
select {
|
||||
case <-time.After(t):
|
||||
e := cmd.Process.Kill()
|
||||
<-out
|
||||
if e != nil {
|
||||
<-done
|
||||
err = e
|
||||
return
|
||||
}
|
||||
err = <-done
|
||||
case e := <-done:
|
||||
err = e
|
||||
output = <-out
|
||||
}
|
||||
return
|
||||
}
|
||||
76
pkg/core/types.go
Normal file
76
pkg/core/types.go
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
// Copyright (C) 2019 Marius Schellenberger
|
||||
|
||||
package core
|
||||
|
||||
import "html/template"
|
||||
|
||||
type Page struct {
|
||||
Title string `json:"title"`
|
||||
StoreTitle string `json:"store_title"`
|
||||
Markdown string `json:"markdown"`
|
||||
TOC template.HTML `json:"toc"`
|
||||
HTML template.HTML `json:"html"`
|
||||
Created string `json:"created"`
|
||||
Updated string `json:"updated"`
|
||||
Perm Permission `json:"perm"`
|
||||
}
|
||||
|
||||
type Pages []Page
|
||||
|
||||
func (p Pages) Len() int { return len(p) }
|
||||
func (p Pages) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
||||
func (p Pages) Less(i, j int) bool { return p[i].StoreTitle < p[j].StoreTitle }
|
||||
|
||||
type UpdatedPages []Page
|
||||
|
||||
func (p UpdatedPages) Len() int { return len(p) }
|
||||
func (p UpdatedPages) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
||||
func (p UpdatedPages) Less(i, j int) bool { return p[i].Updated > p[j].Updated }
|
||||
|
||||
type Section struct {
|
||||
Name string
|
||||
User bool
|
||||
Members []string
|
||||
}
|
||||
|
||||
type Sections []Section
|
||||
|
||||
func (s Sections) Len() int { return len(s) }
|
||||
func (s Sections) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
||||
func (s Sections) Less(i, j int) bool { return s[i].Name < s[j].Name }
|
||||
|
||||
type SectionPage struct {
|
||||
Section string
|
||||
Pages UpdatedPages
|
||||
}
|
||||
|
||||
type SectionPages []SectionPage
|
||||
|
||||
func (s SectionPages) Len() int { return len(s) }
|
||||
func (s SectionPages) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
||||
func (s SectionPages) Less(i, j int) bool { return s[i].Section < s[j].Section }
|
||||
|
||||
type User struct {
|
||||
Username string
|
||||
Password string
|
||||
Secret string
|
||||
Locked int
|
||||
}
|
||||
|
||||
type Result struct {
|
||||
ID string
|
||||
HTML template.HTML
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
DataDir string `json:"data_dir,omitempty"`
|
||||
User string `json:"user,omitempty"`
|
||||
Group string `json:"group,omitempty"`
|
||||
ListenAddr string `json:"listen_addr,omitempty"`
|
||||
LogFile string `json:"log_file,omitempty"`
|
||||
Version string `json:"-"`
|
||||
Secret string `json:"secret,omitempty"`
|
||||
Foreground bool `json:"foreground,omitempty"`
|
||||
SecureCookie bool `json:"secure_cookie,omitempty"`
|
||||
Debug bool `json:"debug,omitempty"`
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue