Skip to content
This repository has been archived by the owner on Jan 12, 2025. It is now read-only.

Commit

Permalink
Merge pull request #24 from ok-borg/borg-usage
Browse files Browse the repository at this point in the history
Borg usage
  • Loading branch information
crufter authored Oct 13, 2016
2 parents c92d498 + 92a3e5a commit 1e94a18
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 33 deletions.
36 changes: 36 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,37 @@

### Go ###
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so

# Folders
_obj
_test

# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out

*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*

_testmain.go

*.exe
*.test
*.prof

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# external packages folder
vendor/

bower_components/

#Atom ctags
.tags*
8 changes: 8 additions & 0 deletions commands/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package commands

var Commands map[string]Command = map[string]Command{}

type Command struct {
F func([]string) error
Summary string
}
16 changes: 13 additions & 3 deletions commands/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/ok-borg/borg/conf"
"github.com/ok-borg/borg/types"
"io/ioutil"
"net/http"
"os"
Expand All @@ -16,8 +14,19 @@ import (
"strings"
"text/template"
"time"

"github.com/ok-borg/borg/conf"
"github.com/ok-borg/borg/types"
)

func init() {
var summary string = "Edit summary"
Commands["edit"] = Command{
F: Edit,
Summary: summary,
}
}

func findIdFromQueryIndex(queryIndex int) (string, error) {
bs, err := ioutil.ReadFile(conf.HomeDir + "/.borg/query")
if err != nil {
Expand All @@ -43,7 +52,8 @@ func findIdFromQueryIndex(queryIndex int) (string, error) {
}

// Edit a snippet based on index from last query results
func Edit(queryIndex string) error {
func Edit(args []string) error {
queryIndex := args[1]
i, err := strconv.ParseInt(queryIndex, 10, 32)
if err != nil {
return err
Expand Down
12 changes: 11 additions & 1 deletion commands/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@ package commands

import (
"errors"

"github.com/ok-borg/borg/conf"
)

func init() {
var summary string = "Editor summary"
Commands["editor"] = Command{
F: Editor,
Summary: summary,
}
}

// Login saves a token acquired from the web page into the user config file
func Editor(editor string) error {
func Editor(args []string) error {
editor := args[1]
if len(editor) == 0 {
return errors.New("Please supply an editor. The default is vim, so if you are happy with that, do nothing.")
}
Expand Down
11 changes: 10 additions & 1 deletion commands/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@ import (
"strconv"
)

func init() {
var summary string = "Link summary"
Commands["link"] = Command{
F: Link,
Summary: summary,
}
}

// Link prints the url to a query result
func Link(queryIndex string) error {
func Link(args []string) error {
queryIndex := args[1]
i, err := strconv.ParseInt(queryIndex, 10, 32)
if err != nil {
return err
Expand Down
12 changes: 11 additions & 1 deletion commands/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@ package commands

import (
"errors"

"github.com/ok-borg/borg/conf"
)

func init() {
var summary string = "Login summary"
Commands["login"] = Command{
F: Login,
Summary: summary,
}
}

// Login saves a token acquired from the web page into the user config file
func Login(token string) error {
func Login(args []string) error {
token := args[1]
if len(token) == 0 {
return errors.New("Please supply a token. Don't have one? Go to http://ok-b.org and get it")
}
Expand Down
15 changes: 12 additions & 3 deletions commands/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,25 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/ok-borg/borg/conf"
"github.com/ok-borg/borg/types"
"io/ioutil"
"net/http"
"os"
"os/exec"
"strings"
"time"

"github.com/ok-borg/borg/conf"
"github.com/ok-borg/borg/types"
)

func init() {
var summary string = "Save summary"
Commands["new"] = Command{
F: New,
Summary: summary,
}
}

func extractPost(s string) (string, string, error) {
ss := strings.Split(s, "\n")
if len(ss) < 3 {
Expand All @@ -26,7 +35,7 @@ func extractPost(s string) (string, string, error) {
}

// New saves a new snippet into the borg mind
func New() error {
func New([]string) error {
c, err := conf.Get()
if err != nil {
return err
Expand Down
14 changes: 12 additions & 2 deletions commands/worked.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,25 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/ok-borg/borg/conf"
"io/ioutil"
"net/http"
"strconv"
"time"

"github.com/ok-borg/borg/conf"
)

func init() {
var summary string = "Worked summary"
Commands["worked"] = Command{
F: Worked,
Summary: summary,
}
}

// Worked lets you mark a result as relevant one for a query
func Worked(queryIndex string) error {
func Worked(args []string) error {
queryIndex := args[1]
i, err := strconv.ParseInt(queryIndex, 10, 32)
if err != nil {
return err
Expand Down
44 changes: 22 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package main

import (
"fmt"
"os"

flag "github.com/juju/gnuflag"
"github.com/ok-borg/borg/commands"
"os"
)

func main() {
Expand All @@ -13,29 +14,28 @@ func main() {
help()
return
}
var err error
switch flag.Arg(0) {
case "new":
err = commands.New()
case "login":
err = commands.Login(flag.Arg(1))
case "edit":
err = commands.Edit(flag.Arg(1))
case "worked":
err = commands.Worked(flag.Arg(1))
case "link":
err = commands.Link(flag.Arg(1))
case "editor":
err = commands.Editor(flag.Arg(1))
default:
err = commands.Query(flag.Arg(0))
}
if err != nil {
fmt.Println(err)
os.Exit(1)

if c, ok := commands.Commands[flag.Arg(0)]; !ok {
commands.Query(flag.Arg(0))
} else {
if err := c.F(flag.Args()); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
}

func help() {
fmt.Println("Usage: borg \"your question\"")
fmt.Print("\033[4mUsage:\033[0m\n\n")
fmt.Print("\t$ \033[32mborg \"your question\"\033[0m\n")
fmt.Print("\t$ \033[32mborg COMMAND\033[0m\n")
fmt.Print("\n\t BORG - A terminal based search engine for bash snippets\n\n")
fmt.Print("\033[4mCommands:\033[0m\n\n")
for k, v := range commands.Commands {
fmt.Printf("\t\033[32m+ %-8s\t\033[0m%s\n", k, v.Summary)
}
// TODO: Display all the possible flags
fmt.Print("\n\033[4mOptions:\033[0m\n\n")
// TODO: Replace --help so that it displays this usage instead
fmt.Printf("\t\033[34m%-8s\t\033[0m%s\n", "--help", "Show help")
}

0 comments on commit 1e94a18

Please sign in to comment.