mirror of
https://github.com/mitchell/selfpass.git
synced 2025-12-13 21:07:22 +00:00
Update list and get commands to use better choosing strategy;
refactor Makefile to use go mod vendoring and docker caching
This commit is contained in:
parent
b2a41cf07c
commit
383a3aa1cd
9 changed files with 193 additions and 224 deletions
|
|
@ -1,112 +0,0 @@
|
|||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/aws/external"
|
||||
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
|
||||
"github.com/aws/aws-sdk-go-v2/service/dynamodb/dynamodbattribute"
|
||||
"github.com/mitchell/selfpass/credentials/types"
|
||||
)
|
||||
|
||||
func NewDynamoTable(name string) DynamoTable {
|
||||
cfg, err := external.LoadDefaultAWSConfig()
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
return DynamoTable{
|
||||
name: name,
|
||||
svc: dynamodb.New(cfg),
|
||||
}
|
||||
}
|
||||
|
||||
type DynamoTable struct {
|
||||
name string
|
||||
svc *dynamodb.DynamoDB
|
||||
}
|
||||
|
||||
func (t DynamoTable) GetAllMetadata(ctx context.Context, sourceHost string, errch chan<- error) (output <-chan types.Metadata) {
|
||||
mdch := make(chan types.Metadata, 1)
|
||||
in := &dynamodb.ScanInput{TableName: &t.name}
|
||||
|
||||
if sourceHost != "" {
|
||||
filterExpr := "SourceHost = :sh"
|
||||
in.FilterExpression = &filterExpr
|
||||
in.ExpressionAttributeValues = map[string]dynamodb.AttributeValue{
|
||||
":sh": {S: &sourceHost},
|
||||
}
|
||||
}
|
||||
|
||||
req := t.svc.ScanRequest(in)
|
||||
|
||||
go func() {
|
||||
defer close(mdch)
|
||||
|
||||
pgr := req.Paginate()
|
||||
for pgr.Next() {
|
||||
mds := []types.Metadata{}
|
||||
out := pgr.CurrentPage()
|
||||
if err := dynamodbattribute.UnmarshalListOfMaps(out.Items, &mds); err != nil {
|
||||
errch <- err
|
||||
return
|
||||
}
|
||||
|
||||
for _, md := range mds {
|
||||
mdch <- md
|
||||
}
|
||||
}
|
||||
|
||||
if err := pgr.Err(); err != nil {
|
||||
errch <- err
|
||||
return
|
||||
}
|
||||
}()
|
||||
|
||||
return mdch
|
||||
}
|
||||
|
||||
func (t DynamoTable) Get(ctx context.Context, id string) (output types.Credential, err error) {
|
||||
req := t.svc.GetItemRequest(&dynamodb.GetItemInput{
|
||||
TableName: &t.name,
|
||||
Key: map[string]dynamodb.AttributeValue{
|
||||
"ID": {S: &id},
|
||||
},
|
||||
})
|
||||
|
||||
out, err := req.Send()
|
||||
if err != nil {
|
||||
return output, err
|
||||
}
|
||||
|
||||
err = dynamodbattribute.UnmarshalMap(out.Item, &output)
|
||||
return output, err
|
||||
}
|
||||
|
||||
func (t DynamoTable) Put(ctx context.Context, c types.Credential) (err error) {
|
||||
item, err := dynamodbattribute.MarshalMap(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req := t.svc.PutItemRequest(&dynamodb.PutItemInput{
|
||||
TableName: &t.name,
|
||||
Item: item,
|
||||
})
|
||||
req.SetContext(ctx)
|
||||
|
||||
_, err = req.Send()
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (t DynamoTable) Delete(ctx context.Context, id string) (err error) {
|
||||
req := t.svc.DeleteItemRequest(&dynamodb.DeleteItemInput{
|
||||
TableName: &t.name,
|
||||
Key: map[string]dynamodb.AttributeValue{
|
||||
"ID": {S: &id},
|
||||
},
|
||||
})
|
||||
|
||||
_, err = req.Send()
|
||||
return err
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue