You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.6 KiB
Go

package commands
import (
"log"
"git.snorba.art/hugo/nssh/storage"
)
type TestCommand struct {
filestore storage.Filestore
}
var fakeDirs = map[string]bool{
"/bin": true,
"/usr/bin": true,
"/sbin": true,
}
func (c *TestCommand) Execute(shell *Shell, args []string) error {
if len(args) > 0 && args[0] == "0" {
_, err := shell.WriteOutputString(trampSuccess)
return err
}
if len(args) > 1 && args[0] == "-d" {
log.Printf("Checking for existance of directory %s", args[1])
var exists bool
var err error
if answer, ok := fakeDirs[args[1]]; ok {
exists = answer
} else {
exists, err = c.filestore.DirectoryExists(args[1])
if err != nil {
return err
}
}
if exists {
log.Printf("Directory %s exists", args[1])
_, err := shell.WriteOutputString(trampSuccess)
return err
}
_, err = shell.WriteOutputString(trampFailure)
return err
}
if len(args) > 1 && (args[0] == "-e" || args[0] == "-r") {
log.Printf("Checking for existance of file %s", args[1])
// Tramp sometimes throws in a sentence "this file does not exist", my
// guess is it does this to check if the test command is working
// properly. This catches that and makes the test command fail
// appropriately.
if len(args) > 5 && args[2] == "this" && args[5] == "not" {
_, err := shell.WriteOutputString(trampFailure)
return err
}
if exists, _ := c.filestore.Exists(args[1]); exists {
log.Printf("File %s exists", args[1])
_, err := shell.WriteOutputString(trampSuccess)
return err
}
_, err := shell.WriteOutputString(trampFailure)
return err
}
return nil
}