Skip to content

Commit

Permalink
added import (wip), small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
DamnWidget committed Dec 3, 2014
1 parent 555891a commit 01ced12
Show file tree
Hide file tree
Showing 14 changed files with 293 additions and 110 deletions.
2 changes: 1 addition & 1 deletion applications/export/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 48,7 @@ func main() {
os.Exit(1)
}

// build the list object based on the given options
// build the list value based on the given options
options := buildCommandListOptions()
// set the environment if any
env := flag.Args()
Expand Down
2 changes: 1 addition & 1 deletion applications/install/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 48,7 @@ func main() {
os.Exit(1)
}

// build the list object based on the given options
// build the list value based on the given options
options := buildCommandListOptions()
// set the version
options = append(options, func(i *commands.Install) {
Expand Down
2 changes: 1 addition & 1 deletion applications/lsenvs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 45,7 @@ func main() {
os.Exit(1)
}

// build the list object based on the given options
// build the list value based on the given options
options := buildCommandListOptions()
nel := commands.NewEnvironmentsList(options...)
data, err := nel.Run()
Expand Down
2 changes: 1 addition & 1 deletion applications/mkenv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 48,7 @@ func main() {
os.Exit(1)
}

// build the list object based on the given options
// build the list value based on the given options
options := buildCommandListOptions()
options = append(options, func(m *commands.Mkenv) {
m.Name = name[0]
Expand Down
110 changes: 110 additions & 0 deletions cache/cache.go
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
}
2 changes: 1 addition & 1 deletion commands/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 200,7 @@ var _ = Describe("Commands", func() {
Ω(len(splitVers)).Should(BeNumerically(">", 100))
})

It("Should return a Json object with 4 elements in installed", func() {
It("Should return a Json value with 4 elements in installed", func() {
jsonFormat := func(l *commands.List) {
l.DisplayAs = commands.Json
}
Expand Down
75 changes: 75 additions & 0 deletions commands/import.go
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
}
86 changes: 2 additions & 84 deletions commands/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 24,7 @@ import (
"encoding/json"
"fmt"
"os"
"path"
"path/filepath"
"sort"
"strings"

"github.com/DamnWidget/VenGO/cache"
Expand Down Expand Up @@ -71,15 69,15 @@ func (l *List) Run() (string, error) {
"installed": []string{},
"available": []string{},
}
installed, err := l.getInstalled(tags, sources, binaries)
installed, err := cache.GetInstalled(tags, sources, binaries)
if err != nil {
fmt.Println("while running List command:", err)
return "error while running the command", err
}
versions["installed"] = append(versions["installed"], installed...)
versions["available"] = append(
versions["available"],
l.getNonInstalled(installed, tags, sources, binaries)...,
cache.GetNonInstalled(installed, tags, sources, binaries)...,
)

return l.display(versions)
Expand Down Expand Up @@ -128,83 126,3 @@ func (l *List) display(versions map[string][]string) (string, error) {

return "", fmt.Errorf("List.DisplayAs is not set to a valid value!")
}

// return a list of installed go versions
func (l *List) getInstalled(tags, sources, binaries []string) ([]string, error) {
files, err := filepath.Glob(filepath.Join(cache.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 l.isValidVersion(filename, tags, sources, binaries) {
versions = append(versions, filename)
}
}
}
}

return versions, nil
}

// return a list of non installed go versions
func (l *List) 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 (l *List) 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
}
21 changes: 3 additions & 18 deletions commands/mkenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 21,6 @@
package commands

import (
"encoding/json"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -83,22 82,8 @@ func (m *Mkenv) Run() (string, error) {
// check if the Go version used to generate the virtual environment is
// installed or not, if is not, return a NotIntalled error type
func (m *Mkenv) checkInstalled() error {
l := NewList(func(l *List) {
l.DisplayAs = Json
})
result, err := l.Run()
if err != nil {
return err
if !env.LookupInstalledVersion(m.Version) {
return ErrNotInstalled
}
jsonData := new(BriefJSON)
if err := json.Unmarshal([]byte(result), jsonData); err != nil {
return err
}
for _, v := range jsonData.Installed {
if v == m.Version {
return nil
}
}

return ErrNotInstalled
return nil
}
2 changes: 1 addition & 1 deletion env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 102,7 @@ func (e *Environment) Generate() error {
}

// checks if the environment path exists, and create it if doesn't
// returns a file object or error if fails
// returns a file value or error if fails
func (e *Environment) checkPath() (*os.File, error) {
fileName := filepath.Join(e.VenGO_PATH, "bin", "activate")
return e.createFile(fileName)
Expand Down
Loading

0 comments on commit 01ced12

Please sign in to comment.