2022-07-26 22:00:59 +02:00
|
|
|
package zrokdir
|
|
|
|
|
|
|
|
import (
|
2023-01-09 20:16:08 +01:00
|
|
|
"fmt"
|
|
|
|
"github.com/pkg/errors"
|
2022-07-26 22:00:59 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2023-01-09 20:16:08 +01:00
|
|
|
"strings"
|
2022-07-26 22:00:59 +02:00
|
|
|
)
|
|
|
|
|
2023-01-09 20:16:08 +01:00
|
|
|
type ZrokDir struct {
|
|
|
|
Env *Environment
|
|
|
|
Cfg *Config
|
|
|
|
identities map[string]struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Initialize() (*ZrokDir, error) {
|
|
|
|
zrd, err := zrokDir()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error getting zrokdir path")
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(zrd, os.FileMode(0700)); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error creating zrokdir root path '%v'", zrd)
|
|
|
|
}
|
|
|
|
if err := DeleteEnvironment(); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error deleting environment")
|
|
|
|
}
|
|
|
|
idd, err := identitiesDir()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error getting zrokdir identities path")
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(idd, os.FileMode(0700)); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error creating zrokdir identities root path '%v'", idd)
|
|
|
|
}
|
|
|
|
return Load()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Load() (*ZrokDir, error) {
|
2023-01-09 22:28:54 +01:00
|
|
|
if err := checkMetadata(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-01-09 20:16:08 +01:00
|
|
|
zrd := &ZrokDir{}
|
|
|
|
|
|
|
|
ids, err := listIdentities()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
zrd.identities = ids
|
|
|
|
|
|
|
|
hasCfg, err := hasConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if hasCfg {
|
|
|
|
cfg, err := loadConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
zrd.Cfg = cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
hasEnv, err := hasEnvironment()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if hasEnv {
|
|
|
|
env, err := loadEnvironment()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
zrd.Env = env
|
|
|
|
}
|
|
|
|
|
|
|
|
return zrd, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (zrd *ZrokDir) Save() error {
|
2023-01-09 22:28:54 +01:00
|
|
|
if err := writeMetadata(); err != nil {
|
|
|
|
return errors.Wrap(err, "error saving metadata")
|
|
|
|
}
|
2023-01-09 20:16:08 +01:00
|
|
|
if zrd.Env != nil {
|
|
|
|
if err := saveEnvironment(zrd.Env); err != nil {
|
|
|
|
return errors.Wrap(err, "error saving environment")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if zrd.Cfg != nil {
|
|
|
|
if err := saveConfig(zrd.Cfg); err != nil {
|
|
|
|
return errors.Wrap(err, "error saving config")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Obliterate() error {
|
|
|
|
zrd, err := zrokDir()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.RemoveAll(zrd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func listIdentities() (map[string]struct{}, error) {
|
2023-01-09 23:37:43 +01:00
|
|
|
ids := make(map[string]struct{})
|
|
|
|
|
2023-01-09 20:16:08 +01:00
|
|
|
idd, err := identitiesDir()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error getting zrokdir identities path")
|
|
|
|
}
|
2023-01-09 23:37:43 +01:00
|
|
|
_, err = os.Stat(idd)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return ids, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error stat-ing zrokdir identities root '%v'", idd)
|
|
|
|
}
|
2023-01-09 20:16:08 +01:00
|
|
|
des, err := os.ReadDir(idd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error listing zrokdir identities from '%v'", idd)
|
|
|
|
}
|
|
|
|
for _, de := range des {
|
|
|
|
if strings.HasSuffix(de.Name(), ".json") && !de.IsDir() {
|
|
|
|
name := strings.TrimSuffix(de.Name(), ".json")
|
|
|
|
ids[name] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ids, nil
|
|
|
|
}
|
|
|
|
|
2023-01-09 17:28:26 +01:00
|
|
|
func configFile() (string, error) {
|
|
|
|
zrd, err := zrokDir()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return filepath.Join(zrd, "config.json"), nil
|
|
|
|
}
|
|
|
|
|
2022-08-22 22:32:50 +02:00
|
|
|
func environmentFile() (string, error) {
|
|
|
|
zrd, err := zrokDir()
|
2022-07-26 22:00:59 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-08-22 22:32:50 +02:00
|
|
|
return filepath.Join(zrd, "environment.json"), nil
|
2022-07-26 22:00:59 +02:00
|
|
|
}
|
|
|
|
|
2023-01-09 20:16:08 +01:00
|
|
|
func identityFile(name string) (string, error) {
|
|
|
|
idd, err := identitiesDir()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return filepath.Join(idd, fmt.Sprintf("%v.json", name)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func identitiesDir() (string, error) {
|
|
|
|
zrd, err := zrokDir()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return filepath.Join(zrd, "identities"), nil
|
|
|
|
}
|
|
|
|
|
2023-01-09 22:28:54 +01:00
|
|
|
func metadataFile() (string, error) {
|
|
|
|
zrd, err := zrokDir()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return filepath.Join(zrd, "metadata.json"), nil
|
|
|
|
}
|
|
|
|
|
2022-07-26 22:00:59 +02:00
|
|
|
func zrokDir() (string, error) {
|
|
|
|
home, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return filepath.Join(home, ".zrok"), nil
|
|
|
|
}
|