Skip to content

Commit

Permalink
Adds documentation to macro
Browse files Browse the repository at this point in the history
  • Loading branch information
ksysoev committed Nov 13, 2023
1 parent 52273db commit 7bf600e
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions pkg/cli/macro.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,21 @@ type Macro struct {
domains []string
}

// NewMacro creates a new Macro instance with the specified domains.
// The domains parameter is a slice of strings representing the allowed domains for the macro.
// Returns a pointer to the newly created Macro instance.
func NewMacro(domains []string) *Macro {
return &Macro{
macro: make(map[string]Executer),
domains: domains,
}
}

// AddCommands adds a new macro with the given name and commands to the Macro instance.
// If a macro with the same name already exists, it returns an error.
// If the rawCommands slice is empty, it returns an error.
// If the rawCommands slice has only one command, it adds the command directly to the macro.
// Otherwise, it creates a new CommandSequence with the commands and adds it to the macro.
func (m *Macro) AddCommands(name string, rawCommands []string) error {
if _, ok := m.macro[name]; ok {
return fmt.Errorf("macro already exists: %s", name)
Expand Down Expand Up @@ -55,6 +63,8 @@ func (m *Macro) AddCommands(name string, rawCommands []string) error {
return nil
}

// merge merges the given macro into the current macro.
// If a macro with the same name already exists, an error is returned.
func (m *Macro) merge(macro *Macro) error {
for name, cmd := range macro.macro {
if _, ok := m.macro[name]; ok {
Expand All @@ -67,6 +77,7 @@ func (m *Macro) merge(macro *Macro) error {
return nil
}

// Get returns the Executer associated with the given name, or an error if the name is not found.
func (m *Macro) Get(name string) (Executer, error) {
if cmd, ok := m.macro[name]; ok {
return cmd, nil
Expand All @@ -75,7 +86,9 @@ func (m *Macro) Get(name string) (Executer, error) {
return nil, fmt.Errorf("unknown command: %s", name)
}

func LoadMacro(path string) (*Macro, error) {
// LoadFromFile loads a macro configuration from a file at the given path.
// It returns a Macro instance and an error if the file cannot be read or parsed.
func LoadFromFile(path string) (*Macro, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
Expand All @@ -101,6 +114,9 @@ func LoadMacro(path string) (*Macro, error) {
return macroCfg, nil
}

// LoadMacroForDomain loads a macro for a given domain from a directory.
// It takes the directory path and the domain name as input parameters.
// It returns a pointer to a Macro struct and an error if any.
func LoadMacroForDomain(macroDir, domain string) (*Macro, error) {
files, err := os.ReadDir(macroDir)
if err != nil {
Expand All @@ -110,7 +126,7 @@ func LoadMacroForDomain(macroDir, domain string) (*Macro, error) {
var macro *Macro

for _, file := range files {
fileMacro, err := LoadMacro(macroDir + "/" + file.Name())
fileMacro, err := LoadFromFile(macroDir + "/" + file.Name())

if err != nil {
return nil, err
Expand Down

0 comments on commit 7bf600e

Please sign in to comment.