diff --git a/cmd/dawg/dawg.go b/cmd/dawg/dawg.go index 8202098..d496477 100644 --- a/cmd/dawg/dawg.go +++ b/cmd/dawg/dawg.go @@ -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)) @@ -43,7 +45,7 @@ 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 { @@ -51,7 +53,7 @@ func makeAlfredWorkflow(c dawg.Config) error { } 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) } diff --git a/dawg.go b/dawg.go index 061ec81..133ecce 100644 --- a/dawg.go +++ b/dawg.go @@ -5,8 +5,6 @@ import ( "encoding/json" "fmt" "os" - "os/user" - "strings" "github.com/jtacoma/uritemplates" ) @@ -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) @@ -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 { diff --git a/make_workflow.go b/make_workflow.go index f23c7d4..c700bd6 100644 --- a/make_workflow.go +++ b/make_workflow.go @@ -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 { @@ -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 { @@ -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),