Skip to content

Commit

Permalink
Merge pull request #40 from ddelnano/add-more-error-handling-to-scrip…
Browse files Browse the repository at this point in the history
…ts-and-schedules

Add more error handling to scheduler and script client
  • Loading branch information
ddelnano authored Jul 13, 2021
2 parents e66b98a + 7210737 commit e40ebe8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
13 changes: 13 additions & 0 deletions client/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ type Scheduler struct {

func (client Mikrotik) FindScheduler(name string) (*Scheduler, error) {
c, err := client.getMikrotikClient()

if err != nil {
return nil, err
}

cmd := []string{"/system/scheduler/print", "?name=" + name}
log.Printf("[INFO] Running the mikrotik command: `%s`", cmd)
r, err := c.RunArgs(cmd)
Expand All @@ -37,6 +42,10 @@ func (client Mikrotik) FindScheduler(name string) (*Scheduler, error) {
func (client Mikrotik) DeleteScheduler(name string) error {
c, err := client.getMikrotikClient()

if err != nil {
return err
}

scheduler, err := client.FindScheduler(name)

if err != nil {
Expand All @@ -53,6 +62,10 @@ func (client Mikrotik) DeleteScheduler(name string) error {
func (client Mikrotik) CreateScheduler(s *Scheduler) (*Scheduler, error) {
c, err := client.getMikrotikClient()

if err != nil {
return nil, err
}

cmd := Marshal("/system/scheduler/add", s)

log.Printf("[INFO] Running the mikrotik command: `%s`", cmd)
Expand Down
10 changes: 10 additions & 0 deletions client/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ func (s *Script) Policy() []string {

func (client Mikrotik) CreateScript(name, owner, source string, policies []string, dontReqPerms bool) (*Script, error) {
c, err := client.getMikrotikClient()
if err != nil {
return nil, err
}

policiesString := strings.Join(policies, ",")
nameArg := fmt.Sprintf("=name=%s", name)
Expand Down Expand Up @@ -85,6 +88,9 @@ func (client Mikrotik) UpdateScript(name, owner, source string, policy []string,

func (client Mikrotik) DeleteScript(name string) error {
c, err := client.getMikrotikClient()
if err != nil {
return err
}

script, err := client.FindScript(name)

Expand All @@ -101,6 +107,10 @@ func (client Mikrotik) DeleteScript(name string) error {

func (client Mikrotik) FindScript(name string) (*Script, error) {
c, err := client.getMikrotikClient()

if err != nil {
return nil, err
}
cmd := []string{"/system/script/print", "?name=" + name}
log.Printf("[INFO] Running the mikrotik command: `%s`", cmd)
r, err := c.RunArgs(cmd)
Expand Down

0 comments on commit e40ebe8

Please sign in to comment.