github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/base/cmd/cmd.go (about)

     1  package cmd
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"strings"
     7  )
     8  
     9  type Command struct {
    10  	Name string
    11  	Args []string
    12  }
    13  
    14  func NewCommand(name string, args string) *Command {
    15  	cmd := &Command{Name: name}
    16  	if _args := strings.Split(args, " "); len(args) > 0 {
    17  		cmd.Args = _args
    18  	}
    19  	return cmd
    20  }
    21  
    22  func (c Command) MarshalText() ([]byte, error) {
    23  	return []byte(strings.Join(append([]string{c.Name}, c.Args...), " ")), nil
    24  }
    25  
    26  func (c *Command) UnmarshalText(data []byte) error {
    27  	parts := bytes.Split(data, []byte(" "))
    28  	if len(parts) == 0 {
    29  		return fmt.Errorf("unexpected command line: %s", string(data))
    30  	}
    31  	c.Name = string(parts[0])
    32  	c.Args = c.Args[0:0]
    33  
    34  	for _, v := range parts[1:] {
    35  		c.Args = append(c.Args, string(v))
    36  	}
    37  	return nil
    38  }