add/rm namespaces via cli
This commit is contained in:
@@ -364,8 +364,8 @@ func (n *Namespace) GetAllPasswords() ([]password.Password, error) {
|
||||
}
|
||||
|
||||
func (n *Namespace) Close() {
|
||||
n.Datastore.Close()
|
||||
n.CancelFunc()
|
||||
n.Datastore.Close()
|
||||
}
|
||||
|
||||
type StorageHandler struct {
|
||||
@@ -380,15 +380,32 @@ type StorageHandler struct {
|
||||
ConfigPath string
|
||||
}
|
||||
|
||||
func (s *StorageHandler) GetSelfPeer() Peer {
|
||||
return Peer {
|
||||
Id: s.Host.ID().String(),
|
||||
Key: s.Key.Recipient().String(),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *StorageHandler) UpdateConfig() {
|
||||
fmt.Println("Updating Config...")
|
||||
s.recreateConfig()
|
||||
s.writeConfig(s.ConfigPath, s.Config)
|
||||
}
|
||||
|
||||
func (s *StorageHandler) recreateConfig() {
|
||||
for idx, namespaceConfig := range s.Config {
|
||||
s.Config[idx].Peers = s.Namespaces[namespaceConfig.Name].TrustedPeers
|
||||
var newCfg []NamespaceConfig
|
||||
for key, val := range s.Namespaces {
|
||||
newCfg = append(newCfg, NamespaceConfig{
|
||||
Name: key,
|
||||
Id: val.ID,
|
||||
Peers: val.TrustedPeers,
|
||||
})
|
||||
}
|
||||
s.Config = newCfg
|
||||
//for idx, namespaceConfig := range s.Config {
|
||||
// s.Config[idx].Peers = s.Namespaces[namespaceConfig.Name].TrustedPeers
|
||||
//}
|
||||
}
|
||||
|
||||
func (s *StorageHandler) writeConfig(filename string, config []NamespaceConfig) error {
|
||||
@@ -454,7 +471,7 @@ func (s *StorageHandler) GetDefaultNamespace(Name string) *Namespace {
|
||||
func (s *StorageHandler) InitNamespaces() {
|
||||
NamespaceMap := make(map[string]*Namespace)
|
||||
for _, nsCfg := range s.Config {
|
||||
ns1, err := CreateNamespace(nsCfg.Id, *s)
|
||||
ns1, err := CreateNamespace(nsCfg.Id, s)
|
||||
|
||||
if err != nil {
|
||||
logger.Fatal(err)
|
||||
@@ -492,7 +509,49 @@ func PrintDBContent(ctx context.Context, store *badger.Datastore) {
|
||||
}
|
||||
}
|
||||
|
||||
func CreateNamespace(ID string, storageHandler StorageHandler) (*Namespace, error) {
|
||||
func (s *StorageHandler) ListNamespaces() []string {
|
||||
var result []string
|
||||
for k, _ := range s.Namespaces {
|
||||
result = append(result, k)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func (s *StorageHandler) DeleteNamespace(ID string) error {
|
||||
ns, ok := s.Namespaces[ID]
|
||||
|
||||
if !ok {
|
||||
fmt.Print("DeleteNamespace that does not exists")
|
||||
return nil
|
||||
}
|
||||
|
||||
delete(s.Namespaces, ID)
|
||||
ns.Close()
|
||||
s.UpdateConfig()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *StorageHandler) AddNamespace(Name string) (*Namespace, error) {
|
||||
ns, ok := s.Namespaces[Name]
|
||||
|
||||
if ok {
|
||||
return ns, nil
|
||||
}
|
||||
|
||||
result, err := CreateNamespace(uuid.New().String(), s)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result.TrustedPeers = append(result.TrustedPeers, s.GetSelfPeer())
|
||||
s.Namespaces[Name] = result
|
||||
s.UpdateConfig()
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func CreateNamespace(ID string, storageHandler *StorageHandler) (*Namespace, error) {
|
||||
fmt.Printf("Creating Namespace %s\n", ID)
|
||||
err := storageHandler.PubSub.RegisterTopicValidator(
|
||||
ID, //== topicName
|
||||
@@ -537,7 +596,7 @@ func CreateNamespace(ID string, storageHandler StorageHandler) (*Namespace, erro
|
||||
val, ok := PeerMap[ID]
|
||||
|
||||
if !ok {
|
||||
logger.Fatal("namespace config does not contain any peers")
|
||||
fmt.Println("namespace config does not contain any peers")
|
||||
}
|
||||
|
||||
return &Namespace{ID: ID, Datastore: crdt, CancelFunc: psubCancel, ctx: storageHandler.Ctx, Key: storageHandler.Key, TrustedPeers: val}, nil
|
||||
|
||||
Reference in New Issue
Block a user