diff --git a/src/persist/persist.go b/src/persist/persist.go new file mode 100644 index 0000000..66f5207 --- /dev/null +++ b/src/persist/persist.go @@ -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 +} diff --git a/src/prompt/prompt.go b/src/prompt/prompt.go index f0b408b..b8e2f17 100644 --- a/src/prompt/prompt.go +++ b/src/prompt/prompt.go @@ -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 { @@ -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) diff --git a/src/rc/main.go b/src/rc/main.go index e63606e..1e9ffd8 100644 --- a/src/rc/main.go +++ b/src/rc/main.go @@ -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" @@ -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) diff --git a/src/storage/storage.go b/src/storage/storage.go new file mode 100644 index 0000000..d974ac6 --- /dev/null +++ b/src/storage/storage.go @@ -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 +}