diff --git a/Readme.md b/Readme.md index 8b0d8cd..a5aa36d 100644 --- a/Readme.md +++ b/Readme.md @@ -4,6 +4,10 @@ Inspired by the book [Deep Work](http://calnewport.com/books/deep-work/) from Ca Currently only working with Mac OS X, more variety to come soon. +## Set-Up + +create a config file under ~/.deepwork/config.json and add the names of all communication applications. An example config can be found in [example-config.json](example-config.json) + ## Usage Close all communication apps to enter a focused working state. diff --git a/example-config.json b/example-config.json new file mode 100644 index 0000000..152e2a2 --- /dev/null +++ b/example-config.json @@ -0,0 +1,7 @@ +{ + "affectedApps": [ + "Mail", + "Rocket.Chat+", + "Calendar" + ] +} \ No newline at end of file diff --git a/main.go b/main.go index d306b40..f80a5a8 100644 --- a/main.go +++ b/main.go @@ -1,21 +1,44 @@ package main import ( + "encoding/json" "flag" "fmt" + "io/ioutil" "log" "os" + "strings" + + homedir "github.com/mitchellh/go-homedir" ) +var configLocation string + +type config struct { + AffectedApps []string `json:"affectedApps"` +} + func main() { - affectedApps := []string{"Mail", "Rocket.Chat+", "Calendar"} + // Get Users homedirectory + configLocation, err := homedir.Dir() + if err != nil { + log.Fatalf("Could not determine users home directory: %v", err) + } + configLocation += "/.deepwork/config.json" - var action func(name string) error + // Parse Configuration + config, err := parseConfig(configLocation) - flag.Parse() + if err != nil { + log.Fatalf("Could not parse config file: %v", err) + } + // Parse Command Line Flags + flag.Parse() desStage := flag.Arg(0) + // Determine desired action + var action func(name string) error switch desStage { case "on": action = CloseApp @@ -26,10 +49,49 @@ func main() { os.Exit(1) } - for _, app := range affectedApps { + // Execute action + for _, app := range config.AffectedApps { err := action(app) if err != nil { log.Printf("Could not close app %s: %v", app, err) } } } + +func parseConfig(configLocation string) (config, error) { + var conf config + jsonFile, err := os.Open(configLocation) + defer jsonFile.Close() + if err != nil { + // Check if there is already a config file => harmless case, just create default conf + if os.IsNotExist(err) { + defaultConfig := []byte(`{"affectedApps":["Mail","Calendar"]}`) + + // Create required directories if necessary + if err = os.Mkdir(strings.TrimRight(configLocation, "/config.json"), 0744); err != nil { + return config{}, fmt.Errorf("Could not create required directories for config: %v", err) + } + // Write File + if err = ioutil.WriteFile(configLocation, defaultConfig, 0644); err != nil { + return config{}, fmt.Errorf("Could not write default config: %v", err) + } + // Call itself again to parse newly created conf + return parseConfig(configLocation) + } + // Otherwise (e.g. no permissions on conf file), return the error + return config{}, err + } + + confByte, err := ioutil.ReadAll(jsonFile) + + if err != nil { + return config{}, fmt.Errorf("Could not read config: %v", err) + } + + err = json.Unmarshal(confByte, &conf) + if err != nil { + return config{}, fmt.Errorf("Could not parse config: %v", err) + } + + return conf, nil +}