Code Monkey home page Code Monkey logo

curly's Introduction

curly

curl-to-go port with the power of Golang.

This is fun project which converts curl command to Golang code with ability of its further execution.

The idea is to re-use existing tools as much as possible in Golang-ecosystem.

This utility is not a replacement for concurrent requests.

As per 7.68.0 curl can do parallel requests. See examples: https://stackoverflow.com/questions/9624967/how-do-i-use-curl-to-perform-multiple-simultaneous-requests/66998509#66998509

Dependencies

It would be impossible to build this utility without next pieces of software:

Library What it does?
mholt/curl-to-go The original Javascript converter code
mholt/json-to-go The auxiliary Javascript library for go structs
jerrybendy/url-search-params-polyfill The missing Javascript bit from V8 engine ECMAScript implementation
dop251/goja ECMAScript/JavaScript engine in pure Go
traefik/yaegi Elegant Go Interpreter written in Go
urfave/cli CLI-app framework for Go

How does it work?

curly itself provides just "glue"-code for all the parts together in form of a CLI-application.

Processing might be explained by following diagram:

diagram

The tool reads curl command from STDIN and executes converter curlToGo on V8 engine Goja.

Received go code enhanced with missing imports and beautified.

Depending on flags code could be dumped or interpreted in runtime via Yaegi.

Installation

Currently, distribution is available in form of a binary executable on Releases page for Linux and Mac OS.

Binary could be added to your bin directory.

Usage

  1. Read curl command and run it with default params:

    echo "curl -X GET https://example.com" | curly
  2. Read curl command and dump generated go code without execution:

    echo "curl -X GET https://example.com" | curly -d

    This will produce following result:

    // This code is auto-generated by curly
    package main
    
    import (
        "fmt"
        "io"
        "net/http"
        "sync"
        "time"
    )
    
    func main() {
        var wg sync.WaitGroup
        var chunkSize int
    
        numReqs := 1
        concurrentReqs := 1
        sleepDuration, _ := time.ParseDuration("0s")
    
        for i := 0; i < numReqs; i += concurrentReqs {
            if i+concurrentReqs < numReqs {
                chunkSize = concurrentReqs
            } else {
                chunkSize = numReqs - i
            }
    
            for r := 0; r < chunkSize; r++ {
                wg.Add(1)
    
                go func() {
                    defer wg.Done()
                    request()
                }()
            }
    
            wg.Wait()
    
            time.Sleep(sleepDuration)
        }
    }
    
    func request() {
        // Generated by curl-to-Go: https://mholt.github.io/curl-to-go
    
        // curl -X GET https://example.com
        //
    
        resp, err := http.Get("https://example.com")
        if err != nil {
            fmt.Println(err)
            return
        }
        defer resp.Body.Close()
    
        respBody, err := io.ReadAll(resp.Body)
    
        if err != nil {
            fmt.Println(err)
            return
        }
    
        fmt.Println(string(respBody))
    }
  3. Read curl command from clipboard and run generated code in 50 requests with 5 concurrency:

    xclip -o | curly -r 50 -c 5
  4. Read curl command from file and run generated code in 10 requests with 1 concurrency and sleep duration of 1 second:

    cat curl.txt | curly -r 10 -s 1s

Build

It's possible to build binary by your own:

make build

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.