mirror of
https://github.com/casdoor/casdoor.git
synced 2025-05-23 02:35:49 +08:00
Add DumpToFile() to export init_data.json
This commit is contained in:
parent
1bf5497d08
commit
ade9de8256
121
object/init_data_dump.go
Normal file
121
object/init_data_dump.go
Normal file
@ -0,0 +1,121 @@
|
|||||||
|
// Copyright 2023 The Casdoor Authors. All Rights Reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package object
|
||||||
|
|
||||||
|
import "github.com/casdoor/casdoor/util"
|
||||||
|
|
||||||
|
func DumpToFile(filePath string) error {
|
||||||
|
return writeInitDataToFile(filePath)
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeInitDataToFile(filePath string) error {
|
||||||
|
organizations, err := GetOrganizations("admin")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
applications, err := GetApplications("admin")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
users, err := GetGlobalUsers()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
certs, err := GetCerts("")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
providers, err := GetGlobalProviders()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
ldaps, err := GetLdaps("")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
models, err := GetModels("")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
permissions, err := GetPermissions("")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
payments, err := GetPayments("")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
products, err := GetProducts("")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
resources, err := GetResources("", "")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
roles, err := GetRoles("")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
syncers, err := GetSyncers("")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
tokens, err := GetTokens("", "")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
webhooks, err := GetWebhooks("", "")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
data := &InitData{
|
||||||
|
Organizations: organizations,
|
||||||
|
Applications: applications,
|
||||||
|
Users: users,
|
||||||
|
Certs: certs,
|
||||||
|
Providers: providers,
|
||||||
|
Ldaps: ldaps,
|
||||||
|
Models: models,
|
||||||
|
Permissions: permissions,
|
||||||
|
Payments: payments,
|
||||||
|
Products: products,
|
||||||
|
Resources: resources,
|
||||||
|
Roles: roles,
|
||||||
|
Syncers: syncers,
|
||||||
|
Tokens: tokens,
|
||||||
|
Webhooks: webhooks,
|
||||||
|
}
|
||||||
|
|
||||||
|
text := util.StructToJsonFormatted(data)
|
||||||
|
util.WriteStringToPath(text, filePath)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
29
object/init_data_dump_test.go
Normal file
29
object/init_data_dump_test.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
// Copyright 2023 The Casdoor Authors. All Rights Reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
//go:build !skipCi
|
||||||
|
// +build !skipCi
|
||||||
|
|
||||||
|
package object
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestDumpToFile(t *testing.T) {
|
||||||
|
InitConfig()
|
||||||
|
|
||||||
|
err := DumpToFile("./init_data_dump.json")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user