-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Persist selection and use as preselection per directory path
- Loading branch information
trusz
committed
Feb 24, 2019
1 parent
d107126
commit 75f3a4d
Showing
4 changed files
with
133 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package persist | ||
|
||
import ( | ||
"log" | ||
"os" | ||
|
||
"github.com/trusz/rapid-compose/src/storage" | ||
) | ||
|
||
// Selection _ | ||
type Selection = storage.Selection | ||
|
||
// SaveSelections _ | ||
func SaveSelections(services []string) { | ||
|
||
var selections = storage.Read() | ||
var si = findIndexByPath(selections, currentDir()) | ||
var newSelection = Selection{ | ||
Path: currentDir(), | ||
Services: services, | ||
} | ||
|
||
if si >= 0 { | ||
selections[si] = newSelection | ||
} else { | ||
selections = append(selections, newSelection) | ||
} | ||
|
||
storage.Write(selections) | ||
|
||
} | ||
|
||
// LoadSelections _ | ||
func LoadSelections() []string { | ||
var selections = storage.Read() | ||
var si = findIndexByPath(selections, currentDir()) | ||
if si >= 0 { | ||
return selections[si].Services | ||
} | ||
return []string{} | ||
} | ||
|
||
func currentDir() string { | ||
dir, err := os.Getwd() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
return dir | ||
} | ||
|
||
func findIndexByPath( | ||
selections []Selection, | ||
path string, | ||
) int { | ||
for si, selection := range selections { | ||
if selection.Path == path { | ||
return si | ||
} | ||
} | ||
|
||
return -1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package storage | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"log" | ||
"os/user" | ||
) | ||
|
||
const fileName = ".rapid-compose" | ||
|
||
var filePath = homeFolder() + "/" + fileName | ||
|
||
// Write _ | ||
func Write(selections []Selection) { | ||
content, _ := json.Marshal(selections) | ||
writeFile(filePath, content) | ||
} | ||
|
||
// Read _ | ||
func Read() []Selection { | ||
var content = readFile(filePath) | ||
var selections = make([]Selection, 0) | ||
err := json.Unmarshal(content, &selections) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return selections | ||
} | ||
|
||
// Selection _ | ||
type Selection struct { | ||
Path string `json:"path"` | ||
Services []string `json:"services"` | ||
} | ||
|
||
func writeFile(path string, content []byte) { | ||
|
||
err := ioutil.WriteFile(path, []byte(content), 0644) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func readFile(path string) []byte { | ||
content, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return content | ||
} | ||
|
||
func homeFolder() string { | ||
usr, err := user.Current() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
return usr.HomeDir | ||
} |