Skip to content

Commit

Permalink
Merge branch 'dev-cur-cmd'
Browse files Browse the repository at this point in the history
  • Loading branch information
slntopp committed Jan 11, 2023
2 parents af82efe + 3c99d21 commit 00a868e
Show file tree
Hide file tree
Showing 9 changed files with 451 additions and 11 deletions.
25 changes: 20 additions & 5 deletions cmd/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -17,17 +17,33 @@ package cmd

import (
"github.com/slntopp/nocloud-cli/cmd/billing"
"github.com/slntopp/nocloud-cli/cmd/currency"
"github.com/spf13/cobra"
)

// accountCmd represents the account command
// billingCmd represents the billing command
var billingCmd = &cobra.Command{
Use: "billing",
Use: "billing",
Aliases: []string{"bill"},
Short: "Manage Billing Plans",
Short: "Manage Billing Plans",
}

// CurrenciesCmd represents the currencies command
var currenciesCmd = &cobra.Command{
Use: "currencies",
Aliases: []string{"cur", "curs"},
Short: "Manage Currencies",
}

func init() {
currenciesCmd.AddCommand(currency.GetCmd)
currenciesCmd.AddCommand(currency.ListCurrenciesCmd)
currenciesCmd.AddCommand(currency.ListRatesCmd)
currenciesCmd.AddCommand(currency.CreateCmd)
currenciesCmd.AddCommand(currency.DeleteCmd)
currenciesCmd.AddCommand(currency.UpdateCmd)
billingCmd.AddCommand(currenciesCmd)

billingCmd.AddCommand(billing.CreateCmd)
billingCmd.AddCommand(billing.GetCmd)
billingCmd.AddCommand(billing.ListCmd)
Expand All @@ -37,4 +53,3 @@ func init() {

rootCmd.AddCommand(billingCmd)
}

76 changes: 76 additions & 0 deletions cmd/currency/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
Copyright © 2021-2022 Nikita Ivanovski info@slnt-opp.xyz
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package currency

import (
"errors"
"fmt"

"github.com/slntopp/nocloud-cli/pkg/tools"
"github.com/slntopp/nocloud/pkg/billing/proto"
"github.com/spf13/cobra"
)

var CreateCmd = &cobra.Command{
Use: "create [flags]",
Aliases: []string{"crt", "c"},
Short: "Create Exchange rate",
RunE: func(cmd *cobra.Command, args []string) error {
var (
from int
to int
rate float64
)
from, err := cmd.Flags().GetInt("from")
if err != nil || from == -1 {
return errors.New("empty from")
}
to, err = cmd.Flags().GetInt("to")
if err != nil || to == -1 {
return errors.New("empty to")
}
rate, err = cmd.Flags().GetFloat64("rate")
if err != nil {
return errors.New("empty rate")
}

ctx, client := MakeCurrencyServiceClientOrFail()
res, err := client.CreateExchangeRate(ctx, &proto.CreateExchangeRateRequest{
From: proto.Currency(from),
To: proto.Currency(to),
Rate: rate,
})
if err != nil {
return err
}

ok, err := tools.PrintJsonDataQ(cmd, res)
if err != nil {
return err
}
if !ok {
fmt.Println("Successfully created ", res)
}

return nil
},
}

func init() {
CreateCmd.Flags().Int("from", -1, "")
CreateCmd.Flags().Int("to", -1, "")
CreateCmd.Flags().Float64("rate", 1.0, "")
}
71 changes: 71 additions & 0 deletions cmd/currency/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Copyright © 2021-2022 Nikita Ivanovski info@slnt-opp.xyz
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package currency

import (
"errors"
"fmt"

"github.com/slntopp/nocloud-cli/pkg/tools"
"github.com/slntopp/nocloud/pkg/billing/proto"
"github.com/spf13/cobra"
)

// DeleteCmd represents the delete command
var DeleteCmd = &cobra.Command{
Use: "delete [flags]",
Aliases: []string{"del", "rm", "r"},
Short: "Delete exchange rate",
Args: cobra.MinimumNArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
var (
from int
to int
)
from, err := cmd.Flags().GetInt("from")
if err != nil || from == -1 {
return errors.New("empty from")
}
to, err = cmd.Flags().GetInt("to")
if err != nil || to == -1 {
return errors.New("empty to")
}

ctx, client := MakeCurrencyServiceClientOrFail()
res, err := client.DeleteExchangeRate(ctx, &proto.DeleteExchangeRateRequest{
From: proto.Currency(from),
To: proto.Currency(to),
})
if err != nil {
return err
}

ok, err := tools.PrintJsonDataQ(cmd, res)
if err != nil {
return err
}
if !ok {
fmt.Println("Success deleted ", res)
}

return nil
},
}

func init() {
DeleteCmd.Flags().Int("from", -1, "")
DeleteCmd.Flags().Int("to", -1, "")
}
70 changes: 70 additions & 0 deletions cmd/currency/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
Copyright © 2021-2022 Nikita Ivanovski info@slnt-opp.xyz
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package currency

import (
"errors"
"fmt"

"github.com/slntopp/nocloud-cli/pkg/tools"
"github.com/slntopp/nocloud/pkg/billing/proto"
"github.com/spf13/cobra"
)

// GetCmd represents the get command
var GetCmd = &cobra.Command{
Use: "get [flags]",
Short: "Get exchange rate",
Args: cobra.MinimumNArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
var (
from int
to int
)
from, err := cmd.Flags().GetInt("from")
if err != nil || from == -1 {
return errors.New("empty from")
}
to, err = cmd.Flags().GetInt("to")
if err != nil || to == -1 {
return errors.New("empty to")
}

ctx, client := MakeCurrencyServiceClientOrFail()
res, err := client.GetExchangeRate(ctx, &proto.GetExchangeRateRequest{
From: proto.Currency(from),
To: proto.Currency(to),
})
if err != nil {
return err
}

ok, err := tools.PrintJsonDataQ(cmd, res)
if err != nil {
return err
}
if !ok {
fmt.Println("Successfully fetched ", res)
}

return nil
},
}

func init() {
GetCmd.Flags().Int("from", -1, "")
GetCmd.Flags().Int("to", -1, "")
}
72 changes: 72 additions & 0 deletions cmd/currency/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Copyright © 2021-2022 Nikita Ivanovski info@slnt-opp.xyz
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package currency

import (
"fmt"

"github.com/slntopp/nocloud-cli/pkg/tools"
"github.com/slntopp/nocloud/pkg/billing/proto"
"github.com/spf13/cobra"
)

// ListCurrenciesCmd represents the list currencies command
var ListCurrenciesCmd = &cobra.Command{
Use: "currencies",
Aliases: []string{"curs"},
Short: "List currencies",
Args: cobra.MinimumNArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
ctx, client := MakeCurrencyServiceClientOrFail()
curs, err := client.GetCurrencies(ctx, &proto.GetCurrenciesRequest{})
if err != nil {
return err
}

ok, err := tools.PrintJsonDataQ(cmd, curs)
if err != nil {
return err
}
if !ok {
fmt.Println("Successfully fetched ", curs)
}
return nil
},
}

// ListRatesCmd represents the list rates command
var ListRatesCmd = &cobra.Command{
Use: "rates",
Aliases: []string{"exchangerates"},
Short: "List exchange rates",
Args: cobra.MinimumNArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
ctx, client := MakeCurrencyServiceClientOrFail()
rates, err := client.GetExchangeRates(ctx, &proto.GetExchangeRatesRequest{})
if err != nil {
return err
}

ok, err := tools.PrintJsonDataQ(cmd, rates)
if err != nil {
return err
}
if !ok {
fmt.Println("Successfully fetched ", rates)
}
return nil
},
}
Loading

0 comments on commit 00a868e

Please sign in to comment.