Skip to content

Commit

Permalink
feat: add some path helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
dpastoor committed Jun 7, 2022
1 parent aa56d7c commit 625b281
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 0 deletions.
69 changes: 69 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package cmd

import (
"fmt"
"os"
"strings"

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

type initCmd struct {
cmd *cobra.Command
opts initOpts
}

type initOpts struct {
}

func newInit(initOpts initOpts) error {
activeDir := config.GetPathToActiveBinDir()
activeIndex := strings.Index(os.Getenv("PATH"), activeDir)
if activeIndex == -1 {
fmt.Println("please add the active bin directory to your path")
fmt.Println(`you can dynamically query the active bin directory by running: "qvm path active"
if you would like to script this, you can add the following to your ~/.bashrc or ~/.zshrc:
export PATH="$(qvm path active):$PATH")
`)

} else {
fmt.Println("already detect qvm active directory on your path - you're good to go!")
}
return nil
}

func setInitOpts(initOpts *initOpts) {

}

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

func newInitCmd() *initCmd {
root := &initCmd{opts: initOpts{}}

cmd := &cobra.Command{
Use: "init",
Short: "initialize qvm",
PreRunE: func(cmd *cobra.Command, args []string) error {
setInitOpts(&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("init-opts")
if err := newInit(root.opts); err != nil {
return err
}
return nil
},
}
root.cmd = cmd
return root
}
64 changes: 64 additions & 0 deletions cmd/path.add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package cmd

import (
"fmt"
"os"
"strings"

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

type pathAddCmd struct {
cmd *cobra.Command
opts pathAddOpts
}

type pathAddOpts struct {
}

func newPathAdd(pathAddOpts pathAddOpts) error {
path := os.Getenv("PATH")
pathToAdd := config.GetPathToActiveBinDir()
activeIndex := strings.Index(path, pathToAdd)
if activeIndex == -1 {
path = fmt.Sprintf("%s:%s", pathToAdd, path)
}
fmt.Println(path)
return nil
}

func setPathAddOpts(pathAddOpts *pathAddOpts) {

}

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

func newPathAddCmd() *pathAddCmd {
root := &pathAddCmd{opts: pathAddOpts{}}

cmd := &cobra.Command{
Use: "add",
Short: "add qvm directories to the path, if they don't exist",
PreRunE: func(cmd *cobra.Command, args []string) error {
setPathAddOpts(&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("pathAdd-opts")
if err := newPathAdd(root.opts); err != nil {
return err
}
return nil
},
}
root.cmd = cmd
return root
}
1 change: 1 addition & 0 deletions cmd/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func newPathCmd() *pathCmd {
cmd.AddCommand(newPathRootCmd().cmd)
cmd.AddCommand(newPathActiveCmd().cmd)
cmd.AddCommand(newPathVersionsCmd().cmd)
cmd.AddCommand(newPathAddCmd().cmd)
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 @@ -61,6 +61,7 @@ func newRootCmd(version string) *rootCmd {
cmd.AddCommand(newPathCmd().cmd)
cmd.AddCommand(newInstallCmd().cmd)
cmd.AddCommand(newUseCmd().cmd)
cmd.AddCommand(newInitCmd().cmd)
root.cmd = cmd
return root
}

0 comments on commit 625b281

Please sign in to comment.