-
Notifications
You must be signed in to change notification settings - Fork 11
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
1 parent
555891a
commit 01ced12
Showing
14 changed files
with
293 additions
and
110 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
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
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
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
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,110 @@ | ||
/* | ||
Copyright (C) 2014 Oscar Campos <[email protected]> | ||
This program is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 2 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License along | ||
with this program; if not, write to the Free Software Foundation, Inc., | ||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
See LICENSE file for more details. | ||
*/ | ||
|
||
package cache | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
"sort" | ||
"strings" | ||
) | ||
|
||
// return a list of installed go versions | ||
func GetInstalled(tags, sources, binaries []string) ([]string, error) { | ||
files, err := filepath.Glob(filepath.Join(CacheDirectory(), "*")) | ||
if err != nil { | ||
fmt.Println("while getting installed versions:", err) | ||
return nil, err | ||
} | ||
versions := []string{} | ||
for _, file := range files { | ||
filename := path.Base(file) | ||
if filename != "mercurial" && filename != "logs" { | ||
stat, err := os.Stat(file) | ||
if err != nil { | ||
fmt.Println("while getting installed versions:", err) | ||
return nil, err | ||
} | ||
if stat.IsDir() { | ||
if isValidVersion(filename, tags, sources, binaries) { | ||
versions = append(versions, filename) | ||
} | ||
} | ||
} | ||
} | ||
|
||
return versions, nil | ||
} | ||
|
||
// return a list of non installed go versions | ||
func GetNonInstalled(v, tags, sources, binaries []string) []string { | ||
var versions = make([]string, len(tags) len(sources) len(binaries)) | ||
installed_versions := make([]string, len(v)) | ||
copy(installed_versions, v) | ||
c := 0 | ||
for _, ver := range append(binaries, append(tags, sources...)...) { | ||
found := false | ||
for i, installed := range installed_versions { | ||
if strings.TrimSpace(installed) == strings.TrimSpace(ver) { | ||
// skip this element and reduce v | ||
installed_versions = append( | ||
installed_versions[:i], installed_versions[i 1:]...) | ||
found = true | ||
continue | ||
} | ||
} | ||
if found { | ||
continue | ||
} | ||
versions[c] = fmt.Sprintf(" %s", ver) | ||
c | ||
} | ||
|
||
return versions | ||
} | ||
|
||
// check if a given version is valid in all the possible containers | ||
func isValidVersion(file string, tags, sources, binaries []string) bool { | ||
// tip is always a valid version | ||
if file == "tip" { | ||
return true | ||
} | ||
// look on the sources first that is the smaller collection | ||
for _, ver := range sources { | ||
if file == ver { | ||
return true | ||
} | ||
} | ||
// now look on the binaries collection using binary search | ||
index := sort.SearchStrings(binaries, file) | ||
if len(binaries) > index && binaries[index] == file { | ||
return true | ||
} | ||
// now look in the mercurial tags using binary search | ||
index = sort.SearchStrings(tags, file) | ||
if len(tags) > index && tags[index] == file { | ||
return true | ||
} | ||
|
||
return false | ||
} |
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
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,75 @@ | ||
/* | ||
Copyright (C) 2014 Oscar Campos <[email protected]> | ||
This program is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 2 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License along | ||
with this program; if not, write to the Free Software Foundation, Inc., | ||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
See LICENSE file for more details. | ||
*/ | ||
|
||
package commands | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/DamnWidget/VenGO/env" | ||
"github.com/DamnWidget/VenGO/utils" | ||
) | ||
|
||
type Import struct { | ||
Manifest string | ||
Prompt string | ||
Verbose bool | ||
Force bool | ||
} | ||
|
||
// create a new import command and return back it's address | ||
func NewImport(options ...func(i *Import)) *Import { | ||
imp := new(Import) | ||
for _, option := range options { | ||
option(imp) | ||
} | ||
if imp.Prompt == "" { | ||
imp.Prompt = fmt.Sprintf("[%s]", imp.Manifest) | ||
} | ||
return imp | ||
} | ||
|
||
// implements the Runner interface importing and recreating an exported env | ||
func (i *Import) Run() (string, error) { | ||
return i.envImport() | ||
} | ||
|
||
// import the given manifest and create a new environment based on it | ||
func (i *Import) envImport() (string, error) { | ||
fmt.Printf("Loading manifest file %s... ", i.Manifest) | ||
manifest, err := env.LoadManifest(i.Manifest) | ||
if err != nil { | ||
fmt.Println(utils.Fail("✖")) | ||
return "", err | ||
} | ||
fmt.Println(utils.Ok("✔")) | ||
fmt.Printf( | ||
"Creating %s environment (this may take a while) ...", manifest.Path) | ||
err = manifest.GenerateEnvironment(i.Verbose, i.Prompt) | ||
if err != nil { | ||
fmt.Println(utils.Fail("✖")) | ||
return "", err | ||
} | ||
fmt.Println(utils.Ok("✔")) | ||
return fmt.Sprintf( | ||
"%s has been created into %s use vengo activate %s to active it", | ||
manifest.Name, manifest.Path, | ||
), nil | ||
} |
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
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
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
Oops, something went wrong.