Modifications for new airtable library

This commit is contained in:
Ian Gulliver
2024-06-22 21:51:00 -07:00
parent ba7ee88e5c
commit 222a912e9f
4 changed files with 23 additions and 65 deletions

View File

@@ -1,45 +0,0 @@
package main
import (
"fmt"
"os"
"github.com/mehanizm/airtable"
)
type AirtableRecord = airtable.Record
type AirtableRecords = airtable.Records
type Airtable struct {
c *airtable.Client
}
func NewAirtableFromEnv() (*Airtable, error) {
airtableToken := os.Getenv("AIRTABLE_TOKEN")
if airtableToken == "" {
return nil, fmt.Errorf("please set $AIRTABLE_TOKEN")
}
return &Airtable{
c: airtable.NewClient(airtableToken),
}, nil
}
func (at *Airtable) GetBaseID(name string) (string, error) {
bases, err := at.c.GetBases().WithOffset("").Do()
if err != nil {
return "", fmt.Errorf("failed to get bases: %w", err)
}
for _, base := range bases.Bases {
if base.Name == name {
return base.ID, nil
}
}
return "", fmt.Errorf("base not found: %s", name)
}
func (at *Airtable) GetTable(baseID, name string) (*airtable.Table, error) {
return at.c.GetTable(baseID, name), nil
}

2
go.mod
View File

@@ -2,4 +2,4 @@ module github.com/flamingcow66/helios-data-pipeline
go 1.22.4
require github.com/mehanizm/airtable v0.3.1
require github.com/flamingcow66/airtable v0.0.0-20240622194452-cd3ae33c490f

4
go.sum
View File

@@ -1,2 +1,2 @@
github.com/mehanizm/airtable v0.3.1 h1:PyuSukkZScm9/onHqJWA1/kscDjp/3c0ANsQiCdJTws=
github.com/mehanizm/airtable v0.3.1/go.mod h1:0wD9HInozzelKMw8XiY6czjsDygmAg1bzxSqAha/WLg=
github.com/flamingcow66/airtable v0.0.0-20240622194452-cd3ae33c490f h1:FkmHdDSTOpTa3iSS/PMPk60yfJMvSubIMdLbTZNUmv0=
github.com/flamingcow66/airtable v0.0.0-20240622194452-cd3ae33c490f/go.mod h1:f88i3bVxrQ5UUxuBGuprC7sCpCc++5BylPwa3sxvfMQ=

37
sync.go
View File

@@ -1,6 +1,7 @@
package main
import (
"context"
"encoding/csv"
"flag"
"fmt"
@@ -9,6 +10,8 @@ import (
"os"
"slices"
"strings"
"github.com/flamingcow66/airtable"
)
type Directory struct {
@@ -50,42 +53,40 @@ func main() {
l.Info("loaded directory", "directory", dir)
at, err := NewAirtableFromEnv()
at, err := airtable.NewFromEnv()
if err != nil {
fatal(l, "failed to create airtable client", "error", err)
}
dirBaseID, err := at.GetBaseID("Directory")
dirBase, err := at.GetBaseByName(context.Background(), "Directory")
if err != nil {
fatal(l, "failed to find Directory base in Airtable", "error", err)
}
parentsTable, err := at.GetTable(dirBaseID, "Parents")
parentsTable, err := dirBase.GetTableByName(context.Background(), "Parents")
if err != nil {
fatal(l, "failed to get Parents table", "error", err)
}
remoteParentsRecords, err := parentsTable.
GetRecords().
FromView("Primary").
ReturnFields("Email", "Name").
Do()
remoteParentsRecords, err := parentsTable.ListRecords(context.Background(), nil)
if err != nil {
fatal(l, "failed to fetch Parents", "error", err)
}
l.Info("tmp", "parents", remoteParentsRecords)
l.Info("tmp", "parents", remoteParentsRecords, "len", len(remoteParentsRecords))
localParentRecords := dir.GetParentRecords()
/*
localParentRecords := dir.GetParentRecords()
for _, record := range localParentRecords {
_, err = parentsTable.AddRecords(&AirtableRecords{
Records: []*AirtableRecord{record},
})
if err != nil {
fatal(l, "failed to add Parents", "error", err)
for _, record := range localParentRecords {
_, err = parentsTable.AddRecords(&AirtableRecords{
Records: []*AirtableRecord{record},
})
if err != nil {
fatal(l, "failed to add Parents", "error", err)
}
}
}
*/
}
func loadDirectory(l *slog.Logger, path string) (*Directory, error) {
@@ -245,6 +246,7 @@ func (d *Directory) AddStudent(email, name, class, grade string, parents []*Pare
return s
}
/*
func (d *Directory) GetParentRecords() []*AirtableRecord {
records := []*AirtableRecord{}
@@ -259,6 +261,7 @@ func (d *Directory) GetParentRecords() []*AirtableRecord {
return records
}
*/
func (p Person) String() string {
return fmt.Sprintf(