Skip to content

Commit

Permalink
Increment version to 2.0.4. Use envconfig now. (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
blacs30 committed Aug 22, 2020
1 parent 65aa38c commit f953ee5
Show file tree
Hide file tree
Showing 14 changed files with 272 additions and 814 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ To use, activate Alfred and type `.bw` to trigger this workflow. From there:
| bw_keyword | defines the keyword which opens the Bitwarden Alfred Workflow | .bw |
| bwauth_keyword | defines the keyword which opens the Bitwarden authentications of the Alfred Workflow | .bwauth |
| bwconf_keyword | defines the keyword which opens the Bitwarden configuration/settings of the Alfred Workflow | .bwconfig |
| CACHE_TIMEOUT | in minutes 1440 = 1 day | 1440 |
| EMAIL | the email which to use for the login via the Bitwarden CLI, will be read from the data.json of the Bitwarden CLI if present | "" |
| EMPTY_DETAIL_RESULTS | Show all information in the detail view, also if the content is empty | false |
| ICON_CACHE_ENABLED | Download icons for login items if a URL is set | true |
Expand Down
246 changes: 72 additions & 174 deletions alfred/config.go
Original file line number Diff line number Diff line change
@@ -1,196 +1,94 @@
package alfred

import (
"encoding/json"
"fmt"
aw "github.com/deanishe/awgo"
"github.com/jychri/tilde"
"io/ioutil"
"log"
"os"
"strings"
"encoding/json"
"fmt"
aw "github.com/deanishe/awgo"
"github.com/jychri/tilde"
"io/ioutil"
"log"
"os"
"strings"
)

const (
server = "SERVER_URL"
email = "EMAIL"
sfa = "2FA_ENABLED"
sfaMode = "2FA_MODE"
BwauthKeyword = "bwauth_keyword"
BwconfKeyword = "bwconf_keyword"
BwKeyword = "bw_keyword"
BwExec = "BW_EXEC"
mod1 = "MODIFIER_1"
mod2 = "MODIFIER_2"
mod3 = "MODIFIER_3"
mod4 = "MODIFIER_4"
ReorderingDisabled = "REORDERING_DISABLED"
MaxResults = "MAX_RESULTS"
EmptyDetailResults = "EMPTY_DETAIL_RESULTS"
IconCacheEnabled = "ICON_CACHE_ENABLED"
IconCacheTimeout = "ICON_CACHE_TIMEOUT"
AutoFetchIconCacheTimeout = "AUTO_FETCH_ICON_CACHE_TIMEOUT"
SyncCacheTimeout = "SYNC_CACHE_TIMEOUT"
CacheTimeout = "CACHE_TIMEOUT"
OutputFolder = "OUTPUT_FOLDER"
)

// Get keys
func GetBwConfKeyword(wf *aw.Workflow) string {
return wf.Config.GetString(BwconfKeyword, ".bwconfig")
}

func GetCacheTimeout(wf *aw.Workflow) int {
return wf.Config.GetInt(CacheTimeout, 1440)
}

func GetOutputFolder(wf *aw.Workflow) string {
folder := wf.Config.GetString(OutputFolder, fmt.Sprintf("%s/Downloads/", ""))
if folder == "" {
defaultFolder, err := os.UserHomeDir()
if err != nil {
log.Print("Error getting UserHomeDir ", err)
}
folder = fmt.Sprintf("%s/Downloads/", defaultFolder)
} else {
// tilde.Abs() expands ~ to /Users/$user
folder = tilde.Abs(folder)
// in case a "/" exist on the right side, remove it and add it again to be sure it exists.
folder = fmt.Sprintf("%s/", strings.TrimRight(folder, "/"))
}
return folder
}

func GetMaxResults(wf *aw.Workflow) int {
return wf.Config.GetInt(MaxResults, 20)
}

func GetIconCacheEnabled(wf *aw.Workflow) bool {
return wf.Config.GetBool(IconCacheEnabled, true)
}

func GetIconCacheTimeout(wf *aw.Workflow) int {
return wf.Config.GetInt(IconCacheTimeout, 43200)
}

// If an error occurs while getting the icons try to get this icon
// not only after this timeout is expired
func GetAutoFetchIconCacheTimeout(wf *aw.Workflow) int {
return wf.Config.GetInt(AutoFetchIconCacheTimeout, 1440)
}

func GetSyncCacheTimeout(wf *aw.Workflow) int {
return wf.Config.GetInt(SyncCacheTimeout, 1440)
}

func GetEmptyDetailResults(wf *aw.Workflow) bool {
return wf.Config.GetBool(EmptyDetailResults, true)
}
func GetReorderingDisabled(wf *aw.Workflow) bool {
return wf.Config.GetBool(ReorderingDisabled, true)
}

func GetBwExec(wf *aw.Workflow) string {
return wf.Config.GetString(BwExec, "bw")
}

func GetBwauthKeyword(wf *aw.Workflow) string {
return wf.Config.GetString(BwauthKeyword, ".bwauth")
}

func GetBwKeyword(wf *aw.Workflow) string {
return wf.Config.GetString(BwKeyword, ".bw")
}

func GetServer(wf *aw.Workflow) string {
return wf.Config.GetString(server)
}

func GetEmail(wf *aw.Workflow) string {
getEmail := wf.Config.GetString(email, "")
if getEmail == "" {
var bwData BwData
succ, err := OpenBitwardenData(&bwData)
if err != nil {
log.Println(err)
return ""
}
if succ {
err := SetEmail(wf, bwData.UserEmail)
if err != nil {
log.Println(err)
return ""
}
getEmail = bwData.UserEmail
}
}
return getEmail
}

func GetSfa(wf *aw.Workflow) bool {
return wf.Config.GetBool(sfa, true)
}

func GetSfaMode(wf *aw.Workflow) int {
return wf.Config.GetInt(sfaMode, 0)
}

//// Modifiers
func GetMod1(wf *aw.Workflow) string {
return wf.Config.GetString(mod1, "alt")
}
func GetMod2(wf *aw.Workflow) string {
return wf.Config.GetString(mod2, "shift")
}
func GetMod3(wf *aw.Workflow) string {
return wf.Config.GetString(mod3, "cmd")
}
func GetMod4(wf *aw.Workflow) string {
return wf.Config.GetString(mod4, "cmd,alt,ctrl")
}

// Set keys
func GetOutputFolder(wf *aw.Workflow, folder string) string {
if folder == "" {
defaultFolder, err := os.UserHomeDir()
if err != nil {
log.Print("Error getting UserHomeDir ", err)
}
folder = fmt.Sprintf("%s/Downloads/", defaultFolder)
} else {
// tilde.Abs() expands ~ to /Users/$user
folder = tilde.Abs(folder)
// in case a "/" exist on the right side, remove it and add it again to be sure it exists.
folder = fmt.Sprintf("%s/", strings.TrimRight(folder, "/"))
}
return folder
}

func GetEmail(wf *aw.Workflow, email string) string {
if email == "" {
var bwData BwData
succ, err := OpenBitwardenData(&bwData)
if err != nil {
log.Println(err)
return ""
}
if succ {
err := SetEmail(wf, bwData.UserEmail)
if err != nil {
log.Println(err)
return ""
}
email = bwData.UserEmail
}
}
return email
}

//Set keys
func SetServer(wf *aw.Workflow, url string) error {
return wf.Config.Set(server, url, false).Do()
return wf.Config.Set("SERVER_URL", url, false).Do()
}

func SetEmail(wf *aw.Workflow, address string) error {
return wf.Config.Set(email, address, false).Do()
return wf.Config.Set("EMAIL", address, false).Do()
}

func SetSfa(wf *aw.Workflow, enabled string) error {
return wf.Config.Set(sfa, enabled, true).Do()
return wf.Config.Set("2FA_ENABLED", enabled, true).Do()
}

func SetSfaMode(wf *aw.Workflow, id string) error {
return wf.Config.Set(sfaMode, id, true).Do()
return wf.Config.Set("2FA_MODE", id, true).Do()
}

func OpenBitwardenData(bwData interface{}) (bool, error) {
homedir, err := os.UserHomeDir()
if err != nil {
return false, err
}
bwDataPath := fmt.Sprintf("%s/Library/Application Support/Bitwarden CLI/data.json", homedir )
log.Println("BW DataPath", bwDataPath)
if _, err := os.Stat(bwDataPath); err != nil {
log.Println("Couldn't find the Bitwarden data.json ", err)
return false, err
}
data, err := ioutil.ReadFile(bwDataPath)
if err != nil {
return false, err
}
if err := json.Unmarshal(data, &bwData); err != nil {
log.Printf("Couldn't load the items cache, error: %s", err)
return false, err
}
log.Println("Got existing Bitwarden CLI data")
return true, nil
homedir, err := os.UserHomeDir()
if err != nil {
return false, err
}
bwDataPath := fmt.Sprintf("%s/Library/Application Support/Bitwarden CLI/data.json", homedir)
log.Println("BW DataPath", bwDataPath)
if _, err := os.Stat(bwDataPath); err != nil {
log.Println("Couldn't find the Bitwarden data.json ", err)
return false, err
}
data, err := ioutil.ReadFile(bwDataPath)
if err != nil {
return false, err
}
if err := json.Unmarshal(data, &bwData); err != nil {
log.Printf("Couldn't load the items cache, error: %s", err)
return false, err
}
log.Println("Got existing Bitwarden CLI data")
return true, nil
}

type BwData struct {
InstalledVersion string `json:"installedVersion"`
UserEmail string `json:"userEmail"`
Unused map[string]interface{} `json:"-"`
InstalledVersion string `json:"installedVersion"`
UserEmail string `json:"userEmail"`
Unused map[string]interface{} `json:"-"`
}
2 changes: 1 addition & 1 deletion alfred/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ func SetToken(wf *aw.Workflow, token string) error {

func RemoveToken(wf *aw.Workflow) error {
return wf.Keychain.Delete(tokenKey)
}
}

0 comments on commit f953ee5

Please sign in to comment.