Skip to content

Commit

Permalink
refactor and simplify configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
mjhuber committed Mar 21, 2022
1 parent 6f556d3 commit de83c6c
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
# Dependency directories (remove the comment below to include it)
# vendor/
.envrc
test.go
36 changes: 14 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"os"
"strings"

aw "github.com/deanishe/awgo"
"github.com/deanishe/awgo/update"
Expand All @@ -18,6 +19,13 @@ var (
wf *aw.Workflow
)

// Options contains options for connecting to the confluence API
type Options struct {
BaseURL string `env:"CONFLUENCE_BASEURL"`
Token string `env:"CONFLUENCE_TOKEN"`
Username string `env:"CONFLUENCE_USERNAME"`
}

func init() {
wf = aw.New(update.GitHub(repo), aw.HelpURL(repo+"/issues"))
}
Expand All @@ -27,30 +35,14 @@ func main() {
}

func run() {
errors := []string{}
token, tFound := wf.Config.Env.Lookup("CONFLUENCE_TOKEN")
if !tFound {
errors = append(errors, "CONFLUENCE_TOKEN is not set!")
}

baseurl, bFound := wf.Config.Env.Lookup("CONFLUENCE_BASEURL")
if !bFound {
errors = append(errors, "CONFLUENCE_BASEURL is not set!")
}

username, uFound := wf.Config.Env.Lookup("CONFLUENCE_USERNAME")
if !uFound {
errors = append(errors, "CONFLUENCE_USERNAME is not set!")
}

if len(errors) > 0 {
for _, err := range errors {
wf.WarnEmpty(err, "")
}
wf.SendFeedback()
opts := &Options{}
cfg := aw.NewConfig()
if err := cfg.To(opts); err != nil {
wf.Fatalf("Error loading variables: %v", err)
return
}

search(token, baseurl, username, os.Args[1])
query := strings.Join(os.Args[1:], " ")
search(opts, query)
wf.SendFeedback()
}
7 changes: 4 additions & 3 deletions search.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
confluence "github.com/virtomize/confluence-go-api"
)

func search(token string, baseurl string, username string, searchQuery string) {
func search(cfg *Options, searchQuery string) {
showUpdateStatus()
api, err := confluence.NewAPI(fmt.Sprintf("%s/wiki/rest/api", baseurl), username, token)
api, err := confluence.NewAPI(fmt.Sprintf("%s/wiki/rest/api", cfg.BaseURL), cfg.Username, cfg.Token)
if err != nil {
log.Fatal(err)
}
Expand All @@ -27,6 +27,7 @@ func search(token string, baseurl string, username string, searchQuery string) {

// loop over results
for _, v := range result.Results {
wf.NewItem(v.Content.Title).Valid(true).Arg(fmt.Sprintf("%s/wiki%s", baseurl, v.URL)).Subtitle(v.Content.Space.Key)
url := fmt.Sprintf("%s/wiki%s", cfg.BaseURL, v.URL)
wf.NewItem(v.Content.Title).Valid(true).Arg(url).Subtitle(url)
}
}
Binary file modified workflow/alfred-go-confluence
Binary file not shown.
2 changes: 1 addition & 1 deletion workflow/info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
<string>CONFLUENCE_BASEURL</string>
</array>
<key>version</key>
<string>0.1.0</string>
<string>1.0.0</string>
<key>webaddress</key>
<string>https://github.com/mjhuber/alfred-go-confluence</string>
</dict>
Expand Down

0 comments on commit de83c6c

Please sign in to comment.