porting all github actions from bluesky-social/indigo to tangled CI
at main 2.0 kB view raw
1package main 2 3import ( 4 "context" 5 "encoding/json" 6 "fmt" 7 "os" 8 9 "github.com/bluesky-social/indigo/api/agnostic" 10 11 "github.com/urfave/cli/v2" 12) 13 14var cmdBskyPrefs = &cli.Command{ 15 Name: "prefs", 16 Usage: "sub-commands for preferences", 17 Flags: []cli.Flag{}, 18 Subcommands: []*cli.Command{ 19 &cli.Command{ 20 Name: "export", 21 Usage: "dump preferences out as JSON", 22 Action: runBskyPrefsExport, 23 }, 24 &cli.Command{ 25 Name: "import", 26 Usage: "upload preferences from JSON file", 27 ArgsUsage: `<file>`, 28 Action: runBskyPrefsImport, 29 }, 30 }, 31} 32 33func runBskyPrefsExport(cctx *cli.Context) error { 34 ctx := context.Background() 35 36 xrpcc, err := loadAuthClient(ctx) 37 if err == ErrNoAuthSession { 38 return fmt.Errorf("auth required, but not logged in") 39 } else if err != nil { 40 return err 41 } 42 43 // TODO: does indigo API code crash with unsupported preference '$type'? Eg "Lexicon decoder" with unsupported type. 44 resp, err := agnostic.ActorGetPreferences(ctx, xrpcc) 45 if err != nil { 46 return fmt.Errorf("failed fetching old preferences: %w", err) 47 } 48 49 b, err := json.MarshalIndent(resp.Preferences, "", " ") 50 if err != nil { 51 return err 52 } 53 fmt.Println(string(b)) 54 55 return nil 56} 57 58func runBskyPrefsImport(cctx *cli.Context) error { 59 ctx := context.Background() 60 prefsPath := cctx.Args().First() 61 if prefsPath == "" { 62 return fmt.Errorf("need to provide file path as an argument") 63 } 64 65 xrpcc, err := loadAuthClient(ctx) 66 if err == ErrNoAuthSession { 67 return fmt.Errorf("auth required, but not logged in") 68 } else if err != nil { 69 return err 70 } 71 72 prefsBytes, err := os.ReadFile(prefsPath) 73 if err != nil { 74 return err 75 } 76 77 var prefsArray []map[string]any 78 if err = json.Unmarshal(prefsBytes, &prefsArray); err != nil { 79 return err 80 } 81 82 err = agnostic.ActorPutPreferences(ctx, xrpcc, &agnostic.ActorPutPreferences_Input{ 83 Preferences: prefsArray, 84 }) 85 if err != nil { 86 return fmt.Errorf("failed fetching old preferences: %w", err) 87 } 88 89 return nil 90}