Skip to content

Commit

Permalink
Add configuration file as json, so user can pick which applications a…
Browse files Browse the repository at this point in the history
…re affected by deepwork (#1)
  • Loading branch information
SimonTheLeg authored Jul 8, 2018
1 parent a4f53ee commit f355453
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 4 deletions.
4 changes: 4 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions example-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"affectedApps": [
"Mail",
"Rocket.Chat+",
"Calendar"
]
}
70 changes: 66 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}

0 comments on commit f355453

Please sign in to comment.