Skip to content

Commit

Permalink
Merge pull request #7 from dpastoor/feat/use
Browse files Browse the repository at this point in the history
interactive use cmd
  • Loading branch information
dpastoor authored Jul 7, 2022
2 parents ce8c98d + aa6c6c4 commit e5a32b3
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,6 @@ jobs:
- name: goreleaser-release
if: startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_TOKEN: ${{ secrets.HOMEBREW_TAP_GITHUB_TOKEN }}
FURY_TOKEN: ${{ secrets.FURY_TOKEN }}
run: task goreleaser
21 changes: 17 additions & 4 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ release:
sudo chmod +x /usr/local/bin/qvm
```
### Linux User
Assumes `~/bin` is available in your PATH
```
wget https://github.com/dpastoor/qvm/releases/download/{{ .Tag }}/qvm_Linux_x86_64.tar.gz -O /tmp/qvm.tar.gz
tar xzf /tmp/qvm.tar.gz qvm
mv qvm ~/bin/qvm
chmod +x ~/bin/qvm
```
before:
hooks:
- go mod tidy
Expand All @@ -40,10 +51,12 @@ builds:
- linux
goarch:
- amd64
# - arm64
# - arm
# goarm:
# - "7"
- arm64
goarm:
- "7"

universal_binaries:
- replace: true

archives:
- name_template: '{{ .ProjectName }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}'
Expand Down
39 changes: 28 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ which ones are available remotely?

```
qvm ls --remote
qvm ls --remote --since 2022-05-01 # releases since 2022-05-01, follows YYYY-MM-DD
qvm ls --remote -n 10 # latest 10 releases
```

### installing versions
Expand All @@ -111,8 +109,20 @@ running `qvm install`
qvm install
```



```
qvm install
? Which version do you want to install? [Use arrows to move, type to filter]
> v0.9.637
v0.9.636 - **installed**
v0.9.634
v0.9.633
v0.9.632
v0.9.629 - **installed**
v0.9.628
v0.9.626
v0.9.624
v0.9.622
```

```shell
qvm install $QUARTO_VERSION $ANOTHER_QUARTO_VERSION
Expand All @@ -134,15 +144,22 @@ qvm install latest
qvm use $QUARTO_VERSION
```

### using a version in a particular shell session
To interactively select a version, run `qvm use`

At any point can use a particular version in your shell session:

```shell
qvm use $QUARTO_VERSION --local
```


qvm use
? Which version do you want to use? [Use arrows to move, type to filter]
> v0.9.636
v0.9.629 - **active**
v0.9.587
v0.9.583
v0.9.565
v0.9.563
v0.9.562
v0.9.561
v0.9.559
v0.9.550
```
### programmatic support utilities

```shell
Expand Down
10 changes: 4 additions & 6 deletions cmd/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"os"
"path/filepath"
"sort"

"github.com/dpastoor/qvm/internal/config"
Expand Down Expand Up @@ -36,10 +35,6 @@ func newLs(lsOpts lsOpts) error {
fmt.Printf("%s | %s | %s\n", r.GetTagName(), createdAt.Format("2006-01-02"), r.GetName())
}
} else {
activePath, err := filepath.EvalSymlinks(filepath.Join(config.GetPathToActiveBinDir(), "quarto"))
if err != nil && !errors.Is(err, os.ErrNotExist) {
return err
}
entries, err := os.ReadDir(config.GetPathToVersionsDir())
if errors.Is(err, os.ErrNotExist) {
fmt.Println("No installed quarto versions found")
Expand All @@ -61,11 +56,14 @@ func newLs(lsOpts lsOpts) error {
sort.Slice(entries, func(i, j int) bool {
return entries[i].Name() > entries[j].Name()
})
// no need to worry about errors since just need to know version
// for matching below and won't match if doesn't exist
activeVersion, _ := config.GetActiveVersion()
for _, e := range entries {
if e.IsDir() {
dinfo, _ := e.Info()
name := e.Name()
if filepath.Base(filepath.Dir(filepath.Dir(activePath))) == e.Name() {
if activeVersion == e.Name() {
name += " (active)"
} else {
name += " "
Expand Down
30 changes: 21 additions & 9 deletions cmd/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,32 @@ func newUse(useOpts useOpts, version string) error {
if len(iv) == 0 {
return errors.New("no installed versions found, please install a version first")
}
if version == "latest" {
version = versions[0]
}
if version == "" {

err := survey.AskOne(&survey.Select{
Message: "Which version do you want to install?",
// not worried about an error here as an active version of
// empty string just won't match any description below
activeVersion, _ := config.GetActiveVersion()
err = survey.AskOne(&survey.Select{
Message: "Which version do you want to use?",
Options: versions,
}, &version)
Description: func(value string, index int) string {
if value == activeVersion {
return "**active**"
}
return ""
},
}, &version, survey.WithPageSize(10))
if err != nil {
return err
}
}
if version == "latest" {
version = versions[0]
}
quartopath, ok := iv[version]
if !ok {
return fmt.Errorf("version %s not found", version)
}
err = os.MkdirAll(config.GetPathToActiveBinDir(), 0700)
err = os.MkdirAll(config.GetPathToActiveBinDir(), 0755)
if err != nil {
return err
}
Expand Down Expand Up @@ -97,7 +105,11 @@ func newUseCmd() *useCmd {
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("use-opts")
if err := newUse(root.opts, args[0]); err != nil {
var version string
if len(args) > 0 {
version = args[0]
}
if err := newUse(root.opts, version); err != nil {
return err
}
return nil
Expand Down
27 changes: 27 additions & 0 deletions internal/config/fs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -34,10 +35,36 @@ func GetPathToActiveBinDir() string {
return path
}

func GetPathToActiveQuartoExe() string {
quartoExe := "quarto"
if runtime.GOOS == "windows" {
quartoExe = "quarto.cmd"
}
return filepath.Join(GetPathToActiveBinDir(), quartoExe)
}
func GetPathToVersionsDir() string {
return filepath.Join(xdg.DataHome, "qvm", "versions")
}

func GetActiveVersion() (string, error) {
path, err := filepath.EvalSymlinks(GetPathToActiveQuartoExe())
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return "", errors.New("no active quarto version detected")
}
return "", err
}
// the path should resolve to .../versions/<version>/bin/quarto
// could also consider filepath.SplitList
version := filepath.Base(filepath.Dir(filepath.Dir(path)))
if version == "." {
return "", errors.New(`
something went wrong with the active version detection,
please contact the developers`)
}
return version, nil
}

// GetInstalledVersions returns a map of installed versions where they key is the version
// and the value is the path to the quarto executable
func GetInstalledVersions() (map[string]string, error) {
Expand Down

0 comments on commit e5a32b3

Please sign in to comment.