2024-02-01 01:15:01 +00:00
|
|
|
package commands
|
2023-11-05 01:28:11 +00:00
|
|
|
|
|
|
|
import (
|
2024-01-28 21:54:23 +00:00
|
|
|
"maps"
|
2023-11-05 01:28:11 +00:00
|
|
|
"os"
|
2024-01-23 17:55:06 +00:00
|
|
|
cli "v/cli"
|
|
|
|
logger "v/logger"
|
2024-01-28 21:54:23 +00:00
|
|
|
python "v/python"
|
2024-01-23 17:45:08 +00:00
|
|
|
state "v/state"
|
2023-11-05 01:28:11 +00:00
|
|
|
)
|
|
|
|
|
2024-01-28 21:54:23 +00:00
|
|
|
var stateDirectories = []string{
|
2023-11-05 01:28:11 +00:00
|
|
|
"cache",
|
|
|
|
"runtimes",
|
|
|
|
"shims",
|
|
|
|
}
|
|
|
|
|
2024-01-28 21:54:23 +00:00
|
|
|
const defaultFilePermissions = 0775
|
2023-11-05 01:28:11 +00:00
|
|
|
|
2024-01-28 21:54:23 +00:00
|
|
|
func writeShim(shimPath string, shimCall string) error {
|
|
|
|
shimContent := []byte("#!/bin/bash\n" + shimCall)
|
|
|
|
if err := os.WriteFile(shimPath, shimContent, defaultFilePermissions); err != nil {
|
2023-11-05 01:28:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets up directories and files used to store downloaded archives,
|
|
|
|
// installed runtimes and metadata.
|
2024-01-23 17:55:06 +00:00
|
|
|
func Initialize(args []string, flags cli.Flags, currentState state.State) error {
|
2023-11-05 15:41:43 +00:00
|
|
|
if flags.AddPath {
|
2024-01-23 17:55:06 +00:00
|
|
|
logger.InfoLogger.Printf("export PATH=%s:$PATH\n", state.GetStatePath("shims"))
|
2023-11-05 15:41:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-28 21:54:23 +00:00
|
|
|
os.Mkdir(state.GetStatePath(), defaultFilePermissions)
|
|
|
|
logger.InfoLogger.Printf("Created state directory: %s\n", state.GetStatePath())
|
|
|
|
for _, dir := range stateDirectories {
|
|
|
|
newPath := state.GetStatePath(dir)
|
|
|
|
os.Mkdir(newPath, defaultFilePermissions)
|
|
|
|
logger.InfoLogger.Printf("Created %s\n", newPath)
|
2023-11-05 01:28:11 +00:00
|
|
|
}
|
|
|
|
|
2024-01-28 21:54:23 +00:00
|
|
|
allShims := map[string]string{}
|
|
|
|
|
|
|
|
maps.Copy(allShims, python.Shims)
|
|
|
|
|
|
|
|
for shimName, shimContent := range allShims {
|
|
|
|
newShim := state.GetStatePath("shims", shimName)
|
|
|
|
writeShim(newShim, shimContent)
|
|
|
|
logger.InfoLogger.Printf("Created shim: %s\n", newShim)
|
2023-11-05 01:28:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|