Skip to content

Commit

Permalink
Merge pull request #116 from buchhalter-ai/rename-prepare-recipes
Browse files Browse the repository at this point in the history
`sync` cmd: Distinguish between "no recipes found" and "no credentials found" in `loadRecipesAndMatchingVaultItems`
  • Loading branch information
brgmn authored Sep 17, 2024
2 parents a57d126 + 26830e6 commit eb93806
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions cmd/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,10 @@ func runSyncCommandLogic(p *tea.Program, logger *slog.Logger, config *syncComman
p.Send(utils.ViewStatusUpdateMsg{
Message: statusUpdateMessage,
})
recipesToExecute, err := prepareRecipes(logger, supplier, vaultProvider, recipeParser)
recipesToExecute, err := loadRecipesAndMatchingVaultItems(logger, supplier, vaultProvider, recipeParser)
if err != nil {
// No error logging needed. This is done in `prepareRecipes`
// No error logging needed. This is done in `loadRecipesAndMatchingVaultItems`
// If an error occurs, this means the recipes could not be loaded.
p.Send(utils.ViewStatusUpdateMsg{
Err: fmt.Errorf("error loading recipes: %w", err),
ShouldQuit: true,
Expand All @@ -275,9 +276,9 @@ func runSyncCommandLogic(p *tea.Program, logger *slog.Logger, config *syncComman

// No pair of credentials found for supplier/recipes
if len(recipesToExecute) == 0 {
loggingErrorMessage := "No recipes found for suppliers"
loggingErrorMessage := "No matching pair of recipes <--> credentials found for suppliers"
if len(supplier) > 0 {
loggingErrorMessage = fmt.Sprintf("No recipe found for supplier `%s`", supplier)
loggingErrorMessage = fmt.Sprintf("No matching pair of recipes <--> credentials found for supplier `%s`", supplier)
}
logger.Error(loggingErrorMessage, "supplier", supplier, "error", err)
p.Send(utils.ViewStatusUpdateMsg{
Expand Down Expand Up @@ -565,29 +566,30 @@ func runSyncCommandLogic(p *tea.Program, logger *slog.Logger, config *syncComman
}
}

// TODO Distinguish between "no recipes found" and "no credentials found"
// TODO Rename function to something more meaningful
func prepareRecipes(logger *slog.Logger, supplier string, vaultProvider *vault.Provider1Password, recipeParser *parser.RecipeParser) ([]recipeToExecute, error) {
var r []recipeToExecute
// loadRecipesAndMatchingVaultItems loads all recipes (or only the one for a specific supplier if `supplier` is set)
// and tries to find matching pairs of credentials in the vault.
func loadRecipesAndMatchingVaultItems(logger *slog.Logger, supplier string, vaultProvider *vault.Provider1Password, recipeParser *parser.RecipeParser) ([]recipeToExecute, error) {
var recipeVaultItemPairs []recipeToExecute

// Load recipes
developmentMode := viper.GetBool("dev")
logger.Info("Loading recipes for suppliers ...", "development_mode", developmentMode)
loadRecipeResult, err := recipeParser.LoadRecipes(developmentMode)
if err != nil {
logger.Error("Error loading recipes for suppliers", "error", err, "load_recipe_result", loadRecipeResult)
return r, err
return recipeVaultItemPairs, err
}

// Run single supplier recipe
// Search for credential pairs matching the recipe(s)
stepCount := 0
vaultItems := vaultProvider.VaultItems
if supplier != "" {
if len(supplier) > 0 {
logger.Info("Search for credentials for suppliers recipe ...", "supplier", supplier)
for i := range vaultItems {
// Check if a recipe exists for the item
recipe := recipeParser.GetRecipeForItem(vaultItems[i], vaultProvider.UrlsByItemId)
if recipe != nil && supplier == recipe.Supplier {
r = append(r, recipeToExecute{recipe, vaultItems[i].ID})
recipeVaultItemPairs = append(recipeVaultItemPairs, recipeToExecute{recipe, vaultItems[i].ID})
logger.Info("Search for credentials for suppliers recipe ... found", "supplier", supplier, "credentials_id", vaultItems[i].ID)
}
}
Expand All @@ -601,13 +603,13 @@ func prepareRecipes(logger *slog.Logger, supplier string, vaultProvider *vault.P
recipe := recipeParser.GetRecipeForItem(vaultItems[i], vaultProvider.UrlsByItemId)
if recipe != nil {
stepCount = stepCount + len(recipe.Steps)
r = append(r, recipeToExecute{recipe, vaultItems[i].ID})
recipeVaultItemPairs = append(recipeVaultItemPairs, recipeToExecute{recipe, vaultItems[i].ID})
logger.Info("Search for matching pairs of recipes for supplier recipes and credentials ... found", "supplier", recipe.Supplier, "credentials_id", vaultItems[i].ID)
}
}
}

return r, nil
return recipeVaultItemPairs, nil
}

func sendMetrics(buchhalterAPIClient *repository.BuchhalterAPIClient, a bool, runData repository.RunData, cliVersion, chromeVersion, vaultVersion, oicdbVersion string) error {
Expand Down

0 comments on commit eb93806

Please sign in to comment.