Skip to content

Commit

Permalink
adds initial implementation for macro
Browse files Browse the repository at this point in the history
  • Loading branch information
ksysoev committed Nov 11, 2023
1 parent d3eb2b7 commit 67aca80
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
9 changes: 9 additions & 0 deletions example-macro-preset.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: '1'

domains:
- derivws.com
- binaryws.com
- deriv.dev

macro:
ticks: ['edit {"ticks": "R_50"}']
50 changes: 50 additions & 0 deletions pkg/cli/macro.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package cli

import (
"fmt"
"os"

"gopkg.in/yaml.v3"
)

type Config struct {
Version string `yaml:"version"`
Macro map[string][]string `yaml:"macro"`
Domains []string `yaml:"domains"`
}

type Macro map[string]Executer

func LoadMacro(path string) (*Macro, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}

var cfg Config
if err = yaml.Unmarshal(data, &cfg); err != nil {

Check failure on line 25 in pkg/cli/macro.go

View workflow job for this annotation

GitHub Actions / tests

sloppyReassign: re-assignment to `err` can be replaced with `err := yaml.Unmarshal(data, &cfg)` (gocritic)
return nil, err
}

if cfg.Version != "1" {
return nil, fmt.Errorf("unsupported macro version: %s", cfg.Version)
}

macro := make(Macro)
for name, rawCommands := range cfg.Macro {

Check failure on line 34 in pkg/cli/macro.go

View workflow job for this annotation

GitHub Actions / tests

ranges should only be cuddled with assignments used in the iteration (wsl)
var command Executer
for _, rawCommand := range rawCommands {

Check failure on line 36 in pkg/cli/macro.go

View workflow job for this annotation

GitHub Actions / tests

ranges should only be cuddled with assignments used in the iteration (wsl)
cmd, err := CommandFactory(rawCommand, nil)
if err != nil {
return nil, err
}

command = cmd
break

Check failure on line 43 in pkg/cli/macro.go

View workflow job for this annotation

GitHub Actions / tests

branch statements should not be cuddled if block has more than two lines (wsl)
}

macro[name] = command
}

return &macro, nil
}

0 comments on commit 67aca80

Please sign in to comment.