Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mastercactapus committed Nov 20, 2016
1 parent cc18a89 commit 98a6dbe
Show file tree
Hide file tree
Showing 2 changed files with 229 additions and 0 deletions.
116 changes: 116 additions & 0 deletions ci/easel-resource/check/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package main

import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"path"
"strings"

"golang.org/x/net/html"
)

type versionDesc struct {
Version string
URL string
Check string
}

const baseURL = "https://easel.inventables.com/downloads"

func getVersionDesc(href string) *versionDesc {
u, err := url.Parse(href)
if err != nil {
// not what we're looking for
return nil
}
if !strings.HasSuffix(u.Path, ".pkg") {
return nil
}

file := path.Base(u.Path)
if !strings.HasPrefix(file, "EaselDriver-") {
return nil
}

return &versionDesc{
Version: strings.TrimPrefix(strings.TrimSuffix(file, ".pkg"), "EaselDriver-"),
URL: href,
Check: u.RawQuery,
}
}

func main() {
var input struct {
Source struct {
URI string
}
Version versionDesc
}

data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatalln("read stdin:", err)
}

err = json.Unmarshal(data, &input)
if err != nil {
log.Fatalln("decode input:", err, "\n\nFrom:\n", string(data))
}

if input.Source.URI == "" {
input.Source.URI = baseURL
}

resp, err := http.Get(input.Source.URI)
if err != nil {
log.Fatalln("GET", input.Source.URI, err)
}
if resp.StatusCode != 200 {
log.Fatalln("GET", input.Source.URI, resp.Status)
}

versions := make([]versionDesc, 0, 50)

z := html.NewTokenizer(resp.Body)
scan:
for {
tt := z.Next()
switch {
case tt == html.ErrorToken:
break scan
case tt == html.StartTagToken:
t := z.Token()
if t.Data == "a" {
for _, a := range t.Attr {
if a.Key != "href" {
continue
}
vs := getVersionDesc(a.Val)
if vs != nil {
versions = append(versions, *vs)
}
break
}
}
}
}

for i, v := range versions {
if v != input.Version {
continue
}

versions = versions[:i+1]

break
}

err = json.NewEncoder(os.Stdout).Encode(versions)
if err != nil {
log.Fatalln("encode:", err)
}
}
113 changes: 113 additions & 0 deletions ci/easel-resource/in/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package main

import (
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"

"github.com/mastercactapus/gocpio"
"github.com/mastercactapus/goxar"
)

type versionDesc struct {
Version string
URL string
Check string
}

func getPayload(xarData []byte) (io.ReadCloser, error) {
b := bytes.NewReader(xarData)
r, err := xar.NewReader(b, int64(b.Len()))
if err != nil {
return nil, err
}

filename := "IrisLib-0.3.1.pkg/Payload"
for _, f := range r.File {
if f.Name != filename {
continue
}
return f.Open()

}

return nil, fmt.Errorf("could not find file in xar: %s", filename)
}

func main() {

var input struct {
Version versionDesc
}

data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatalln("read stdin:", err)
}

err = json.Unmarshal(data, &input)
if err != nil {
log.Fatalln("decode input:", err, "\n\nFrom:\n", string(data))
}

log.Println("Downloading:", input.Version.URL)
resp, err := http.Get(input.Version.URL)
if err != nil {
log.Fatalln("GET", input.Version.URL, err)
}

data, err = ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln("download:", err)
}
resp.Body.Close()

root := os.Args[1]
log.Println("Extracting to:", root)
r, err := getPayload(data)
if err != nil {
log.Fatalln("processing xar:", err)
}
defer r.Close()

gr, err := gzip.NewReader(r)
if err != nil {
log.Fatalln("gunzip payload:", err)
}

cr := cpio.NewReader(gr)

for {
hdr, err := cr.Next()
if err == io.EOF {
break
}
if err != nil {
log.Fatalln("extract cpio payload:", err)
}
filename := filepath.Join(root, hdr.Name)
if hdr.FileInfo().IsDir() {
err = os.MkdirAll(filename, hdr.FileInfo().Mode())
if err != nil {
log.Fatalln("mkdir,", filename, ":", err)
}
continue
}
fd, err := os.Create(filename)
if err != nil {
log.Fatalln(filename, ":", err)
}
_, err = io.Copy(fd, cr)
if err != nil {
log.Fatalln("extract", filename, err)
}
fd.Close()
}
}

0 comments on commit 98a6dbe

Please sign in to comment.