From 16509e82fee3ef1e5add04a6646e033657df35c6 Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Thu, 25 Jan 2024 23:20:26 -0500 Subject: [PATCH] refactor(python): move python commands under python module --- commands.go | 110 ------------------ python/commands.go | 119 ++++++++++++++++++++ commands_test.go => python/commands_test.go | 2 +- scripts/test | 2 +- v.go | 13 ++- 5 files changed, 128 insertions(+), 118 deletions(-) create mode 100644 python/commands.go rename commands_test.go => python/commands_test.go (99%) diff --git a/commands.go b/commands.go index 81c6acf..70d2c7b 100644 --- a/commands.go +++ b/commands.go @@ -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 -} diff --git a/python/commands.go b/python/commands.go new file mode 100644 index 0000000..4435a4c --- /dev/null +++ b/python/commands.go @@ -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 +} diff --git a/commands_test.go b/python/commands_test.go similarity index 99% rename from commands_test.go rename to python/commands_test.go index ad3f469..d5e903a 100644 --- a/commands_test.go +++ b/python/commands_test.go @@ -1,4 +1,4 @@ -package main +package python import ( "bytes" diff --git a/scripts/test b/scripts/test index 64d49f3..09ea7ec 100644 --- a/scripts/test +++ b/scripts/test @@ -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 diff --git a/v.go b/v.go index 413c1c6..7d97013 100644 --- a/v.go +++ b/v.go @@ -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 ", "Downloads, builds and installs a new version of Python.", + "install", python.InstallPython, "v install ", "Downloads, builds and installs a new version of Python.", ).AddCommand( - "uninstall", UninstallPython, "v uninstall ", "Uninstalls the given Python version.", + "uninstall", python.UninstallPython, "v uninstall ", "Uninstalls the given Python version.", ).AddCommand( - "use", Use, "v use ", "Selects which Python version to use.", + "use", python.Use, "v use ", "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)