Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
markelog committed Jul 5, 2016
0 parents commit 57a199d
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# http://editorconfig.org

root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* text eol=lf
/vendor
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
os:
- linux
- osx

language: go

go:
- tip
27 changes: 27 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"os"
"fmt"

"github.com/urfave/cli"

"github.com/markelog/eclectica/detect"
)


func main() {
cli.AppHelpTemplate = `
Usage: e <name>, <name>@<version>
`
if len(os.Args) == 2 {
cli.NewApp().Run(os.Args)
} else {
err := detect.Detect("node")

if err != nil {
fmt.Println(err)
}
}
}
26 changes: 26 additions & 0 deletions detect/detect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package detect

import (
"github.com/markelog/eclectica/download"
"github.com/markelog/eclectica/dists/nodejs"
)

func Detect(dist string) error {
var (
version map[string]string
err error
)

switch {
case dist == "node":
version, err = nodejs.Latest()
}

if err != nil {
return err
}

download.Download(version["url"])

return nil
}
49 changes: 49 additions & 0 deletions dists/nodejs/nodejs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package nodejs

import (
"net/http"
"io/ioutil"
"regexp"
"fmt"
)

var (
client = &http.Client{}

partLatest = "https://nodejs.org/dist/latest"
latest = fmt.Sprintf("%s/SHASUMS256.txt", partLatest)
)

func info(url string) (file string, err error){
response, err := client.Get(latest)

if err != nil {
return "", err
}

defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)

if err != nil {
return "", err
}

return string(contents), nil
}

func Latest() (map[string]string, error) {
file, err := info(latest)
result := make(map[string]string)

if err != nil {
return result, err
}

versionReg := regexp.MustCompile(`node-v(\d+\.\d+\.\d)`)

version := versionReg.FindStringSubmatch(file)[1]
result["version"] = version
result["url"] = fmt.Sprintf("%s/node-v%s-darwin-x64.tar.gz", partLatest, version)

return result, nil
}
39 changes: 39 additions & 0 deletions download/download.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package download

import (
"fmt"
"github.com/cavaliercoder/grab"
"os"
"time"
)

func Download(url string) {
// start file download
fmt.Printf("Downloading %s...\n", url)
respch, err := grab.GetAsync(".", url)
if err != nil {
fmt.Fprintf(os.Stderr, "Error downloading %s: %v\n", url, err)
os.Exit(1)
}

// block until HTTP/1.1 GET response is received
fmt.Printf("Initializing download...\n")
resp := <-respch

// print progress until transfer is complete
for !resp.IsComplete() {
fmt.Printf("\033[1AProgress %d / %d bytes (%d%%)\033[K\n", resp.BytesTransferred(), resp.Size, int(100*resp.Progress()))
time.Sleep(200 * time.Millisecond)
}

// clear progress line
fmt.Printf("\033[1A\033[K")

// check for errors
if resp.Error != nil {
fmt.Fprintf(os.Stderr, "Error downloading %s: %v\n", url, resp.Error)
os.Exit(1)
}

fmt.Printf("Successfully downloaded to ./%s\n", resp.Filename)
}
21 changes: 21 additions & 0 deletions license
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) Oleg Gaidarenko <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[![Build Status](https://travis-ci.org/markelog/eclectica.svg?branch=master)](https://travis-ci.org/markelog/eclectica)

# Eclectica

0 comments on commit 57a199d

Please sign in to comment.