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

add remove capability #13

Merged
merged 2 commits into from
Oct 27, 2022
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
2 changes: 1 addition & 1 deletion cmd/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func newLs(lsOpts lsOpts) error {
if len(entries) < lsOpts.num {
lsOpts.num = len(entries)
}
entries = entries[:lsOpts.num-1]
entries = entries[:lsOpts.num]
// TODO: replace with actual table
fmt.Println("version | install time")
fmt.Println("--------------------------------")
Expand Down
85 changes: 85 additions & 0 deletions cmd/rm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package cmd

import (
"fmt"
"os"
"path/filepath"

"github.com/dpastoor/qvm/internal/config"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

type rmCmd struct {
cmd *cobra.Command
opts rmOpts
}

type rmOpts struct {
}

func newRm(rmOpts rmOpts, versions []string) error {
for _, v := range versions {
if v == "latest" || v == "release" {
log.Fatalf("qvm rm does not support latest/release terms for now, please use explicit versions only, such as v1.1.251\n")
}
}
activeVersion, err := config.GetActiveVersion()
if err != nil {
return err
}
allVersions, err := config.GetInstalledVersions()
if err != nil {
return err
}
for _, v := range versions {
if _, ok := allVersions[v]; !ok {
log.Warnf("version %s is not installed, so not removing\n", v)
continue
}
log.Tracef("about to remove %s\n", v)
if v == activeVersion {
log.Warnf("removing active version %s, be sure to set a new active version\n", v)
}
err := os.RemoveAll(filepath.Join(config.GetPathToVersionsDir(), v))
if err != nil {
return err
}
log.Infof("removed version %s\n", v)
}
return nil
}

func setRmOpts(rmOpts *rmOpts) {

}

func (opts *rmOpts) Validate() error {
return nil
}

func newRmCmd() *rmCmd {
root := &rmCmd{opts: rmOpts{}}

cmd := &cobra.Command{
Use: "rm",
Short: "remove a version of quarto",
PreRunE: func(cmd *cobra.Command, args []string) error {
setRmOpts(&root.opts)
if err := root.opts.Validate(); err != nil {
return err
}
return nil
},
RunE: func(_ *cobra.Command, args []string) error {
//TODO: Add your logic to gather config to pass code here
log.WithField("opts", fmt.Sprintf("%+v", root.opts)).Trace("rm-opts")
if err := newRm(root.opts, args); err != nil {
return err
}
return nil
},
}
root.cmd = cmd
return root
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func newRootCmd(version string) *rootCmd {
cmd.AddCommand(newInstallCmd().cmd)
cmd.AddCommand(newUseCmd().cmd)
cmd.AddCommand(newInitCmd().cmd)
cmd.AddCommand(newRmCmd().cmd)
root.cmd = cmd
return root
}