Skip to content

Commit

Permalink
refactor: update describe command structures
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickMenoti committed Jul 15, 2024
1 parent 9781642 commit 4b93360
Show file tree
Hide file tree
Showing 6 changed files with 255 additions and 120 deletions.
55 changes: 40 additions & 15 deletions pkg/cmd/describe/domains/domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,68 @@ import (

"github.com/MakeNowJust/heredoc"
msg "github.com/aziontech/azion-cli/messages/describe/domain"

api "github.com/aziontech/azion-cli/pkg/api/domain"
"github.com/aziontech/azion-cli/pkg/cmdutil"
"github.com/aziontech/azion-cli/pkg/contracts"
"github.com/aziontech/azion-cli/pkg/iostreams"
"github.com/aziontech/azion-cli/pkg/output"
"github.com/aziontech/azion-cli/utils"
"github.com/spf13/cobra"
)

func NewCmd(f *cmdutil.Factory) *cobra.Command {
var domainID string
var (
domainID string
)

type DescribeCmd struct {
Io *iostreams.IOStreams
AskInput func(string) (string, error)
Get func(context.Context, string) (api.DomainResponse, error)
}

func NewDescribeCmd(f *cmdutil.Factory) *DescribeCmd {
return &DescribeCmd{
Io: f.IOStreams,
AskInput: func(prompt string) (string, error) {
return utils.AskInput(prompt)
},
Get: func(ctx context.Context, domainID string) (api.DomainResponse, error) {
client := api.NewClient(f.HttpClient, f.Config.GetString("api_url"), f.Config.GetString("token"))
return client.Get(ctx, domainID)
},
}
}

func NewCobraCmd(describe *DescribeCmd, f *cmdutil.Factory) *cobra.Command {
opts := &contracts.DescribeOptions{}
cmd := &cobra.Command{
cobraCmd := &cobra.Command{
Use: msg.Usage,
Short: msg.ShortDescription,
Long: msg.LongDescription,
SilenceUsage: true,
SilenceErrors: true,
Example: heredoc.Doc(`
$ azion describe domain --domain-id 4312
$ azion describe domain --domain-id 1337 --out "./tmp/test.json" --format json
$ azion describe domain --domain-id 1337 --format json
`),
$ azion describe domain --domain-id 4312
$ azion describe domain --domain-id 1337 --out "./tmp/test.json" --format json
$ azion describe domain --domain-id 1337 --format json
`),
RunE: func(cmd *cobra.Command, _ []string) error {
if !cmd.Flags().Changed("domain-id") {
answer, err := utils.AskInput(msg.AskInputDomainID)
answer, err := describe.AskInput(msg.AskInputDomainID)
if err != nil {
return err
}

domainID = answer
}
client := api.NewClient(f.HttpClient, f.Config.GetString("api_url"), f.Config.GetString("token"))

ctx := context.Background()
domain, err := client.Get(ctx, domainID)
domain, err := describe.Get(ctx, domainID)
if err != nil {
return fmt.Errorf(msg.ErrorGetDomain.Error(), err.Error())
}

fields := make(map[string]string, 0)
fields := make(map[string]string)
fields["Id"] = "ID"
fields["Name"] = "Name"
fields["DomainName"] = "Domain"
Expand All @@ -69,8 +90,12 @@ func NewCmd(f *cmdutil.Factory) *cobra.Command {
},
}

cmd.Flags().StringVar(&domainID, "domain-id", "", msg.FlagDomainID)
cmd.Flags().BoolP("help", "h", false, msg.HelpFlag)
cobraCmd.Flags().StringVar(&domainID, "domain-id", "", msg.FlagDomainID)
cobraCmd.Flags().BoolP("help", "h", false, msg.HelpFlag)

return cobraCmd
}

return cmd
func NewCmd(f *cmdutil.Factory) *cobra.Command {
return NewCobraCmd(NewDescribeCmd(f), f)
}
92 changes: 58 additions & 34 deletions pkg/cmd/describe/edge_applications/edge_applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,63 +10,83 @@ import (
api "github.com/aziontech/azion-cli/pkg/api/edge_applications"
"github.com/aziontech/azion-cli/pkg/cmdutil"
"github.com/aziontech/azion-cli/pkg/contracts"
"github.com/aziontech/azion-cli/pkg/iostreams"
"github.com/aziontech/azion-cli/pkg/output"
"github.com/aziontech/azion-cli/utils"
"github.com/spf13/cobra"
)

func NewCmd(f *cmdutil.Factory) *cobra.Command {
var applicationID string
var (
applicationID string
)

type DescribeCmd struct {
Io *iostreams.IOStreams
AskInput func(string) (string, error)
Get func(context.Context, string) (api.EdgeApplicationResponse, error)
}

func NewDescribeCmd(f *cmdutil.Factory) *DescribeCmd {
return &DescribeCmd{
Io: f.IOStreams,
AskInput: func(prompt string) (string, error) {
return utils.AskInput(prompt)
},
Get: func(ctx context.Context, applicationID string) (api.EdgeApplicationResponse, error) {
client := api.NewClient(f.HttpClient, f.Config.GetString("api_url"), f.Config.GetString("token"))
return client.Get(ctx, applicationID)
},
}
}

func NewCobraCmd(describe *DescribeCmd, f *cmdutil.Factory) *cobra.Command {
opts := &contracts.DescribeOptions{}
cmd := &cobra.Command{
cobraCmd := &cobra.Command{
Use: msg.Usage,
Short: msg.ShortDescription,
Long: msg.LongDescription,
SilenceUsage: true,
SilenceErrors: true,
Example: heredoc.Doc(`
$ azion describe edge-application --application-id 4312
$ azion describe edge-application --application-id 1337 --out "./tmp/test.json"
$ azion describe edge-application --application-id 1337 --format json
`),
$ azion describe edge-application --application-id 4312
$ azion describe edge-application --application-id 1337 --out "./tmp/test.json"
$ azion describe edge-application --application-id 1337 --format json
`),
RunE: func(cmd *cobra.Command, _ []string) error {

if !cmd.Flags().Changed("application-id") {
answer, err := utils.AskInput(msg.AskInputApplicationID)
answer, err := describe.AskInput(msg.AskInputApplicationID)
if err != nil {
return err
}

applicationID = answer
}

client := api.NewClient(f.HttpClient, f.Config.GetString("api_url"), f.Config.GetString("token"))

ctx := context.Background()
resp, err := client.Get(ctx, applicationID)
resp, err := describe.Get(ctx, applicationID)
if err != nil {
return fmt.Errorf(msg.ErrorGetApplication.Error(), err)
}

fields := make(map[string]string, 0)

fields["Id"] = "ID"
fields["Name"] = "Name"
fields["Active"] = "Active"
fields["ApplicationAcceleration"] = "Application Acceleration"
fields["Caching"] = "Caching"
fields["DeliveryProtocol"] = "Delivery Protocol"
fields["DeviceDetection"] = "Device Detection"
fields["EdgeFirewall"] = "Edge Firewall"
fields["EdgeFunctions"] = "Edge Functions"
fields["HttpPort"] = "Http Port"
fields["HttpsPort"] = "HttpsPort"
fields["ImageOptimization"] = "Image Optimization"
fields["L2Caching"] = "L2 Caching"
fields["LoadBalancer"] = "Load Balancer"
fields["MinimumTlsVersion"] = "Minimum TLS Version"
fields["RawLogs"] = "Raw Logs"
fields["WebApplicationFirewall"] = "Web Application Firewall"
fields := map[string]string{
"Id": "ID",
"Name": "Name",
"Active": "Active",
"ApplicationAcceleration": "Application Acceleration",
"Caching": "Caching",
"DeliveryProtocol": "Delivery Protocol",
"DeviceDetection": "Device Detection",
"EdgeFirewall": "Edge Firewall",
"EdgeFunctions": "Edge Functions",
"HttpPort": "Http Port",
"HttpsPort": "HttpsPort",
"ImageOptimization": "Image Optimization",
"L2Caching": "L2 Caching",
"LoadBalancer": "Load Balancer",
"MinimumTlsVersion": "Minimum TLS Version",
"RawLogs": "Raw Logs",
"WebApplicationFirewall": "Web Application Firewall",
}

describeOut := output.DescribeOutput{
GeneralOutput: output.GeneralOutput{
Expand All @@ -82,8 +102,12 @@ func NewCmd(f *cmdutil.Factory) *cobra.Command {
},
}

cmd.Flags().StringVar(&applicationID, "application-id", "", msg.FlagId)
cmd.Flags().BoolP("help", "h", false, msg.HelpFlag)
cobraCmd.Flags().StringVar(&applicationID, "application-id", "", msg.FlagId)
cobraCmd.Flags().BoolP("help", "h", false, msg.HelpFlag)

return cobraCmd
}

return cmd
func NewCmd(f *cmdutil.Factory) *cobra.Command {
return NewCobraCmd(NewDescribeCmd(f), f)
}
74 changes: 50 additions & 24 deletions pkg/cmd/describe/edge_function/edge_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,40 @@ import (
api "github.com/aziontech/azion-cli/pkg/api/edge_function"
"github.com/aziontech/azion-cli/pkg/cmdutil"
"github.com/aziontech/azion-cli/pkg/contracts"
"github.com/aziontech/azion-cli/pkg/iostreams"
"github.com/aziontech/azion-cli/pkg/logger"
"github.com/aziontech/azion-cli/pkg/output"
"github.com/aziontech/azion-cli/utils"
"github.com/spf13/cobra"
"go.uber.org/zap"
)

func NewCmd(f *cmdutil.Factory) *cobra.Command {
var function_id int64
var (
functionID int64
)

type DescribeCmd struct {
Io *iostreams.IOStreams
AskInput func(string) (string, error)
Get func(context.Context, int64) (api.EdgeFunctionResponse, error)
}

func NewDescribeCmd(f *cmdutil.Factory) *DescribeCmd {
return &DescribeCmd{
Io: f.IOStreams,
AskInput: func(prompt string) (string, error) {
return utils.AskInput(prompt)
},
Get: func(ctx context.Context, functionID int64) (api.EdgeFunctionResponse, error) {
client := api.NewClient(f.HttpClient, f.Config.GetString("api_url"), f.Config.GetString("token"))
return client.Get(ctx, functionID)
},
}
}

func NewCobraCmd(describe *DescribeCmd, f *cmdutil.Factory) *cobra.Command {
opts := &contracts.DescribeOptions{}
cmd := &cobra.Command{
cobraCmd := &cobra.Command{
Use: msg.Usage,
Short: msg.DescribeShortDescription,
Long: msg.DescribeLongDescription,
Expand All @@ -35,7 +58,7 @@ func NewCmd(f *cmdutil.Factory) *cobra.Command {
`),
RunE: func(cmd *cobra.Command, args []string) error {
if !cmd.Flags().Changed("function-id") {
answer, err := utils.AskInput(msg.AskEdgeFunctionID)
answer, err := describe.AskInput(msg.AskEdgeFunctionID)
if err != nil {
return err
}
Expand All @@ -46,34 +69,33 @@ func NewCmd(f *cmdutil.Factory) *cobra.Command {
return msg.ErrorConvertIdFunction
}

function_id = num
functionID = num
}

client := api.NewClient(f.HttpClient, f.Config.GetString("api_url"), f.Config.GetString("token"))

ctx := context.Background()
resp, err := client.Get(ctx, function_id)
resp, err := describe.Get(ctx, functionID)
if err != nil {
return fmt.Errorf(msg.ErrorGetFunction.Error(), err)
}

fields := make(map[string]string, 0)
fields["Id"] = "ID"
fields["Name"] = "Name"
fields["Active"] = "Active"
fields["Language"] = "Language"
fields["ReferenceCount"] = "Reference Count"
fields["Modified"] = "Modified at"
fields["InitiatorType"] = "Initiator Type"
fields["LastEditor"] = "Last Editor"
fields["FunctionToRun"] = "Function to run"
fields["JsonArgs"] = "JSON Args"
fields := map[string]string{
"Id": "ID",
"Name": "Name",
"Active": "Active",
"Language": "Language",
"ReferenceCount": "Reference Count",
"Modified": "Modified at",
"InitiatorType": "Initiator Type",
"LastEditor": "Last Editor",
"FunctionToRun": "Function to run",
"JsonArgs": "JSON Args",
}

describeOut := output.DescribeOutput{
GeneralOutput: output.GeneralOutput{
Out: f.IOStreams.Out,
Msg: filepath.Clean(opts.OutPath),
Flags: f.Flags,
Out: f.IOStreams.Out,
},
Fields: fields,
Values: resp,
Expand All @@ -87,9 +109,13 @@ func NewCmd(f *cmdutil.Factory) *cobra.Command {
},
}

cmd.Flags().Int64Var(&function_id, "function-id", 0, msg.FlagID)
cmd.Flags().Bool("with-code", false, msg.DescribeFlagWithCode)
cmd.Flags().BoolP("help", "h", false, msg.DescribeHelpFlag)
cobraCmd.Flags().Int64Var(&functionID, "function-id", 0, msg.FlagID)
cobraCmd.Flags().Bool("with-code", false, msg.DescribeFlagWithCode)
cobraCmd.Flags().BoolP("help", "h", false, msg.DescribeHelpFlag)

return cobraCmd
}

return cmd
func NewCmd(f *cmdutil.Factory) *cobra.Command {
return NewCobraCmd(NewDescribeCmd(f), f)
}
14 changes: 13 additions & 1 deletion pkg/cmd/describe/edge_function/edge_function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func TestDescribe(t *testing.T) {
response httpmock.Responder
args []string
expectErr bool
mockInput func(string) (string, error)
}{
{
name: "describe a function",
Expand All @@ -46,6 +47,15 @@ func TestDescribe(t *testing.T) {
args: []string{"--function-id", "123"},
expectErr: false,
},
{
name: "describe a function - no function id",
request: httpmock.REST("GET", "edge_functions/123"),
response: httpmock.JSONFromString(successResponse),
expectErr: false,
mockInput: func(s string) (string, error) {
return "123", nil
},
},
{
name: "with code",
request: httpmock.REST("GET", "edge_functions/123"),
Expand All @@ -68,7 +78,9 @@ func TestDescribe(t *testing.T) {
mock.Register(tt.request, tt.response)

f, _, _ := testutils.NewFactory(mock)
cmd := NewCmd(f)
descCmd := NewDescribeCmd(f)
descCmd.AskInput = tt.mockInput
cmd := NewCobraCmd(descCmd, f)
cmd.SetArgs(tt.args)

err := cmd.Execute()
Expand Down
Loading

0 comments on commit 4b93360

Please sign in to comment.