Skip to content

Commit

Permalink
style
Browse files Browse the repository at this point in the history
  • Loading branch information
joshspicer committed Feb 1, 2025
1 parent 7f8e325 commit 7d5295b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 24 deletions.
10 changes: 3 additions & 7 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,15 +508,11 @@ func New() *Cmd {
options = append(options, server.UseStorage(store))
}
case "azure":
storageAccountName := c.String("azure-storage-account")
containerName := c.String("azure-storage-container")
if storageAccountName == "" {
if storageAccountName := c.String("azure-storage-account"); storageAccountName == "" {
return errors.New("azure-storage-account not set.")
}
if containerName == "" {
} else if containerName := c.String("azure-storage-container"); containerName == "" {
return errors.New("azure-storage-container not set.")
}
if store, err := storage.NewAzureBlobStorage(c.Context, storageAccountName, containerName, logger); err != nil {
} else if store, err := storage.NewAzureBlobStorage(c.Context, storageAccountName, containerName, logger); err != nil {
return err
} else {
options = append(options, server.UseStorage(store))
Expand Down
19 changes: 2 additions & 17 deletions server/storage/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,12 @@ func getCredentials() (*azidentity.DefaultAzureCredential, error) {
}

func NewAzureBlobStorage(ctx context.Context, storageAccountName string, containerName string, logger *log.Logger) (Storage, error) {
logger.Println("Creating Azure Blob Storage")

if storageAccountName == "" {
return nil, fmt.Errorf("missing storage account name")
}

if containerName == "" {
return nil, fmt.Errorf("missing container name")
}

credentials, err := getCredentials()
if err != nil {
return nil, err
}

serviceUrl := "https://" + storageAccountName + ".blob.core.windows.net"
serviceUrl := fmt.Sprintf("https://%s.blob.core.windows.net", storageAccountName)
client, err := azblob.NewClient(serviceUrl, credentials, nil)
if err != nil {
return nil, err
Expand Down Expand Up @@ -91,10 +81,8 @@ func (s *AzureStorage) Delete(ctx context.Context, token string, filename string
blobClient := s.containerClient.NewBlobClient(key)
_, err := blobClient.Delete(ctx, nil)
if err != nil {
s.logger.Printf("Failed to delete blob %s: %v", key, err)
return err
}
s.logger.Printf("Successfully deleted blob %s", key)
return nil
}

Expand All @@ -103,24 +91,21 @@ func (s *AzureStorage) IsNotExist(err error) bool {
}

func (s *AzureStorage) Purge(ctx context.Context, days time.Duration) error {
s.logger.Printf("Purging blobs older than %v days", days)
pager := s.containerClient.NewListBlobsFlatPager(nil)

for pager.More() {
page, err := pager.NextPage(ctx)
if err != nil {
return fmt.Errorf("failed to list blobs: %w", err)
return err
}

for _, blob := range page.Segment.BlobItems {
if time.Since(*blob.Properties.LastModified) > days {
blobClient := s.containerClient.NewBlobClient(*blob.Name)
_, err := blobClient.Delete(ctx, nil)
if err != nil {
s.logger.Printf("Failed to delete blob %s: %v", *blob.Name, err)
continue
}
s.logger.Printf("Successfully deleted blob %s", *blob.Name)
}
}
}
Expand Down

0 comments on commit 7d5295b

Please sign in to comment.