<commit_before>const util = require('../util')
const jsesc = require('jsesc')

const toGo = curlCommand => {
  const request = util.parseCurlCommand(curlCommand)
  let goCode = 'package main\n\n'
  goCode += 'import (\n\t"fmt"\n\t"io/ioutil"\n\t"log"\n\t"net/http"\n)\n\n'
  goCode += 'func main() {\n'
  goCode += '\tclient := &http.Client{}\n'
  if (request.data === true) {
    request.data = ''
  }
  if (request.data) {
    if (typeof request.data === 'number') {
      request.data = request.data.toString()
    }
    if (request.data.indexOf("'") > -1) {
      request.data = jsesc(request.data)
    }
    // import strings
    goCode = goCode.replace('\n)', '\n\t"strings"\n)')
    goCode += '\tvar data = strings.NewReader(`' + request.data + '`)\n'
    goCode += '\treq, err := http.NewRequest("' + request.method.toUpperCase() + '", "' + request.url + '", data)\n'
  } else {
    goCode += '\treq, err := http.NewRequest("' + request.method.toUpperCase() + '", "' + request.url + '", nil)\n'
  }
  goCode += '\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n'
  if (request.headers || request.cookies) {
    for (const headerName in request.headers) {
      goCode += '\treq.Header.Set(`' + headerName + '`, `' + request.headers[headerName] + '`)\n'
    }
    if (request.cookies) {
      const cookieString = util.serializeCookies(request.cookies)
      goCode += '\treq.Header.Set("Cookie", "' + cookieString + '")\n'
    }
  }

  if (request.auth) {
    const splitAuth = request.auth.split(':')
    const user = splitAuth[0] || ''
    const password = splitAuth[1] || ''
    goCode += '\treq.SetBasicAuth("' + user + '", "' + password + '")\n'
  }
  goCode += '\tresp, err := client.Do(req)\n'
  goCode += '\tif err != nil {\n'
  goCode += '\t\tlog.Fatal(err)\n'
  goCode += '\t}\n'
  goCode += '\tdefer resp.Body.Close()\n'
  goCode += '\tbodyText, err := ioutil.ReadAll(resp.Body)\n'
  goCode += '\tif err != nil {\n'
  goCode += '\t\tlog.Fatal(err)\n'
  goCode += '\t}\n'
  goCode += '\tfmt.Printf("%s\\n", bodyText)\n'
  goCode += '}'

  return goCode + '\n'
}

module.exports = toGo<commit_msg>Revert "Update go.js (#273)"<commit_after>const util = require('../util')
const jsesc = require('jsesc')

const toGo = curlCommand => {
  const request = util.parseCurlCommand(curlCommand)
  let goCode = 'package main\n\n'
  goCode += 'import (\n\t"fmt"\n\t"io/ioutil"\n\t"log"\n\t"net/http"\n)\n\n'
  goCode += 'func main() {\n'
  goCode += '\tclient := &http.Client{}\n'
  if (request.data === true) {
    request.data = ''
  }
  if (request.data) {
    if (typeof request.data === 'number') {
      request.data = request.data.toString()
    }
    if (request.data.indexOf("'") > -1) {
      request.data = jsesc(request.data)
    }
    // import strings
    goCode = goCode.replace('\n)', '\n\t"strings"\n)')
    goCode += '\tvar data = strings.NewReader(`' + request.data + '`)\n'
    goCode += '\treq, err := http.NewRequest("' + request.method.toUpperCase() + '", "' + request.url + '", data)\n'
  } else {
    goCode += '\treq, err := http.NewRequest("' + request.method.toUpperCase() + '", "' + request.url + '", nil)\n'
  }
  goCode += '\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n'
  if (request.headers || request.cookies) {
    for (const headerName in request.headers) {
      goCode += '\treq.Header.Set("' + headerName + '", "' + request.headers[headerName] + '")\n'
    }
    if (request.cookies) {
      const cookieString = util.serializeCookies(request.cookies)
      goCode += '\treq.Header.Set("Cookie", "' + cookieString + '")\n'
    }
  }

  if (request.auth) {
    const splitAuth = request.auth.split(':')
    const user = splitAuth[0] || ''
    const password = splitAuth[1] || ''
    goCode += '\treq.SetBasicAuth("' + user + '", "' + password + '")\n'
  }
  goCode += '\tresp, err := client.Do(req)\n'
  goCode += '\tif err != nil {\n'
  goCode += '\t\tlog.Fatal(err)\n'
  goCode += '\t}\n'
  goCode += '\tdefer resp.Body.Close()\n'
  goCode += '\tbodyText, err := ioutil.ReadAll(resp.Body)\n'
  goCode += '\tif err != nil {\n'
  goCode += '\t\tlog.Fatal(err)\n'
  goCode += '\t}\n'
  goCode += '\tfmt.Printf("%s\\n", bodyText)\n'
  goCode += '}'

  return goCode + '\n'
}

module.exports = toGo
{'fraction_non_alphanumeric': 0.22631703283723129, 'fraction_numerical': 0.002126151665485471, 'mean_word_length': 2.9167437557816838, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '716260', 'n_tokens_mistral': 1613, 'n_tokens_neox': 1586, 'n_words': 302}