1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-10-30 11:46:16 +00:00
mycorrhiza/files/files.go

70 lines
2.2 KiB
Go
Raw Normal View History

2021-05-11 08:33:00 +00:00
// Package files is used to get paths to different files Mycorrhiza uses. Also see cfg.
package files
import (
2021-06-19 04:51:10 +00:00
"os"
"path"
2021-04-28 10:12:36 +00:00
2021-06-19 04:51:10 +00:00
"github.com/bouncepaw/mycorrhiza/cfg"
)
var paths struct {
2021-06-19 04:51:10 +00:00
gitRepo string
cacheDir string
staticFiles string
tokensJSON string
registrationCredentialsJSON string
fixedCredentialsJSON string
}
2021-06-19 04:51:10 +00:00
// HyphaeDir returns the path to hyphae storage.
// A separate function is needed to easily know where a general storage path is
// needed rather than a concrete Git or the whole wiki storage path, so that we
// could easily refactor things later if we'll ever support different storages.
func HyphaeDir() string { return paths.gitRepo }
// GitRepo returns the path to the Git repository of the wiki.
func GitRepo() string { return paths.gitRepo }
// StaticFiles returns the path to static files directory
func StaticFiles() string { return paths.staticFiles }
// TokensJSON returns the path to the JSON user tokens storage.
2021-05-11 08:33:00 +00:00
func TokensJSON() string { return paths.tokensJSON }
2021-06-19 04:51:10 +00:00
// RegistrationCredentialsJSON returns the path to the JSON registration
// credentials storage.
func RegistrationCredentialsJSON() string { return paths.registrationCredentialsJSON }
2021-05-11 08:33:00 +00:00
2021-06-19 04:51:10 +00:00
// FixedCredentialsJSON returns the path to the JSON fixed credentials storage.
2021-05-11 08:33:00 +00:00
func FixedCredentialsJSON() string { return paths.fixedCredentialsJSON }
2021-06-19 04:51:10 +00:00
// PrepareWikiRoot ensures all needed directories and files exist and have
// correct permissions.
func PrepareWikiRoot() error {
if err := os.MkdirAll(cfg.WikiDir, os.ModeDir|0777); err != nil {
return err
}
2021-06-19 04:51:10 +00:00
paths.cacheDir = path.Join(cfg.WikiDir, "cache")
if err := os.MkdirAll(paths.cacheDir, os.ModeDir|0777); err != nil {
return err
}
2021-06-19 04:51:10 +00:00
paths.gitRepo = path.Join(cfg.WikiDir, "wiki.git")
if err := os.MkdirAll(paths.gitRepo, os.ModeDir|0777); err != nil {
return err
}
2021-04-28 10:37:04 +00:00
2021-06-19 04:51:10 +00:00
paths.staticFiles = path.Join(cfg.WikiDir, "static")
if err := os.MkdirAll(paths.staticFiles, os.ModeDir|0777); err != nil {
return err
}
2021-04-28 10:37:04 +00:00
2021-06-19 04:51:10 +00:00
paths.tokensJSON = path.Join(paths.cacheDir, "tokens.json")
paths.fixedCredentialsJSON = path.Join(cfg.WikiDir, "fixed-users.json")
paths.registrationCredentialsJSON = path.Join(paths.cacheDir, "registered-users.json")
2021-06-19 04:51:10 +00:00
return nil
}