Skip to content

Commit

Permalink
Persist selection and use as preselection per directory path
Browse files Browse the repository at this point in the history
  • Loading branch information
trusz committed Feb 24, 2019
1 parent d107126 commit 75f3a4d
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 2 deletions.
62 changes: 62 additions & 0 deletions src/persist/persist.go
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
}
7 changes: 6 additions & 1 deletion src/prompt/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import (
)

// Question _
func Question(possibleServices []string, inverse bool) []string {
func Question(
possibleServices []string,
oldSelection []string,
inverse bool,
) []string {

var message = "RAPID COMPOSE(RC) \nSelect services to start:"
if inverse {
Expand All @@ -16,6 +20,7 @@ func Question(possibleServices []string, inverse bool) []string {
prompt := &survey.MultiSelect{
Message: message,
Options: possibleServices,
Default: oldSelection,
}
survey.AskOne(prompt, &choosenServices, nil)

Expand Down
7 changes: 6 additions & 1 deletion src/rc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"os"

"github.com/trusz/rapid-compose/src/persist"

"github.com/trusz/rapid-compose/src/dc"
"github.com/trusz/rapid-compose/src/prompt"
"github.com/trusz/rapid-compose/src/yaml"
Expand All @@ -23,8 +25,11 @@ func main() {
var inverse = flag.Bool("i", false, "Inverse selection. Start everything except selected ones.")
flag.Parse()

var oldSelection = persist.LoadSelections()
possibleServices := yaml.LoadPossibleServices(*showAll)
services := prompt.Question(possibleServices, *inverse)
services := prompt.Question(possibleServices, oldSelection, *inverse)

persist.SaveSelections(services)

if len(services) > 0 {
dc.Start(services)
Expand Down
59 changes: 59 additions & 0 deletions src/storage/storage.go
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
}

0 comments on commit 75f3a4d

Please sign in to comment.