Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
138 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
) | ||
|
||
type sqlOptions struct { | ||
path string | ||
key string | ||
} | ||
|
||
func sqlSetupFlags(flags *pflag.FlagSet, opts *sqlOptions) { | ||
flags.StringVarP(&opts.key, "sql-key", "", "s3cur3", "sqlcipher database encryption key") | ||
flags.StringVarP(&opts.path, "sql-path", "", "/tmp/berty.db", "sqlcipher database path") | ||
} | ||
|
||
func newSQLCommand() *cobra.Command { | ||
// sql (root) | ||
cmd := &cobra.Command{ | ||
Use: "sql", | ||
} | ||
cmd.AddCommand(newSQLDumpCommand()) | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
|
||
"github.com/berty/berty/core/api/p2p" | ||
"github.com/berty/berty/core/entity" | ||
"github.com/berty/berty/core/sql" | ||
"github.com/berty/berty/core/sql/sqlcipher" | ||
) | ||
|
||
type sqlDumpOptions struct { | ||
sql sqlOptions | ||
autoPreload bool | ||
} | ||
|
||
func sqlDumpSetupFlags(flags *pflag.FlagSet, opts *sqlDumpOptions) { | ||
flags.BoolVar(&opts.autoPreload, "auto-preload", false, "preload associations") | ||
} | ||
|
||
func newSQLDumpCommand() *cobra.Command { | ||
// sql dump | ||
opts := &sqlDumpOptions{} | ||
cmd := &cobra.Command{ | ||
Use: "dump", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return sqlDump(opts) | ||
}, | ||
} | ||
sqlDumpSetupFlags(cmd.Flags(), opts) | ||
sqlSetupFlags(cmd.Flags(), &opts.sql) | ||
return cmd | ||
} | ||
|
||
func sqlDump(opts *sqlDumpOptions) error { | ||
// initialize sql | ||
db, err := sqlcipher.Open(opts.sql.path, []byte(opts.sql.key)) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to open sqlcipher") | ||
} | ||
defer db.Close() | ||
if db, err = sql.Init(db); err != nil { | ||
return errors.Wrap(err, "failed to initialize sql") | ||
} | ||
|
||
db = db.Set("gorm:auto_preload", opts.autoPreload) | ||
|
||
dump := struct { | ||
Config entity.Config `json:"config"` | ||
Contacts []entity.Contact `json:"contacts"` | ||
Events []p2p.Event `json:"events"` | ||
Conversations []entity.Conversation `json:"conversations"` | ||
ConversationMembers []entity.ConversationMember `json:"conversation_members"` | ||
Devices []entity.Device `json:"devices"` | ||
}{ | ||
Contacts: []entity.Contact{}, | ||
Events: []p2p.Event{}, | ||
Conversations: []entity.Conversation{}, | ||
ConversationMembers: []entity.ConversationMember{}, | ||
Devices: []entity.Device{}, | ||
} | ||
|
||
// fetch entities | ||
if err := db.Set("gorm:auto_preload", false).Find(&dump.Config).Error; err != nil { | ||
return err | ||
} | ||
if err := db.Find(&dump.Contacts).Error; err != nil { | ||
return err | ||
} | ||
if err := db.Find(&dump.Conversations).Error; err != nil { | ||
return err | ||
} | ||
if err := db.Find(&dump.ConversationMembers).Error; err != nil { | ||
return err | ||
} | ||
if err := db.Find(&dump.Devices).Error; err != nil { | ||
return err | ||
} | ||
if err := db.Find(&dump.Events).Error; err != nil { | ||
return err | ||
} | ||
|
||
// rendering | ||
out, err := json.MarshalIndent(dump, "", " ") | ||
if err != nil { | ||
return errors.Wrap(err, "failed to marshal the dump") | ||
} | ||
fmt.Println(string(out)) | ||
|
||
return nil | ||
} |