Skip to content

Commit

Permalink
update the code logic
Browse files Browse the repository at this point in the history
  • Loading branch information
xwa153 committed Sep 13, 2023
1 parent c745c88 commit 73119d9
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 42 deletions.
26 changes: 8 additions & 18 deletions command/resource/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@ import (

"github.com/mitchellh/cli"

"github.com/hashicorp/consul/agent/consul"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/command/flags"
"github.com/hashicorp/consul/command/helpers"
"github.com/hashicorp/consul/command/resource"
"github.com/hashicorp/consul/internal/resourcehcl"
)

func New(ui cli.Ui) *cmd {
Expand Down Expand Up @@ -49,11 +46,6 @@ func (c *cmd) Run(args []string) int {
var resourceName string
var opts *api.QueryOptions

if len(args) == 0 {
c.UI.Error("Please provide required arguments")
return 1
}

if err := c.flags.Parse(args); err != nil {
if !errors.Is(err, flag.ErrHelp) {
c.UI.Error(fmt.Sprintf("Failed to parse args: %v", err))
Expand All @@ -63,14 +55,14 @@ func (c *cmd) Run(args []string) int {

if c.flags.Lookup("f").Value.String() != "" {
if c.filePath != "" {
data, err := helpers.LoadDataSourceNoRaw(c.filePath, nil)
parsedResource, err := resource.ParseResourceFromFile(c.filePath)
if err != nil {
c.UI.Error(fmt.Sprintf("Failed to load data: %v", err))
c.UI.Error(fmt.Sprintf("Failed to decode resource from input file: %v", err))
return 1
}
parsedResource, err := resourcehcl.Unmarshal([]byte(data), consul.NewTypeRegistry())
if err != nil {
c.UI.Error(fmt.Sprintf("Failed to decode resource from input file: %v", err))

if parsedResource == nil {
c.UI.Error("Unable to parse the file argument")
return 1
}

Expand Down Expand Up @@ -103,11 +95,9 @@ func (c *cmd) Run(args []string) int {
}

inputArgs := args[2:]
if err := c.flags.Parse(inputArgs); err != nil {
if errors.Is(err, flag.ErrHelp) {
return 0
}
c.UI.Error(fmt.Sprintf("Failed to parse args: %v", err))
err = resource.ParseInputParams(inputArgs, c.flags)
if err != nil {
c.UI.Error(fmt.Sprintf("Error parsing input arguments: %v", err))
return 1
}
if c.filePath != "" {
Expand Down
11 changes: 8 additions & 3 deletions command/resource/delete/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,28 @@ func TestResourceDeleteInvalidArgs(t *testing.T) {
"nil args": {
args: nil,
expectedCode: 1,
expectedErrMsg: "Please provide required arguments",
expectedErrMsg: "Must specify two arguments: resource type and resource name\n",
},
"empty args": {
args: []string{},
expectedCode: 1,
expectedErrMsg: "Please provide required arguments",
expectedErrMsg: "Must specify two arguments: resource type and resource name\n",
},
"missing file path": {
args: []string{"-f"},
expectedCode: 1,
expectedErrMsg: "Failed to parse args: flag needs an argument: -f",
},
"provide type and name": {
"missing resource name": {
args: []string{"a.b.c"},
expectedCode: 1,
expectedErrMsg: "Must specify two arguments: resource type and resource name",
},
"mal-formed group.version.kind": {
args: []string{"a.b", "name"},
expectedCode: 1,
expectedErrMsg: "Must include resource type argument in group.verion.kind format",
},
"does not provide resource name after type": {
args: []string{"a.b.c", "-namespace", "default"},
expectedCode: 1,
Expand Down
4 changes: 4 additions & 0 deletions command/resource/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func GetTypeAndResourceName(args []string) (gvk *api.GVK, resourceName string, e
}

s := strings.Split(args[0], ".")
if len(s) != 3 {
return nil, "", fmt.Errorf("Must include resource type argument in group.verion.kind format")
}

gvk = &api.GVK{
Group: s[0],
Version: s[1],
Expand Down
26 changes: 8 additions & 18 deletions command/resource/read/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@ import (

"github.com/mitchellh/cli"

"github.com/hashicorp/consul/agent/consul"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/command/flags"
"github.com/hashicorp/consul/command/helpers"
"github.com/hashicorp/consul/command/resource"
"github.com/hashicorp/consul/internal/resourcehcl"
)

func New(ui cli.Ui) *cmd {
Expand Down Expand Up @@ -50,11 +47,6 @@ func (c *cmd) Run(args []string) int {
var resourceName string
var opts *api.QueryOptions

if len(args) == 0 {
c.UI.Error("Please provide required arguments")
return 1
}

if err := c.flags.Parse(args); err != nil {
if !errors.Is(err, flag.ErrHelp) {
c.UI.Error(fmt.Sprintf("Failed to parse args: %v", err))
Expand All @@ -64,14 +56,14 @@ func (c *cmd) Run(args []string) int {

if c.flags.Lookup("f").Value.String() != "" {
if c.filePath != "" {
data, err := helpers.LoadDataSourceNoRaw(c.filePath, nil)
parsedResource, err := resource.ParseResourceFromFile(c.filePath)
if err != nil {
c.UI.Error(fmt.Sprintf("Failed to load data: %v", err))
c.UI.Error(fmt.Sprintf("Failed to decode resource from input file: %v", err))
return 1
}
parsedResource, err := resourcehcl.Unmarshal([]byte(data), consul.NewTypeRegistry())
if err != nil {
c.UI.Error(fmt.Sprintf("Failed to decode resource from input file: %v", err))

if parsedResource == nil {
c.UI.Error("Unable to parse the file argument")
return 1
}

Expand Down Expand Up @@ -105,11 +97,9 @@ func (c *cmd) Run(args []string) int {
}

inputArgs := args[2:]
if err := c.flags.Parse(inputArgs); err != nil {
if errors.Is(err, flag.ErrHelp) {
return 0
}
c.UI.Error(fmt.Sprintf("Failed to parse args: %v", err))
err = resource.ParseInputParams(inputArgs, c.flags)
if err != nil {
c.UI.Error(fmt.Sprintf("Error parsing input arguments: %v", err))
return 1
}
if c.filePath != "" {
Expand Down
11 changes: 8 additions & 3 deletions command/resource/read/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,28 @@ func TestResourceReadInvalidArgs(t *testing.T) {
"nil args": {
args: nil,
expectedCode: 1,
expectedErrMsg: "Please provide required arguments",
expectedErrMsg: "Must specify two arguments: resource type and resource name\n",
},
"empty args": {
args: []string{},
expectedCode: 1,
expectedErrMsg: "Please provide required arguments",
expectedErrMsg: "Must specify two arguments: resource type and resource name\n",
},
"missing file path": {
args: []string{"-f"},
expectedCode: 1,
expectedErrMsg: "Failed to parse args: flag needs an argument: -f",
},
"provide type and name": {
"missing resource name": {
args: []string{"a.b.c"},
expectedCode: 1,
expectedErrMsg: "Must specify two arguments: resource type and resource name",
},
"mal-formed group.version.kind": {
args: []string{"a.b", "name"},
expectedCode: 1,
expectedErrMsg: "Must include resource type argument in group.verion.kind format",
},
"does not provide resource name after type": {
args: []string{"a.b.c", "-namespace", "default"},
expectedCode: 1,
Expand Down

0 comments on commit 73119d9

Please sign in to comment.