MCPcopy
hub / github.com/ddworken/hishtory / registerAndBootstrapDevice

Function registerAndBootstrapDevice

client/cmd/install.go:646–675  ·  view source on GitHub ↗
(ctx context.Context, config *hctx.ClientConfig, db *gorm.DB, userSecret string)

Source from the content-addressed store, hash-verified

644}
645
646func registerAndBootstrapDevice(ctx context.Context, config *hctx.ClientConfig, db *gorm.DB, userSecret string) error {
647 registerPath := "/api/v1/register?user_id=" + data.UserId(userSecret) + "&device_id=" + config.DeviceId
648 if isIntegrationTestDevice() {
649 registerPath += "&is_integration_test_device=true"
650 }
651 _, err := lib.ApiGet(ctx, registerPath)
652 if err != nil {
653 return fmt.Errorf("failed to register device with backend: %w", err)
654 }
655
656 respBody, err := lib.ApiGet(ctx, "/api/v1/bootstrap?user_id="+data.UserId(userSecret)+"&device_id="+config.DeviceId)
657 if err != nil {
658 return fmt.Errorf("failed to bootstrap device from the backend: %w", err)
659 }
660 var retrievedEntries []*shared.EncHistoryEntry
661 err = json.Unmarshal(respBody, &retrievedEntries)
662 if err != nil {
663 return fmt.Errorf("failed to load JSON response: %w", err)
664 }
665 hctx.GetLogger().Infof("Bootstrapping new device: Found %d entries", len(retrievedEntries))
666 for _, entry := range retrievedEntries {
667 decEntry, err := data.DecryptHistoryEntry(userSecret, *entry)
668 if err != nil {
669 return fmt.Errorf("failed to decrypt history entry from server: %w", err)
670 }
671 lib.AddToDbIfNew(db, decEntry)
672 }
673
674 return nil
675}
676
677func isIntegrationTestDevice() bool {
678 if os.Getenv("HISHTORY_TEST") != "" {

Callers 2

setupFunction · 0.85
switchToOnlineFunction · 0.85

Calls 6

UserIdFunction · 0.92
ApiGetFunction · 0.92
GetLoggerFunction · 0.92
DecryptHistoryEntryFunction · 0.92
AddToDbIfNewFunction · 0.92
isIntegrationTestDeviceFunction · 0.85

Tested by

no test coverage detected