<commit_before>package utility

import (
	"database/sql"
	"fmt"
	"math"
	"strconv"
	"strings"
)

func RequiredNullable(s interface{}) error {
	err := fmt.Errorf("Invalid value")
	switch v := s.(type) {
	case sql.NullString:
		if v.Valid && len(strings.Trim(v.String, " ")) > 0 {
			return nil
		}
	case sql.NullInt64:
		if v.Valid && v.Int64 != 0 {
			return nil
		}
	case sql.NullFloat64:
		if v.Valid && v.Float64 != 0.00 {
			return nil
		}
	case sql.NullBool:
		if v.Valid {
			return nil
		}
	default:
		return fmt.Errorf("Invalid argument type")
	}

	return err
}

func StringWithDefault(s sql.NullString, d string) string {
	if s.Valid && len(strings.Trim(s.String, " ")) > 0 {
		return s.String
	}

	return d
}

func FromNullString(s sql.NullString) string {
	if s.Valid {
		return s.String
	}
	return ""
}

func FromNullFloat(s sql.NullFloat64) float64 {
	if s.Valid {
		return s.Float64
	}
	return 0.00
}

func FromNullInt(i sql.NullInt64) int64 {
	if i.Valid {
		return i.Int64
	}

	return 0
}
<commit_msg>removed unused imports<commit_after>package utility

import (
	"database/sql"
	"fmt"
	"strings"
)

func RequiredNullable(s interface{}) error {
	err := fmt.Errorf("Invalid value")
	switch v := s.(type) {
	case sql.NullString:
		if v.Valid && len(strings.Trim(v.String, " ")) > 0 {
			return nil
		}
	case sql.NullInt64:
		if v.Valid && v.Int64 != 0 {
			return nil
		}
	case sql.NullFloat64:
		if v.Valid && v.Float64 != 0.00 {
			return nil
		}
	case sql.NullBool:
		if v.Valid {
			return nil
		}
	default:
		return fmt.Errorf("Invalid argument type")
	}

	return err
}

func StringWithDefault(s sql.NullString, d string) string {
	if s.Valid && len(strings.Trim(s.String, " ")) > 0 {
		return s.String
	}

	return d
}

func FromNullString(s sql.NullString) string {
	if s.Valid {
		return s.String
	}
	return ""
}

func FromNullFloat(s sql.NullFloat64) float64 {
	if s.Valid {
		return s.Float64
	}
	return 0.00
}

func FromNullInt(i sql.NullInt64) int64 {
	if i.Valid {
		return i.Int64
	}

	return 0
}
<|endoftext|>


<commit_before>package utils

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"os"
	"strings"
)

var configFile string
var hc = http.Client{}

// Error .. an error returned from the API
type Error struct {
	Message  string `json:"message"`
	Code     int    `json:"code"`
	MoreInfo string `json:"moreInfo"`
}

// ErrorsList .. a list of errors returned from the API
type ErrorsList struct {
	errorList []Error
}

// Get .. issues an HTTP GET request
func Get(endpoint string) []byte {
	response, err := http.Get(endpoint)
	if err != nil {
		fmt.Print(err.Error())
		os.Exit(1)
	}

	// Response
	responseData, err := ioutil.ReadAll(response.Body)
	if err != nil {
		log.Fatal(err)
	}
	return responseData
}

// Post .. issues an HTTP POST request
func Post(endpoint string, form url.Values) ([]byte, error) {

	req, err := http.NewRequest("POST", endpoint, strings.NewReader(form.Encode()))
	req.PostForm = form
	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")

	// Fetch
	response, err := hc.Do(req)
	if err != nil {
		log.Fatal(err)
	}

	responseData, err := ioutil.ReadAll(response.Body)
	if err != nil {
		log.Fatal(err)
	}

	return responseData, err
}
<commit_msg>#21 Add GetParams<commit_after>package utils

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"os"
	"strings"
)

var configFile string
var hc = http.Client{}

// Error .. an error returned from the API
type Error struct {
	Message  string `json:"message"`
	Code     int    `json:"code"`
	MoreInfo string `json:"moreInfo"`
}

// ErrorsList .. a list of errors returned from the API
type ErrorsList struct {
	errorList []Error
}

// Get .. issues an HTTP GET request
func Get(endpoint string) []byte {
	response, err := http.Get(endpoint)
	if err != nil {
		fmt.Print(err.Error())
		os.Exit(1)
	}

	// Response
	responseData, err := ioutil.ReadAll(response.Body)
	if err != nil {
		log.Fatal(err)
	}
	return responseData
}

// GetParams .. issues an HTTP Get request with parameters
func GetParams(endpoint string, queryParams map[string]int) []byte {
	queryString := ""

	for k, v := range queryParams {
		queryString = fmt.Sprintf("%s&%s=%d", queryString, k, v)
	}

	endpoint = endpoint + queryString

	response, err := http.Get(endpoint)
	if err != nil {
		fmt.Print(err.Error())
		os.Exit(1)
	}

	// Response
	responseData, err := ioutil.ReadAll(response.Body)
	if err != nil {
		log.Fatal(err)
	}
	return responseData
}

// Post .. issues an HTTP POST request
func Post(endpoint string, form url.Values) ([]byte, error) {

	req, err := http.NewRequest("POST", endpoint, strings.NewReader(form.Encode()))
	req.PostForm = form
	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")

	// Fetch
	response, err := hc.Do(req)
	if err != nil {
		log.Fatal(err)
	}

	responseData, err := ioutil.ReadAll(response.Body)
	if err != nil {
		log.Fatal(err)
	}

	return responseData, err
}
<|endoftext|>


<commit_before>package utils

import (
	"crypto/sha512"
	"fmt"
)

const ChainCNIPrefix = ("CNI-")
const MaxChainLength = 29 - len(ChainCNIPrefix)

// Generates a chain name to be used with iptables.
// Ensures that the generated name is less than
// 29 chars in length
func FormatChainName(name string, id string) string {
	chain := fmt.Sprintf("%x", sha512.Sum512([]byte(name+id)))
	if len(chain) > MaxChainLength {
		chain = chain[:MaxChainLength]
	}
	return fmt.Sprintf("%s%s", ChainCNIPrefix, chain)
}
<commit_msg>pkg/utils: fix docstring<commit_after>package utils

import (
	"crypto/sha512"
	"fmt"
)

const ChainCNIPrefix = ("CNI-")
const MaxChainLength = 29 - len(ChainCNIPrefix)

// Generates a chain name to be used with iptables.
// Ensures that the generated chain name is less than
// 29 chars in length
func FormatChainName(name string, id string) string {
	chain := fmt.Sprintf("%x", sha512.Sum512([]byte(name+id)))
	if len(chain) > MaxChainLength {
		chain = chain[:MaxChainLength]
	}
	return fmt.Sprintf("%s%s", ChainCNIPrefix, chain)
}
<|endoftext|>


<commit_before>package nummethod
<commit_msg>nummethod: add FooBar<commit_after>package nummethod

type FooBar interface {
	foo()
	Bar()
}
<|endoftext|>


<commit_before>package utils

import (
	"os/exec"
)

func Cat_file(filepath string) (ret string, err error) {
	cmd := exec.Command("/bin/cat", filepath)
	buf, err := cmd.Output()
	if err != nil {
		//fmt.Println(err)
		return
	}
	ret = string(buf)
	return
}
<commit_msg>Use io/ioutil instead.<commit_after>package utils

import (
	"io/ioutil"
	//"os/exec"
)

func Cat_file(filepath string) (ret string, err error) {
	buf, err := ioutil.ReadFile(filepath)
	if err != nil {
		return
	}

	ret = string(buf)
	return
}

/*
func Cat_file(filepath string) (ret string, err error) {
	cmd := exec.Command("/bin/cat", filepath)
	buf, err := cmd.Output()
	if err != nil {
		//fmt.Println(err)
		return
	}
	ret = string(buf)
	return
}
*/
<|endoftext|>


<commit_before>// Package utils - 套件所需的公用工具
//
package utils

import (
	"regexp"
	"strconv"
	"time"
)

// ExchangeMap is simple to check tse or otc.
var ExchangeMap = map[string]bool{"tse": true, "otc": true}

// TaipeiTimeZone is for time.Data() setting.
var TaipeiTimeZone = time.FixedZone("Asia/Taipei", 8*3600)

// TWSE base url.
const (
	TWSEURL     string = "http://mis.tse.com.tw"
	TWSEHOST    string = "http://www.twse.com.tw"
	OTCHOST     string = "http://www.tpex.org.tw"
	OTCCSV      string = "/ch/stock/aftertrading/daily_trading_info/st43_download.php?d=%d/%02d&stkno=%s&r=%d" // year, mon, stock, rand
	TWSECSV     string = "/ch/trading/exchange/STOCK_DAY/STOCK_DAY_print.php?genpage=genpage/Report%d%02d/%d%02d_F3_1_8_%s.php&type=csv&r=%d"
	TWSELISTCSV string = "/ch/trading/exchange/MI_INDEX/MI_INDEX.php" // year, mon, day, type
	TWSEREAL    string = "/stock/api/getStockInfo.jsp?ex_ch=%s_%s.tw_%s&json=1&delay=0&_=%d"
)

// RandInt return random int.
func RandInt() int {
	return time.Now().Nanosecond()
}

//func RandInt() int64 {
//	result, _ := rand.Int(rand.Reader, big.NewInt(102400))
//	return result.Int64()
//}

//func RandInt() int64 {
//	r := rand.New(rand.NewSource(time.Now().UnixNano()))
//	return r.Int63n(100000)
//}

var dateReg = regexp.MustCompile(`([\d]{2,})/([\d]{1,2})/([\d]{1,2})`)

// ParseDate is to parse "104/01/13" format.
func ParseDate(strDate string) time.Time {
	p := dateReg.FindStringSubmatch(strDate)
	year, _ := strconv.Atoi(p[1])
	mon, _ := strconv.Atoi(p[2])
	day, _ := strconv.Atoi(p[3])
	return time.Date(year+1911, time.Month(mon), day, 0, 0, 0, 0, TaipeiTimeZone)
}

// SumFloat64 計算總和（float64）
func SumFloat64(data []float64) float64 {
	var result float64
	for _, v := range data {
		result += v
	}
	return result
}

// AvgFloat64 計算平均（float64）
func AvgFloat64(data []float64) float64 {
	return float64(int(SumFloat64(data)*100)/len(data)) / 100
}

// SumUint64 計算總和（uint64）
func SumUint64(data []uint64) uint64 {
	var result uint64
	for _, v := range data {
		result += v
	}
	return result
}

// AvgUint64 計算平均（uint64）
func AvgUint64(data []uint64) uint64 {
	return SumUint64(data) / uint64(len(data))
}

// ThanPastUint64 計算最後一個數值是否為過去幾天最大或最小（uint64）
func ThanPastUint64(data []uint64, days int, max bool) bool {
	var dataFloat64 = make([]float64, days+1)
	for i, v := range data[len(data)-1-days:] {
		dataFloat64[i] = float64(v)
	}
	return thanPast(dataFloat64, max)
}

// ThanPastFloat64 計算最後一個數值是否為過去幾天最大或最小（float64）
func ThanPastFloat64(data []float64, days int, max bool) bool {
	return thanPast(data[len(data)-1-days:], max)
}

func thanPast(data []float64, max bool) bool {
	//var dataFloat64 []float64
	//dataFloat64 = make([]float64, days+1)
	//for i, v := range data[len(data)-1-days : len(data)-1] {
	//	switch v.(type) {
	//	case int64:
	//		dataFloat64[i] = float64(v.(int64))
	//	case float64:
	//		dataFloat64[i] = v.(float64)
	//	}
	//}

	var base = data[len(data)-1]
	var condition func(b float64) bool

	if max {
		condition = func(b float64) bool { return base > b }
	} else {
		condition = func(b float64) bool { return base < b }
	}

	for _, v := range data[:len(data)-1] {
		if condition(v) {
			continue
		} else {
			return false
		}
	}
	return true
}

// ThanSumPastFloat64 計算最後一個數值是否為過去幾天的總和大或小（float64）
func ThanSumPastFloat64(data []float64, days int, max bool) bool {
	return thanSumPast(data[len(data)-1-days:], max)
}

// ThanSumPastUint64 計算最後一個數值是否為過去幾天的總和大或小（uint64）
func ThanSumPastUint64(data []uint64, days int, max bool) bool {
	var dataFloat64 = make([]float64, days+1)
	for i, v := range data[len(data)-1-days:] {
		dataFloat64[i] = float64(v)
	}
	return thanSumPast(dataFloat64, max)
}

func thanSumPast(data []float64, max bool) bool {
	var result = data[len(data)-1] > SumFloat64(data[:len(data)-1])
	if max {
		return result
	}
	return !result
}
<commit_msg>Add `countCountineFloat64`.<commit_after>// Package utils - 套件所需的公用工具
//
package utils

import (
	"regexp"
	"strconv"
	"time"
)

// ExchangeMap is simple to check tse or otc.
var ExchangeMap = map[string]bool{"tse": true, "otc": true}

// TaipeiTimeZone is for time.Data() setting.
var TaipeiTimeZone = time.FixedZone("Asia/Taipei", 8*3600)

// TWSE base url.
const (
	TWSEURL     string = "http://mis.tse.com.tw"
	TWSEHOST    string = "http://www.twse.com.tw"
	OTCHOST     string = "http://www.tpex.org.tw"
	OTCCSV      string = "/ch/stock/aftertrading/daily_trading_info/st43_download.php?d=%d/%02d&stkno=%s&r=%d" // year, mon, stock, rand
	TWSECSV     string = "/ch/trading/exchange/STOCK_DAY/STOCK_DAY_print.php?genpage=genpage/Report%d%02d/%d%02d_F3_1_8_%s.php&type=csv&r=%d"
	TWSELISTCSV string = "/ch/trading/exchange/MI_INDEX/MI_INDEX.php" // year, mon, day, type
	TWSEREAL    string = "/stock/api/getStockInfo.jsp?ex_ch=%s_%s.tw_%s&json=1&delay=0&_=%d"
)

// RandInt return random int.
func RandInt() int {
	return time.Now().Nanosecond()
}

//func RandInt() int64 {
//	result, _ := rand.Int(rand.Reader, big.NewInt(102400))
//	return result.Int64()
//}

//func RandInt() int64 {
//	r := rand.New(rand.NewSource(time.Now().UnixNano()))
//	return r.Int63n(100000)
//}

var dateReg = regexp.MustCompile(`([\d]{2,})/([\d]{1,2})/([\d]{1,2})`)

// ParseDate is to parse "104/01/13" format.
func ParseDate(strDate string) time.Time {
	p := dateReg.FindStringSubmatch(strDate)
	year, _ := strconv.Atoi(p[1])
	mon, _ := strconv.Atoi(p[2])
	day, _ := strconv.Atoi(p[3])
	return time.Date(year+1911, time.Month(mon), day, 0, 0, 0, 0, TaipeiTimeZone)
}

// SumFloat64 計算總和（float64）
func SumFloat64(data []float64) float64 {
	var result float64
	for _, v := range data {
		result += v
	}
	return result
}

// AvgFloat64 計算平均（float64）
func AvgFloat64(data []float64) float64 {
	return float64(int(SumFloat64(data)*100)/len(data)) / 100
}

// SumUint64 計算總和（uint64）
func SumUint64(data []uint64) uint64 {
	var result uint64
	for _, v := range data {
		result += v
	}
	return result
}

// AvgUint64 計算平均（uint64）
func AvgUint64(data []uint64) uint64 {
	return SumUint64(data) / uint64(len(data))
}

// ThanPastUint64 計算最後一個數值是否為過去幾天最大或最小（uint64）
func ThanPastUint64(data []uint64, days int, max bool) bool {
	var dataFloat64 = make([]float64, days+1)
	for i, v := range data[len(data)-1-days:] {
		dataFloat64[i] = float64(v)
	}
	return thanPast(dataFloat64, max)
}

// ThanPastFloat64 計算最後一個數值是否為過去幾天最大或最小（float64）
func ThanPastFloat64(data []float64, days int, max bool) bool {
	return thanPast(data[len(data)-1-days:], max)
}

func thanPast(data []float64, max bool) bool {
	//var dataFloat64 []float64
	//dataFloat64 = make([]float64, days+1)
	//for i, v := range data[len(data)-1-days : len(data)-1] {
	//	switch v.(type) {
	//	case int64:
	//		dataFloat64[i] = float64(v.(int64))
	//	case float64:
	//		dataFloat64[i] = v.(float64)
	//	}
	//}

	var base = data[len(data)-1]
	var condition func(b float64) bool

	if max {
		condition = func(b float64) bool { return base > b }
	} else {
		condition = func(b float64) bool { return base < b }
	}

	for _, v := range data[:len(data)-1] {
		if condition(v) {
			continue
		} else {
			return false
		}
	}
	return true
}

// ThanSumPastFloat64 計算最後一個數值是否為過去幾天的總和大或小（float64）
func ThanSumPastFloat64(data []float64, days int, max bool) bool {
	return thanSumPast(data[len(data)-1-days:], max)
}

// ThanSumPastUint64 計算最後一個數值是否為過去幾天的總和大或小（uint64）
func ThanSumPastUint64(data []uint64, days int, max bool) bool {
	var dataFloat64 = make([]float64, days+1)
	for i, v := range data[len(data)-1-days:] {
		dataFloat64[i] = float64(v)
	}
	return thanSumPast(dataFloat64, max)
}

func thanSumPast(data []float64, max bool) bool {
	var result = data[len(data)-1] > SumFloat64(data[:len(data)-1])
	if max {
		return result
	}
	return !result
}

func countCountineFloat64(data []float64) (int, bool) {
	var condition func(x float64) bool
	if data[len(data)-1] > 0 {
		condition = func(x float64) bool { return x > 0 }
	} else {
		condition = func(x float64) bool { return x < 0 }
	}
	counter := 1
	for i := 1; i <= len(data)-1; i++ {
		if condition(data[len(data)-1-i]) {
			counter++
		} else {
			break
		}
	}
	return counter, data[len(data)-1] > 0
}
<|endoftext|>


<commit_before>package utils

import (
	"encoding/binary"
	"errors"

	"github.com/joushou/qp"
)

// Readdir interprets a 9p2000 directory listing.
func Readdir(b []byte) ([]qp.Stat, error) {
	var stats []qp.Stat
	for len(b) > 0 {
		l := int(binary.LittleEndian.Uint16(b[0:2]))
		if l+2 > len(b) {
			return stats, errors.New("short input")
		}

		var stat qp.Stat
		if err := stat.Unmarshal(b[0 : 2+l]); err != nil {
			return stats, err
		}
		b = b[2+l:]
		stats = append(stats, stat)
	}

	return stats, nil
}
<commit_msg>Guess capacity for qp.Stat slice in Readdir<commit_after>package utils

import (
	"encoding/binary"
	"errors"

	"github.com/joushou/qp"
)

// Readdir interprets a 9p2000 directory listing.
func Readdir(b []byte) ([]qp.Stat, error) {
	stats := make([]qp.Stat, 0, len(b)/64)
	for len(b) > 0 {
		l := int(binary.LittleEndian.Uint16(b[0:2]))
		if l+2 > len(b) {
			return stats, errors.New("short input")
		}

		var stat qp.Stat
		if err := stat.Unmarshal(b[0 : 2+l]); err != nil {
			return stats, err
		}
		b = b[2+l:]
		stats = append(stats, stat)
	}

	return stats, nil
}
<|endoftext|>


<commit_before>package oauth2

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"

	"github.com/tarent/loginsrv/model"
)

var gitlabAPI = "https://gitlab.com/api/v4"

func init() {
	RegisterProvider(providerGitlab)
}

// GitlabUser is used for parsing the gitlab response
type GitlabUser struct {
	Username  string `json:"username,omitempty"`
	AvatarURL string `json:"avatar_url,omitempty"`
	Name      string `json:"name,omitempty"`
	Email     string `json:"email,omitempty"`
}

type GitlabGroup struct {
	FullPath string `json:"full_path,omitempty"`
}

var providerGitlab = Provider{
	Name:     "gitlab",
	AuthURL:  "https://gitlab.com/oauth/authorize",
	TokenURL: "https://gitlab.com/oauth/token",
	GetUserInfo: func(token TokenInfo) (model.UserInfo, string, error) {
		gu := GitlabUser{}
		url := fmt.Sprintf("%v/user?access_token=%v", gitlabAPI, token.AccessToken)
		resp, err := http.Get(url)
		if err != nil {
			return model.UserInfo{}, "", err
		}

		if !strings.Contains(resp.Header.Get("Content-Type"), "application/json") {
			return model.UserInfo{}, "", fmt.Errorf("wrong content-type on gitlab get user info: %v", resp.Header.Get("Content-Type"))
		}

		if resp.StatusCode != 200 {
			return model.UserInfo{}, "", fmt.Errorf("got http status %v on gitlab get user info", resp.StatusCode)
		}

		b, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			return model.UserInfo{}, "", fmt.Errorf("error reading gitlab get user info: %v", err)
		}

		err = json.Unmarshal(b, &gu)
		if err != nil {
			return model.UserInfo{}, "", fmt.Errorf("error parsing gitlab get user info: %v", err)
		}

		gg := []*GitlabGroup{}
		url = fmt.Sprintf("%v/groups?access_token=%v", gitlabAPI, token.AccessToken)
		resp, err = http.Get(url)
		if err != nil {
			return model.UserInfo{}, "", err
		}

		if !strings.Contains(resp.Header.Get("Content-Type"), "application/json") {
			return model.UserInfo{}, "", fmt.Errorf("wrong content-type on gitlab get groups info: %v", resp.Header.Get("Content-Type"))
		}

		if resp.StatusCode != 200 {
			return model.UserInfo{}, "", fmt.Errorf("got http status %v on gitlab get groups info", resp.StatusCode)
		}

		g, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			return model.UserInfo{}, "", fmt.Errorf("error reading gitlab get groups info: %v", err)
		}

		err = json.Unmarshal(g, &gg)
		if err != nil {
			return model.UserInfo{}, "", fmt.Errorf("error parsing gitlab get groups info: %v", err)
		}

		groups := make([]string, len(gg))
		for i := 0; i < len(gg); i++ {
			groups[i] = gg[i].FullPath
		}

		return model.UserInfo{
			Sub:     gu.Username,
			Picture: gu.AvatarURL,
			Name:    gu.Name,
			Email:   gu.Email,
			Groups:  groups,
			Origin:  "gitlab",
		}, `{"user":` + string(b) + `,"groups":` + string(g) + `}`, nil
	},
}
<commit_msg>closing request body<commit_after>package oauth2

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"

	"github.com/tarent/loginsrv/model"
)

var gitlabAPI = "https://gitlab.com/api/v4"

func init() {
	RegisterProvider(providerGitlab)
}

// GitlabUser is used for parsing the gitlab response
type GitlabUser struct {
	Username  string `json:"username,omitempty"`
	AvatarURL string `json:"avatar_url,omitempty"`
	Name      string `json:"name,omitempty"`
	Email     string `json:"email,omitempty"`
}

type GitlabGroup struct {
	FullPath string `json:"full_path,omitempty"`
}

var providerGitlab = Provider{
	Name:     "gitlab",
	AuthURL:  "https://gitlab.com/oauth/authorize",
	TokenURL: "https://gitlab.com/oauth/token",
	GetUserInfo: func(token TokenInfo) (model.UserInfo, string, error) {
		gu := GitlabUser{}
		url := fmt.Sprintf("%v/user?access_token=%v", gitlabAPI, token.AccessToken)
		resp, err := http.Get(url)
		if err != nil {
			return model.UserInfo{}, "", err
		}
		defer resp.Body.Close()

		if !strings.Contains(resp.Header.Get("Content-Type"), "application/json") {
			return model.UserInfo{}, "", fmt.Errorf("wrong content-type on gitlab get user info: %v", resp.Header.Get("Content-Type"))
		}

		if resp.StatusCode != 200 {
			return model.UserInfo{}, "", fmt.Errorf("got http status %v on gitlab get user info", resp.StatusCode)
		}

		b, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			return model.UserInfo{}, "", fmt.Errorf("error reading gitlab get user info: %v", err)
		}

		err = json.Unmarshal(b, &gu)
		if err != nil {
			return model.UserInfo{}, "", fmt.Errorf("error parsing gitlab get user info: %v", err)
		}

		gg := []*GitlabGroup{}
		url = fmt.Sprintf("%v/groups?access_token=%v", gitlabAPI, token.AccessToken)
		resp, err = http.Get(url)
		if err != nil {
			return model.UserInfo{}, "", err
		}
		defer resp.Body.Close()

		if !strings.Contains(resp.Header.Get("Content-Type"), "application/json") {
			return model.UserInfo{}, "", fmt.Errorf("wrong content-type on gitlab get groups info: %v", resp.Header.Get("Content-Type"))
		}

		if resp.StatusCode != 200 {
			return model.UserInfo{}, "", fmt.Errorf("got http status %v on gitlab get groups info", resp.StatusCode)
		}

		g, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			return model.UserInfo{}, "", fmt.Errorf("error reading gitlab get groups info: %v", err)
		}

		err = json.Unmarshal(g, &gg)
		if err != nil {
			return model.UserInfo{}, "", fmt.Errorf("error parsing gitlab get groups info: %v", err)
		}

		groups := make([]string, len(gg))
		for i := 0; i < len(gg); i++ {
			groups[i] = gg[i].FullPath
		}

		return model.UserInfo{
			Sub:     gu.Username,
			Picture: gu.AvatarURL,
			Name:    gu.Name,
			Email:   gu.Email,
			Groups:  groups,
			Origin:  "gitlab",
		}, `{"user":` + string(b) + `,"groups":` + string(g) + `}`, nil
	},
}
<|endoftext|>


<commit_before>// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------

package utils

var capitalizedToCapitalizedWordsMap = map[string]string{
	"Dns":     "DNS",
	"Dyn":     "DYN",
	"Eip":     "EIP",
	"Keypair": "KeyPair",
	"Vxnet":   "VxNet",
}

var lowerCaseToLowercaseWordsMap = map[string]string{
	"lastest": "latest", // Fix typo
}

var lowerCaseToCapitalizedWordsMap = map[string]string{
	"acl":           "ACL",
	"cors":          "CORS",
	"cpu":           "CPU",
	"datadir":       "DataDir",
	"dhcp":          "DHCP",
	"dns":           "DNS",
	"dyn":           "DYN",
	"eip":           "EIP",
	"eips":          "EIPs",
	"http":          "HTTP",
	"icp":           "ICP",
	"id":            "ID",
	"ids":           "IDs",
	"innodb":        "InnoDB",
	"io":            "IO",
	"ip":            "IP",
	"ips":           "IPs",
	"ipset":         "IPSet",
	"ipsets":        "IPSets",
	"keypair":       "KeyPair",
	"keypairs":      "KeyPairs",
	"lastest":       "Latest", // Fix typo
	"loadbalancer":  "LoadBalancer",
	"loadbalancers": "LoadBalancers",
	"newsid":        "NewSID",
	"nic":           "NIC",
	"os":            "OS",
	"opt":           "OPT",
	"qingstor":      "QingStor",
	"qingcloud":     "QingCloud",
	"qs":            "QS",
	"rdb":           "RDB",
	"rdbs":          "RDBs",
	"sql":           "SQL",
	"tmp":           "TMP",
	"tmpdir":        "TMPDir",
	"topslave":      "TopSlave",
	"trx":           "TRX",
	"ui":            "UI",
	"uri":           "URI",
	"url":           "URL",
	"usb":           "USB",
	"vcpus":         "VCPUs",
	"vxnet":         "VxNet",
	"vxnets":        "VxNets",
}

var abbreviateWordsMap = []string{
	"ACL",
	"CORS",
	"CPU",
	"DHCP",
	"DNS",
	"DYN",
	"EIP",
	"ETag",
	"IaaS",
	"ICP",
	"ID",
	"IO",
	"IP",
	"IPSets",
	"MD5",
	"NIC",
	"OS",
	"OPT",
	"QingStor",
	"QingCloud",
	"QS",
	"RDB",
	"SQL",
	"SSO",
	"TMP",
	"TMPDir",
	"TRX",
	"UI",
	"URL",
	"VCPUs",
	"VxNet",
}
<commit_msg>Add OAuth to abbreviate words map<commit_after>// +-------------------------------------------------------------------------
// | Copyright (C) 2016 Yunify, Inc.
// +-------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this work except in compliance with the License.
// | You may obtain a copy of the License in the LICENSE file, or at:
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +-------------------------------------------------------------------------

package utils

var capitalizedToCapitalizedWordsMap = map[string]string{
	"Dns":     "DNS",
	"Dyn":     "DYN",
	"Eip":     "EIP",
	"Keypair": "KeyPair",
	"Vxnet":   "VxNet",
}

var lowerCaseToLowercaseWordsMap = map[string]string{
	"lastest": "latest", // Fix typo
}

var lowerCaseToCapitalizedWordsMap = map[string]string{
	"acl":           "ACL",
	"cors":          "CORS",
	"cpu":           "CPU",
	"datadir":       "DataDir",
	"dhcp":          "DHCP",
	"dns":           "DNS",
	"dyn":           "DYN",
	"eip":           "EIP",
	"eips":          "EIPs",
	"http":          "HTTP",
	"icp":           "ICP",
	"id":            "ID",
	"ids":           "IDs",
	"innodb":        "InnoDB",
	"io":            "IO",
	"ip":            "IP",
	"ips":           "IPs",
	"ipset":         "IPSet",
	"ipsets":        "IPSets",
	"keypair":       "KeyPair",
	"keypairs":      "KeyPairs",
	"lastest":       "Latest", // Fix typo
	"loadbalancer":  "LoadBalancer",
	"loadbalancers": "LoadBalancers",
	"newsid":        "NewSID",
	"nic":           "NIC",
	"os":            "OS",
	"opt":           "OPT",
	"qingstor":      "QingStor",
	"qingcloud":     "QingCloud",
	"qs":            "QS",
	"rdb":           "RDB",
	"rdbs":          "RDBs",
	"sql":           "SQL",
	"tmp":           "TMP",
	"tmpdir":        "TMPDir",
	"topslave":      "TopSlave",
	"trx":           "TRX",
	"ui":            "UI",
	"uri":           "URI",
	"url":           "URL",
	"usb":           "USB",
	"vcpus":         "VCPUs",
	"vxnet":         "VxNet",
	"vxnets":        "VxNets",
}

var abbreviateWordsMap = []string{
	"ACL",
	"CORS",
	"CPU",
	"DHCP",
	"DNS",
	"DYN",
	"EIP",
	"ETag",
	"IaaS",
	"ICP",
	"ID",
	"IO",
	"IP",
	"IPSets",
	"MD5",
	"NIC",
	"OAuth",
	"OS",
	"OPT",
	"QingStor",
	"QingCloud",
	"QS",
	"RDB",
	"SQL",
	"SSO",
	"TMP",
	"TMPDir",
	"TRX",
	"UI",
	"URL",
	"VCPUs",
	"VxNet",
}
<|endoftext|>


<commit_before>package oauth2

import (
	"encoding/json"
	"net/http"

	"github.com/influxdata/chronograf"

	"golang.org/x/oauth2"
	hrk "golang.org/x/oauth2/heroku"
)

// Ensure that Heroku is an oauth2.Provider
var _ Provider = &Heroku{}

const (
	// HerokuAccountRoute is required for interacting with Heroku API
	HerokuAccountRoute string = "https://api.heroku.com/account"
)

// Heroku is an OAuth2 Provider allowing users to authenticate with Heroku to
// gain access to Chronograf
type Heroku struct {
	// OAuth2 Secrets
	ClientID     string
	ClientSecret string

	Organizations []string // set of organizations permitted to access the protected resource. Empty means "all"

	Logger chronograf.Logger
}

// Config returns the OAuth2 exchange information and endpoints
func (h *Heroku) Config() *oauth2.Config {
	return &oauth2.Config{
		ClientID:     h.ID(),
		ClientSecret: h.Secret(),
		Scopes:       h.Scopes(),
		Endpoint:     hrk.Endpoint,
	}
}

// ID returns the Heroku application client ID
func (h *Heroku) ID() string {
	return h.ClientID
}

// Name returns the name of this provider (heroku)
func (h *Heroku) Name() string {
	return "heroku"
}

// PrincipalID returns the Heroku email address of the user.
func (h *Heroku) PrincipalID(provider *http.Client) (string, error) {
	type DefaultOrg struct {
		ID   string `json:"id"`
		Name string `json:"name"`
	}
	type Account struct {
		Email               string     `json:"email"`
		DefaultOrganization DefaultOrg `json:"default_organization"`
	}

	req, err := http.NewRequest("GET", HerokuAccountRoute, nil)
	// Requests fail to Heroku unless this Accept header is set.
	req.Header.Set("Accept", "application/vnd.heroku+json; version=3")
	resp, err := provider.Do(req)
	if err != nil {
		h.Logger.Error("Unable to communicate with Heroku. err:", err)
		return "", err
	}
	defer resp.Body.Close()
	d := json.NewDecoder(resp.Body)

	var account Account
	if err := d.Decode(&account); err != nil {
		h.Logger.Error("Unable to decode response from Heroku. err:", err)
		return "", err
	}

	// check if member of org
	if len(h.Organizations) > 0 {
		for _, org := range h.Organizations {
			if account.DefaultOrganization.Name == org {
				return account.Email, nil
			}
		}
		h.Logger.Error(ErrOrgMembership)
		return "", ErrOrgMembership
	}
	return account.Email, nil
}

// Group returns the Heroku organization that user belongs to.
func (h *Heroku) Group(provider *http.Client) (string, error) {
	type DefaultOrg struct {
		ID   string `json:"id"`
		Name string `json:"name"`
	}
	type Account struct {
		Email               string     `json:"email"`
		DefaultOrganization DefaultOrg `json:"default_organization"`
	}

	resp, err := provider.Get(HerokuAccountRoute)
	if err != nil {
		h.Logger.Error("Unable to communicate with Heroku. err:", err)
		return "", err
	}
	defer resp.Body.Close()
	d := json.NewDecoder(resp.Body)

	var account Account
	if err := d.Decode(&account); err != nil {
		h.Logger.Error("Unable to decode response from Heroku. err:", err)
		return "", err
	}

	return account.DefaultOrganization.Name, nil
}

// Scopes for heroku is "identity" which grants access to user account
// information. This will grant us access to the user's email address which is
// used as the Principal's identifier.
func (h *Heroku) Scopes() []string {
	return []string{"identity"}
}

// Secret returns the Heroku application client secret
func (h *Heroku) Secret() string {
	return h.ClientSecret
}
<commit_msg>Log error if Heroku API req responds with non-200-range code<commit_after>package oauth2

import (
	"encoding/json"
	"fmt"
	"net/http"

	"github.com/influxdata/chronograf"

	"golang.org/x/oauth2"
	hrk "golang.org/x/oauth2/heroku"
)

// Ensure that Heroku is an oauth2.Provider
var _ Provider = &Heroku{}

const (
	// HerokuAccountRoute is required for interacting with Heroku API
	HerokuAccountRoute string = "https://api.heroku.com/account"
)

// Heroku is an OAuth2 Provider allowing users to authenticate with Heroku to
// gain access to Chronograf
type Heroku struct {
	// OAuth2 Secrets
	ClientID     string
	ClientSecret string

	Organizations []string // set of organizations permitted to access the protected resource. Empty means "all"

	Logger chronograf.Logger
}

// Config returns the OAuth2 exchange information and endpoints
func (h *Heroku) Config() *oauth2.Config {
	return &oauth2.Config{
		ClientID:     h.ID(),
		ClientSecret: h.Secret(),
		Scopes:       h.Scopes(),
		Endpoint:     hrk.Endpoint,
	}
}

// ID returns the Heroku application client ID
func (h *Heroku) ID() string {
	return h.ClientID
}

// Name returns the name of this provider (heroku)
func (h *Heroku) Name() string {
	return "heroku"
}

// PrincipalID returns the Heroku email address of the user.
func (h *Heroku) PrincipalID(provider *http.Client) (string, error) {
	type DefaultOrg struct {
		ID   string `json:"id"`
		Name string `json:"name"`
	}
	type Account struct {
		Email               string     `json:"email"`
		DefaultOrganization DefaultOrg `json:"default_organization"`
	}

	req, err := http.NewRequest("GET", HerokuAccountRoute, nil)
	// Requests fail to Heroku unless this Accept header is set.
	req.Header.Set("Accept", "application/vnd.heroku+json; version=3")
	resp, err := provider.Do(req)
	if resp.StatusCode/100 != 2 {
		err := fmt.Errorf(
			"Unable to GET user data from %s. Status: %s",
			HerokuAccountRoute,
			resp.Status,
		)
		h.Logger.Error("", err)
		return "", err
	}
	if err != nil {
		h.Logger.Error("Unable to communicate with Heroku. err:", err)
		return "", err
	}
	defer resp.Body.Close()
	d := json.NewDecoder(resp.Body)

	var account Account
	if err := d.Decode(&account); err != nil {
		h.Logger.Error("Unable to decode response from Heroku. err:", err)
		return "", err
	}

	// check if member of org
	if len(h.Organizations) > 0 {
		for _, org := range h.Organizations {
			if account.DefaultOrganization.Name == org {
				return account.Email, nil
			}
		}
		h.Logger.Error(ErrOrgMembership)
		return "", ErrOrgMembership
	}
	return account.Email, nil
}

// Group returns the Heroku organization that user belongs to.
func (h *Heroku) Group(provider *http.Client) (string, error) {
	type DefaultOrg struct {
		ID   string `json:"id"`
		Name string `json:"name"`
	}
	type Account struct {
		Email               string     `json:"email"`
		DefaultOrganization DefaultOrg `json:"default_organization"`
	}

	resp, err := provider.Get(HerokuAccountRoute)
	if err != nil {
		h.Logger.Error("Unable to communicate with Heroku. err:", err)
		return "", err
	}
	defer resp.Body.Close()
	d := json.NewDecoder(resp.Body)

	var account Account
	if err := d.Decode(&account); err != nil {
		h.Logger.Error("Unable to decode response from Heroku. err:", err)
		return "", err
	}

	return account.DefaultOrganization.Name, nil
}

// Scopes for heroku is "identity" which grants access to user account
// information. This will grant us access to the user's email address which is
// used as the Principal's identifier.
func (h *Heroku) Scopes() []string {
	return []string{"identity"}
}

// Secret returns the Heroku application client secret
func (h *Heroku) Secret() string {
	return h.ClientSecret
}
<|endoftext|>


<commit_before>package colonycore

import (
	"github.com/eaciit/dbox"
	"github.com/eaciit/orm/v1"
	"github.com/eaciit/toolkit"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
	"strings"
)

type Widget struct {
	orm.ModelBase
	ID           string              `json:"_id"`
	Title        string              `json:"title"`
	DataSourceID []string            `json:"dataSourceId"`
	DataSource   []*DataSourceWidget `json:"dataSource"`
	Description  string              `json:"description"`
	Params       toolkit.M           `json:"params"`
}

type DataSourceWidget struct {
	ID   string      `json:"_id"`
	Data []toolkit.M `json:"data"`
}

type Config struct {
	orm.ModelBase
	data []toolkit.M `json:"data"`
}

func (w *Widget) TableName() string {
	return "widgets"
}

func (w *Widget) RecordID() interface{} {
	return w.ID
}

func (w *Widget) Get(search string) ([]Widget, error) {
	var query *dbox.Filter

	if search != "" {
		query = dbox.Contains("_id", search)
	}

	data := []Widget{}
	cursor, err := Find(new(Widget), query)
	if err != nil {
		return nil, err
	}
	if err := cursor.Fetch(&data, 0, false); err != nil {
		return nil, err
	}
	defer cursor.Close()
	return data, nil
}

func (w *Widget) GetById() error {
	if err := Get(w, w.ID); err != nil {
		return err
	}
	return nil
}

func (w *Widget) Save() error {
	if err := Save(w); err != nil {
		return err
	}
	return nil
}

func checkDir(basepath string, scanDir string, dirName string) error {
	dirList, err := ioutil.ReadDir(scanDir)
	if err != nil {
		return err
	}

	if toolkit.SliceLen(dirList) == 1 {
		for _, f := range dirList {
			if f.IsDir() {
				oldpath := filepath.Join(scanDir, f.Name())
				temp_oldpath := filepath.Join(scanDir, dirName)
				if err := os.Rename(oldpath, temp_oldpath); err != nil {
					return err
				}
				if err := toolkit.ZipCompress(temp_oldpath, scanDir+".zip"); err != nil {
					return err
				}
				if err := os.RemoveAll(scanDir); err != nil {
					return err
				}
				if err := toolkit.ZipExtract(scanDir+".zip", basepath); err != nil {
					return err
				}
				if err := os.Remove(scanDir + ".zip"); err != nil {
					return err
				}
			}
		}
	}

	return nil
}

func (w *Widget) ExtractFile(compressedSource string, fileName string) error {
	compressedFile := filepath.Join(compressedSource, fileName)
	extractDest := filepath.Join(compressedSource, w.ID)

	if runtime.GOOS == "windows" {
		exec.Command("cmd", "/C", "rmdir", "/s", "/q", extractDest).Run()
	} else {
		exec.Command("rm", "-rf", extractDest).Run()
	}

	if strings.Contains(fileName, ".tar.gz") {
		if err := toolkit.TarGzExtract(compressedFile, extractDest); err != nil {
			return err
		}
	} else if strings.Contains(fileName, ".gz") {
		if err := toolkit.GzExtract(compressedFile, extractDest); err != nil {
			return err
		}
	} else if strings.Contains(fileName, ".tar") {
		if err := toolkit.TarExtract(compressedFile, extractDest); err != nil {
			return err
		}
	} else if strings.Contains(fileName, ".zip") {
		if err := toolkit.ZipExtract(compressedFile, extractDest); err != nil {
			return err
		}
	}

	if err := os.Remove(compressedFile); err != nil {
		return err
	}

	// checkDir(compressedSource, extractDest, w.ID)

	return nil
}

func (w *Widget) Delete() error {
	if err := Delete(w); err != nil {
		return err
	}
	return nil
}
<commit_msg>added datasource and config widget<commit_after>package colonycore

import (
	"github.com/eaciit/dbox"
	"github.com/eaciit/orm/v1"
	"github.com/eaciit/toolkit"
	"io/ioutil"
	"os"
	// "os/exec"
	"path/filepath"
	// "runtime"
	"strings"
)

type Widget struct {
	orm.ModelBase
	ID    string `json:"_id"`
	Title string `json:"title"`
	// DataSourceID []string            `json:"dataSourceId"`
	DataSource  []*DataSourceWidget `json:"dataSource"`
	Description string              `json:"description"`
	Config      toolkit.Ms          `json:"config"`
	Params      toolkit.M           `json:"params"`
}

type DataSourceWidget struct {
	ID   string      `json:"_id"`
	Data []toolkit.M `json:"data"`
}

type Config struct {
	orm.ModelBase
	data toolkit.M `json:"data"`
}

func (w *Widget) TableName() string {
	return "widgets"
}

func (w *Widget) RecordID() interface{} {
	return w.ID
}

func (w *Widget) Get(search string) ([]Widget, error) {
	var query *dbox.Filter

	if search != "" {
		query = dbox.Contains("_id", search)
	}

	data := []Widget{}
	cursor, err := Find(new(Widget), query)
	if err != nil {
		return nil, err
	}
	if err := cursor.Fetch(&data, 0, false); err != nil {
		return nil, err
	}
	defer cursor.Close()
	return data, nil
}

func (w *Widget) GetById() error {
	if err := Get(w, w.ID); err != nil {
		return err
	}
	return nil
}

func (w *Widget) Save() error {
	if err := Save(w); err != nil {
		return err
	}
	return nil
}

func checkDir(basepath string, scanDir string, dirName string) error {
	dirList, err := ioutil.ReadDir(scanDir)
	if err != nil {
		return err
	}

	if toolkit.SliceLen(dirList) == 1 {
		for _, f := range dirList {
			if f.IsDir() {
				oldpath := filepath.Join(scanDir, f.Name())
				temp_oldpath := filepath.Join(scanDir, dirName)
				if err := os.Rename(oldpath, temp_oldpath); err != nil {
					return err
				}
				if err := toolkit.ZipCompress(temp_oldpath, scanDir+".zip"); err != nil {
					return err
				}
				if err := os.RemoveAll(scanDir); err != nil {
					return err
				}
				if err := toolkit.ZipExtract(scanDir+".zip", basepath); err != nil {
					return err
				}
				if err := os.Remove(scanDir + ".zip"); err != nil {
					return err
				}
			}
		}
	}

	return nil
}

func (w *Widget) ExtractFile(compressedSource string, fileName string) (toolkit.Ms, error) {
	compressedFile := filepath.Join(compressedSource, fileName)
	extractDest := filepath.Join(compressedSource, w.ID)

	if err := os.RemoveAll(extractDest); err != nil {
		return nil, err
	}

	if strings.Contains(fileName, ".tar.gz") {
		if err := toolkit.TarGzExtract(compressedFile, extractDest); err != nil {
			return nil, err
		}
	} else if strings.Contains(fileName, ".gz") {
		if err := toolkit.GzExtract(compressedFile, extractDest); err != nil {
			return nil, err
		}
	} else if strings.Contains(fileName, ".tar") {
		if err := toolkit.TarExtract(compressedFile, extractDest); err != nil {
			return nil, err
		}
	} else if strings.Contains(fileName, ".zip") {
		if err := toolkit.ZipExtract(compressedFile, extractDest); err != nil {
			return nil, err
		}
	}

	if err := os.Remove(compressedFile); err != nil {
		return nil, err
	}

	getConfigFile := filepath.Join(extractDest, "config.json")
	content, err := ioutil.ReadFile(getConfigFile)
	if err != nil {
		return nil, err
	}

	result := toolkit.Ms{}
	err = toolkit.Unjson(content, &result)
	if err != nil {
		return nil, err
	}
	// checkDir(compressedSource, extractDest, w.ID)

	return result, nil
}

func (w *Widget) Delete(compressedSource string) error {
	extractDest := filepath.Join(compressedSource, w.ID)
	if err := Delete(w); err != nil {
		return err
	}

	if err := os.RemoveAll(extractDest); err != nil {
		return err
	}
	return nil
}
<|endoftext|>


<commit_before>package forecast

import (
	"crypto/tls"
	"encoding/json"
	"io/ioutil"
	"net/http"
	"strconv"
)

// URL example:  "https://api.forecast.io/forecast/APIKEY/LATITUDE,LONGITUDE,TIME?units=ca"
const (
	BASEURL = "https://api.forecast.io/forecast"
)

type Flags struct {
	DarkSkyUnavailable string
	DarkSkyStations    []string
	DataPointStations  []string
	ISDStations        []string
	LAMPStations       []string
	METARStations      []string
	METNOLicense       string
	Sources            []string
	Units              string
}

type DataPoint struct {
	Time                   float64
	Summary                string
	Icon                   string
	SunriseTime            float64
	SunsetTime             float64
	PrecipIntensity        float64
	PrecipIntensityMax     float64
	PrecipIntensityMaxTime float64
	PrecipProbability      float64
	PrecipType             string
	PrecipAccumulation     float64
	Temperature            float64
	TemperatureMin         float64
	TemperatureMinTime     float64
	TemperatureMax         float64
	TemperatureMaxTime     float64
	DewPoint               float64
	WindSpeed              float64
	WindBearing            float64
	CloudCover             float64
	Humidity               float64
	Pressure               float64
	Visibility             float64
	Ozone                  float64
}

type DataBlock struct {
	Summary string
	Icon    string
	Data    []DataPoint
}

type alert struct {
	Title   string
	Expires float64
	URI     string
}

type Forecast struct {
	Latitude  float64
	Longitude float64
	Timezone  string
	Offset    float64
	Currently DataPoint
	Minutely  DataBlock
	Hourly    DataBlock
	Daily     DataBlock
	Alerts    []alert
	Flags     Flags
	APICalls  int
}

type Units string

const (
	CA Units = "ca"
	SI Units = "si"
)

func Get(key string, lat string, long string, time string, units Units) (*Forecast, error) {
	coord := lat + "," + long

	var url string
	if time == "now" {
		url = BASEURL + "/" + key + "/" + coord + "?units=" + string(units)
	} else {
		url = BASEURL + "/" + key + "/" + coord + "," + time + "?units=" + string(units)
	}

	tr := &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: false}, // does not seem required any longer
	}
	client := &http.Client{Transport: tr}
	resp, err := client.Get(url)

	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)

	var f Forecast
	err = json.Unmarshal(body, &f)
	if err != nil {
		return nil, err
	}

	calls, _ := strconv.Atoi(resp.Header.Get("X-Forecast-API-Calls"))
	f.APICalls = calls

	return &f, nil
}
<commit_msg>using default http client<commit_after>package forecast

import (
//	"crypto/tls"
	"encoding/json"
	"io/ioutil"
	"net/http"
	"strconv"
)

// URL example:  "https://api.forecast.io/forecast/APIKEY/LATITUDE,LONGITUDE,TIME?units=ca"
const (
	BASEURL = "https://api.forecast.io/forecast"
)

type Flags struct {
	DarkSkyUnavailable string
	DarkSkyStations    []string
	DataPointStations  []string
	ISDStations        []string
	LAMPStations       []string
	METARStations      []string
	METNOLicense       string
	Sources            []string
	Units              string
}

type DataPoint struct {
	Time                   float64
	Summary                string
	Icon                   string
	SunriseTime            float64
	SunsetTime             float64
	PrecipIntensity        float64
	PrecipIntensityMax     float64
	PrecipIntensityMaxTime float64
	PrecipProbability      float64
	PrecipType             string
	PrecipAccumulation     float64
	Temperature            float64
	TemperatureMin         float64
	TemperatureMinTime     float64
	TemperatureMax         float64
	TemperatureMaxTime     float64
	DewPoint               float64
	WindSpeed              float64
	WindBearing            float64
	CloudCover             float64
	Humidity               float64
	Pressure               float64
	Visibility             float64
	Ozone                  float64
}

type DataBlock struct {
	Summary string
	Icon    string
	Data    []DataPoint
}

type alert struct {
	Title   string
	Expires float64
	URI     string
}

type Forecast struct {
	Latitude  float64
	Longitude float64
	Timezone  string
	Offset    float64
	Currently DataPoint
	Minutely  DataBlock
	Hourly    DataBlock
	Daily     DataBlock
	Alerts    []alert
	Flags     Flags
	APICalls  int
}

type Units string

const (
	CA Units = "ca"
	SI Units = "si"
)

func Get(key string, lat string, long string, time string, units Units) (*Forecast, error) {
	coord := lat + "," + long

	var url string
	if time == "now" {
		url = BASEURL + "/" + key + "/" + coord + "?units=" + string(units)
	} else {
		url = BASEURL + "/" + key + "/" + coord + "," + time + "?units=" + string(units)
	}

/*	tr := &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: false}, // does not seem required any longer
	}
	client := &http.Client{Transport: tr}*/
	resp, err := http.Get(url)

	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)

	var f Forecast
	err = json.Unmarshal(body, &f)
	if err != nil {
		return nil, err
	}

	calls, _ := strconv.Atoi(resp.Header.Get("X-Forecast-API-Calls"))
	f.APICalls = calls

	return &f, nil
}
<|endoftext|>


<commit_before>package commander

import (
	"strconv"
	"strings"

	"github.com/litl/galaxy/config"
	"github.com/litl/galaxy/log"
	"github.com/litl/galaxy/utils"
	"github.com/ryanuber/columnize"
)

type RuntimeOptions struct {
	Ps          int
	Memory      string
	CPUShares   string
	VirtualHost string
	Port        string
}

func RuntimeList(configStore *config.Store, app, env, pool string) error {

	envs := []string{env}

	if env == "" {
		var err error
		envs, err = configStore.ListEnvs()
		if err != nil {
			return err
		}
	}

	columns := []string{"ENV | NAME | POOL | PS | MEM | VHOSTS | PORT"}

	for _, env := range envs {

		appList, err := configStore.ListApps(env)
		if err != nil {
			return err
		}

		for _, appCfg := range appList {

			if app != "" && appCfg.Name() != app {
				continue
			}

			for _, p := range appCfg.RuntimePools() {

				if pool != "" && p != pool {
					continue
				}

				name := appCfg.Name()
				ps := appCfg.GetProcesses(p)
				mem := appCfg.GetMemory(p)

				columns = append(columns, strings.Join([]string{
					env,
					name,
					p,
					strconv.FormatInt(int64(ps), 10),
					mem,
					appCfg.Env()["VIRTUAL_HOST"],
					appCfg.Env()["GALAXY_PORT"],
				}, " | "))
			}
		}
	}
	output, _ := columnize.SimpleFormat(columns)
	log.Println(output)
	return nil

}

func RuntimeSet(configStore *config.Store, app, env, pool string, options RuntimeOptions) (bool, error) {

	cfg, err := configStore.GetApp(app, env)
	if err != nil {
		return false, err
	}

	if options.Ps != 0 && options.Ps != cfg.GetProcesses(pool) {
		cfg.SetProcesses(pool, options.Ps)
	}

	if options.Memory != "" && options.Memory != cfg.GetMemory(pool) {
		cfg.SetMemory(pool, options.Memory)
	}

	vhosts := strings.Split(cfg.Env()["VIRTUAL_HOST"], ",")
	if options.VirtualHost != "" && !utils.StringInSlice(options.VirtualHost, vhosts) {
		vhosts = append(vhosts, options.VirtualHost)
		cfg.EnvSet("VIRTUAL_HOST", strings.Join(vhosts, ","))
	}

	if options.Port != "" {
		cfg.EnvSet("GALAXY_PORT", options.Port)
	}

	return configStore.UpdateApp(cfg, env)
}

func RuntimeUnset(configStore *config.Store, app, env, pool string, options RuntimeOptions) (bool, error) {

	cfg, err := configStore.GetApp(app, env)
	if err != nil {
		return false, err
	}

	if options.Ps != 0 {
		cfg.SetProcesses(pool, -1)
	}

	if options.Memory != "" {
		cfg.SetMemory(pool, "")
	}

	vhosts := strings.Split(cfg.Env()["VIRTUAL_HOST"], ",")
	if options.VirtualHost != "" && utils.StringInSlice(options.VirtualHost, vhosts) {
		vhosts = utils.RemoveStringInSlice(options.VirtualHost, vhosts)
		cfg.EnvSet("VIRTUAL_HOST", strings.Join(vhosts, ","))
	}

	if options.Port != "" {
		cfg.EnvSet("GALAXY_PORT", "")
	}

	return configStore.UpdateApp(cfg, env)
}
<commit_msg>Don't insert an empty virtual host<commit_after>package commander

import (
	"strconv"
	"strings"

	"github.com/litl/galaxy/config"
	"github.com/litl/galaxy/log"
	"github.com/litl/galaxy/utils"
	"github.com/ryanuber/columnize"
)

type RuntimeOptions struct {
	Ps          int
	Memory      string
	CPUShares   string
	VirtualHost string
	Port        string
}

func RuntimeList(configStore *config.Store, app, env, pool string) error {

	envs := []string{env}

	if env == "" {
		var err error
		envs, err = configStore.ListEnvs()
		if err != nil {
			return err
		}
	}

	columns := []string{"ENV | NAME | POOL | PS | MEM | VHOSTS | PORT"}

	for _, env := range envs {

		appList, err := configStore.ListApps(env)
		if err != nil {
			return err
		}

		for _, appCfg := range appList {

			if app != "" && appCfg.Name() != app {
				continue
			}

			for _, p := range appCfg.RuntimePools() {

				if pool != "" && p != pool {
					continue
				}

				name := appCfg.Name()
				ps := appCfg.GetProcesses(p)
				mem := appCfg.GetMemory(p)

				columns = append(columns, strings.Join([]string{
					env,
					name,
					p,
					strconv.FormatInt(int64(ps), 10),
					mem,
					appCfg.Env()["VIRTUAL_HOST"],
					appCfg.Env()["GALAXY_PORT"],
				}, " | "))
			}
		}
	}
	output, _ := columnize.SimpleFormat(columns)
	log.Println(output)
	return nil

}

func RuntimeSet(configStore *config.Store, app, env, pool string, options RuntimeOptions) (bool, error) {

	cfg, err := configStore.GetApp(app, env)
	if err != nil {
		return false, err
	}

	if options.Ps != 0 && options.Ps != cfg.GetProcesses(pool) {
		cfg.SetProcesses(pool, options.Ps)
	}

	if options.Memory != "" && options.Memory != cfg.GetMemory(pool) {
		cfg.SetMemory(pool, options.Memory)
	}

	vhosts := []string{}
	vhostsFromEnv := cfg.Env()["VIRTUAL_HOST"]
	if vhostsFromEnv != "" {
		vhosts = strings.Split(cfg.Env()["VIRTUAL_HOST"], ",")
	}

	if options.VirtualHost != "" && !utils.StringInSlice(options.VirtualHost, vhosts) {
		vhosts = append(vhosts, options.VirtualHost)
		cfg.EnvSet("VIRTUAL_HOST", strings.Join(vhosts, ","))
	}

	if options.Port != "" {
		cfg.EnvSet("GALAXY_PORT", options.Port)
	}

	return configStore.UpdateApp(cfg, env)
}

func RuntimeUnset(configStore *config.Store, app, env, pool string, options RuntimeOptions) (bool, error) {

	cfg, err := configStore.GetApp(app, env)
	if err != nil {
		return false, err
	}

	if options.Ps != 0 {
		cfg.SetProcesses(pool, -1)
	}

	if options.Memory != "" {
		cfg.SetMemory(pool, "")
	}

	vhosts := strings.Split(cfg.Env()["VIRTUAL_HOST"], ",")
	if options.VirtualHost != "" && utils.StringInSlice(options.VirtualHost, vhosts) {
		vhosts = utils.RemoveStringInSlice(options.VirtualHost, vhosts)
		cfg.EnvSet("VIRTUAL_HOST", strings.Join(vhosts, ","))
	}

	if options.Port != "" {
		cfg.EnvSet("GALAXY_PORT", "")
	}

	return configStore.UpdateApp(cfg, env)
}
<|endoftext|>


<commit_before>package commands

import (
	"log"

	kingpin "gopkg.in/alecthomas/kingpin.v2"
)

type register interface {
	register(app *kingpin.Application)
}

// Configure sets up the kingpin commands for the omg-cli.
func Configure(logger *log.Logger, app *kingpin.Application) {
	cmds := []register{
		&BuildCommand{logger: logger},
	}

	for _, c := range cmds {
		c.register(app)
	}
}
<commit_msg>add daemon command<commit_after>package commands

import (
	"log"

	kingpin "gopkg.in/alecthomas/kingpin.v2"
)

type register interface {
	register(app *kingpin.Application)
}

// Configure sets up the kingpin commands for the omg-cli.
func Configure(logger *log.Logger, app *kingpin.Application) {
	cmds := []register{
		&BuildCommand{logger: logger},
		&DaemonCommand{logger: logger},
	}

	for _, c := range cmds {
		c.register(app)
	}
}
<|endoftext|>


<commit_before>// Package util contains a variety of general purpose functions, such
// as the SelfUpdate() function, for writing tools.
package util

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
	"strings"

	"tools/lib/envutil"
)

const (
	rootEnv = "VEYRON_ROOT"
)

var (
	baseEnv map[string]string
)

// Config holds configuration for various veyron tools.
type Config struct {
	// GoRepos identifies top-level VEYRON_ROOT directories that
	// host Go repositories.
	GoRepos []string
	// Tools maps tool names to package paths. This map is used
	// by the self-update logic invoked by various tools to
	// identify the package path of the tools' implementation.
	Tools map[string]string
}

func init() {
	// Initialize baseEnv with a snapshot of the environment at startup.
	baseEnv = envutil.ToMap(os.Environ())
}

// Config returns the config for veyron tools.
func VeyronConfig() (*Config, error) {
	root, err := VeyronRoot()
	if err != nil {
		return nil, err
	}
	confPath := filepath.Join(root, "tools", "conf", "veyron")
	confBytes, err := ioutil.ReadFile(confPath)
	if err != nil {
		return nil, fmt.Errorf("ReadFile(%v) failed: %v", confPath, err)
	}
	var conf Config
	if err := json.Unmarshal(confBytes, &conf); err != nil {
		return nil, fmt.Errorf("Unmarshal(%v) failed: %v", string(confBytes), err)
	}
	return &conf, nil
}

// SetupVeyronEnvironment sets up the environment variables used by
// veyron for the given platform.
func SetupVeyronEnvironment(platform Platform) error {
	if os.Getenv("VEYRON_ENV_SETUP") == "none" {
		return nil
	}
	env, err := VeyronEnvironment(platform)
	if err != nil {
		return err
	}
	for key, value := range env {
		if err := os.Setenv(key, value); err != nil {
			return fmt.Errorf("Setenv(%v, %v) failed: %v", key, value, err)
		}
	}
	return nil
}

// VeyronEnvironment returns the environment variables setting for
// veyron. The util package captures the original state of the
// relevant environment variables when the tool is initialized, and
// every invocation of this function updates this original state
// according to the current config of the veyron tool.
func VeyronEnvironment(platform Platform) (map[string]string, error) {
	root, err := VeyronRoot()
	if err != nil {
		return nil, err
	}
	conf, err := VeyronConfig()
	if err != nil {
		return nil, err
	}
	env := map[string]string{}
	setGoPath(env, root, conf)
	setVdlPath(env, root, conf)
	archCmd := exec.Command("uname", "-m")
	arch, err := archCmd.Output()
	if err != nil {
		return nil, fmt.Errorf("failed to get host architecture: %v\n%v\n%s", err, strings.Join(archCmd.Args, " "))
	}
	if platform.OS == "linux" {
		if err := setBluetoothCgoEnv(env, root, strings.TrimSpace(string(arch))); err != nil {
			return nil, err
		}
	}
	if platform.Arch == runtime.GOARCH && platform.OS == runtime.GOOS {
		// If setting up the environment for the host, we are done.
		return env, nil
	}
	// Otherwise, set up the cross-compilation environment.
	if platform.Arch == "arm" && platform.OS == "linux" {
		if platform.Environment == "android" {
			if err := setAndroidEnv(platform, env); err != nil {
				return nil, err
			}
			return env, nil
		} else {
			if err := setArmEnv(platform, env); err != nil {
				return nil, err
			}
			return env, nil
		}
	}
	if platform.Arch == "386" && platform.OS == "nacl" {
		if err := setNaclEnv(platform, env); err != nil {
			return nil, err
		}
		return env, nil
	}
	return nil, UnsupportedPlatformErr{platform}
}

// VeyronRoot returns the root of the veyron universe.
func VeyronRoot() (string, error) {
	root := os.Getenv(rootEnv)
	if root == "" {
		return "", fmt.Errorf("%v is not set", rootEnv)
	}
	return root, nil
}

// getEnvTokens fetches a value for the environment variable identified by
// the given key from the given map, or if it does not exist there,
// from the baseEnv map. The value is then tokenized using the given
// separator and returned as a slice of string tokens.
func getEnvTokens(env map[string]string, key, separator string) []string {
	tokens, ok := env[key]
	if !ok {
		tokens = baseEnv[key]
	}
	result := []string{}
	for _, token := range strings.Split(tokens, separator) {
		if token != "" {
			result = append(result, token)
		}
	}
	return result
}

// setAndroidEnv sets the environment variables used for android
// cross-compilation.
func setAndroidEnv(platform Platform, env map[string]string) error {
	root, err := VeyronRoot()
	if err != nil {
		return err
	}
	// Set CC specific environment variables.
	env["CC"] = filepath.Join(root, "environment", "android", "ndk-toolchain", "bin", "arm-linux-androideabi-gcc")
	// Set Go specific environment variables.
	env["GOARCH"] = platform.Arch
	env["GOARM"] = strings.TrimPrefix(platform.SubArch, "v")
	env["GOOS"] = platform.OS
	if err := setJniCgoEnv(env, root, "android"); err != nil {
		return err
	}
	// Add the paths to veyron cross-compilation tools to the PATH.
	path := getEnvTokens(env, "PATH", ":")
	path = append([]string{
		filepath.Join(root, "environment", "android", "go", "bin"),
	}, path...)
	env["PATH"] = strings.Join(path, ":")
	return nil
}

// setArmEnv sets the environment variables used for android
// cross-compilation.
func setArmEnv(platform Platform, env map[string]string) error {
	root, err := VeyronRoot()
	if err != nil {
		return err
	}
	// Set Go specific environment variables.
	env["GOARCH"] = platform.Arch
	env["GOARM"] = strings.TrimPrefix(platform.SubArch, "v")
	env["GOOS"] = platform.OS

	// Add the paths to veyron cross-compilation tools to the PATH.
	path := getEnvTokens(env, "PATH", ":")
	path = append([]string{
		filepath.Join(root, "environment", "cout", "xgcc", "cross_arm"),
		filepath.Join(root, "environment", "go", "linux", "arm", "go", "bin"),
	}, path...)
	env["PATH"] = strings.Join(path, ":")
	return nil
}

// setGoPath adds the paths to veyron Go workspaces to the GOPATH
// variable.
func setGoPath(env map[string]string, root string, conf *Config) {
	gopath := getEnvTokens(env, "GOPATH", ":")
	// Append an entry to gopath for each veyron go repo.
	for _, repo := range conf.GoRepos {
		gopath = append(gopath, filepath.Join(root, repo, "go"))
	}
	env["GOPATH"] = strings.Join(gopath, ":")
}

// setVdlPath adds the paths to veyron Go workspaces to the VDLPATH
// variable.
func setVdlPath(env map[string]string, root string, conf *Config) {
	vdlpath := getEnvTokens(env, "VDLPATH", ":")
	// Append an entry to vdlpath for each veyron go repo.
	//
	// TODO(toddw): This logic will change when we pull vdl into a
	// separate repo.
	for _, repo := range conf.GoRepos {
		vdlpath = append(vdlpath, filepath.Join(root, repo, "go"))
	}
	env["VDLPATH"] = strings.Join(vdlpath, ":")
}

// setBluetoothCgoEnv sets the CGO_ENABLED variable and adds the
// bluetooth third-party C libraries veyron Go code depends on to the
// CGO_CFLAGS and CGO_LDFLAGS variables.
func setBluetoothCgoEnv(env map[string]string, root, arch string) error {
	// Set the CGO_* variables for the veyron proximity component.
	env["CGO_ENABLED"] = "1"
	libs := []string{
		"dbus-1.6.14",
		"expat-2.1.0",
		"bluez-4.101",
		"libusb-1.0.16-rc10",
		"libusb-compat-0.1.5",
	}
	cflags := getEnvTokens(env, "CGO_CFLAGS", " ")
	ldflags := getEnvTokens(env, "CGO_LDFLAGS", " ")
	for _, lib := range libs {
		dir := filepath.Join(root, "environment", "cout", lib, arch)
		if _, err := os.Stat(dir); err != nil {
			if !os.IsNotExist(err) {
				return fmt.Errorf("Stat(%v) failed: %v", dir, err)
			}
		} else {
			if lib == "dbus-1.6.14" {
				cflags = append(cflags, filepath.Join("-I"+dir, "include", "dbus-1.0", "dbus"))
			} else {
				cflags = append(cflags, filepath.Join("-I"+dir, "include"))
			}
			ldflags = append(ldflags, filepath.Join("-L"+dir, "lib"), "-Wl,-rpath", filepath.Join(dir, "lib"))
		}
	}
	env["CGO_CFLAGS"] = strings.Join(cflags, " ")
	env["CGO_LDFLAGS"] = strings.Join(ldflags, " ")
	return nil
}

// setJniCgoEnv sets the CGO_ENABLED variable and adds the JNI
// third-party C libraries veyron Go code depends on to the CGO_CFLAGS
// and CGO_LDFLAGS variables.
func setJniCgoEnv(env map[string]string, root, arch string) error {
	env["CGO_ENABLED"] = "1"
	cflags := getEnvTokens(env, "CGO_CFLAGS", " ")
	ldflags := getEnvTokens(env, "CGO_LDFLAGS", " ")
	dir := filepath.Join(root, "environment", "cout", "jni-wrapper-1.0", arch)
	if _, err := os.Stat(dir); err != nil {
		if !os.IsNotExist(err) {
			return fmt.Errorf("Stat(%v) failed: %v", dir, err)
		}
	} else {
		cflags = append(cflags, filepath.Join("-I"+dir, "include"))
		ldflags = append(ldflags, filepath.Join("-L"+dir, "lib"), "-Wl,-rpath", filepath.Join(dir, "lib"))
	}
	env["CGO_CFLAGS"] = strings.Join(cflags, " ")
	env["CGO_LDFLAGS"] = strings.Join(ldflags, " ")
	return nil
}

// setNaclEnv sets the environment variables used for nacl
// cross-compilation.
func setNaclEnv(platform Platform, env map[string]string) error {
	env["GOARCH"] = platform.Arch
	env["GOOS"] = platform.OS
	return nil
}
<commit_msg>tools/veyron: Make VeyronEnvironment obey VEYRON_ENV_SETUP=none.<commit_after>// Package util contains a variety of general purpose functions, such
// as the SelfUpdate() function, for writing tools.
package util

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
	"strings"

	"tools/lib/envutil"
)

const (
	rootEnv = "VEYRON_ROOT"
)

var (
	baseEnv map[string]string
)

// Config holds configuration for various veyron tools.
type Config struct {
	// GoRepos identifies top-level VEYRON_ROOT directories that
	// host Go repositories.
	GoRepos []string
	// Tools maps tool names to package paths. This map is used
	// by the self-update logic invoked by various tools to
	// identify the package path of the tools' implementation.
	Tools map[string]string
}

func init() {
	// Initialize baseEnv with a snapshot of the environment at startup.
	baseEnv = envutil.ToMap(os.Environ())
}

// Config returns the config for veyron tools.
func VeyronConfig() (*Config, error) {
	root, err := VeyronRoot()
	if err != nil {
		return nil, err
	}
	confPath := filepath.Join(root, "tools", "conf", "veyron")
	confBytes, err := ioutil.ReadFile(confPath)
	if err != nil {
		return nil, fmt.Errorf("ReadFile(%v) failed: %v", confPath, err)
	}
	var conf Config
	if err := json.Unmarshal(confBytes, &conf); err != nil {
		return nil, fmt.Errorf("Unmarshal(%v) failed: %v", string(confBytes), err)
	}
	return &conf, nil
}

// SetupVeyronEnvironment sets up the environment variables used by
// veyron for the given platform.
func SetupVeyronEnvironment(platform Platform) error {
	env, err := VeyronEnvironment(platform)
	if err != nil {
		return err
	}
	for key, value := range env {
		if err := os.Setenv(key, value); err != nil {
			return fmt.Errorf("Setenv(%v, %v) failed: %v", key, value, err)
		}
	}
	return nil
}

// VeyronEnvironment returns the environment variables setting for
// veyron. The util package captures the original state of the
// relevant environment variables when the tool is initialized, and
// every invocation of this function updates this original state
// according to the current config of the veyron tool.
func VeyronEnvironment(platform Platform) (map[string]string, error) {
	root, err := VeyronRoot()
	if err != nil {
		return nil, err
	}
	conf, err := VeyronConfig()
	if err != nil {
		return nil, err
	}
	env := map[string]string{}
	setGoPath(env, root, conf)
	setVdlPath(env, root, conf)
	archCmd := exec.Command("uname", "-m")
	arch, err := archCmd.Output()
	if err != nil {
		return nil, fmt.Errorf("failed to get host architecture: %v\n%v\n%s", err, strings.Join(archCmd.Args, " "))
	}
	if platform.OS == "linux" {
		if err := setBluetoothCgoEnv(env, root, strings.TrimSpace(string(arch))); err != nil {
			return nil, err
		}
	}
	switch {
	case platform.Arch == runtime.GOARCH && platform.OS == runtime.GOOS:
		// If setting up the environment for the host, we are done.
	case platform.Arch == "arm" && platform.OS == "linux":
		// Set up cross-compilation for arm / linux.
		setArmLinuxEnv := setArmEnv
		if platform.Environment == "android" {
			setArmLinuxEnv = setAndroidEnv
		}
		if err := setArmLinuxEnv(platform, env); err != nil {
			return nil, err
		}
	case platform.Arch == "386" && platform.OS == "nacl":
		// Set up cross-compilation for 386 / nacl.
		if err := setNaclEnv(platform, env); err != nil {
			return nil, err
		}
	default:
		return nil, UnsupportedPlatformErr{platform}
	}
	// If VEYRON_ENV_SETUP==none, we revert all of the changes to variable values,
	// but keep the map populated so we know which variables are important.
	//
	// TODO(toddw): Remove this logic when "veyron clone" is implemented.
	if baseEnv["VEYRON_ENV_SETUP"] == "none" {
		for key := range env {
			env[key] = baseEnv[key]
		}
	}
	return env, nil
}

// VeyronRoot returns the root of the veyron universe.
func VeyronRoot() (string, error) {
	root := baseEnv[rootEnv]
	if root == "" {
		return "", fmt.Errorf("%v is not set", rootEnv)
	}
	return root, nil
}

// getEnvTokens fetches a value for the environment variable identified by
// the given key from the given map, or if it does not exist there,
// from the baseEnv map. The value is then tokenized using the given
// separator and returned as a slice of string tokens.
func getEnvTokens(env map[string]string, key, separator string) []string {
	tokens, ok := env[key]
	if !ok {
		tokens = baseEnv[key]
	}
	result := []string{}
	for _, token := range strings.Split(tokens, separator) {
		if token != "" {
			result = append(result, token)
		}
	}
	return result
}

// setAndroidEnv sets the environment variables used for android
// cross-compilation.
func setAndroidEnv(platform Platform, env map[string]string) error {
	root, err := VeyronRoot()
	if err != nil {
		return err
	}
	// Set CC specific environment variables.
	env["CC"] = filepath.Join(root, "environment", "android", "ndk-toolchain", "bin", "arm-linux-androideabi-gcc")
	// Set Go specific environment variables.
	env["GOARCH"] = platform.Arch
	env["GOARM"] = strings.TrimPrefix(platform.SubArch, "v")
	env["GOOS"] = platform.OS
	if err := setJniCgoEnv(env, root, "android"); err != nil {
		return err
	}
	// Add the paths to veyron cross-compilation tools to the PATH.
	path := getEnvTokens(env, "PATH", ":")
	path = append([]string{
		filepath.Join(root, "environment", "android", "go", "bin"),
	}, path...)
	env["PATH"] = strings.Join(path, ":")
	return nil
}

// setArmEnv sets the environment variables used for android
// cross-compilation.
func setArmEnv(platform Platform, env map[string]string) error {
	root, err := VeyronRoot()
	if err != nil {
		return err
	}
	// Set Go specific environment variables.
	env["GOARCH"] = platform.Arch
	env["GOARM"] = strings.TrimPrefix(platform.SubArch, "v")
	env["GOOS"] = platform.OS

	// Add the paths to veyron cross-compilation tools to the PATH.
	path := getEnvTokens(env, "PATH", ":")
	path = append([]string{
		filepath.Join(root, "environment", "cout", "xgcc", "cross_arm"),
		filepath.Join(root, "environment", "go", "linux", "arm", "go", "bin"),
	}, path...)
	env["PATH"] = strings.Join(path, ":")
	return nil
}

// setGoPath adds the paths to veyron Go workspaces to the GOPATH
// variable.
func setGoPath(env map[string]string, root string, conf *Config) {
	gopath := getEnvTokens(env, "GOPATH", ":")
	// Append an entry to gopath for each veyron go repo.
	for _, repo := range conf.GoRepos {
		gopath = append(gopath, filepath.Join(root, repo, "go"))
	}
	env["GOPATH"] = strings.Join(gopath, ":")
}

// setVdlPath adds the paths to veyron Go workspaces to the VDLPATH
// variable.
func setVdlPath(env map[string]string, root string, conf *Config) {
	vdlpath := getEnvTokens(env, "VDLPATH", ":")
	// Append an entry to vdlpath for each veyron go repo.
	//
	// TODO(toddw): This logic will change when we pull vdl into a
	// separate repo.
	for _, repo := range conf.GoRepos {
		vdlpath = append(vdlpath, filepath.Join(root, repo, "go"))
	}
	env["VDLPATH"] = strings.Join(vdlpath, ":")
}

// setBluetoothCgoEnv sets the CGO_ENABLED variable and adds the
// bluetooth third-party C libraries veyron Go code depends on to the
// CGO_CFLAGS and CGO_LDFLAGS variables.
func setBluetoothCgoEnv(env map[string]string, root, arch string) error {
	// Set the CGO_* variables for the veyron proximity component.
	env["CGO_ENABLED"] = "1"
	libs := []string{
		"dbus-1.6.14",
		"expat-2.1.0",
		"bluez-4.101",
		"libusb-1.0.16-rc10",
		"libusb-compat-0.1.5",
	}
	cflags := getEnvTokens(env, "CGO_CFLAGS", " ")
	ldflags := getEnvTokens(env, "CGO_LDFLAGS", " ")
	for _, lib := range libs {
		dir := filepath.Join(root, "environment", "cout", lib, arch)
		if _, err := os.Stat(dir); err != nil {
			if !os.IsNotExist(err) {
				return fmt.Errorf("Stat(%v) failed: %v", dir, err)
			}
		} else {
			if lib == "dbus-1.6.14" {
				cflags = append(cflags, filepath.Join("-I"+dir, "include", "dbus-1.0", "dbus"))
			} else {
				cflags = append(cflags, filepath.Join("-I"+dir, "include"))
			}
			ldflags = append(ldflags, filepath.Join("-L"+dir, "lib"), "-Wl,-rpath", filepath.Join(dir, "lib"))
		}
	}
	env["CGO_CFLAGS"] = strings.Join(cflags, " ")
	env["CGO_LDFLAGS"] = strings.Join(ldflags, " ")
	return nil
}

// setJniCgoEnv sets the CGO_ENABLED variable and adds the JNI
// third-party C libraries veyron Go code depends on to the CGO_CFLAGS
// and CGO_LDFLAGS variables.
func setJniCgoEnv(env map[string]string, root, arch string) error {
	env["CGO_ENABLED"] = "1"
	cflags := getEnvTokens(env, "CGO_CFLAGS", " ")
	ldflags := getEnvTokens(env, "CGO_LDFLAGS", " ")
	dir := filepath.Join(root, "environment", "cout", "jni-wrapper-1.0", arch)
	if _, err := os.Stat(dir); err != nil {
		if !os.IsNotExist(err) {
			return fmt.Errorf("Stat(%v) failed: %v", dir, err)
		}
	} else {
		cflags = append(cflags, filepath.Join("-I"+dir, "include"))
		ldflags = append(ldflags, filepath.Join("-L"+dir, "lib"), "-Wl,-rpath", filepath.Join(dir, "lib"))
	}
	env["CGO_CFLAGS"] = strings.Join(cflags, " ")
	env["CGO_LDFLAGS"] = strings.Join(ldflags, " ")
	return nil
}

// setNaclEnv sets the environment variables used for nacl
// cross-compilation.
func setNaclEnv(platform Platform, env map[string]string) error {
	env["GOARCH"] = platform.Arch
	env["GOOS"] = platform.OS
	return nil
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"log"
	"time"

	"github.com/kidoman/embd"
	_ "github.com/kidoman/embd/host/all"
)

type LidarSensor struct {
	bus     embd.I2CBus
	address byte
}

func NewLidarSensor(i2cbus, addr byte) *LidarSensor {

	lSensor := LidarSensor{bus: embd.NewI2CBus(i2cbus), address: byte(addr)}
	if e := lSensor.bus.WriteByteToReg(lSensor.address, 0x00, 0x00); e != nil {
		log.Panic("Write ", e)
	}
	time.Sleep(300 * time.Millisecond)
	return &lSensor

}

func (ls *LidarSensor) Distance() {

	if v, e := ls.bus.ReadByteFromReg(ls.address, 0x01); e != nil {
		log.Panic("ReadByteFromReg ", e)
	} else {
		fmt.Printf("status: %.8b\n", v)
	}

	for {
		if e := ls.bus.WriteByteToReg(ls.address, 0x00, 0x04); e != nil {
			log.Panic("Write ", e)
		}

		time.Sleep(100 * time.Millisecond)

		if v, e := ls.bus.ReadByteFromReg(ls.address, 0x01); e != nil {
			log.Panic("ReadByteFromReg ", e)
		} else {
			fmt.Printf("status: %.8b\n", v)
		}

		// TODO: error check
		v1, _ := ls.bus.ReadByteFromReg(ls.address, 0x10)
		v2, _ := ls.bus.ReadByteFromReg(ls.address, 0x0f)

		fmt.Println((int(v2) << 8) + int(v1))

		time.Sleep(1 * time.Second)
	}
}

func main() {
	log.SetFlags(log.Lshortfile)
	lidarSensor := NewLidarSensor(1, 0x62)
	lidarSensor.Distance()
	defer lidarSensor.bus.Close()
}
<commit_msg>Add a flag in Distance func for taking val with stabilization or without<commit_after>package main

import (
	"fmt"
	"log"
	"time"

	"github.com/kidoman/embd"
	_ "github.com/kidoman/embd/host/all"
)

type Lidar struct {
	bus     embd.I2CBus
	address byte
}

//Ready - Ready status. 0 - ready for a new command, 1 - busy
//          with acquisition
//RefOverflow - Overflow detected in correlation process assotieted with
//          a reference acquisition
//SignalOverflow - Overflow detected in correlation process assotieted with a signal
//          acquisition
//SignalNotValid - Indicates that the signal correlation peak is equal to or below
//          correlation record threshold
//SecondaryReturn - Secondary return detected above correlation noise floor threshold
//Health - 1 if is good, 0 if is bad
//ErrorDetection - Process error detected/measurement invalid
//EyeSafe - This bit will go high if eye-safety protection has been activated
const (
	Ready           = 1 << iota
	RefOverflow     = 1 << iota
	SigOverflow     = 1 << iota
	SignalNotValid  = 1 << iota
	SecondatyReturn = 1 << iota
	Health          = 1 << iota
	ErrorDetection  = 1 << iota
	EyeSafe         = 1 << iota
)

func NewLidar(i2cbus, addr byte) *Lidar {

	lSensor := Lidar{bus: embd.NewI2CBus(i2cbus), address: byte(addr)}
	if e := lSensor.bus.WriteByteToReg(lSensor.address, 0x00, 0x00); e != nil {
		log.Panic("Write ", e)
	}
	time.Sleep(300 * time.Millisecond)
	return &lSensor

}

func (ls *Lidar) GetStatus() (byte, error) {

	if val, err := ls.bus.ReadByteFromReg(ls.address, 0x01); err == nil {
		fmt.Printf("status: %.8b\n", val)
		return val, nil
	} else {
		log.Panic("GetStatus ", err)
		return 0, err
	}

}

//stablizePreampFlag - true - take aquisition with DC stabilisation/correction.
//false - it will read faster, but you will need to stabilize DC every once in
//awhile(ex. 1 out of every 100 readings is typically good)
func (ls *Lidar) Distance(stablizePreampFlag bool) {

	ls.GetStatus()

	for {
		if stablizePreampFlag {
			if e := ls.bus.WriteByteToReg(ls.address, 0x00, 0x04); e != nil {
				log.Panic("Write ", e)
			}
		} else if e := ls.bus.WriteByteToReg(ls.address, 0x00, 0x03); e != nil {
			log.Panic("Write ", e)
		}

		time.Sleep(100 * time.Millisecond)

		ls.GetStatus()
		// TODO: error check
		v1, _ := ls.bus.ReadByteFromReg(ls.address, 0x10)
		v2, _ := ls.bus.ReadByteFromReg(ls.address, 0x0f)

		fmt.Println((int(v2) << 8) + int(v1))

		time.Sleep(1 * time.Second)
	}
}

func main() {
	log.SetFlags(log.Lshortfile)
	lidar := NewLidar(1, 0x62)
	lidar.Distance(true)
	defer lidar.bus.Close()
}
<|endoftext|>


<commit_before>package commands

import (
	"bitbucket.org/ww/cabinet"
	"bytes"
	"fmt"
	"github.com/brettlangdon/ferrite/common"
	"github.com/brettlangdon/ferrite/proxy"
	"net/url"
	"os"
	"strconv"
	"strings"
	"sync/atomic"
	"time"
)

func ParseCommand(request string) (string, []string) {
	request = strings.ToLower(request)
	tokens := strings.Split(strings.Trim(request, " \r\n"), " ")
	var command string
	if len(tokens) > 0 {
		command = tokens[0]
	}
	return command, tokens
}

func HandleCommand(command string, tokens []string, db *cabinet.KCDB) string {
	var result string
	switch command {
	case "get":
		result = HandleGet(tokens[1:], db)
		break
	case "stats":
		result = HandleStats(db)
		break
	case "flush_all":
		result = HandleFlushAll(db)
		break
	case "delete":
		result = HandleDelete(tokens[1:], db)
		break
	case "version":
		result = HandleVersion()
		break
	default:
		result = "ERROR\r\n"
		break
	}

	return result
}

func HandleGet(tokens []string, db *cabinet.KCDB) string {
	var out string
	if len(tokens) >= 1 {
		key := tokens[0]
		if strings.HasPrefix(key, "http%3a%2f%2f") || strings.HasPrefix(key, "https%3a%2f%2f") {
			key, _ = url.QueryUnescape(key)
		}
		result, err := db.Get([]byte(key))
		if err != nil {
			atomic.AddInt32(&common.MISSES, 1)
			err := db.Set([]byte(key), []byte("0"))
			if err == nil {
				go proxy.CallProxy(key, db)
			} else {
				fmt.Fprintf(os.Stderr, "Error Setting Key \"%s\" Value \"0\": $s\r\n", key, err.Error())
			}
			out = "END\r\n"
		} else if bytes.Equal(result, []byte("0")) {
			atomic.AddInt32(&common.MISSES, 1)
			out = "END\r\n"
		} else {
			parts := strings.SplitN(string(result), ":", 2)
			var expire int64 = 0
			if len(parts) == 2 {
				expire, err = strconv.ParseInt(parts[0], 10, 32)
				if err != nil {
					expire = 0
					fmt.Fprintf(os.Stderr, "Error Parsing Expiration \"%s\": %s\r\n", parts[0], err.Error())
				}
				result = []byte(parts[1])
			}
			now := int64(time.Now().Unix())
			if expire > 0 && expire < now {
				err := db.Set([]byte(key), []byte("0"))
				if err == nil {
					go proxy.CallProxy(key, db)
				} else {
					fmt.Fprintf(os.Stderr, "Error Setting Key \"%s\" Value \"0\": $s\r\n", key, err.Error())
				}
				atomic.AddInt32(&common.MISSES, 1)
				out = "END\r\n"
			} else {
				atomic.AddInt32(&common.HITS, 1)
				out = fmt.Sprintf("VALUE %s 0 %d\r\n%s\r\nEND\r\n", key, len(result), result)
			}
		}
	} else {
		out = "ERROR\r\n"
	}
	return out
}

func HandleStats(db *cabinet.KCDB) string {
	hits := atomic.LoadInt32(&common.HITS)
	misses := atomic.LoadInt32(&common.MISSES)
	connections := atomic.LoadInt32(&common.CONNECTIONS)
	var hit_ratio float32 = 0
	if hits > 0 {
		hit_ratio = float32(hits) / float32(hits+misses)
	}

	db_stats := ""
	status, err := db.Status()
	if err == nil {
		lines := strings.Split(status, "\n")
		for _, line := range lines {
			parts := strings.SplitN(strings.Trim(line, "\r\n"), "\t", 2)
			if len(parts) == 2 {
				db_stats += fmt.Sprintf("STAT %s %s\r\n", parts[0], parts[1])
			}
		}
	} else {
		fmt.Fprintf(os.Stderr, "Error Retrieving DB Status: %s\r\n", err.Error())
	}
	return fmt.Sprintf("STAT hits %d\r\n"+
		"STAT misses %d\r\n"+
		"STAT hit_ratio %1.4f\r\n"+
		"STAT connections %d\r\n"+
		"%s"+
		"END\r\n",
		hits, misses, hit_ratio, connections, db_stats,
	)
}

func HandleFlushAll(db *cabinet.KCDB) string {
	db.Clear()
	return "OK\r\n"
}

func HandleDelete(tokens []string, db *cabinet.KCDB) string {
	if len(tokens) <= 0 {
		return "ERROR\r\n"
	}

	key := tokens[0]
	err := db.Remove([]byte(key))
	if err != nil {
		return "NOT_FOUND\r\n"
	}
	return "DELETED\r\n"
}

func HandleVersion() string {
	return fmt.Sprintf("VERSION %s\r\n", common.VERSION)
}
<commit_msg>add help command<commit_after>package commands

import (
	"bitbucket.org/ww/cabinet"
	"bytes"
	"fmt"
	"github.com/brettlangdon/ferrite/common"
	"github.com/brettlangdon/ferrite/proxy"
	"net/url"
	"os"
	"strconv"
	"strings"
	"sync/atomic"
	"time"
)

func ParseCommand(request string) (string, []string) {
	request = strings.ToLower(request)
	tokens := strings.Split(strings.Trim(request, " \r\n"), " ")
	var command string
	if len(tokens) > 0 {
		command = tokens[0]
	}
	return command, tokens
}

func HandleCommand(command string, tokens []string, db *cabinet.KCDB) string {
	var result string
	switch command {
	case "get":
		result = HandleGet(tokens[1:], db)
		break
	case "stats":
		result = HandleStats(db)
		break
	case "flush_all":
		result = HandleFlushAll(db)
		break
	case "delete":
		result = HandleDelete(tokens[1:], db)
		break
	case "version":
		result = HandleVersion()
		break
	case "help":
		result = HandleHelp()
		break
	default:
		result = "ERROR\r\n"
		break
	}

	return result
}

func HandleGet(tokens []string, db *cabinet.KCDB) string {
	var out string
	if len(tokens) >= 1 {
		key := tokens[0]
		if strings.HasPrefix(key, "http%3a%2f%2f") || strings.HasPrefix(key, "https%3a%2f%2f") {
			key, _ = url.QueryUnescape(key)
		}
		result, err := db.Get([]byte(key))
		if err != nil {
			atomic.AddInt32(&common.MISSES, 1)
			err := db.Set([]byte(key), []byte("0"))
			if err == nil {
				go proxy.CallProxy(key, db)
			} else {
				fmt.Fprintf(os.Stderr, "Error Setting Key \"%s\" Value \"0\": $s\r\n", key, err.Error())
			}
			out = "END\r\n"
		} else if bytes.Equal(result, []byte("0")) {
			atomic.AddInt32(&common.MISSES, 1)
			out = "END\r\n"
		} else {
			parts := strings.SplitN(string(result), ":", 2)
			var expire int64 = 0
			if len(parts) == 2 {
				expire, err = strconv.ParseInt(parts[0], 10, 32)
				if err != nil {
					expire = 0
					fmt.Fprintf(os.Stderr, "Error Parsing Expiration \"%s\": %s\r\n", parts[0], err.Error())
				}
				result = []byte(parts[1])
			}
			now := int64(time.Now().Unix())
			if expire > 0 && expire < now {
				err := db.Set([]byte(key), []byte("0"))
				if err == nil {
					go proxy.CallProxy(key, db)
				} else {
					fmt.Fprintf(os.Stderr, "Error Setting Key \"%s\" Value \"0\": $s\r\n", key, err.Error())
				}
				atomic.AddInt32(&common.MISSES, 1)
				out = "END\r\n"
			} else {
				atomic.AddInt32(&common.HITS, 1)
				out = fmt.Sprintf("VALUE %s 0 %d\r\n%s\r\nEND\r\n", key, len(result), result)
			}
		}
	} else {
		out = "ERROR\r\n"
	}
	return out
}

func HandleStats(db *cabinet.KCDB) string {
	hits := atomic.LoadInt32(&common.HITS)
	misses := atomic.LoadInt32(&common.MISSES)
	connections := atomic.LoadInt32(&common.CONNECTIONS)
	var hit_ratio float32 = 0
	if hits > 0 {
		hit_ratio = float32(hits) / float32(hits+misses)
	}

	db_stats := ""
	status, err := db.Status()
	if err == nil {
		lines := strings.Split(status, "\n")
		for _, line := range lines {
			parts := strings.SplitN(strings.Trim(line, "\r\n"), "\t", 2)
			if len(parts) == 2 {
				db_stats += fmt.Sprintf("STAT %s %s\r\n", parts[0], parts[1])
			}
		}
	} else {
		fmt.Fprintf(os.Stderr, "Error Retrieving DB Status: %s\r\n", err.Error())
	}
	return fmt.Sprintf("STAT hits %d\r\n"+
		"STAT misses %d\r\n"+
		"STAT hit_ratio %1.4f\r\n"+
		"STAT connections %d\r\n"+
		"%s"+
		"END\r\n",
		hits, misses, hit_ratio, connections, db_stats,
	)
}

func HandleFlushAll(db *cabinet.KCDB) string {
	db.Clear()
	return "OK\r\n"
}

func HandleDelete(tokens []string, db *cabinet.KCDB) string {
	if len(tokens) <= 0 {
		return "ERROR\r\n"
	}

	key := tokens[0]
	err := db.Remove([]byte(key))
	if err != nil {
		return "NOT_FOUND\r\n"
	}
	return "DELETED\r\n"
}

func HandleVersion() string {
	return fmt.Sprintf("VERSION %s\r\n", common.VERSION)
}

func HandleHelp() string {
	return "COMMAND GET <KEY> - retrieve <KEY> from the cache\r\n" +
		"COMMAND FLUSH_ALL - remove all records from the cache\r\n" +
		"COMMAND DELETE <KEY> - remove <KEY> from the cache\r\n" +
		"COMMAND VERSION - display the application version\r\n" +
		"COMMAND STATS - display application statistics\r\n" +
		"COMMAND QUIT - close the connection\r\n" +
		"COMMAND EXIT - same as QUIT\r\n" +
		"COMMAND HELP - display this information\r\n" +
		"END\r\n"
}
<|endoftext|>