marknotes-go/command.go
2015-09-28 18:38:17 -04:00

72 lines
2.2 KiB
Go

package main
import (
"strings"
"bytes"
"fmt"
)
// define command structure
type command struct {
action string
object string
extra string
} // end command structure
// Function to parse a string into a command structure
func CommandParse (CommandString string) (*command) {
CommandSlice := strings.SplitN(CommandString, " ", 3)
cmd := new(command)
if len(CommandSlice) > 0 {
cmd.action = strings.ToLower(CommandSlice[0])
} // if len(CommandSlice) > 0
if len(CommandSlice) > 1 {
cmd.object = strings.ToLower(CommandSlice[1])
} // if len(CommandSlice) > 1
if len(CommandSlice) > 2 {
cmd.extra = CommandSlice[2]
} // if len(CommandSlice) > 2
return cmd
} // end func ParseCommand
// Do a command
func CommandDo (cmd *command) () {
//defer DBLog(cmd.action, cmd.object, cmd.extra)
var buf bytes.Buffer
if cmd.action == "new" {
buf.WriteString("Creating a new ")
if strings.TrimRight(cmd.object, "s") == "note" {
buf.WriteString("note ")
if cmd.extra != "" {
buf.WriteString("with a title of ")
buf.WriteString(cmd.extra)
} // if extra not nil
} // if object == note
if strings.TrimRight(cmd.object, "s") == "log" {
buf.WriteString("log entry ")
if cmd.extra != "" {
buf.WriteString(" with an extra of ")
buf.WriteString(cmd.extra)
} // if cmd.extra != ""
} // if cmd.object == "log"
} else if cmd.action == "search" {
buf.WriteString("Searching ")
if strings.TrimRight(cmd.object, "s") == "note" {
buf.WriteString("notes for ")
if cmd.extra != "" {
buf.WriteString(strings.TrimLeft(cmd.extra, "for "))
} // if cmd.extra not nil
} // if cmd.object == note
if strings.TrimRight(cmd.object, "s") == "log" {
buf.WriteString("logs for ")
if cmd.extra != "" {
buf.WriteString(strings.TrimLeft(cmd.extra, "for "))
} // if cmd.extra != ""
} // if cmd.object == "log"
} else {
buf.WriteString("Not a valid command")
} // else
fmt.Println(buf.String())
return
} // end CommandDo