refactor(python): move python commands under python module

This commit is contained in:
Marc 2024-01-25 23:20:26 -05:00
parent c0d8d31ace
commit 16509e82fe
Signed by: marc
GPG key ID: 048E042F22B5DC79
5 changed files with 128 additions and 118 deletions

View file

@ -2,10 +2,8 @@ package main
import (
"os"
"slices"
cli "v/cli"
logger "v/logger"
python "v/python"
state "v/state"
)
@ -50,111 +48,3 @@ func Initialize(args []string, flags cli.Flags, currentState state.State) error
return nil
}
func UninstallPython(args []string, flags cli.Flags, currentState state.State) error {
runtimePath := state.GetStatePath("runtimes", "py-"+args[1])
err := os.RemoveAll(runtimePath)
return err
}
func InstallPython(args []string, flags cli.Flags, currentState state.State) error {
version := args[1]
return python.InstallPythonDistribution(version, flags.NoCache, flags.Verbose)
}
func Use(args []string, flags cli.Flags, currentState state.State) error {
version := args[1]
if err := python.ValidateVersion(version); err != nil {
return err
}
availableVersions := state.GetAvailableVersions()
found := false
for _, v := range availableVersions {
if v == version {
found = true
break
}
}
if !found {
logger.InfoLogger.Println("Version not installed. Installing it first.")
python.InstallPythonDistribution(version, flags.NoCache, flags.Verbose)
}
state.WriteState(version)
logger.InfoLogger.Printf("Now using Python %s\n", version)
return nil
}
func ListVersions(args []string, flags cli.Flags, currentState state.State) error {
installedVersions, err := python.ListInstalledVersions()
if err != nil {
return err
}
if len(installedVersions) == 0 {
logger.InfoLogger.Println("No versions installed!")
return nil
}
for _, d := range installedVersions {
logger.InfoLogger.Println(d)
}
return nil
}
// Which prints out the system path to the executable being used by `python`.
func Which(args []string, flags cli.Flags, currentState state.State) error {
selectedVersion, _ := python.DetermineSelectedPythonVersion(currentState)
installedVersions, _ := python.ListInstalledVersions()
isInstalled := slices.Contains(installedVersions, selectedVersion.Version)
var printedPath string
if selectedVersion.Source == "system" {
_, sysPath := python.DetermineSystemPython()
printedPath = sysPath + " (system)"
} else if isInstalled {
tag := python.VersionStringToStruct(selectedVersion.Version)
printedPath = state.GetStatePath("runtimes", "py-"+selectedVersion.Version, "bin", "python"+tag.MajorMinor())
} else {
logger.InfoLogger.Printf("The desired version (%s) is not installed.\n", selectedVersion.Version)
return nil
}
prefix := "Python path: "
if flags.RawOutput {
prefix = ""
} else {
printedPath = logger.Bold(printedPath)
}
logger.InfoLogger.Printf("%s%s\n", prefix, printedPath)
return nil
}
// CurrentVersion (called via `v version`) outputs the currently selected version
// and what configures it. If the version is configured by a file, the file is returned
// under "source", if the system Python is used, "system" is returned as a source.
func CurrentVersion(args []string, flags cli.Flags, currentState state.State) error {
selectedVersion, _ := python.DetermineSelectedPythonVersion(currentState)
installedVersions, _ := python.ListInstalledVersions()
isInstalled := slices.Contains(installedVersions, selectedVersion.Version)
if !isInstalled {
logger.InfoLogger.Println(logger.Bold(logger.Yellow("WARNING: This version is not installed.")))
}
if flags.RawOutput {
logger.InfoLogger.Println(selectedVersion.Version)
return nil
}
logger.InfoLogger.Printf("Python version: %s\nSource: %s\n", logger.Bold(selectedVersion.Version), logger.Bold(selectedVersion.Source))
return nil
}

119
python/commands.go Normal file
View file

@ -0,0 +1,119 @@
package python
import (
"os"
"slices"
cli "v/cli"
logger "v/logger"
state "v/state"
)
func UninstallPython(args []string, flags cli.Flags, currentState state.State) error {
runtimePath := state.GetStatePath("runtimes", "py-"+args[1])
err := os.RemoveAll(runtimePath)
return err
}
func InstallPython(args []string, flags cli.Flags, currentState state.State) error {
version := args[1]
return InstallPythonDistribution(version, flags.NoCache, flags.Verbose)
}
func Use(args []string, flags cli.Flags, currentState state.State) error {
version := args[1]
if err := ValidateVersion(version); err != nil {
return err
}
availableVersions := state.GetAvailableVersions()
found := false
for _, v := range availableVersions {
if v == version {
found = true
break
}
}
if !found {
logger.InfoLogger.Println("Version not installed. Installing it first.")
InstallPythonDistribution(version, flags.NoCache, flags.Verbose)
}
state.WriteState(version)
logger.InfoLogger.Printf("Now using Python %s\n", version)
return nil
}
func ListVersions(args []string, flags cli.Flags, currentState state.State) error {
installedVersions, err := ListInstalledVersions()
if err != nil {
return err
}
if len(installedVersions) == 0 {
logger.InfoLogger.Println("No versions installed!")
return nil
}
for _, d := range installedVersions {
logger.InfoLogger.Println(d)
}
return nil
}
// Which prints out the system path to the executable being used by `python`.
func Which(args []string, flags cli.Flags, currentState state.State) error {
selectedVersion, _ := DetermineSelectedPythonVersion(currentState)
installedVersions, _ := ListInstalledVersions()
isInstalled := slices.Contains(installedVersions, selectedVersion.Version)
var printedPath string
if selectedVersion.Source == "system" {
_, sysPath := DetermineSystemPython()
printedPath = sysPath + " (system)"
} else if isInstalled {
tag := VersionStringToStruct(selectedVersion.Version)
printedPath = state.GetStatePath("runtimes", "py-"+selectedVersion.Version, "bin", "python"+tag.MajorMinor())
} else {
logger.InfoLogger.Printf("The desired version (%s) is not installed.\n", selectedVersion.Version)
return nil
}
prefix := "Python path: "
if flags.RawOutput {
prefix = ""
} else {
printedPath = logger.Bold(printedPath)
}
logger.InfoLogger.Printf("%s%s\n", prefix, printedPath)
return nil
}
// CurrentVersion (called via `v version`) outputs the currently selected version
// and what configures it. If the version is configured by a file, the file is returned
// under "source", if the system Python is used, "system" is returned as a source.
func CurrentVersion(args []string, flags cli.Flags, currentState state.State) error {
selectedVersion, _ := DetermineSelectedPythonVersion(currentState)
installedVersions, _ := ListInstalledVersions()
isInstalled := slices.Contains(installedVersions, selectedVersion.Version)
if !isInstalled {
logger.InfoLogger.Println(logger.Bold(logger.Yellow("WARNING: This version is not installed.")))
}
if flags.RawOutput {
logger.InfoLogger.Println(selectedVersion.Version)
return nil
}
logger.InfoLogger.Printf("Python version: %s\nSource: %s\n", logger.Bold(selectedVersion.Version), logger.Bold(selectedVersion.Source))
return nil
}

View file

@ -1,4 +1,4 @@
package main
package python
import (
"bytes"

View file

@ -1,4 +1,4 @@
#!/bin/bash
go get golang.org/x/tools/cmd/cover
go test -cover -v -coverprofile=cov.out
go test ./... -cover -v -coverprofile=cov.out

13
v.go
View file

@ -3,6 +3,7 @@ package main
import (
"os"
cli "v/cli"
python "v/python"
state "v/state"
)
@ -24,17 +25,17 @@ func main() {
}
err := cli.AddCommand(
"install", InstallPython, "v install <version>", "Downloads, builds and installs a new version of Python.",
"install", python.InstallPython, "v install <version>", "Downloads, builds and installs a new version of Python.",
).AddCommand(
"uninstall", UninstallPython, "v uninstall <version>", "Uninstalls the given Python version.",
"uninstall", python.UninstallPython, "v uninstall <version>", "Uninstalls the given Python version.",
).AddCommand(
"use", Use, "v use <version>", "Selects which Python version to use.",
"use", python.Use, "v use <version>", "Selects which Python version to use.",
).AddCommand(
"ls", ListVersions, "v ls", "Lists the installed Python versions.",
"ls", python.ListVersions, "v ls", "Lists the installed Python versions.",
).AddCommand(
"version", CurrentVersion, "v version", "Prints the current version and its source.",
"version", python.CurrentVersion, "v version", "Prints the current version and its source.",
).AddCommand(
"which", Which, "v which", "Prints the path to the current Python version.",
"which", python.Which, "v which", "Prints the path to the current Python version.",
).AddCommand(
"init", Initialize, "v init", "Initializes the v state.",
).Run(args, currentState)