Skip to content

Instantly share code, notes, and snippets.

@eculver
Created March 27, 2023 17:54
Show Gist options
  • Save eculver/28fa76c97f7e02416865bdad0805f9c2 to your computer and use it in GitHub Desktop.
Save eculver/28fa76c97f7e02416865bdad0805f9c2 to your computer and use it in GitHub Desktop.

Revisions

  1. eculver created this gist Mar 27, 2023.
    60 changes: 60 additions & 0 deletions bunny.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    // package bunny provides the public API for the eBunny tool.
    package bunny

    import (
    "errors"
    "fmt"
    "strconv"
    "strings"

    "github.com/eculver/go-play/pkg/bunny/github"
    )

    // New returns a new Bunny that is JACKED and ready to JUMP
    func New() Bunny {
    // all our Bunny can do for now is jump to GitHub URLs in a browser
    return &GitHubBunny{
    Commands: []string{"github", "gh"},
    }
    }

    // Bunny is an incredibly cute animal that jumps. When a bunny lands its jump,
    // it can either stop or... keep jumping! The options are limitless!
    //
    // From a practical perspective, this is root of our execution path. A Bunny
    // will call Jump's until the path terminates. It's just a way to express paths
    // through the tree of all known Jumps. When the bunny starts up, a Jump graph
    // is generated from config/code and the root Jump
    // starts traversing until it's done.
    type Bunny interface {
    Jump(args ...string) error
    }

    // GitHubBunny Jumps to GitHub URLs
    type GitHubBunny struct {
    Commands []string
    }

    // Jump ...
    func (gh *GitHubBunny) Jump(args ...string) error {
    if len(args) == 0 {
    return errors.New("GitHub Bunny requires arguments. Usage will be here eventually")
    }

    // this is going to change to be a proper traversal
    if len(args) == 3 {
    switch args[0] {
    case "pr":
    num, err := strconv.Atoi(args[2])
    if err != nil {
    return fmt.Errorf("must pass number for PR num, got: %s. %w", args[2], err)
    }
    orgRepo := strings.Split(args[1], "/")
    if len(orgRepo) > 1 {
    return github.OpenPullRequest(orgRepo[0], orgRepo[1], num)
    }
    return fmt.Errorf("must pass / delimited org/repo string, got: %s", args[1])
    }
    }
    return fmt.Errorf("Nothing happened for args: %v", args)
    }