mholt/curl-to-go

Idea: parse $VAR and ${VAR} from "..." strings and convert to go code

drnic opened this issue · 4 comments

drnic commented
curl -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Token ${PIVOTAL_NETWORK_TOKEN}" -X GET https://network.pivotal.io/api/v2/products

would use https://golang.org/pkg/os/#ExpandEnv to become:

// Generated by curl-to-Go: https://mholt.github.io/curl-to-go

req, err := http.NewRequest("GET", "https://network.pivotal.io/api/v2/products", nil)
if err != nil {
    // handle err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", os.ExpandEnv("Token ${PIVOTAL_NETWORK_TOKEN}"))

resp, err := http.DefaultClient.Do(req)
if err != nil {
    // handle err
}
defer resp.Body.Close()

Perhaps the algorithm would detect $ in any string and wrap the string in os.ExpandEnv(...)?

mholt commented

That's probably a good idea -- although looking for $ in any string is a little dubious. How do we prevent false positives, e.g. actual dollar amounts or anything else that isn't an env var?

mholt commented

I guess we'd just parse it like bash does. If the dollar sign is escaped \$ then we treat it as a literal, otherwise an env var, thus triggering the os.ExpandEnv call.

drnic commented

As a generator we don't haven't to solve all problems :)

Even if we don't implement this at least now I (and anyone finding this issue) know to just wrap the string in that helper.

On Sat, Jan 30, 2016 at 4:48 PM, Matt Holt notifications@github.com
wrote:

That's probably a good idea -- although looking for $ in any string is a little dubious. How do we prevent false positives, e.g. actual dollar amounts or anything else that isn't an env var?

Reply to this email directly or view it on GitHub:
#5 (comment)

mholt commented

Cheers for the suggestion. Wasn't obvious how to do this but I think this should work in most cases.