all repos

viye @ 089144f

my shot at reimplementing xiki
3 files changed, 77 insertions(+), 0 deletions(-)
add curl tool
Author: Oleksandr Smirnov olexsmir@gmail.com
Committed at: 2026-08-27 23:00:09 +0300
Authored at: 2026-08-27 21:46:56 +0300
Change ID: oxmoukxrvutxvvvyplqlqrkxxnowxrqp
Parent: 0dd0513
A internal/plumbing/curl/curl.go
···
        
        1
        +package curl

      
        
        2
        +

      
        
        3
        +import (

      
        
        4
        +	"context"

      
        
        5
        +	"fmt"

      
        
        6
        +	"os/exec"

      
        
        7
        +	"strings"

      
        
        8
        +	"time"

      
        
        9
        +

      
        
        10
        +	"github.com/olexsmir/viye/internal/viye"

      
        
        11
        +)

      
        
        12
        +

      
        
        13
        +type Tool struct{}

      
        
        14
        +

      
        
        15
        +func (Tool) Name() string               { return "curl" }

      
        
        16
        +func (Tool) Match(c *viye.Context) bool { return c.Cmd == "curl" }

      
        
        17
        +func (Tool) Execute(c *viye.Context) (string, error) {

      
        
        18
        +	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)

      
        
        19
        +	defer cancel()

      
        
        20
        +

      
        
        21
        +	args := append([]string{"-sSL"}, c.Args...)

      
        
        22
        +	args = append(args, viye.ParseData(c.Body)...)

      
        
        23
        +	cmd := exec.CommandContext(ctx, "curl", args...)

      
        
        24
        +	cmd.Dir = c.Dir

      
        
        25
        +

      
        
        26
        +	output, err := cmd.CombinedOutput()

      
        
        27
        +	if err != nil && ctx.Err() == context.DeadlineExceeded {

      
        
        28
        +		return "", fmt.Errorf("timeout: command exceeded 5s")

      
        
        29
        +	}

      
        
        30
        +

      
        
        31
        +	out := strings.ReplaceAll(string(output), "\r", "")

      
        
        32
        +	return viye.FormatOutput(out), nil

      
        
        33
        +}

      
A internal/plumbing/curl/curl_test.go
···
        
        1
        +package curl

      
        
        2
        +

      
        
        3
        +import (

      
        
        4
        +	"os/exec"

      
        
        5
        +	"testing"

      
        
        6
        +

      
        
        7
        +	"github.com/olexsmir/viye/internal/viye"

      
        
        8
        +

      
        
        9
        +	"olexsmir.xyz/x/is"

      
        
        10
        +)

      
        
        11
        +

      
        
        12
        +func TestMatch(t *testing.T) {

      
        
        13
        +	is.Equal(t, true, (Tool{}).Match(&viye.Context{Cmd: "curl"}))

      
        
        14
        +	is.Equal(t, false, (Tool{}).Match(&viye.Context{Cmd: "get"}))

      
        
        15
        +}

      
        
        16
        +

      
        
        17
        +func TestExecute(t *testing.T) {

      
        
        18
        +	if _, err := exec.LookPath("curl"); err != nil {

      
        
        19
        +		t.Skip("curl not installed")

      
        
        20
        +	}

      
        
        21
        +

      
        
        22
        +	t.Run("version", func(t *testing.T) {

      
        
        23
        +		got, err := (Tool{}).Execute(&viye.Context{

      
        
        24
        +			Cmd:  "curl",

      
        
        25
        +			Args: []string{"--version"},

      
        
        26
        +			Dir:  ".",

      
        
        27
        +		})

      
        
        28
        +		is.Err(t, err, nil)

      
        
        29
        +		is.NotEqual(t, "", got)

      
        
        30
        +	})

      
        
        31
        +

      
        
        32
        +	t.Run("extra tags from body", func(t *testing.T) {

      
        
        33
        +		got, err := (Tool{}).Execute(&viye.Context{

      
        
        34
        +			Cmd:  "curl",

      
        
        35
        +			Args: []string{"--version"},

      
        
        36
        +			Body: []string{": --help"},

      
        
        37
        +			Dir:  ".",

      
        
        38
        +		})

      
        
        39
        +		is.Err(t, err, nil)

      
        
        40
        +		is.NotEqual(t, "", got)

      
        
        41
        +	})

      
        
        42
        +}

      
M main.go
···
        6
        6
         	"runtime/debug"

      
        7
        7
         

      
        8
        8
         	"github.com/olexsmir/viye/internal/plumbing/config"

      
        
        9
        +	"github.com/olexsmir/viye/internal/plumbing/curl"

      
        9
        10
         	"github.com/olexsmir/viye/internal/plumbing/files"

      
        10
        11
         	"github.com/olexsmir/viye/internal/plumbing/gobin"

      
        11
        12
         	"github.com/olexsmir/viye/internal/plumbing/http"

      ···
        32
        33
         	v.Register(&config.Tool{})

      
        33
        34
         	v.Register(&neofetch.Tool{})

      
        34
        35
         	v.Register(&makefile.Tool{})

      
        
        36
        +	v.Register(&curl.Tool{})

      
        35
        37
         

      
        36
        38
         	if err := v.Run(os.Stdout, os.Args); err != nil {

      
        37
        39
         		fmt.Fprintf(os.Stderr, "viye: %v\n", err)