tidy-up refactor, add readme

This commit is contained in:
ari melody 2025-06-22 20:49:19 +01:00
parent 6bde84b7e3
commit cd48757272
Signed by: ari
GPG key ID: CF99829C92678188
6 changed files with 104 additions and 77 deletions

61
main.go
View file

@ -4,6 +4,7 @@ import (
"fmt"
"os"
"slices"
"strings"
)
func main() {
@ -21,16 +22,13 @@ func main() {
var dryrun bool = false
var verbose bool = false
var importProfile bool = false
var importFollows bool = false
var importPreferences bool = false
collections := []string{}
i := 1
for {
if i >= len(os.Args) { break }
if os.Args[i][0] != '-' {
fmt.Fprintf(os.Stderr, "unrecognised argument `%s`.\n", os.Args[i])
os.Exit(1)
collections = append(collections, os.Args[i])
}
getValue := func(i int) (int, string) {
@ -58,12 +56,6 @@ func main() {
dryrun = true
case "v":
verbose = true
case "profile":
importProfile = true
case "follows":
importFollows = true
case "preferences":
importPreferences = true
}
i += 1
}
@ -72,12 +64,16 @@ func main() {
fmt.Fprintf(os.Stderr, "missing required argument -from.\n")
os.Exit(1)
}
if fromPassword == "" {
fmt.Fprintf(os.Stderr, "missing required argument -frompass.\n")
os.Exit(1)
}
if toUser == "" {
fmt.Fprintf(os.Stderr, "missing required argument -to.\n")
os.Exit(1)
}
if toPassword == "" {
fmt.Fprintf(os.Stderr, "missing required argument -pass.\n")
fmt.Fprintf(os.Stderr, "missing required argument -topass.\n")
os.Exit(1)
}
if pdsUrl == "" {
@ -86,19 +82,15 @@ func main() {
}
if fromPdsUrl == "" { fromPdsUrl = pdsUrl }
if !(importProfile || importFollows || importPreferences) {
if len(collections) == 0 {
fmt.Fprintf(os.Stderr, "no action was specified.\n")
os.Exit(1)
}
var fromSession *AtprotoSession
if !(fromPassword == "" || fromPdsUrl == "") {
var err error
fromSession, err = CreateSession(fromPdsUrl, fromUser, fromPassword)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to create session for source account: %v\n", err)
os.Exit(1)
}
fromSession, err := CreateSession(fromPdsUrl, fromUser, fromPassword)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to create session for source account: %v\n", err)
os.Exit(1)
}
toSession, err := CreateSession(pdsUrl, toUser, toPassword)
@ -107,26 +99,22 @@ func main() {
os.Exit(1)
}
if importProfile {
err := ImportProfile(toSession, pdsUrl, fromUser, dryrun, verbose)
if slices.Contains(collections, "profile") {
err := ImportProfile(fromSession, toSession, dryrun, verbose)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to import profile: %v\n", err)
os.Exit(1)
}
}
if importPreferences {
if fromSession == nil {
fmt.Fprintf(os.Stderr, "-frompass and -frompds must be provided for a preferences import.\n")
os.Exit(1)
}
err := ImportPreferences(fromSession, toSession, pdsUrl, dryrun, verbose)
if slices.Contains(collections, "preferences") {
err := ImportPreferences(fromSession, toSession, dryrun, verbose)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to import preferences: %v\n", err)
os.Exit(1)
}
}
if importFollows {
err := ImportFollows(toSession, pdsUrl, fromUser, dryrun, verbose)
if slices.Contains(collections, "follows") {
err := ImportFollows(fromSession, toSession, dryrun, verbose)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to import follows: %v\n", err)
os.Exit(1)
@ -136,7 +124,7 @@ func main() {
func printHelp() {
fmt.Printf(
`usage: %s <arguments...> <imports...>
`usage: %s <-from <identifier>> <-frompass <password>> <-to <identifier>> <-topass <identifier>> <-pds <pds-url>> [collections...]
required arguments:
-from <identifier>: the source account handle or DID.
@ -145,9 +133,10 @@ required arguments:
-topass <password>: the destination account password.
-pds <pds-url>: the full https:// url of the destination account's PDS.
imports (at least one required):
-profile: imports the account profile, including avatar, banner, display name, and description.
-follows: imports the following list of the account.
collections:
profile: imports account profile, including avatar, banner, display name, and description.
follows: imports all following accounts.
preferences: imports account preferences; including feeds, labeller settings, and content filters.
optional arguments:
-frompds <pds-url>: the source account's PDS url (defaults to the value of -pds).
@ -155,6 +144,6 @@ optional arguments:
-v: verbose output
-help: shows this help message.
`,
os.Args[0],
os.Args[0][strings.LastIndex(os.Args[0], "/") + 1:],
)
}