From 6d72d1e51dc5aae429cf22881d1342b08c6dfb9c Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Fri, 26 Jan 2024 00:40:16 -0500 Subject: [PATCH] refactor(python-cli): push python namespace to python module fix: use namespace in integration test docs: update references to commands with namespace --- README.md | 2 +- integration/install_specific_version | 4 ++-- python/cli.go | 24 ++++++++++++++++++++++++ python/commands.go | 12 ++++++------ python/commands_test.go | 14 +++++++------- v.go | 17 +---------------- 6 files changed, 41 insertions(+), 32 deletions(-) create mode 100644 python/cli.go diff --git a/README.md b/README.md index f8372c3..a59167a 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ This will handle adding shim paths to your shell without hassle. `v` will print a helpful list of available commands. -The most important things to know include `v install ` to install new versions and `v use ` to use a specific version of Python. +The most important things to know include `v python install ` to install new versions and `v python use ` to use a specific version of Python. ## Contributing diff --git a/integration/install_specific_version b/integration/install_specific_version index 2fa29cb..ed63ee6 100644 --- a/integration/install_specific_version +++ b/integration/install_specific_version @@ -9,9 +9,9 @@ go build . TARGET_VERSION="3.10.0" V_ROOT=/tmp/v ./v init -V_ROOT=/tmp/v ./v install $TARGET_VERSION --no-cache +V_ROOT=/tmp/v ./v python install $TARGET_VERSION --no-cache -INSTALLED_VERSIONS=$(V_ROOT=/tmp/v ./v ls) +INSTALLED_VERSIONS=$(V_ROOT=/tmp/v ./v python ls) if [ -z "$(echo $INSTALLED_VERSIONS | grep $TARGET_VERSION)" ]; then echo "FAIL: Could not find target version." diff --git a/python/cli.go b/python/cli.go new file mode 100644 index 0000000..a4e67ab --- /dev/null +++ b/python/cli.go @@ -0,0 +1,24 @@ +package python + +import ( + cli "v/cli" +) + +func GetNamespace() cli.Namespace { + pythonCommands := cli.Namespace{Label: "python"} + pythonCommands.AddCommand( + "install", installPython, "v python install ", "Downloads, builds and installs a new version of Python.", + ).AddCommand( + "uninstall", uninstallPython, "v python uninstall ", "Uninstalls the given Python version.", + ).AddCommand( + "use", use, "v python use ", "Selects which Python version to use.", + ).AddCommand( + "ls", listVersions, "v python ls", "Lists the installed Python versions.", + ).AddCommand( + "version", currentVersion, "v python version", "Prints the current version and its source.", + ).AddCommand( + "which", which, "v python which", "Prints the path to the current Python version.", + ) + + return pythonCommands +} diff --git a/python/commands.go b/python/commands.go index 4435a4c..58f7836 100644 --- a/python/commands.go +++ b/python/commands.go @@ -8,19 +8,19 @@ import ( state "v/state" ) -func UninstallPython(args []string, flags cli.Flags, currentState state.State) error { +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 { +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 { +func use(args []string, flags cli.Flags, currentState state.State) error { version := args[1] if err := ValidateVersion(version); err != nil { return err @@ -47,7 +47,7 @@ func Use(args []string, flags cli.Flags, currentState state.State) error { return nil } -func ListVersions(args []string, flags cli.Flags, currentState state.State) error { +func listVersions(args []string, flags cli.Flags, currentState state.State) error { installedVersions, err := ListInstalledVersions() if err != nil { @@ -67,7 +67,7 @@ func ListVersions(args []string, flags cli.Flags, currentState state.State) erro } // Which prints out the system path to the executable being used by `python`. -func Which(args []string, flags cli.Flags, currentState state.State) error { +func which(args []string, flags cli.Flags, currentState state.State) error { selectedVersion, _ := DetermineSelectedPythonVersion(currentState) installedVersions, _ := ListInstalledVersions() isInstalled := slices.Contains(installedVersions, selectedVersion.Version) @@ -100,7 +100,7 @@ func Which(args []string, flags cli.Flags, currentState state.State) error { // 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 { +func currentVersion(args []string, flags cli.Flags, currentState state.State) error { selectedVersion, _ := DetermineSelectedPythonVersion(currentState) installedVersions, _ := ListInstalledVersions() isInstalled := slices.Contains(installedVersions, selectedVersion.Version) diff --git a/python/commands_test.go b/python/commands_test.go index d5e903a..77ed829 100644 --- a/python/commands_test.go +++ b/python/commands_test.go @@ -20,7 +20,7 @@ func TestListVersionOutputsNoticeIfNoVersionsInstalled(t *testing.T) { logger.InfoLogger.SetOutput(&out) defer logger.InfoLogger.SetOutput(os.Stdout) - ListVersions([]string{}, cli.Flags{}, state.State{}) + listVersions([]string{}, cli.Flags{}, state.State{}) captured := out.String() if captured != "No versions installed!\n" { @@ -37,7 +37,7 @@ func TestListVersionOutputsVersionsInstalled(t *testing.T) { logger.InfoLogger.SetOutput(&out) defer logger.InfoLogger.SetOutput(os.Stdout) - ListVersions([]string{}, cli.Flags{}, state.State{}) + listVersions([]string{}, cli.Flags{}, state.State{}) captured := out.String() if captured != "1.2.3\n" { @@ -53,7 +53,7 @@ func TestListVersionReturnsErrorOnFailure(t *testing.T) { logger.InfoLogger.SetOutput(&out) defer logger.InfoLogger.SetOutput(os.Stdout) - err := ListVersions([]string{}, cli.Flags{}, state.State{}) + err := listVersions([]string{}, cli.Flags{}, state.State{}) captured := out.String() if captured != "" { @@ -73,7 +73,7 @@ func TestListVersionOutputsVersionSelectedAndWarnsNotInstalled(t *testing.T) { logger.InfoLogger.SetOutput(&out) defer logger.InfoLogger.SetOutput(os.Stdout) - Which([]string{}, cli.Flags{}, state.State{GlobalVersion: "1.2.3"}) + which([]string{}, cli.Flags{}, state.State{GlobalVersion: "1.2.3"}) captured := out.String() if captured != "The desired version (1.2.3) is not installed.\n" { @@ -90,7 +90,7 @@ func TestWhichOutputsVersionSelectedIfInstalled(t *testing.T) { defer logger.InfoLogger.SetOutput(os.Stdout) os.MkdirAll(state.GetStatePath("runtimes", "py-1.2.3"), 0750) - Which([]string{}, cli.Flags{}, state.State{GlobalVersion: "1.2.3"}) + which([]string{}, cli.Flags{}, state.State{GlobalVersion: "1.2.3"}) captured := strings.TrimSpace(out.String()) expected := state.GetStatePath("runtimes", "py-1.2.3", "bin", "python1.2") @@ -107,7 +107,7 @@ func TestWhichOutputsSystemVersionIfNoneSelected(t *testing.T) { logger.InfoLogger.SetOutput(&out) defer logger.InfoLogger.SetOutput(os.Stdout) - Which([]string{}, cli.Flags{RawOutput: true}, state.State{}) + which([]string{}, cli.Flags{RawOutput: true}, state.State{}) captured := strings.TrimSpace(out.String()) @@ -125,7 +125,7 @@ func TestWhichOutputsVersionWithoutPrefixesIfRawOutput(t *testing.T) { defer logger.InfoLogger.SetOutput(os.Stdout) os.MkdirAll(state.GetStatePath("runtimes", "py-1.2.3"), 0750) - Which([]string{}, cli.Flags{RawOutput: true}, state.State{GlobalVersion: "1.2.3"}) + which([]string{}, cli.Flags{RawOutput: true}, state.State{GlobalVersion: "1.2.3"}) captured := strings.TrimSpace(out.String()) expected := state.GetStatePath("runtimes", "py-1.2.3", "bin", "python1.2") diff --git a/v.go b/v.go index 73b87fd..38c7904 100644 --- a/v.go +++ b/v.go @@ -23,28 +23,13 @@ func main() { "init", Initialize, "v init", "Initializes the v state.", ) - pythonCommands := cli.Namespace{Label: "python"} - pythonCommands.AddCommand( - "install", python.InstallPython, "v python install ", "Downloads, builds and installs a new version of Python.", - ).AddCommand( - "uninstall", python.UninstallPython, "v python uninstall ", "Uninstalls the given Python version.", - ).AddCommand( - "use", python.Use, "v python use ", "Selects which Python version to use.", - ).AddCommand( - "ls", python.ListVersions, "v python ls", "Lists the installed Python versions.", - ).AddCommand( - "version", python.CurrentVersion, "v python version", "Prints the current version and its source.", - ).AddCommand( - "which", python.Which, "v python which", "Prints the path to the current Python version.", - ) - cli := cli.CLI{ Metadata: map[string]string{ "Version": Version, }, } - cli.AddNamespace(root).AddNamespace(pythonCommands) + cli.AddNamespace(root).AddNamespace(python.GetNamespace()) err := cli.Run(args, currentState)