mirror of
https://github.com/osmarks/mycorrhiza.git
synced 2024-12-05 02:29:54 +00:00
Move some file-related stuff to a separate module
This commit is contained in:
parent
bcdb38bd68
commit
53981c28ca
89
files/files.go
Normal file
89
files/files.go
Normal file
@ -0,0 +1,89 @@
|
||||
package files
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/adrg/xdg"
|
||||
"github.com/bouncepaw/mycorrhiza/util"
|
||||
"log"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var paths struct {
|
||||
tokensJSON string
|
||||
registrationCredentialsJSON string
|
||||
fixedCredentialsJSON string
|
||||
configINI string
|
||||
}
|
||||
|
||||
func TokensJSON() string { return paths.tokensJSON }
|
||||
func RegistrationCredentialsJSON() string { return paths.registrationCredentialsJSON }
|
||||
func FixedCredentialsJSON() string { return paths.fixedCredentialsJSON }
|
||||
func ConfigINI() string { return paths.configINI }
|
||||
|
||||
// CalculatePaths looks for all external paths and stores them. Tries its best to find any errors. It is safe it to call it multiple times in order to save new paths.
|
||||
func CalculatePaths() error {
|
||||
if dir, err := registrationCredentialsPath(); err != nil {
|
||||
return err
|
||||
} else {
|
||||
paths.registrationCredentialsJSON = dir
|
||||
}
|
||||
|
||||
if dir, err := tokenStoragePath(); err != nil {
|
||||
return err
|
||||
} else {
|
||||
paths.tokensJSON = dir
|
||||
}
|
||||
|
||||
if dir, err := fixedCredentialsPath(); err != nil {
|
||||
return err
|
||||
} else {
|
||||
paths.tokensJSON = dir
|
||||
}
|
||||
|
||||
if dir, err := configPath(); err != nil {
|
||||
return err
|
||||
} else {
|
||||
paths.configINI = dir
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func tokenStoragePath() (string, error) {
|
||||
dir, err := xdg.DataFile("mycorrhiza/tokens.json")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if strings.HasPrefix(dir, util.WikiDir) {
|
||||
return "", errors.New("wiki storage directory includes private config files")
|
||||
}
|
||||
return dir, nil
|
||||
}
|
||||
|
||||
func registrationCredentialsPath() (string, error) {
|
||||
path, err := filepath.Abs(util.RegistrationCredentialsPath)
|
||||
if err != nil {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
if path == "" {
|
||||
dir, err := xdg.DataFile("mycorrhiza/registration.json")
|
||||
if err != nil {
|
||||
log.Println("Error: cannot get a file to registration credentials, so no registered users will be saved:", err)
|
||||
return "", err
|
||||
}
|
||||
path = dir
|
||||
}
|
||||
return path, nil
|
||||
}
|
||||
|
||||
func fixedCredentialsPath() (string, error) {
|
||||
path, err := filepath.Abs(util.FixedCredentialsPath)
|
||||
return path, err
|
||||
}
|
||||
|
||||
func configPath() (string, error) {
|
||||
path, err := filepath.Abs(util.ConfigFilePath)
|
||||
return path, err
|
||||
}
|
3
flag.go
3
flag.go
@ -41,9 +41,6 @@ func parseCliArgs() {
|
||||
log.Fatal("Error: pass a wiki directory")
|
||||
}
|
||||
|
||||
// It is ok if the path is ""
|
||||
util.ReadConfigFile(util.ConfigFilePath)
|
||||
|
||||
var err error
|
||||
WikiDir, err = filepath.Abs(args[0])
|
||||
util.WikiDir = WikiDir
|
||||
|
7
main.go
7
main.go
@ -6,6 +6,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/bouncepaw/mycorrhiza/files"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"math/rand"
|
||||
@ -178,6 +179,12 @@ Crawl-delay: 5`))
|
||||
|
||||
func main() {
|
||||
parseCliArgs()
|
||||
if err := files.CalculatePaths(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
// It is ok if the path is ""
|
||||
util.ReadConfigFile(files.ConfigINI())
|
||||
|
||||
log.Println("Running MycorrhizaWiki")
|
||||
if err := os.Chdir(WikiDir); err != nil {
|
||||
log.Fatal(err)
|
||||
|
@ -2,13 +2,11 @@ package user
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/bouncepaw/mycorrhiza/files"
|
||||
"github.com/bouncepaw/mycorrhiza/util"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/adrg/xdg"
|
||||
"github.com/bouncepaw/mycorrhiza/util"
|
||||
)
|
||||
|
||||
// ReadUsersFromFilesystem reads all user information from filesystem and stores it internally. Call it during initialization.
|
||||
@ -41,13 +39,13 @@ func usersFromFile(path string, source UserSource) (users []*User) {
|
||||
}
|
||||
|
||||
func usersFromFixedCredentials() (users []*User) {
|
||||
users = usersFromFile(util.FixedCredentialsPath, SourceFixed)
|
||||
users = usersFromFile(files.FixedCredentialsJSON(), SourceFixed)
|
||||
log.Println("Found", len(users), "fixed users")
|
||||
return users
|
||||
}
|
||||
|
||||
func usersFromRegistrationCredentials() (users []*User) {
|
||||
users = usersFromFile(registrationCredentialsPath(), SourceRegistration)
|
||||
users = usersFromFile(files.RegistrationCredentialsJSON(), SourceRegistration)
|
||||
log.Println("Found", len(users), "registered users")
|
||||
return users
|
||||
}
|
||||
@ -60,7 +58,7 @@ func rememberUsers(uu []*User) {
|
||||
}
|
||||
|
||||
func readTokensToUsers() {
|
||||
contents, err := ioutil.ReadFile(tokenStoragePath())
|
||||
contents, err := ioutil.ReadFile(files.TokensJSON())
|
||||
if os.IsNotExist(err) {
|
||||
return
|
||||
}
|
||||
@ -80,33 +78,6 @@ func readTokensToUsers() {
|
||||
log.Println("Found", len(tmp), "active sessions")
|
||||
}
|
||||
|
||||
// Return path to tokens.json. Creates folders if needed.
|
||||
func tokenStoragePath() string {
|
||||
dir, err := xdg.DataFile("mycorrhiza/tokens.json")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if strings.HasPrefix(dir, util.WikiDir) {
|
||||
log.Fatal("Error: Wiki storage directory includes private config files")
|
||||
}
|
||||
log.Println("Path for saving tokens:", dir)
|
||||
return dir
|
||||
}
|
||||
|
||||
func registrationCredentialsPath() string {
|
||||
path := util.RegistrationCredentialsPath
|
||||
if path == "" {
|
||||
dir, err := xdg.DataFile("mycorrhiza/registration.json")
|
||||
if err != nil {
|
||||
// No error handling, because the program will fail later anyway when trying to read file ""
|
||||
log.Println("Error: cannot get a file to registration credentials, so no registered users will be saved.")
|
||||
} else {
|
||||
path = dir
|
||||
}
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
func dumpRegistrationCredentials() error {
|
||||
tmp := []*User{}
|
||||
|
||||
@ -124,7 +95,7 @@ func dumpRegistrationCredentials() error {
|
||||
log.Println(err)
|
||||
return err
|
||||
}
|
||||
err = ioutil.WriteFile(registrationCredentialsPath(), blob, 0644)
|
||||
err = ioutil.WriteFile(files.RegistrationCredentialsJSON(), blob, 0644)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return err
|
||||
@ -146,6 +117,6 @@ func dumpTokens() {
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
} else {
|
||||
ioutil.WriteFile(tokenStoragePath(), blob, 0644)
|
||||
ioutil.WriteFile(files.TokensJSON(), blob, 0644)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user