cmdref

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 14, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const CmdDirName = "cmdref"

CmdDirName is the name of the command directory for config storage.

View Source
const CmdFileName = "cmdref.json"

CmdFileName is the name of the file for command storage.

Variables

View Source
var Actions = []string{"Create", "Update", "Remove", "View", "Import", "Exit"}

Actions is a list of strings representing all actions.

Functions

func CreateDirIfNotExists

func CreateDirIfNotExists(dpath string) error

CreateDirIfNotExists creates the given directory if it doesn't exist.

func CreateHandler

func CreateHandler(cmdMap map[string]Command) (map[string]Command, error)

CreateHandler handles the creation of a new command.

func DeleteHandler

func DeleteHandler(cmdMap map[string]Command) (map[string]Command, error)

DeleteHandler handles the delete action.

func ImportHandler

func ImportHandler(fpath string, isMerge bool, cmdMap map[string]Command, fileOps CmdFileOperater) (map[string]Command, error)

ImportHandler loads the commands from the given path, and either merges or replaces the command map with new commands.

func LoadCommands

func LoadCommands(fileOps CmdFileOperater) (map[string]Command, error)

LoadCommands reads existing commands from the commands file.

func ProcessAction

func ProcessAction(cmdMap map[string]Command, action Action, fileOps CmdFileOperater) (map[string]Command, error)

ProcessAction takes the command file path, command map and an action to invoke the action.

func UpdateHandler

func UpdateHandler(cmdMap map[string]Command) (map[string]Command, error)

UpdateHandler handles the update action.

func ViewHandler

func ViewHandler(cmdMap map[string]Command) error

ViewHandler handles viewing commands.

Types

type Action

type Action int

Action represents any action supported by the app.

const (
	// Create action for creating a command
	Create Action = iota
	// Update action for updating an existing command
	Update
	// Remove action for removing a command
	Remove
	// View action for viewing a command
	View
	// Import action for importing commands from another file
	Import
	// Exit action for quitting the app
	Exit
)

func GetSelectedAction

func GetSelectedAction() (Action, error)

GetSelectedAction returns the action the user chose from the prompt.

type CmdFileOperater

type CmdFileOperater interface {
	Load() ([]Command, error)
	LoadExternal(filepath string) ([]Command, error)
	Save(commands []Command) error
	GetFilePath() string
}

CmdFileOperater defines an interface for saving/loading commands from the commands file. This is needed to create mock operations for unit tests, along with a live implementation for the actual file.

type CmdFileOps

type CmdFileOps struct{}

CmdFileOps implements the CommandsFileOperator for live/side-effectful operations on the commands file.

func NewCmdFileOps

func NewCmdFileOps() CmdFileOps

NewCmdFileOps creates and returns a new file operations struct.

func (*CmdFileOps) GetFilePath

func (f *CmdFileOps) GetFilePath() string

GetFilePath returns the absolute path to the commands file.

func (*CmdFileOps) Load

func (f *CmdFileOps) Load() ([]Command, error)

Load parses the commands file and returns the list of commands.

func (*CmdFileOps) LoadExternal

func (f *CmdFileOps) LoadExternal(filepath string) ([]Command, error)

LoadExternal parses the commands file and returns the list of commands.

func (*CmdFileOps) Save

func (f *CmdFileOps) Save(commands []Command) error

Save writes the list of commands to the commands file.

type Command

type Command struct {
	Name        string `json:"name"`
	Command     string `json:"command"`
	Platform    string `json:"platform"`
	Description string `json:"description"`
}

Command represents a command to store and query.

func (Command) String

func (cmd Command) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL