Bootstrap Python rewrite skeleton

This commit is contained in:
2026-04-19 20:19:58 +02:00
parent 7818a3fb44
commit 31390882ef
72 changed files with 2273 additions and 62 deletions
@@ -0,0 +1,40 @@
/*
Copyright © 2024 Tianyu Liu
*/
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
// addgpxCmd represents the addgpx command
var addgpxCmd = &cobra.Command{
Use: "addgpx",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("addgpx called")
},
}
func init() {
rootCmd.AddCommand(addgpxCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// addgpxCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// addgpxCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
@@ -0,0 +1,51 @@
/*
Copyright © 2024 Tianyu Liu
*/
package cmd
import (
"os"
"github.com/spf13/cobra"
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "location_recorder",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.location_recorder.yaml)")
// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
@@ -0,0 +1,11 @@
/*
Copyright © 2024 Tianyu Liu
*/
package main
import "github.com/t-liu93/home-automation-backend/helper/location_recorder/cmd"
func main() {
cmd.Execute()
}
@@ -0,0 +1,127 @@
/*
Copyright © 2024 Tianyu Liu
*/
package cmd
import (
"context"
"fmt"
"log"
"os"
"time"
"github.com/jomei/notionapi"
"github.com/spf13/cobra"
"golang.org/x/term"
)
var notionToken string
var notionTableId string
// reverseCmd represents the reverse command
var reverseCmd = &cobra.Command{
Use: "reverse",
Short: "Reverse given poo recording table",
Long: `Reverse the given poo recording table. Provide the Notion API token and the table ID to reverse.
The Notion API token can be obtained from https://www.notion.so/my-integrations. The table ID can be obtained from the URL of the table.
The token and table ID will be input in the following prompt.
`,
Run: readCredentials,
}
func readCredentials(cmd *cobra.Command, args []string) {
if notionToken == "" || notionTableId == "" {
fmt.Print("Enter Notion API token: ")
pw, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
log.Fatalf("failed to read NOTION API Token: %v", err)
}
notionToken = string(pw)
fmt.Print("\nEnter Notion table ID: ")
tableId, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
log.Fatalf("failed to read NOTION table ID: %v", err)
}
notionTableId = string(tableId)
}
reverseRun()
}
func reverseRun() {
client := notionapi.NewClient(notionapi.Token(notionToken))
rows := []notionapi.Block{}
fmt.Println("Reverse table ID: ", notionTableId)
block, err := client.Block.Get(context.Background(), notionapi.BlockID(notionTableId))
if err != nil {
log.Fatalf("Failed to get table detail: %v", err)
}
if block.GetType().String() != "table" {
log.Fatalf("Block ID %s is not a table", notionTableId)
}
headerBlock, _ := client.Block.GetChildren(context.Background(), notionapi.BlockID(notionTableId), &notionapi.Pagination{
StartCursor: "",
PageSize: 100,
})
headerId := headerBlock.Results[0].GetID()
nextCursor := headerId.String()
hasMore := true
for hasMore {
blockChildren, _ := client.Block.GetChildren(context.Background(), notionapi.BlockID(notionTableId), &notionapi.Pagination{
StartCursor: notionapi.Cursor(nextCursor),
PageSize: 100,
})
rows = append(rows, blockChildren.Results...)
hasMore = blockChildren.HasMore
nextCursor = blockChildren.NextCursor
}
rows = rows[1:]
rowsR := reverseTable(rows)
nrRowsToDelete := len(rowsR)
for index, row := range rowsR {
client.Block.Delete(context.Background(), row.GetID())
if index%10 == 0 || index == nrRowsToDelete-1 {
fmt.Printf("Deleted %d/%d rows\n", index, nrRowsToDelete)
}
time.Sleep(400 * time.Millisecond)
}
after := headerId
fmt.Println("Writing rows back to table")
for len(rowsR) > 0 {
var rowsToWrite []notionapi.Block
if len(rowsR) > 100 {
rowsToWrite = rowsR[:100]
} else {
rowsToWrite = rowsR
}
client.Block.AppendChildren(context.Background(), notionapi.BlockID(notionTableId), &notionapi.AppendBlockChildrenRequest{
After: after,
Children: rowsToWrite,
})
after = rowsToWrite[len(rowsToWrite)-1].GetID()
rowsR = rowsR[len(rowsToWrite):]
}
}
func reverseTable[T any](rows []T) []T {
for i, j := 0, len(rows)-1; i < j; i, j = i+1, j-1 {
rows[i], rows[j] = rows[j], rows[i]
}
return rows
}
func init() {
rootCmd.AddCommand(reverseCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// reverseCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// reverseCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
reverseCmd.Flags().StringVar(&notionToken, "token", "", "Notion API token")
reverseCmd.Flags().StringVar(&notionTableId, "table-id", "", "Notion table id to reverse")
}
@@ -0,0 +1,39 @@
/*
Copyright © 2024 Tianyu Liu
*/
package cmd
import (
"os"
"github.com/spf13/cobra"
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "poo_recorder_helper",
Short: "Poo recorder helper executables.",
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.poo_recorder_helper.yaml)")
// Cobra also supports local flags, which will only run
// when this action is called directly.
}
@@ -0,0 +1,11 @@
/*
Copyright © 2024 Tianyu Liu
*/
package main
import "github.com/t-liu93/home-automation-backend/helper/poo_recorder_helper/cmd"
func main() {
cmd.Execute()
}