Skip to content

Commit

Permalink
Embed config into the workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Vlad Yarotsky committed Feb 4, 2016
1 parent 2bec380 commit 1e90ff9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
12 changes: 7 additions & 5 deletions cmd/dawg/dawg.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ import (
)

func main() {
service := flag.String("s", "", "Service name")
configPath := flag.String("config", "dawg.json", "Path to config")
service := flag.String("service", "", "Service name")
makeWorkflow := flag.Bool("generate", false, "Generate Alfred Workflow")
flag.Parse()

c := dawg.MustReadConfig()
c, err := dawg.ReadConfig(*configPath)
handleError(err)

switch true {
case *makeWorkflow:
handleError(makeAlfredWorkflow(c))
handleError(makeAlfredWorkflow(*configPath, c))
case *service != "":
pattern := flag.Arg(0)
handleError(printAlfredXML(c, *service, pattern))
Expand All @@ -43,15 +45,15 @@ func printAlfredXML(c dawg.Config, service, pattern string) error {
return nil
}

func makeAlfredWorkflow(c dawg.Config) error {
func makeAlfredWorkflow(embeddableConfigPath string, c dawg.Config) error {
plist := dawg.MakeWorkflowPList(c)
icons, err := dawg.DownloadServiceIcons(c)
if err != nil {
return fmt.Errorf("could not download service icons: %v", err)
}
out, _ := plist.PListWithHeader()

zipfile, err := dawg.MakeWorkflowZIP(out, icons)
zipfile, err := dawg.MakeWorkflowZIP(embeddableConfigPath, out, icons)
if err != nil {
return fmt.Errorf("could not make a workflow zip file: %v", err)
}
Expand Down
16 changes: 6 additions & 10 deletions dawg.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"encoding/json"
"fmt"
"os"
"os/user"
"strings"

"github.com/jtacoma/uritemplates"
)
Expand Down Expand Up @@ -37,7 +35,7 @@ func (t *URITemplate) UnmarshalJSON(b []byte) error {
type Config map[string]*ServiceConfig

func (c Config) GetService(service string) (ServiceConfig, error) {
if s, ok := map[string]*ServiceConfig(c)[service]; ok {
if s, ok := c[service]; ok {
return *s, nil
} else {
return ServiceConfig{}, fmt.Errorf("service '%s' not found", service)
Expand Down Expand Up @@ -66,26 +64,24 @@ func (s ServiceConfig) GetURL(shortcut string) (string, error) {
}
}

func MustReadConfig() Config {
usr, _ := user.Current()
dir := usr.HomeDir
file, err := os.Open(strings.Join([]string{dir, ".dawg.json"}, "/"))
func ReadConfig(path string) (Config, error) {
file, err := os.Open(path)
if err != nil {
panic(err)
return Config{}, fmt.Errorf("could not open config file: %s", err)
}
defer file.Close()

var cfg Config
dec := json.NewDecoder(file)
if err := dec.Decode(&cfg); err != nil {
panic(err)
return Config{}, fmt.Errorf("could not parse config file: %s", err)
}

for svc, _ := range cfg {
cfg[svc].GUID = GUID() // randomly assign guids for alfred workflow objects
}

return cfg
return cfg, nil
}

func GUID() string {
Expand Down
16 changes: 11 additions & 5 deletions make_workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,23 @@ import (
"github.com/kardianos/osext"
)

func MakeWorkflowZIP(plist []byte, icons []ServiceIcon) (*bytes.Buffer, error) {
func MakeWorkflowZIP(configPath string, plist []byte, icons []ServiceIcon) (*bytes.Buffer, error) {
fname, err := osext.Executable()
if err != nil {
return nil, err
}
dawgFile, err := os.Open(fname)
defer dawgFile.Close()

configFile, err := os.Open(configPath)
if err != nil {
return nil, err
}
defer configFile.Close()

buf := new(bytes.Buffer)
w := zip.NewWriter(buf)

dawgFile, err := os.Open(fname)
defer dawgFile.Close()

iconReader, _ := gzip.NewReader(bytes.NewReader(iconPNG))

type File struct {
Expand All @@ -33,6 +38,7 @@ func MakeWorkflowZIP(plist []byte, icons []ServiceIcon) (*bytes.Buffer, error) {
{"info.plist", bytes.NewReader(plist)},
{"icon.png", iconReader},
{"dawg", dawgFile},
{"dawg.json", configFile},
}

for _, icon := range icons {
Expand Down Expand Up @@ -99,7 +105,7 @@ func MakeWorkflowPList(c Config) PList {
"queuedelaycustom": PBool(true),
"queuedelaymode": PInteger(0),
"queuemode": PInteger(1),
"script": PString(fmt.Sprintf("chmod +x ./dawg && ./dawg -s %s \"{query}\"", service)),
"script": PString(fmt.Sprintf("chmod +x ./dawg && ./dawg -service %s \"{query}\"", service)),
"title": PString(service),
"type": PInteger(0),
"withspace": PBool(true),
Expand Down

0 comments on commit 1e90ff9

Please sign in to comment.