Skip to content

Commit

Permalink
add quote only if necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
dcb9 committed Dec 21, 2022
1 parent 220baff commit fc54a35
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
19 changes: 17 additions & 2 deletions httpie/cmdline.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ func (cl *CmdLine) AddItem(i *Item) {
cl.Items = append(cl.Items, i)
}

func addQuoteIfNeeded(s string) string {
specialCharIndex := strings.IndexFunc(s, func(r rune) bool {
switch r {
case '&', '@', '#', '[', ']', '{', '}':
return true
}
return false
})
if -1 == specialCharIndex {
return fmt.Sprintf("%s", s)
}

return fmt.Sprintf("'%s'", s)
}

func (cl *CmdLine) String() string {
// slice
s := make([]string, 0, len(cl.Flags)+len(cl.Items)+3) // http method url
Expand All @@ -50,7 +65,7 @@ func (cl *CmdLine) String() string {

// default flag
for _, v := range cl.Flags {
s = append(s, v.String())
s = append(s, addQuoteIfNeeded(v.String()))
}

if cl.Method == nil {
Expand All @@ -69,7 +84,7 @@ func (cl *CmdLine) String() string {
s = append(s, "--"+cl.ContentType)
}

s = append(s, cl.Method.String(), fmt.Sprintf("'%s'", cl.URL))
s = append(s, cl.Method.String(), addQuoteIfNeeded(cl.URL))

for _, v := range cl.Items {
s = append(s, v.String())
Expand Down
2 changes: 1 addition & 1 deletion httpie/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Item struct {
}

func (i *Item) String() string {
return fmt.Sprintf(`'%s%s%s'`, i.K, i.S, i.V)
return fmt.Sprintf(`%s%s%s`, i.K, i.S, i.V)
}

func NewHeader(key, val string) *Item {
Expand Down

0 comments on commit fc54a35

Please sign in to comment.