@@ -71,18 +71,30 @@ func runCommand(command string, output io.Writer) error {
71
71
return cmd .Run ()
72
72
}
73
73
74
+ // Check if package is installed
75
+ func isPackageInstalled (pkgName string , pkgVersion string ) bool {
76
+ var output io.Writer
77
+
78
+ pkg , err := translatePkgName (pkgName , pkgVersion )
79
+ if err != nil {
80
+ return false
81
+ }
82
+
83
+ // hab pkg path command exits with zero if pkg exists
84
+ checkCmd := habPath + " pkg path " + pkg + " >/dev/null 2>&1"
85
+ checkCmdResult := runCommand (checkCmd , output )
86
+
87
+ return checkCmdResult == nil
88
+ }
89
+
74
90
// execHab installs habitat package and executes habitat command
75
91
func execHab (pkgName string , pkgVersion string , habChannel string , command []string , output io.Writer ) error {
76
92
pkg , verErr := translatePkgName (pkgName , pkgVersion )
77
93
if verErr != nil {
78
94
return verErr
79
95
}
80
96
81
- // Check if the desired version of the package exists
82
- checkCmd := habPath + " pkg path " + pkg + " >/dev/null 2>&1"
83
- checkCmdResult := runCommand (checkCmd , output )
84
-
85
- if checkCmdResult != nil {
97
+ if ! isPackageInstalled (pkgName , pkgVersion ) {
86
98
installCmd := []string {habPath , "pkg" , "install" , pkg , "-c" , habChannel , ">/dev/null" }
87
99
if u , userErr := user .Current (); userErr != nil || u .Uid != "0" {
88
100
// execute sudo command if not root user
@@ -200,7 +212,10 @@ func main() {
200
212
201
213
depot := hab .New (habDepotURL )
202
214
203
- if pkgVerExp != "" {
215
+ // Use verExp as an exact package version if it is already installed
216
+ if isPackageInstalled (pkgName , pkgVerExp ) {
217
+ pkgVersion = pkgVerExp
218
+ } else if pkgVerExp != "" {
204
219
pkgVersion , err = getPackageVersion (depot , pkgName , pkgVerExp , habChannel )
205
220
206
221
if err != nil {
0 commit comments