Skip to content

Commit bd839f0

Browse files
authored
feat(billing): remove v2alpha1 and add download & export custom commands (#3677)
1 parent 283ebcf commit bd839f0

File tree

9 files changed

+266
-263
lines changed

9 files changed

+266
-263
lines changed

cmd/scw/testdata/test-all-usage-billing-invoice-download-usage.golden

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ USAGE:
66
scw billing invoice download <invoice-id ...> [arg=value ...]
77

88
ARGS:
9-
invoice-id Invoice ID
10-
[file-type] File type. PDF by default (pdf)
9+
invoice-id Invoice ID
10+
[file-path=./] Wanted file path
11+
[file-type=pdf] Wanted file extension
12+
[force-replace=false] Force file replacement
1113

1214
FLAGS:
1315
-h, --help help for download

cmd/scw/testdata/test-all-usage-billing-invoice-export-usage.golden

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ USAGE:
66
scw billing invoice export [arg=value ...]
77

88
ARGS:
9+
[organization-id] Organization ID. If specified, only invoices from this Organization will be returned
910
[billing-period-start-after] Return only invoice with start date greater than billing_period_start
1011
[billing-period-start-before] Return only invoice with start date less than billing_period_start
11-
[invoice-type] Invoice type. It can either be `periodic` or `purchase` (unknown_type | periodic | purchase)
12-
[page] Page number
13-
[page-size=20] Positive integer lower or equal to 100 to select the number of items to return
14-
[order-by] How invoices are ordered in the response (invoice_number_desc | invoice_number_asc | start_date_desc | start_date_asc | issued_date_desc | issued_date_asc | due_date_desc | due_date_asc | total_untaxed_desc | total_untaxed_asc | total_taxed_desc | total_taxed_asc | invoice_type_desc | invoice_type_asc)
15-
[file-type=CSV] File format for exporting the invoice list (csv)
16-
[organization-id] Organization ID. If specified, only invoices from this Organization will be returned
12+
[invoice-type] Invoice type. It can either be `periodic` or `purchase`
13+
[file-path=./] Wanted file path
14+
[file-type=csv] Wanted file extension
15+
[force-replace=false] Force file replacement
1716

1817
FLAGS:
1918
-h, --help help for export

docs/commands/billing.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ scw billing invoice download <invoice-id ...> [arg=value ...]
109109
| Name | | Description |
110110
|------|---|-------------|
111111
| invoice-id | Required | Invoice ID |
112-
| file-type | One of: `pdf` | File type. PDF by default |
112+
| file-path | Default: `./` | Wanted file path |
113+
| file-type | Default: `pdf` | Wanted file extension |
114+
| force-replace | Default: `false` | Force file replacement |
113115

114116

115117

@@ -128,14 +130,13 @@ scw billing invoice export [arg=value ...]
128130

129131
| Name | | Description |
130132
|------|---|-------------|
133+
| organization-id | | Organization ID. If specified, only invoices from this Organization will be returned |
131134
| billing-period-start-after | | Return only invoice with start date greater than billing_period_start |
132135
| billing-period-start-before | | Return only invoice with start date less than billing_period_start |
133-
| invoice-type | One of: `unknown_type`, `periodic`, `purchase` | Invoice type. It can either be `periodic` or `purchase` |
134-
| page | | Page number |
135-
| page-size | Default: `20` | Positive integer lower or equal to 100 to select the number of items to return |
136-
| order-by | One of: `invoice_number_desc`, `invoice_number_asc`, `start_date_desc`, `start_date_asc`, `issued_date_desc`, `issued_date_asc`, `due_date_desc`, `due_date_asc`, `total_untaxed_desc`, `total_untaxed_asc`, `total_taxed_desc`, `total_taxed_asc`, `invoice_type_desc`, `invoice_type_asc` | How invoices are ordered in the response |
137-
| file-type | Default: `CSV`<br />One of: `csv` | File format for exporting the invoice list |
138-
| organization-id | | Organization ID. If specified, only invoices from this Organization will be returned |
136+
| invoice-type | | Invoice type. It can either be `periodic` or `purchase` |
137+
| file-path | Default: `./` | Wanted file path |
138+
| file-type | Default: `csv` | Wanted file extension |
139+
| force-replace | Default: `false` | Force file replacement |
139140

140141

141142

internal/namespaces/billing/v2alpha1/billing_cli.go

Lines changed: 0 additions & 197 deletions
This file was deleted.

internal/namespaces/billing/v2alpha1/custom.go

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
package billing
22

33
import (
4+
"github.com/fatih/color"
45
"github.com/scaleway/scaleway-cli/v2/internal/core"
6+
"github.com/scaleway/scaleway-cli/v2/internal/human"
7+
billing "github.com/scaleway/scaleway-sdk-go/api/billing/v2beta1"
8+
)
9+
10+
var (
11+
invoiceTypeMarshalSpecs = human.EnumMarshalSpecs{
12+
billing.DownloadInvoiceRequestFileTypePdf: &human.EnumMarshalSpec{Attribute: color.FgHiBlue, Value: "pdf"},
13+
}
514
)
615

716
func GetCommands() *core.Commands {
817
cmds := GetGeneratedCommands()
918

19+
human.RegisterMarshalerFunc(billing.DownloadInvoiceRequestFileType("pdf"), human.EnumMarshalFunc(invoiceTypeMarshalSpecs))
20+
cmds.MustFind("billing", "invoice", "download").Override(invoiceDownloadBuilder)
21+
cmds.MustFind("billing", "invoice", "export").Override(invoiceExportBuilder)
22+
1023
return cmds
1124
}

internal/namespaces/billing/v2alpha1/custom_billing_download.go renamed to internal/namespaces/billing/v2beta1/custom_invoice_download.go

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@ import (
1414
"github.com/scaleway/scaleway-cli/v2/internal/core"
1515
"github.com/scaleway/scaleway-cli/v2/internal/interactive"
1616
"github.com/scaleway/scaleway-cli/v2/internal/terminal"
17-
billing "github.com/scaleway/scaleway-sdk-go/api/billing/v2alpha1"
18-
)
19-
20-
const (
21-
invoiceDefaultPrefix = "scaleway-invoice"
17+
billing "github.com/scaleway/scaleway-sdk-go/api/billing/v2beta1"
2218
)
2319

2420
type billingDownloadRequest struct {
@@ -28,19 +24,15 @@ type billingDownloadRequest struct {
2824
ForceReplace bool
2925
}
3026

31-
func fileNameWithoutExtTrimSuffix(fileName string) string {
32-
return strings.TrimSuffix(fileName, filepath.Ext(fileName))
33-
}
34-
35-
func buildDownloadCommand(command *core.Command) *core.Command {
27+
func invoiceDownloadBuilder(command *core.Command) *core.Command {
3628
command.ArgsType = reflect.TypeOf(billingDownloadRequest{})
3729
command.ArgSpecs = core.ArgSpecs{
3830
{
3931
Name: "invoice-id",
4032
Short: `Invoice ID`,
4133
Required: true,
4234
Deprecated: false,
43-
Positional: false,
35+
Positional: true,
4436
},
4537
{
4638
Name: "file-path",
@@ -90,7 +82,7 @@ func buildDownloadCommand(command *core.Command) *core.Command {
9082
dir, file := getDirFile(args.FilePath)
9183
if len(file) > 0 {
9284
fileExtension := filepath.Ext(file)
93-
if extensionOnFile := checkInvoiceExt(fileExtension); !extensionOnFile {
85+
if extensionOnFile := checkDownloadInvoiceExt(fileExtension); !extensionOnFile {
9486
return fmt.Errorf("file has not supported extension")
9587
}
9688
}
@@ -144,21 +136,7 @@ func buildDownloadCommand(command *core.Command) *core.Command {
144136
return command
145137
}
146138

147-
func getDirFile(filePath string) (string, string) {
148-
dir := "."
149-
dirTmp, file := filepath.Split(filePath)
150-
if len(dirTmp) > 0 {
151-
dir = dirTmp
152-
}
153-
154-
if file == "." {
155-
file = ""
156-
}
157-
158-
return dir, file
159-
}
160-
161-
func addExt(fileName, contentType string) string {
139+
func addDownloadExt(fileName, contentType string) string {
162140
switch contentType {
163141
case "application/pdf":
164142
fileName = fmt.Sprintf("%s.pdf", fileName)
@@ -167,7 +145,7 @@ func addExt(fileName, contentType string) string {
167145
return fileName
168146
}
169147

170-
func checkInvoiceExt(ext string) bool {
148+
func checkDownloadInvoiceExt(ext string) bool {
171149
switch ext {
172150
case ".pdf":
173151
return true
@@ -211,7 +189,7 @@ func billingDownloadRun(ctx context.Context, argsI interface{}) (interface{}, er
211189
}
212190
}
213191
// add supported extension
214-
fileName = addExt(fileName, resp.ContentType)
192+
fileName = addDownloadExt(fileName, resp.ContentType)
215193

216194
fileOutput, err := os.Create(fileName)
217195
if err != nil {

0 commit comments

Comments
 (0)