diff --git a/app-cache.go b/app-cache.go deleted file mode 100644 index a7f458e..0000000 --- a/app-cache.go +++ /dev/null @@ -1,49 +0,0 @@ -package main - -import "federated.computer/wp-sync-slowtwitch/services/wordpress" - -type AppCache struct { - UsersCache map[string]wordpress.UserData - TagCache map[string]wordpress.TagData - CategoryCache map[string]wordpress.CategoryData -} - -func (appCache *AppCache) GetUser(username string) (wordpress.UserData, bool) { - user, ok := appCache.UsersCache[username] - return user, ok -} - -func (appCache *AppCache) SetUser(user wordpress.UserData) { - if appCache.UsersCache == nil { - appCache.UsersCache = make(map[string]wordpress.UserData) - } - appCache.UsersCache[user.Name] = user -} - -func (appCache *AppCache) GetTag(tagName string) (wordpress.TagData, bool) { - tag, ok := appCache.TagCache[tagName] - return tag, ok -} - -func (appCache *AppCache) SetTag(tag wordpress.TagData) { - if appCache.TagCache == nil { - appCache.TagCache = make(map[string]wordpress.TagData) - } - appCache.TagCache[tag.Name] = tag -} - -func (appCache *AppCache) GetCategory(categoryName string) (wordpress.CategoryData, bool) { - if appCache.CategoryCache != nil { - category, ok := appCache.CategoryCache[categoryName] - return category, ok - } - - return wordpress.CategoryData{}, false -} - -func (appCache *AppCache) SetCategory(category wordpress.CategoryData) { - if appCache.CategoryCache == nil { - appCache.CategoryCache = make(map[string]wordpress.CategoryData) - } - appCache.CategoryCache[category.Name] = category -} diff --git a/main.go b/main.go index 51fdcaf..15ee2e5 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "database/sql" "federated.computer/wp-sync-slowtwitch/services/migration" + "fmt" ) const baseUrl = "https://slowtwitch.cloud/" @@ -16,7 +17,6 @@ const migrationDbName = "slowtwitch_transfer" const federatedDbUrl = "slowtwitch.northend.network" const federatedDbPort = "3306" -var appCache AppCache var slowtwitchDB *sql.DB var resultsDB *sql.DB @@ -35,6 +35,7 @@ func main() { resultsDB = resultsDatabase } + fmt.Println("Migrating Authors and Editors") editorMigration := migration.MigrateAuthors{ SlowtwitchDatabase: slowtwitchDB, ResultsDatabase: resultsDB, @@ -42,7 +43,10 @@ func main() { WordpressUser: wordpressKey, WordpressPassword: wordpressSecret, } - editorMigration.Execute() + editorResults := editorMigration.Execute() + fmt.Println("Migrated", len(editorResults), "Editors and Authors") + + fmt.Println("Migrating Categories") categoryMigration := migration.MigrateCategories{ SlowtwitchDatabase: slowtwitchDB, ResultsDatabase: resultsDB, @@ -50,7 +54,10 @@ func main() { WordpressUser: wordpressKey, WordpressPassword: wordpressSecret, } - categoryMigration.Execute() + categoryResults := categoryMigration.Execute() + fmt.Println("Migrated", len(categoryResults), "Categories") + + fmt.Println("Migrating Posts") postMigration := migration.MigratePosts{ SlowtwitchDatabase: slowtwitchDB, ResultsDatabase: resultsDB, @@ -59,4 +66,5 @@ func main() { WordpressPassword: wordpressSecret, } postMigration.Execute() + fmt.Println("Migration complete, please check database for results.") } diff --git a/services/migration/migrate-categories.go b/services/migration/migrate-categories.go index de0a2dc..616334b 100644 --- a/services/migration/migrate-categories.go +++ b/services/migration/migrate-categories.go @@ -23,7 +23,6 @@ func (migration *MigrateCategories) Execute() []CategoryResult { hasBeenMigrated := HasBeenMigrated(category.Id, migration.ResultsDatabase) if hasBeenMigrated { - fmt.Println("This category has already been migrated.") continue } diff --git a/services/migration/migrate-posts.go b/services/migration/migrate-posts.go index b868954..22f4158 100644 --- a/services/migration/migrate-posts.go +++ b/services/migration/migrate-posts.go @@ -27,16 +27,12 @@ func (migration MigratePosts) Execute() { if err != nil { panic("Could not migrate posts:" + err.Error()) } + //get wordpress tag data, there are only 3 tags := []string{"swim", "bike", "run"} - var wpTagData []wordpress.TagData - - for _, tag := range tags { - tagData, ok := wordpress.GetTag(tag, migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword) - if ok == false { - panic("could not get tag data from wp") - } - wpTagData = append(wpTagData, tagData) + wpTagData, wpTagErr := wordpress.GetTags(tags, migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword) + if wpTagErr != nil { + panic("Could not migrate posts due to tags not found:" + wpTagErr.Error()) } slowtwitchPostIdsForMigration := getPostIdsThatNeedMigration(slowtwitchPostIds, migratedPostIds) diff --git a/services/wordpress/get-tag.go b/services/wordpress/get-tag.go index d35f018..b9cfbb5 100644 --- a/services/wordpress/get-tag.go +++ b/services/wordpress/get-tag.go @@ -2,6 +2,7 @@ package wordpress import ( "encoding/json" + "errors" "federated.computer/wp-sync-slowtwitch/utilities" "net/url" ) @@ -12,6 +13,18 @@ type TagData struct { Slug string `json:"slug"` } +func GetTags(tags []string, baseUrl, user, pass string) ([]TagData, error) { + var wpTagData []TagData + for _, tag := range tags { + tagData, ok := GetTag(tag, baseUrl, user, pass) + if ok == false { + return wpTagData, errors.New("Tag Not Found") + } + wpTagData = append(wpTagData, tagData) + } + return wpTagData, nil +} + func GetTag(tagName, baseUrl, user, pass string) (TagData, bool) { endpoint := baseUrl + "wp-json/wp/v2/tags?search=" + url.QueryEscape(tagName) body := utilities.GetHttpRequestToWordpress(endpoint, user, pass)