2024-01-23 17:45:08 +00:00
|
|
|
package state
|
2023-11-03 04:41:05 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Persistent state used by the CLI to track runtime information
|
|
|
|
// between calls.
|
|
|
|
type State struct {
|
|
|
|
GlobalVersion string `json:"globalVersion"`
|
|
|
|
}
|
|
|
|
|
2023-11-05 15:19:53 +00:00
|
|
|
func GetStatePath(pathSegments ...string) string {
|
2023-11-03 04:41:05 +00:00
|
|
|
home, _ := os.UserHomeDir()
|
|
|
|
userDefinedRoot, found := os.LookupEnv("V_ROOT")
|
|
|
|
|
|
|
|
root := path.Join(home, ".v")
|
|
|
|
|
|
|
|
if found {
|
|
|
|
root = userDefinedRoot
|
|
|
|
}
|
2023-11-05 15:19:53 +00:00
|
|
|
allSegments := []string{root}
|
|
|
|
allSegments = append(allSegments, pathSegments...)
|
|
|
|
return path.Join(allSegments...)
|
2023-11-03 04:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ReadState() State {
|
2023-11-05 15:19:53 +00:00
|
|
|
c, _ := ioutil.ReadFile(GetStatePath("state.json"))
|
2023-11-03 04:41:05 +00:00
|
|
|
|
|
|
|
state := State{}
|
|
|
|
|
|
|
|
json.Unmarshal(c, &state)
|
|
|
|
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
|
|
|
func WriteState(version string) {
|
|
|
|
state := State{GlobalVersion: version}
|
|
|
|
|
|
|
|
d, _ := json.Marshal(state)
|
2023-11-05 15:19:53 +00:00
|
|
|
ioutil.WriteFile(GetStatePath("state.json"), d, 0750)
|
2023-11-03 04:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetAvailableVersions() []string {
|
2024-01-29 03:25:24 +00:00
|
|
|
entries, _ := os.ReadDir(GetStatePath("runtimes", "python"))
|
2023-11-03 04:41:05 +00:00
|
|
|
|
|
|
|
versions := []string{}
|
|
|
|
|
|
|
|
for _, d := range entries {
|
2024-01-29 03:25:24 +00:00
|
|
|
versions = append(versions, d.Name())
|
2023-11-03 04:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return versions
|
|
|
|
}
|