Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle several uninstall at once #203

Merged
merged 6 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -398,9 +398,18 @@ Removed /home/dvaumoron/.tenv/OpenTofu/version
</details>


<details><summary><b>tenv &lt;tool&gt; uninstall [version]</b></summary><br>
<details><summary><b>tenv &lt;tool&gt; uninstall &lt;version&gt;</b></summary><br>

Uninstall a specific version of the tool (remove it from `TENV_ROOT` directory without interpretation).
Uninstall versions of the tool (remove it from `TENV_ROOT` directory without interpretation).

Available parameter options:

- an exact [Semver 2.0.0](https://semver.org/) version string to remove (no confirmation required)
- a [version constraint](https://opentofu.org/docs/language/expressions/version-constraints) string
- `all`
- `but-last` (all versions except the highest installed)
- `not-used-for:<duration>`, `<duration>` in days or months, like "14d" or "2m"
- `not-used-since:<date>`, `<date>` format is YYYY-MM-DD, like "2024-06-30"

```console
$ tenv tofu uninstall v1.6.0-alpha4
Expand Down
14 changes: 11 additions & 3 deletions cmd/tenv/subcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,13 +300,21 @@ func newResetCmd(conf *config.Config, versionManager versionmanager.VersionManag

func newUninstallCmd(conf *config.Config, versionManager versionmanager.VersionManager) *cobra.Command {
var descBuilder strings.Builder
descBuilder.WriteString("Uninstall a specific version of ")
descBuilder.WriteString("Uninstall versions of ")
descBuilder.WriteString(versionManager.FolderName)
descBuilder.WriteString(" (remove it from TENV_ROOT directory).")
descBuilder.WriteString(` (remove them from TENV_ROOT directory).

Available parameter options:
- an exact Semver 2.0.0 version string to remove (no confirmation required)
- a version constraint expression
- all
- but-last (all versions except the highest installed)
- not-used-for:<duration>, <duration> in days or months, like "14d" or "2m"
- not-used-since:<date>, <date> format is YYYY-MM-DD, like "2024-06-30"`)

uninstallCmd := &cobra.Command{
Use: "uninstall version",
Short: loghelper.Concat("Uninstall a specific version of ", versionManager.FolderName, "."),
Short: loghelper.Concat("Uninstall versions of ", versionManager.FolderName, "."),
Long: descBuilder.String(),
Args: cobra.ExactArgs(1),
Run: func(_ *cobra.Command, args []string) {
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/stretchr/testify v1.8.4 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/mod v0.18.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/mod v0.19.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/tools v0.22.0 // indirect
)
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,21 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.3.1-0.20221117191849-2c476679df9a/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0=
golang.org/x/mod v0.18.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.19.0 h1:fEdghXQSo20giMthA7cd28ZC+jts4amQ3YMXiP5oMQ8=
golang.org/x/mod v0.19.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -110,8 +110,8 @@ golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
Expand Down
60 changes: 60 additions & 0 deletions versionmanager/lastuse/last.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
*
* Copyright 2024 tofuutils authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package lastuse

import (
"errors"
"io/fs"
"os"
"path/filepath"
"time"

"github.com/hashicorp/go-hclog"

"github.com/tofuutils/tenv/v2/pkg/loghelper"
)

const fileName = "last-use.txt"

func Read(dirPath string, displayer loghelper.Displayer) time.Time {
data, err := os.ReadFile(filepath.Join(dirPath, fileName))
if err != nil {
displayer.Log(loghelper.LevelWarnOrDebug(errors.Is(err, fs.ErrNotExist)), "Unable to read date in file", loghelper.Error, err)

return time.Time{}
}

parsed, err := time.Parse(time.DateOnly, string(data)) //nolint
if err != nil {
displayer.Log(hclog.Warn, "Unable to parse date in file", loghelper.Error, err)

return time.Time{}
}

return parsed
}

func WriteNow(dirPath string, displayer loghelper.Displayer) {
lastUsePath := filepath.Join(dirPath, fileName)
nowData := time.Now().AppendFormat(nil, time.DateOnly) //nolint

if err := os.WriteFile(lastUsePath, nowData, 0o644); err != nil {
displayer.Log(hclog.Warn, "Unable to write date in file", loghelper.Error, err)
}
}
68 changes: 56 additions & 12 deletions versionmanager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ func (m VersionManager) Install(requestedVersion string) error {
// try to ensure the directory exists with a MkdirAll call.
// (made lazy method : not always useful and allows flag override for root path).
func (m VersionManager) InstallPath() (string, error) {
dir := filepath.Join(m.conf.RootPath, m.FolderName)
dirPath := filepath.Join(m.conf.RootPath, m.FolderName)

return dir, os.MkdirAll(dir, 0o755)
return dirPath, os.MkdirAll(dirPath, 0o755)
}

func (m VersionManager) ListLocal(reverseOrder bool) ([]string, error) {
Expand Down Expand Up @@ -282,11 +282,6 @@ func (m VersionManager) SetConstraint(constraint string) error {
}

func (m VersionManager) Uninstall(requestedVersion string) error {
parsedVersion, err := version.NewVersion(requestedVersion) // check the use of a parsable version
if err != nil {
return err
}

installPath, err := m.InstallPath()
if err != nil {
return err
Expand All @@ -297,13 +292,46 @@ func (m VersionManager) Uninstall(requestedVersion string) error {
defer disableExit()
defer deleteLock()

cleanedVersion := parsedVersion.String()
targetPath := filepath.Join(installPath, cleanedVersion)
if err = os.RemoveAll(targetPath); err == nil {
m.conf.Displayer.Display(loghelper.Concat("Uninstallation of ", m.FolderName, " ", cleanedVersion, " successful (directory ", targetPath, " removed)"))
parsedVersion, err := version.NewVersion(requestedVersion) // check the use of a parsable version
if err == nil {
m.uninstallSpecificVersion(installPath, parsedVersion.String())

return nil
}

return err
versions, err := m.ListLocal(true)
if err != nil {
return err
}

selected, err := semantic.SelectVersionsToUninstall(requestedVersion, installPath, versions, m.conf.Displayer)
if err != nil {
return err
}

if len(selected) == 0 {
m.conf.Displayer.Display(loghelper.Concat("No matching ", m.FolderName, " versions"))

return nil
}

m.conf.Displayer.Display(loghelper.Concat("Selected ", m.FolderName, " versions for uninstallation :"))
m.conf.Displayer.Display(strings.Join(selected, ", "))
m.conf.Displayer.Display("Uninstall ? [y/N]")

buffer := make([]byte, 1)
os.Stdin.Read(buffer)
read := buffer[0]

if doUninstall := read == 'y' || read == 'Y'; !doUninstall {
return nil
}

for _, version := range selected {
m.uninstallSpecificVersion(installPath, version)
}

return nil
}

func (m VersionManager) Use(requestedVersion string, workingDir bool) error {
Expand Down Expand Up @@ -428,6 +456,22 @@ func (m VersionManager) searchInstallRemote(predicateInfo types.PredicateInfo, n
return "", errNoCompatible
}

func (m VersionManager) uninstallSpecificVersion(installPath string, version string) {
if version == "" {
m.conf.Displayer.Display(errEmptyVersion.Error())

return
}

targetPath := filepath.Join(installPath, version)
err := os.RemoveAll(targetPath)
if err == nil {
m.conf.Displayer.Display(loghelper.Concat("Uninstallation of ", m.FolderName, " ", version, " successful (directory ", targetPath, " removed)"))
} else {
m.conf.Displayer.Display(loghelper.Concat("Uninstallation of ", m.FolderName, " ", version, " failed with error : ", err.Error()))
}
}

func removeFile(filePath string, conf *config.Config) error {
err := os.RemoveAll(filePath)
if err == nil {
Expand Down
2 changes: 1 addition & 1 deletion versionmanager/proxy/agnostic.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,5 @@ func ExecAgnostic(conf *config.Config, builders map[string]builder.BuilderFunc,
os.Exit(1)
}

RunCmd(installPath, detectedVersion, execName, cmdArgs, conf.GithubActions)
RunCmd(installPath, detectedVersion, execName, cmdArgs, conf.GithubActions, conf.Displayer)
}
12 changes: 9 additions & 3 deletions versionmanager/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ import (

"github.com/tofuutils/tenv/v2/config"
cmdproxy "github.com/tofuutils/tenv/v2/pkg/cmdproxy"
"github.com/tofuutils/tenv/v2/pkg/loghelper"
"github.com/tofuutils/tenv/v2/versionmanager/builder"
"github.com/tofuutils/tenv/v2/versionmanager/lastuse"
)

var errDelimiter = errors.New("key and value should not contains delimiter")
Expand All @@ -48,9 +50,13 @@ func Exec(conf *config.Config, builderFunc builder.BuilderFunc, hclParser *hclpa
os.Exit(1)
}

RunCmd(installPath, detectedVersion, execName, cmdArgs, conf.GithubActions)
RunCmd(installPath, detectedVersion, execName, cmdArgs, conf.GithubActions, conf.Displayer)
}

func RunCmd(installPath string, detectedVersion string, execName string, cmdArgs []string, gha bool) {
cmdproxy.Run(filepath.Join(installPath, detectedVersion, execName), cmdArgs, gha)
func RunCmd(installPath string, detectedVersion string, execName string, cmdArgs []string, gha bool, displayer loghelper.Displayer) {
versionPath := filepath.Join(installPath, detectedVersion)

lastuse.WriteNow(versionPath, displayer)

cmdproxy.Run(filepath.Join(versionPath, execName), cmdArgs, gha)
}
Loading
Loading