Skip to content
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
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ generate: deps
mockgen --destination ./internal/mocks/ssl_service.go --package=mocks --source ./vendor/github.com/serverscom/serverscom-go-client/pkg/ssl_certificates.go
mockgen --destination ./internal/mocks/load_balancers_service.go --package=mocks --source ./vendor/github.com/serverscom/serverscom-go-client/pkg/load_balancers.go
mockgen --destination ./internal/mocks/racks_service.go --package=mocks --source ./vendor/github.com/serverscom/serverscom-go-client/pkg/racks.go
mockgen --destination ./internal/mocks/invoices_service.go --package=mocks --source ./vendor/github.com/serverscom/serverscom-go-client/pkg/invoices.go
sed -i '' 's|github.com/serverscom/srvctl/vendor/github.com/serverscom/serverscom-go-client/pkg|github.com/serverscom/serverscom-go-client/pkg|g' \
./internal/mocks/ssh_service.go \
./internal/mocks/hosts_service.go \
./internal/mocks/ssl_service.go \
./internal/mocks/load_balancers_service.go \
./internal/mocks/racks_service.go \
./internal/mocks/invoices_service.go \
./internal/mocks/collection.go

91 changes: 91 additions & 0 deletions cmd/base/list_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,94 @@ func (o *ClusterIDOption[T]) ApplyToCollection(collection serverscom.Collection[
collection.SetParam("cluster_id", o.clusterID)
}
}

// status option
type StatusOption[T any] struct {
status string
}

func (o *StatusOption[T]) AddFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&o.status, "status", "", "Filter results by status")
}

func (o *StatusOption[T]) ApplyToCollection(collection serverscom.Collection[T]) {
if o.status != "" {
collection.SetParam("status", o.status)
}
}

// invoice type option
type InvoiceTypeOption[T any] struct {
typeVal string
}

// use itype instead of type to avoid conflict with baseList 'type' hidden flag which we use for subcommands
func (o *InvoiceTypeOption[T]) AddFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&o.typeVal, "itype", "", "Filter results by type")
}

func (o *InvoiceTypeOption[T]) ApplyToCollection(collection serverscom.Collection[T]) {
if o.typeVal != "" {
collection.SetParam("type", o.typeVal)
}
}

// parent id option
type ParentIDOption[T any] struct {
parentID string
}

func (o *ParentIDOption[T]) AddFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&o.parentID, "parent-id", "", "Filter results by parent ID")
}

func (o *ParentIDOption[T]) ApplyToCollection(collection serverscom.Collection[T]) {
if o.parentID != "" {
collection.SetParam("parent_id", o.parentID)
}
}

// currency option
type CurrencyOption[T any] struct {
currency string
}

func (o *CurrencyOption[T]) AddFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&o.currency, "currency", "", "Filter results by currency")
}

func (o *CurrencyOption[T]) ApplyToCollection(collection serverscom.Collection[T]) {
if o.currency != "" {
collection.SetParam("currency", o.currency)
}
}

// start date option
type StartDateOption[T any] struct {
startDate string
}

func (o *StartDateOption[T]) AddFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&o.startDate, "start-date", "", "Filter results by start date")
}

func (o *StartDateOption[T]) ApplyToCollection(collection serverscom.Collection[T]) {
if o.startDate != "" {
collection.SetParam("start_date", o.startDate)
}
}

// end date option
type EndDateOption[T any] struct {
endDate string
}

func (o *EndDateOption[T]) AddFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&o.endDate, "end-date", "", "Filter results by end date")
}

func (o *EndDateOption[T]) ApplyToCollection(collection serverscom.Collection[T]) {
if o.endDate != "" {
collection.SetParam("end_date", o.endDate)
}
}
39 changes: 39 additions & 0 deletions cmd/entities/invoices/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package invoices

import (
"github.com/serverscom/srvctl/cmd/base"
"github.com/spf13/cobra"
)

func newGetCmd(cmdContext *base.CmdContext) *cobra.Command {
cmd := &cobra.Command{
Use: "get <id>",
Short: "Get an invoice",
Long: "Get an invoice by id",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
manager := cmdContext.GetManager()

ctx, cancel := base.SetupContext(cmd, manager)
defer cancel()

base.SetupProxy(cmd, manager)

scClient := cmdContext.GetClient().SetVerbose(manager.GetVerbose(cmd)).GetScClient()

id := args[0]
invoice, err := scClient.Invoices.GetBillingInvoice(ctx, id)
if err != nil {
return err
}

if invoice != nil {
formatter := cmdContext.GetOrCreateFormatter(cmd)
return formatter.Format(invoice)
}
return nil
},
}

return cmd
}
38 changes: 38 additions & 0 deletions cmd/entities/invoices/invoices.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package invoices

import (
"log"

serverscom "github.com/serverscom/serverscom-go-client/pkg"
"github.com/serverscom/srvctl/cmd/base"
"github.com/serverscom/srvctl/internal/output/entities"
"github.com/spf13/cobra"
)

func NewCmd(cmdContext *base.CmdContext) *cobra.Command {
invoiceEntity, err := entities.Registry.GetEntityFromValue(serverscom.Invoice{})
if err != nil {
log.Fatal(err)
}
entitiesMap := make(map[string]entities.EntityInterface)
entitiesMap["invoices"] = invoiceEntity
cmd := &cobra.Command{
Use: "invoices",
Short: "Manage invoices",
PersistentPreRunE: base.CombinePreRunE(
base.CheckFormatterFlags(cmdContext, entitiesMap),
base.CheckEmptyContexts(cmdContext),
),
Args: base.NoArgs,
Run: base.UsageRun,
}

cmd.AddCommand(
newListCmd(cmdContext),
newGetCmd(cmdContext),
)

base.AddFormatFlags(cmd)

return cmd
}
Loading
Loading