2022-07-26 22:00:59 +02:00
|
|
|
package zrokdir
|
|
|
|
|
|
|
|
import (
|
2022-08-22 22:32:50 +02:00
|
|
|
"encoding/json"
|
2022-08-23 16:43:24 +02:00
|
|
|
"fmt"
|
2022-08-22 22:32:50 +02:00
|
|
|
"github.com/pkg/errors"
|
2022-07-26 22:00:59 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
2022-08-22 22:32:50 +02:00
|
|
|
type Environment struct {
|
|
|
|
ZrokToken string `json:"zrok_token"`
|
|
|
|
ZitiIdentityId string `json:"ziti_identity"`
|
2022-07-26 22:00:59 +02:00
|
|
|
}
|
|
|
|
|
2022-09-02 19:07:27 +02:00
|
|
|
func Delete() error {
|
|
|
|
path, err := zrokDir()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.RemoveAll(path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-22 22:32:50 +02:00
|
|
|
func LoadEnvironment() (*Environment, error) {
|
|
|
|
ef, err := environmentFile()
|
2022-07-26 22:00:59 +02:00
|
|
|
if err != nil {
|
2022-08-22 22:32:50 +02:00
|
|
|
return nil, errors.Wrap(err, "error getting environment file")
|
2022-07-26 22:00:59 +02:00
|
|
|
}
|
2022-08-22 22:32:50 +02:00
|
|
|
data, err := os.ReadFile(ef)
|
2022-07-26 23:17:37 +02:00
|
|
|
if err != nil {
|
2022-08-22 22:32:50 +02:00
|
|
|
return nil, errors.Wrapf(err, "error reading environment file '%v'", ef)
|
2022-07-26 23:17:37 +02:00
|
|
|
}
|
2022-08-22 22:32:50 +02:00
|
|
|
env := &Environment{}
|
|
|
|
if err := json.Unmarshal(data, env); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error unmarshaling environment file '%v'", ef)
|
2022-07-26 23:17:37 +02:00
|
|
|
}
|
2022-08-22 22:32:50 +02:00
|
|
|
return env, nil
|
2022-07-26 23:17:37 +02:00
|
|
|
}
|
|
|
|
|
2022-08-22 22:32:50 +02:00
|
|
|
func SaveEnvironment(env *Environment) error {
|
|
|
|
data, err := json.MarshalIndent(env, "", " ")
|
2022-07-26 23:17:37 +02:00
|
|
|
if err != nil {
|
2022-08-22 22:32:50 +02:00
|
|
|
return errors.Wrap(err, "error marshaling environment")
|
2022-07-26 23:17:37 +02:00
|
|
|
}
|
2022-08-22 22:32:50 +02:00
|
|
|
ef, err := environmentFile()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error getting environment file")
|
|
|
|
}
|
2022-08-23 16:43:24 +02:00
|
|
|
if err := os.MkdirAll(filepath.Dir(ef), os.FileMode(0700)); err != nil {
|
|
|
|
return errors.Wrapf(err, "error creating zrokdir path '%v'", filepath.Dir(ef))
|
|
|
|
}
|
2022-08-22 22:32:50 +02:00
|
|
|
if err := os.WriteFile(ef, data, os.FileMode(0600)); err != nil {
|
|
|
|
return errors.Wrap(err, "error saving environment file")
|
2022-07-26 23:17:37 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-23 16:43:24 +02:00
|
|
|
func WriteZitiIdentity(name, data string) error {
|
|
|
|
zif, err := ZitiIdentityFile(name)
|
2022-07-26 22:00:59 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-23 16:43:24 +02:00
|
|
|
if err := os.MkdirAll(filepath.Dir(zif), os.FileMode(0700)); err != nil {
|
|
|
|
return errors.Wrapf(err, "error creating zrokdir path '%v'", filepath.Dir(zif))
|
|
|
|
}
|
|
|
|
if err := os.WriteFile(zif, []byte(data), os.FileMode(0600)); err != nil {
|
|
|
|
return errors.Wrapf(err, "error writing ziti identity file '%v'", zif)
|
2022-07-26 22:00:59 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-23 16:43:24 +02:00
|
|
|
func ZitiIdentityFile(name string) (string, error) {
|
|
|
|
zrd, err := zrokDir()
|
2022-07-26 22:00:59 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-08-23 16:43:24 +02:00
|
|
|
return filepath.Join(zrd, "identities", fmt.Sprintf("%v.json", name)), nil
|
2022-07-26 22:00:59 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func zrokDir() (string, error) {
|
|
|
|
home, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return filepath.Join(home, ".zrok"), nil
|
|
|
|
}
|