-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
81 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/ekalinin/pbvm/utils" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var version string | ||
|
||
// runCmd represents the run command | ||
var runCmd = &cobra.Command{ | ||
Use: "run <command>", | ||
Args: cobra.ExactArgs(1), | ||
Short: "Run a command under a version", | ||
Long: `Run a command under a version`, | ||
SilenceUsage: true, | ||
SilenceErrors: true, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if len(version) < 5 { | ||
return errors.New("Version is incorrect: " + version) | ||
} | ||
|
||
installed, _, err := utils.IsInstalledVersion(pbName, version) | ||
if err != nil { | ||
return err | ||
} | ||
if !installed { | ||
return errors.New("Version is not installed: " + version) | ||
} | ||
|
||
originVersion, err := utils.GetActiveVersion(pbName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// TODO: run command without affecting other sessions | ||
// (change PATH & syscall.Exec?) | ||
|
||
if err := utils.ActivateVersion(pbName, version); err != nil { | ||
return err | ||
} | ||
defer utils.ActivateVersion(pbName, originVersion) | ||
|
||
cs := strings.Split(args[0], " ") | ||
command := exec.Command(cs[0], cs[1:]...) | ||
out, err := command.CombinedOutput() | ||
if err != nil { | ||
return err | ||
} | ||
println(string(out)) | ||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(runCmd) | ||
|
||
runCmd.Flags().StringVar(&version, "version", "v", | ||
"Version used for command execution") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters