Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the error console when there is an exception #151

Merged
merged 6 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,12 @@ jobs:
run-azure-cost-cli:
runs-on: ubuntu-latest
steps:
- name: Azure Login
uses: azure/login@v1
- name: Azure login
uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}

- name: Install Azure Cost CLI
run: dotnet tool install -g azure-cost-cli
Expand All @@ -140,7 +142,7 @@ jobs:

```

The last step output the markdown to the Job Summary. This can be used to show the cost of the subscription in the workflow summary. Use it on a schedule to get for example a daily overview. Alternatively you can use the `-o json` parameter to get the results in JSON format and use it for further processing.
The last step outputs the markdown to the Job Summary. This can be used to show the cost of the subscription in the workflow summary. Use it on a schedule to get for example a daily overview. Alternatively you can use the `-o json` parameter to get the results in JSON format and use it for further processing.

## Available commands

Expand Down Expand Up @@ -604,6 +606,16 @@ azure-cost accumulatedCost -s 574385a9-08e9-49fe-91a2-27660d92b8f5 -o markdown >

Excluded in the above sample, but it will also include mermaidjs diagrams as well.

## Error handling

When the tool detects an exception, it will write this to the error stream and return with a -1. This can be used in scripts to detect if something went wrong. If you output using one of the formatters and want to save this to a file, you can use the `>` operator to redirect the output to a file. If you want to capture any errors as well, you can use the `2>&1` operator to redirect the error stream to the output stream. Or output it to a different file.

```bash
azure-cost accumulatedCost -o markdown > cost.md 2>error.log
```

> Breaking change in version 0.41.0: The tool will now return with a -1 when an error occurs. This is to make it easier to detect errors in scripts.

## Iterate over multiple subscriptions

Since the tool operates on a single subscription only, you will need to loop over multiple subscriptions yourself. You can do this by using the `az account list` command and then using the `--subscription` parameter to switch between subscriptions.
Expand Down
11 changes: 6 additions & 5 deletions src/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using System.ComponentModel;
using AzureCostCli.Commands;
using AzureCostCli.Commands.AccumulatedCost;
using AzureCostCli.Commands.Budgets;
using AzureCostCli.Commands.CostByResource;
using AzureCostCli.Commands.CostByTag;
using AzureCostCli.Commands.DailyCost;
using AzureCostCli.Commands.DetectAnomaly;
using AzureCostCli.Commands.Prices;
using AzureCostCli.Commands.Regions;
using AzureCostCli.Commands.WhatIf;
using AzureCostCli.CostApi;
Expand Down Expand Up @@ -67,9 +65,12 @@
config.AddExample(new[] { "detectAnomalies", "--dimension", "ResourceId", "--recent-activity-days", "4" });
config.AddExample(new[] { "costByTag", "--tag", "cost-center" });

#if DEBUG
config.PropagateExceptions();
#endif
config.SetExceptionHandler((ex, resolver) =>
{
// Explicitly write to error output
Console.Error.WriteLine(ex);
return -1;
});

config.AddCommand<AccumulatedCostCommand>("accumulatedCost")
.WithDescription("Show the accumulated cost details.");
Expand Down
Loading