<commit_before>//部分代码参考(Ctrl+c)自golang bufio
package util

import (
	"errors"
	"io"
	"unicode/utf8"
)

const (
	defaultBufSize = 4096
)

type Reader struct {
	buf  []byte
	rd   io.Reader
	r, w int
	err  error
}

const minReadBufferSize = 16
const maxConsecutiveEmptyReads = 100

func NewReaderSize(rd io.Reader, size int) *Reader {
	b, ok := rd.(*Reader)
	if ok && len(b.buf) >= size {
		return b
	}
	if size < minReadBufferSize {
		size = minReadBufferSize
	}
	r := new(Reader)
	r.reset(make([]byte, size), rd)
	return r
}

// NewReader returns a new Reader whose buffer has the default size.
func NewReader(rd io.Reader) *Reader {
	return NewReaderSize(rd, defaultBufSize)
}

func (b *Reader) reset(buf []byte, r io.Reader) {
	*b = Reader{
		buf: buf,
		rd:  r,
	}
}

func (b *Reader) Read(buf []byte) (count int, err error) {
	return
}

var errNegativeRead = errors.New("bufio: reader returned negative count from Read")

// fill reads a new chunk into the buffer.
func (b *Reader) fill() {
	// Slide existing data to beginning.
	if b.r > 0 {
		copy(b.buf, b.buf[b.r:b.w])
		b.w -= b.r
		b.r = 0
	}

	if b.w >= len(b.buf) {
		panic("bufio: tried to fill full buffer")
	}

	// Read new data: try a limited number of times.
	for i := maxConsecutiveEmptyReads; i > 0; i-- {
		n, err := b.rd.Read(b.buf[b.w:])
		if n < 0 {
			panic(errNegativeRead)
		}
		b.w += n
		if err != nil {
			b.err = err
			return
		}
		if n > 0 {
			return
		}
	}
	b.err = io.ErrNoProgress
}

func (b *Reader) readErr() error {
	err := b.err
	b.err = nil
	return err
}

func (b *Reader) Buffered() int { return b.w - b.r }

func (b *Reader) ReadData() (line string, err error) {
	if n := b.Buffered(); n < len(b.buf) {
		b.fill()
	}
	if b.err != nil {
		line = string(b.buf[b.r:b.w])
		b.r = b.w
		err = b.readErr()
		return
	}
	index := b.w
	for {
		r, _ := utf8.DecodeLastRune(b.buf[b.r:index])
		if r == utf8.RuneError {
			index = index - 1
			if index < b.r {
				break
			}
		} else {
			break
		}
	}
	if index > b.r {
		line = string(b.buf[b.r : b.r+index])
		b.r += index
	} else {
		panic("known error")
	}
	return
}
<commit_msg>Update utf8reader.go<commit_after>//部分代码参考(Ctrl+c)自golang bufio
package util

import (
	"errors"
	"io"
	"unicode/utf8"
)

const (
	defaultBufSize = 4096
)

type Reader struct {
	buf  []byte
	rd   io.Reader
	r, w int
	err  error
}

const minReadBufferSize = 16
const maxConsecutiveEmptyReads = 100

func NewReaderSize(rd io.Reader, size int) *Reader {
	b, ok := rd.(*Reader)
	if ok && len(b.buf) >= size {
		return b
	}
	if size < minReadBufferSize {
		size = minReadBufferSize
	}
	r := new(Reader)
	r.reset(make([]byte, size), rd)
	return r
}

// NewReader returns a new Reader whose buffer has the default size.
func NewReader(rd io.Reader) *Reader {
	return NewReaderSize(rd, defaultBufSize)
}

func (b *Reader) reset(buf []byte, r io.Reader) {
	*b = Reader{
		buf: buf,
		rd:  r,
	}
}

func (b *Reader) Read(buf []byte) (count int, err error) {
	return
}

var errNegativeRead = errors.New("bufio: reader returned negative count from Read")

// fill reads a new chunk into the buffer.
func (b *Reader) fill() {
	// Slide existing data to beginning.
	if b.r > 0 {
		copy(b.buf, b.buf[b.r:b.w])
		b.w -= b.r
		b.r = 0
	}

	if b.w >= len(b.buf) {
		panic("bufio: tried to fill full buffer")
	}

	// Read new data: try a limited number of times.
	for i := maxConsecutiveEmptyReads; i > 0; i-- {
		n, err := b.rd.Read(b.buf[b.w:])
		if n < 0 {
			panic(errNegativeRead)
		}
		b.w += n
		if err != nil {
			b.err = err
			return
		}
		if n > 0 {
			return
		}
	}
	b.err = io.ErrNoProgress
}

func (b *Reader) readErr() error {
	err := b.err
	b.err = nil
	return err
}

func (b *Reader) Buffered() int { return b.w - b.r }

func (b *Reader) ReadData() (line string, err error) {
	if n := b.Buffered(); n < len(b.buf) {
		b.fill()
	}
	if b.err != nil {
		line = string(b.buf[b.r:b.w])
		b.r = b.w
		err = b.readErr()
		return
	}
	index := b.w
	for {
		r, _ := utf8.DecodeLastRune(b.buf[b.r:index])
		if r == utf8.RuneError {
			index = index - 1
			if index < b.r {
				break
			}
		} else {
			break
		}
	}
	if index > b.r {
		line = string(b.buf[b.r : b.r+index])
		b.r += index
	} else {
		panic("no utf8 char found")
	}
	return
}
<|endoftext|>


<commit_before>package main

import (
	"bufio"
	"flag"
	"fmt"
	"log"
	"os"

	"github.com/kward/tabulate/go/tabulate"
)

const (
	defaultIFS    = " "
	defaultOFS    = " "
	defaultRender = "plain"
)

var (
	columns = flag.Int("cols", 0, "Number of columns; 0=all.")

	ifs, ofs string
	render   string

	comment  = flag.String("comment_prefix", "#", "Comment prefix.")
	comments = flag.Bool("comments", true, "Ignore comments.")
)

func flagInit(renderers []tabulate.Renderer) {
	// Flag initialization.
	flag.StringVar(&ifs, "I", defaultIFS, "Input field separator.")
	flag.StringVar(&ofs, "O", defaultOFS, "Output field separator.")
	flag.StringVar(&render, "r", defaultRender, "Output renderer.")

	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
		flag.PrintDefaults()

		fmt.Fprintln(os.Stderr, "Supported renderers:")
		for _, r := range renderers {
			fmt.Fprintf(os.Stderr, "  %v\n", r.Type())
		}
	}

	flag.Parse()

	// Flag validation.
	if *columns < 0 {
		log.Fatalf("invalid number of columns: %v", *columns)
	}
}

func read(fh *os.File, data *[]string) error {
	s := bufio.NewScanner(fh)
	for s.Scan() {
		*data = append(*data, s.Text())
	}
	if err := s.Err(); err != nil {
		return fmt.Errorf("ERROR Reading file: %v", err)
	}
	return nil
}

func main() {
	var (
		err  error
		data []string
	)

	flagInit(tabulate.Renderers)

	rmap := map[string]tabulate.Renderer{}
	for _, r := range tabulate.Renderers {
		rmap[r.Type()] = r
	}

	// Open file.
	fh := os.Stdin
	if len(flag.Args()) > 0 {
		fh, err = os.Open(flag.Arg(0))
		if err != nil {
			log.Fatal(err)
		}
		defer fh.Close()
	}

	// Read file.
	err = read(fh, &data)
	if err != nil {
		log.Fatal(err)
	}

	// Parse file.
	t := tabulate.NewTable(tabulate.NewTableConfig())
	t.Split(data, ifs, *columns)

	// Render file.
	renderer, ok := rmap[render]
	if !ok {
		log.Fatalf("Invalid --render flag value %v.", render)
	}
	switch renderer.(type) {
	case *tabulate.PlainRenderer:
		renderer.(*tabulate.PlainRenderer).OFS = ofs
	}
	fmt.Print(renderer.Render(&t))
}
<commit_msg>Moved go library from go/tabulate to tabulate.<commit_after>package main

import (
	"bufio"
	"flag"
	"fmt"
	"log"
	"os"

	"github.com/kward/tabulate/tabulate"
)

const (
	defaultIFS    = " "
	defaultOFS    = " "
	defaultRender = "plain"
)

var (
	columns = flag.Int("cols", 0, "Number of columns; 0=all.")

	ifs, ofs string
	render   string

	comment  = flag.String("comment_prefix", "#", "Comment prefix.")
	comments = flag.Bool("comments", true, "Ignore comments.")
)

func flagInit(renderers []tabulate.Renderer) {
	// Flag initialization.
	flag.StringVar(&ifs, "I", defaultIFS, "Input field separator.")
	flag.StringVar(&ofs, "O", defaultOFS, "Output field separator.")
	flag.StringVar(&render, "r", defaultRender, "Output renderer.")

	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
		flag.PrintDefaults()

		fmt.Fprintln(os.Stderr, "Supported renderers:")
		for _, r := range renderers {
			fmt.Fprintf(os.Stderr, "  %v\n", r.Type())
		}
	}

	flag.Parse()

	// Flag validation.
	if *columns < 0 {
		log.Fatalf("invalid number of columns: %v", *columns)
	}
}

func read(fh *os.File, data *[]string) error {
	s := bufio.NewScanner(fh)
	for s.Scan() {
		*data = append(*data, s.Text())
	}
	if err := s.Err(); err != nil {
		return fmt.Errorf("ERROR Reading file: %v", err)
	}
	return nil
}

func main() {
	var (
		err  error
		data []string
	)

	flagInit(tabulate.Renderers)

	rmap := map[string]tabulate.Renderer{}
	for _, r := range tabulate.Renderers {
		rmap[r.Type()] = r
	}

	// Open file.
	fh := os.Stdin
	if len(flag.Args()) > 0 {
		fh, err = os.Open(flag.Arg(0))
		if err != nil {
			log.Fatal(err)
		}
		defer fh.Close()
	}

	// Read file.
	err = read(fh, &data)
	if err != nil {
		log.Fatal(err)
	}

	// Parse file.
	t := tabulate.NewTable(tabulate.NewTableConfig())
	t.Split(data, ifs, *columns)

	// Render file.
	renderer, ok := rmap[render]
	if !ok {
		log.Fatalf("Invalid --render flag value %v.", render)
	}
	switch renderer.(type) {
	case *tabulate.PlainRenderer:
		renderer.(*tabulate.PlainRenderer).OFS = ofs
	}
	fmt.Print(renderer.Render(&t))
}
<|endoftext|>


<commit_before>package main

import (
	"./interface/ihttp"
	"./network"
	proto "code.google.com/p/goprotobuf/proto"
	"flag"
	"fmt"
)

var serverHello = &network.ServerHello{
	CodeName:      proto.String("BX"),
	VersionNumber: proto.String("0.0 pre-alpha"),
}

var about = flag.Bool("about", false, "shows server information")
var ip = flag.String("ip", "0.0.0.0", "the ip address to listen on")
var port = flag.Int("port", 6170, "the UDP port to listen on")

var httpOn = flag.Bool("http", false, "runs the http interface")
var httpListen = flag.String("httpL", "localhost:8088", "the address and port of http interface")

func main() {
	flag.Parse()
	if *about {
		fmt.Println(proto.MarshalTextString(serverHello))
	}
	if *httpOn {
		fmt.Printf("starting server: %v\n", *httpListen)
		ihttp.StartServer(*httpListen)
	}
}
<commit_msg>changed port for consistency<commit_after>package main

import (
	"./interface/ihttp"
	"./network"
	proto "code.google.com/p/goprotobuf/proto"
	"flag"
	"fmt"
)

var serverHello = &network.ServerHello{
	CodeName:      proto.String("BX"),
	VersionNumber: proto.String("0.0 pre-alpha"),
}

var about = flag.Bool("about", false, "shows server information")
var ip = flag.String("ip", "0.0.0.0", "the ip address to listen on")
var port = flag.Int("port", 6170, "the UDP port to listen on")

var httpOn = flag.Bool("http", false, "runs the http interface")
var httpListen = flag.String("httpL", "localhost:6178", "the address and port of http interface")

func main() {
	flag.Parse()
	if *about {
		fmt.Println(proto.MarshalTextString(serverHello))
	}
	if *httpOn {
		fmt.Printf("starting server: %v\n", *httpListen)
		ihttp.StartServer(*httpListen)
	}
}
<|endoftext|>


<commit_before>// Copyright 2011 Alexander Orlov <alexander.orlov@loxal.net>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"fmt"
	"http"
	"io"
	"os"
	"strconv"
	"template"
	"time"

	"appengine"
	"appengine/datastore"
	"appengine/memcache"
	"appengine/user"
)

type Greeting struct {
	Author  string
	Content string
	Date    datastore.Time

	Title	string
	Body	string
}

func hello(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "text/plain; charset=utf-8")
	fmt.Fprint(w, "Hello, ...!\n")
}

func serveError(c appengine.Context, w http.ResponseWriter, err os.Error) {
	w.WriteHeader(http.StatusInternalServerError)
	w.Header().Set("Content-Type", "text/plain")
	io.WriteString(w, "Internal Server Error")
	c.Logf("%v", err)
}

func serve404(w http.ResponseWriter) {
	w.WriteHeader(http.StatusNotFound)
	w.Header().Set("Content-Type", "text/plain")
	io.WriteString(w, "Not Found")
}

func count(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)

	item, err := memcache.Get(c, r.URL.Path)
	if err != nil && err != memcache.ErrCacheMiss {
		serveError(c, w, err)
		return
	}
	n := 0
	if err == nil {
		n, err = strconv.Atoi(string(item.Value))
		if err != nil {
			serveError(c, w, err)
			return
		}
	}
	n++
	item = &memcache.Item{
		Key:   r.URL.Path,
		Value: []byte(strconv.Itoa(n)),
	}
	err = memcache.Set(c, item)
	if err != nil {
		serveError(c, w, err)
		return
	}

	w.Header().Set("Content-Type", "text/plain")
	fmt.Fprintf(w, "%q has been visited %d times", r.URL.Path, n)
}

func handlePost(w http.ResponseWriter, r *http.Request) {
	if r.Method != "GET" || r.URL.Path != postHandler {
		serve404(w)
		return
	}
	c := appengine.NewContext(r)
	q := datastore.NewQuery("Greeting").Order("-Date").Limit(10)
	var gg []*Greeting
	_, err := q.GetAll(c, &gg)
	if err != nil {
		serveError(c, w, err)
		return
	}

    for i := 0; i < len(gg); i++ {
    //     gg[i]= &Greeting{Title: "my TITLE", Body: "my BODY"}
    }

	w.Header().Set("Content-Type", "text/html")
	if err := mainPage.Execute(w, gg); err != nil {
		c.Logf("%v", err)
	}
}

func handleStore(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		serve404(w)
		return
	}
	c := appengine.NewContext(r)
	if err := r.ParseForm(); err != nil {
		serveError(c, w, err)
		return
	}
	g := &Greeting{
		Content: r.FormValue("content"),
		Date:    datastore.SecondsToTime(time.Seconds()),
	}
	if u := user.Current(c); u != nil {
		g.Author = u.String()
	}
	if _, err := datastore.Put(c, datastore.NewIncompleteKey("Greeting"), g); err != nil {
		serveError(c, w, err)
		return
	}
	http.Redirect(w, r, postHandler, http.StatusFound)
}

func cmd(w http.ResponseWriter, r *http.Request) {
//    c := appengine.NewContext(r)
//    c.Logf("r.URL.Path: " + r.URL.Path)
//    c.Logf("r.FormValue(\"foo\"): " + r.FormValue("foo"))
//    c.Logf(r.FormValue("bar"))
//    c.Logf("r.URL.RawQuery: " + r.URL.RawQuery)
//
//     c.Logf("m[r.URL.RawQuery]" + m[r.URL.RawQuery])
//    http.Redirect(w, r, m[r.URL.RawQuery], http.StatusFound)
}

func WebCmd(cmd string) string {
     m := map[string]string {
        "c":"https://mail.google.com/mail/?shva=1#compose",
        "t":"http://twitter.com",
        "sem":"https://github.com/loxal/Sem",
     }

    return m[cmd]
}

var postHandler = "/post"
var storeHandler = "/store"
var mainPage = template.MustParseFile("template.html", nil)

func Double(i int) int {
	return i * 2
}

func init() {
	http.HandleFunc("/", hello)
	http.HandleFunc(postHandler, handlePost)
	http.HandleFunc(storeHandler, handleStore)
	http.HandleFunc("/hello", hello)
	http.HandleFunc("/count", count)
	http.HandleFunc("/cmd", cmd)
}

<commit_msg>+ named return parameters in WebCmd<commit_after>// Copyright 2011 Alexander Orlov <alexander.orlov@loxal.net>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"fmt"
	"http"
	"io"
	"os"
	"strconv"
	"template"
	"time"

	"appengine"
	"appengine/datastore"
	"appengine/memcache"
	"appengine/user"
)

type Greeting struct {
	Author  string
	Content string
	Date    datastore.Time

	Title	string
	Body	string
}

func hello(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "text/plain; charset=utf-8")
	fmt.Fprint(w, "Hello, ...!\n")
}

func serveError(c appengine.Context, w http.ResponseWriter, err os.Error) {
	w.WriteHeader(http.StatusInternalServerError)
	w.Header().Set("Content-Type", "text/plain")
	io.WriteString(w, "Internal Server Error")
	c.Logf("%v", err)
}

func serve404(w http.ResponseWriter) {
	w.WriteHeader(http.StatusNotFound)
	w.Header().Set("Content-Type", "text/plain")
	io.WriteString(w, "Not Found")
}

func count(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)

	item, err := memcache.Get(c, r.URL.Path)
	if err != nil && err != memcache.ErrCacheMiss {
		serveError(c, w, err)
		return
	}
	n := 0
	if err == nil {
		n, err = strconv.Atoi(string(item.Value))
		if err != nil {
			serveError(c, w, err)
			return
		}
	}
	n++
	item = &memcache.Item{
		Key:   r.URL.Path,
		Value: []byte(strconv.Itoa(n)),
	}
	err = memcache.Set(c, item)
	if err != nil {
		serveError(c, w, err)
		return
	}

	w.Header().Set("Content-Type", "text/plain")
	fmt.Fprintf(w, "%q has been visited %d times", r.URL.Path, n)
}

func handlePost(w http.ResponseWriter, r *http.Request) {
	if r.Method != "GET" || r.URL.Path != postHandler {
		serve404(w)
		return
	}
	c := appengine.NewContext(r)
	q := datastore.NewQuery("Greeting").Order("-Date").Limit(10)
	var gg []*Greeting
	_, err := q.GetAll(c, &gg)
	if err != nil {
		serveError(c, w, err)
		return
	}

    for i := 0; i < len(gg); i++ {
    //     gg[i]= &Greeting{Title: "my TITLE", Body: "my BODY"}
    }

	w.Header().Set("Content-Type", "text/html")
	if err := mainPage.Execute(w, gg); err != nil {
		c.Logf("%v", err)
	}
}

func handleStore(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		serve404(w)
		return
	}
	c := appengine.NewContext(r)
	if err := r.ParseForm(); err != nil {
		serveError(c, w, err)
		return
	}
	g := &Greeting{
		Content: r.FormValue("content"),
		Date:    datastore.SecondsToTime(time.Seconds()),
	}
	if u := user.Current(c); u != nil {
		g.Author = u.String()
	}
	if _, err := datastore.Put(c, datastore.NewIncompleteKey("Greeting"), g); err != nil {
		serveError(c, w, err)
		return
	}
	http.Redirect(w, r, postHandler, http.StatusFound)
}

func cmd(w http.ResponseWriter, r *http.Request) {
//    c := appengine.NewContext(r)
//    c.Logf("r.URL.Path: " + r.URL.Path)
//    c.Logf("r.FormValue(\"foo\"): " + r.FormValue("foo"))
//    c.Logf(r.FormValue("bar"))
//    c.Logf("r.URL.RawQuery: " + r.URL.RawQuery)
//
//     c.Logf("m[r.URL.RawQuery]" + m[r.URL.RawQuery])
//    http.Redirect(w, r, m[r.URL.RawQuery], http.StatusFound)
}

func WebCmd(cmd string) (restCall string) {
     m := map[string]string {
        "c":"https://mail.google.com/mail/?shva=1#compose",
        "t":"http://twitter.com",
        "sem":"https://github.com/loxal/Sem",
        // shortcut for making notes/tasks/todos
     }

     restCall = m[cmd]
//    return m[cmd]
    return
}

var postHandler = "/post"
var storeHandler = "/store"
var mainPage = template.MustParseFile("template.html", nil)

func Double(i int) int {
	return i * 2
}

func init() {
	http.HandleFunc("/", hello)
	http.HandleFunc(postHandler, handlePost)
	http.HandleFunc(storeHandler, handleStore)
	http.HandleFunc("/hello", hello)
	http.HandleFunc("/count", count)
	http.HandleFunc("/cmd", cmd)
}

<|endoftext|>


<commit_before>// Copyright 2015 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License 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 main

import (
	"flag"
	"fmt"
	"os"
	"time"

	"github.com/coreos/ignition/src/exec"
	"github.com/coreos/ignition/src/exec/stages"
	_ "github.com/coreos/ignition/src/exec/stages/disks"
	_ "github.com/coreos/ignition/src/exec/stages/files"
	"github.com/coreos/ignition/src/log"
	"github.com/coreos/ignition/src/oem"
	"github.com/coreos/ignition/src/providers"
	_ "github.com/coreos/ignition/src/providers/cmdline"
	_ "github.com/coreos/ignition/src/providers/ec2"
	_ "github.com/coreos/ignition/src/providers/file"
	_ "github.com/coreos/ignition/src/providers/noop"

	"github.com/coreos/ignition/third_party/github.com/coreos/go-semver/semver"
)

const versionString = "0.1.0+git"

var version = *semver.Must(semver.NewVersion(versionString))

func main() {
	flags := struct {
		clearCache   bool
		configCache  string
		fetchTimeout time.Duration
		oem          oem.Name
		providers    providers.List
		root         string
		stage        stages.Name
		version      bool
	}{}

	flag.BoolVar(&flags.clearCache, "clear-cache", false, "clear any cached config")
	flag.StringVar(&flags.configCache, "config-cache", "/tmp/ignition.json", "where to cache the config")
	flag.DurationVar(&flags.fetchTimeout, "fetchtimeout", exec.DefaultFetchTimeout, "")
	flag.Var(&flags.oem, "oem", fmt.Sprintf("current oem. %v", oem.Names()))
	flag.Var(&flags.providers, "provider", fmt.Sprintf("provider of config. can be specified multiple times. %v", providers.Names()))
	flag.StringVar(&flags.root, "root", "/", "root of the filesystem")
	flag.Var(&flags.stage, "stage", fmt.Sprintf("execution stage. %v", stages.Names()))
	flag.BoolVar(&flags.version, "version", false, "print the version and exit")

	flag.Parse()

	if config, ok := oem.Get(flags.oem.String()); ok {
		for k, v := range config.Flags() {
			flag.Set(k, v)
		}
	}

	if flags.version {
		fmt.Printf("ignition %s\n", versionString)
		return
	}

	if flags.stage == "" {
		fmt.Fprint(os.Stderr, "'--stage' must be provided\n")
		os.Exit(2)
	}

	logger := log.New()
	defer logger.Close()

	if flags.clearCache {
		if err := os.Remove(flags.configCache); err != nil {
			logger.Err("unable to clear cache: %v", err)
		}
	}

	engine := exec.Engine{
		Root:         flags.root,
		FetchTimeout: flags.fetchTimeout,
		Logger:       logger,
		ConfigCache:  flags.configCache,
	}.Init()
	for _, name := range flags.providers {
		engine.AddProvider(providers.Get(name).Create(logger))
	}

	if !engine.Run(flags.stage.String()) {
		os.Exit(1)
	}
}
<commit_msg>flags: panic on bad OEM flag<commit_after>// Copyright 2015 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License 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 main

import (
	"flag"
	"fmt"
	"os"
	"time"

	"github.com/coreos/ignition/src/exec"
	"github.com/coreos/ignition/src/exec/stages"
	_ "github.com/coreos/ignition/src/exec/stages/disks"
	_ "github.com/coreos/ignition/src/exec/stages/files"
	"github.com/coreos/ignition/src/log"
	"github.com/coreos/ignition/src/oem"
	"github.com/coreos/ignition/src/providers"
	_ "github.com/coreos/ignition/src/providers/cmdline"
	_ "github.com/coreos/ignition/src/providers/ec2"
	_ "github.com/coreos/ignition/src/providers/file"
	_ "github.com/coreos/ignition/src/providers/noop"

	"github.com/coreos/ignition/third_party/github.com/coreos/go-semver/semver"
)

const versionString = "0.1.0+git"

var version = *semver.Must(semver.NewVersion(versionString))

func main() {
	flags := struct {
		clearCache   bool
		configCache  string
		fetchTimeout time.Duration
		oem          oem.Name
		providers    providers.List
		root         string
		stage        stages.Name
		version      bool
	}{}

	flag.BoolVar(&flags.clearCache, "clear-cache", false, "clear any cached config")
	flag.StringVar(&flags.configCache, "config-cache", "/tmp/ignition.json", "where to cache the config")
	flag.DurationVar(&flags.fetchTimeout, "fetchtimeout", exec.DefaultFetchTimeout, "")
	flag.Var(&flags.oem, "oem", fmt.Sprintf("current oem. %v", oem.Names()))
	flag.Var(&flags.providers, "provider", fmt.Sprintf("provider of config. can be specified multiple times. %v", providers.Names()))
	flag.StringVar(&flags.root, "root", "/", "root of the filesystem")
	flag.Var(&flags.stage, "stage", fmt.Sprintf("execution stage. %v", stages.Names()))
	flag.BoolVar(&flags.version, "version", false, "print the version and exit")

	flag.Parse()

	if config, ok := oem.Get(flags.oem.String()); ok {
		for k, v := range config.Flags() {
			if err := flag.Set(k, v); err != nil {
				panic(err)
			}
		}
	}

	if flags.version {
		fmt.Printf("ignition %s\n", versionString)
		return
	}

	if flags.stage == "" {
		fmt.Fprint(os.Stderr, "'--stage' must be provided\n")
		os.Exit(2)
	}

	logger := log.New()
	defer logger.Close()

	if flags.clearCache {
		if err := os.Remove(flags.configCache); err != nil {
			logger.Err("unable to clear cache: %v", err)
		}
	}

	engine := exec.Engine{
		Root:         flags.root,
		FetchTimeout: flags.fetchTimeout,
		Logger:       logger,
		ConfigCache:  flags.configCache,
	}.Init()
	for _, name := range flags.providers {
		engine.AddProvider(providers.Get(name).Create(logger))
	}

	if !engine.Run(flags.stage.String()) {
		os.Exit(1)
	}
}
<|endoftext|>


<commit_before>package main

import (
	"container/list"
	"encoding/json"
	"entity"
	"fmt"
	"io/ioutil"
	"os/exec"
	"runtime"
	"time"
	"util"
)

const (
	BROWSER_PATH = "/usr/local/firefox/firefox"
)

var (
	LAST_MODIFIED time.Time
	IS_WORKER     bool
	RANGE_TIME    int64 = 600
	QUEUE               = list.New()
)

func Load(url string) *entity.Task {
	response, err_con := util.GetUrlInUserAgent(url)
	task := &entity.Task{}
	if err_con != nil {
		util.ERROR("connect ERROR, %s", err_con)
		util.Connect()
	} else {
		defer response.Body.Close()
		body, _ := ioutil.ReadAll(response.Body)
		json.Unmarshal(body, &task)
		task.Size = len(task.Users)
		las_modify, err_parse := time.Parse(time.RFC1123, response.Header.Get("Last-Modified"))
		if err_parse != nil {
			util.ERROR("Parse time is ERROR: %s", err_parse)
		} else {
			if las_modify.After(LAST_MODIFIED) {
				if LAST_MODIFIED.IsZero() {
					util.INFO("Last-Modified is NULL, program is first run, Last-Modifyed: %s", las_modify)
				} else {
					util.INFO("file is change, Last-Modifyed: %s", las_modify)
				}
				LAST_MODIFIED = las_modify
				if task.Start {
					IS_WORKER = true
					util.INFO("start worker!")
				} else {
					IS_WORKER = false
					util.INFO("worker is not start!")
				}
			}
		}
	}
	return task
}

func Jobs(task *entity.Task) {
	for _, user := range task.Users {
		user.Date = time.Unix(user.Trigger, user.Trigger)
		if user.Start && time.Now().Unix()-user.Trigger < RANGE_TIME {
			go Task(user)
		}
	}
	util.INFO("shutdown worker!")
	IS_WORKER = false
}

func Task(user *entity.User) {
	runtime.Gosched()
	util.DEBUG("add job username: %s", user.UserName)
	for {
		util.DEBUG("loop task username: %s, trigger: %d, current: %d", user.UserName, user.Trigger, time.Now().Unix())
		if time.Now().After(user.Date) && time.Now().Unix()-user.Trigger < RANGE_TIME {
			util.DEBUG("jobs username: %s, password: %s, start: %t, trigger: %d, date: %s",
				user.UserName, user.PassWord, user.Start, user.Trigger, user.Date)
			QUEUE.PushBack(util.HtmlFile(user))
			break
		} else {
		}
		time.Sleep(time.Duration(10) * time.Second)
	}
}

func OpenBrowser(filename string) {
	runtime.Gosched()
	cmd := exec.Command(BROWSER_PATH, filename)
	err_run := cmd.Run()
	if err_run != nil {
		util.ERROR("start browser file [%s] ERROR: %s", filename, err_run)
	}
}

func start() {
	runtime.Gosched()
	util.INFO("start ....")
	for {
		if QUEUE.Len() > 0 {
			task := QUEUE.Back()
			filename := fmt.Sprintf("%s", task.Value)
			util.INFO("open browser file: %s", filename)
			go OpenBrowser(filename)
			QUEUE.Remove(task)
		} else {
			time.Sleep(time.Duration(10) * time.Second)
		}
		time.Sleep(time.Duration(5) * time.Second)
	}
}

func main() {
	runtime.GOMAXPROCS(8)
	go start()
	for {
		task := Load("http://task.open-ns.org/task.json")
		if IS_WORKER {
			util.DEBUG("load user [%d] size", task.Size)
			util.INFO("worker is true, go jobs")
			Jobs(task)
		}
		time.Sleep(time.Duration(3) * time.Second)
	}
}
<commit_msg>add check exits task<commit_after>package main

import (
	"container/list"
	"encoding/json"
	"entity"
	"fmt"
	"io/ioutil"
	"os/exec"
	"runtime"
	"time"
	"util"
)

const (
	BROWSER_PATH = "/usr/local/firefox/firefox"
	FMT          = "%s-%d"
)

var (
	LAST_MODIFIED time.Time
	IS_WORKER     bool
	CURRENT_TASK  map[string]*entity.User = map[string]*entity.User{}
	RANGE_TIME    int64                   = 600
	QUEUE                                 = list.New()
)

func Load(url string) *entity.Task {
	response, err_con := util.GetUrlInUserAgent(url)
	task := &entity.Task{}
	if err_con != nil {
		util.ERROR("connect ERROR, %s", err_con)
		util.Connect()
	} else {
		defer response.Body.Close()
		body, _ := ioutil.ReadAll(response.Body)
		json.Unmarshal(body, &task)
		task.Size = len(task.Users)
		las_modify, err_parse := time.Parse(time.RFC1123, response.Header.Get("Last-Modified"))
		if err_parse != nil {
			util.ERROR("Parse time is ERROR: %s", err_parse)
		} else {
			if las_modify.After(LAST_MODIFIED) {
				if LAST_MODIFIED.IsZero() {
					util.INFO("Last-Modified is NULL, program is first run, Last-Modifyed: %s", las_modify)
				} else {
					util.INFO("file is change, Last-Modifyed: %s", las_modify)
				}
				LAST_MODIFIED = las_modify
				if task.Start {
					IS_WORKER = true
					util.INFO("start worker!")
				} else {
					IS_WORKER = false
					util.INFO("worker is not start!")
				}
			}
		}
	}
	return task
}

func Jobs(task *entity.Task) {
	for _, user := range task.Users {
		user.Date = time.Unix(user.Trigger, user.Trigger)
		if user.Start && time.Now().Unix()-user.Trigger < RANGE_TIME {
			value, ok := CURRENT_TASK[fmt.Sprintf(FMT, user.UserName, user.Trigger)]
			if ok {
				util.INFO("task is exits, username: %s, trigger: %d", value.UserName, value.Trigger)
			} else {
				CURRENT_TASK[fmt.Sprintf(FMT, user.UserName, user.Trigger)] = user
				go Task(user)
			}
		}
	}
	util.INFO("shutdown worker!")
	IS_WORKER = false
}

func Task(user *entity.User) {
	runtime.Gosched()
	util.DEBUG("add job username: %s", user.UserName)
	for {
		util.DEBUG("loop task username: %s, trigger: %d, current: %d", user.UserName, user.Trigger, time.Now().Unix())
		if time.Now().After(user.Date) && time.Now().Unix()-user.Trigger < RANGE_TIME {
			util.DEBUG("jobs username: %s, password: %s, start: %t, trigger: %d, date: %s",
				user.UserName, user.PassWord, user.Start, user.Trigger, user.Date)
			QUEUE.PushBack(user)
			break
		}
		time.Sleep(time.Duration(10) * time.Second)
	}
}

func OpenBrowser(filename string) {
	runtime.Gosched()
	cmd := exec.Command(BROWSER_PATH, filename)
	err_run := cmd.Run()
	if err_run != nil {
		util.ERROR("start browser file [%s] ERROR: %s", filename, err_run)
	}
}

func start() {
	runtime.Gosched()
	util.INFO("start ....")
	var user *entity.User
	for {
		if QUEUE.Len() > 0 {
			task := QUEUE.Back()
			user = task.Value.(*entity.User)
			filename := fmt.Sprintf("%s", util.HtmlFile(user))
			util.INFO("open browser file: %s", filename)
			go OpenBrowser(filename)
			QUEUE.Remove(task)
			delete(CURRENT_TASK, fmt.Sprintf(FMT, user.UserName, user.Trigger))
		} else {
			time.Sleep(time.Duration(10) * time.Second)
		}
		time.Sleep(time.Duration(5) * time.Second)
	}
}

func main() {
	runtime.GOMAXPROCS(8)
	go start()
	for {
		task := Load("http://task.open-ns.org/task.json")
		if IS_WORKER {
			util.DEBUG("load user [%d] size", task.Size)
			util.INFO("worker is true, go jobs")
			Jobs(task)
		}
		util.DEBUG("task size: %d, queue size: %d", len(CURRENT_TASK), QUEUE.Len())
		time.Sleep(time.Duration(3) * time.Second)
	}
}
<|endoftext|>


<commit_before>package main

import (
	"bytes"
	"errors"
	"flag"
	"go/ast"
	"go/build"
	"go/parser"
	"go/token"
	"io/ioutil"
	"log"
	"os"
	"path/filepath"
	"sort"
	"strings"
	"fmt"
)

type Args struct {
	path   string
	prefix string
	overwrite bool
}

func isStdLib(name, srcDir string) bool {
	pkg, err := build.Default.Import(name, srcDir, build.FindOnly)
	if err != nil {
		log.Println(err)
		os.Exit(1)
	}

	return pkg.Goroot
}

func getArgs() Args {
	f := flag.String("file", "", "path too a file.")
	prefix := flag.String("prefix", "", "prefix of the local packages.")
	overwrite := flag.Bool("w", false, "overwrite file.")
	flag.Parse()

	path := *f
	if path == "" {
		flag.Usage()
		os.Exit(1)
	}

	return Args{
		path:   path,
		prefix: *prefix,
		overwrite: *overwrite,
	}
}

func loadFile(path string) (*token.FileSet, *ast.File, error) {
	fileSet := token.NewFileSet()
	file, err := parser.ParseFile(fileSet, path, nil, parser.ImportsOnly)
	if err != nil {
		return nil, nil, err
	}

	return fileSet, file, nil
}

func reorderImports(prefix string, srcDir string, file *ast.File) {
	for _, d := range file.Decls {
		genDecl, ok := d.(*ast.GenDecl)
		if !ok {
			continue
		}
		if genDecl.Tok != token.IMPORT {
			continue
		}
		var stdLibAst []ast.Spec
		var localLibAst []ast.Spec
		var otherLibAst []ast.Spec
		for _, s := range genDecl.Specs {
			spec := s.(*ast.ImportSpec)
			name := strings.Trim(spec.Path.Value, `"`)

			if isStdLib(name, srcDir) {
				stdLibAst = append(stdLibAst, spec)
			} else if strings.HasPrefix(name, prefix) {
				localLibAst = append(localLibAst, spec)
			} else {
				otherLibAst = append(otherLibAst, spec)
			}
		}

		sort.Slice(stdLibAst, func(i, j int) bool {
			return stdLibAst[i].(*ast.ImportSpec).Path.Value < stdLibAst[j].(*ast.ImportSpec).Path.Value
		})
		if len(stdLibAst) > 0 {
			stdLibAst = append(stdLibAst, &ast.ImportSpec{Path: &ast.BasicLit{}})
		}

		sort.Slice(otherLibAst, func(i, j int) bool {
			return otherLibAst[i].(*ast.ImportSpec).Path.Value < otherLibAst[j].(*ast.ImportSpec).Path.Value
		})
		if len(otherLibAst) > 0 {
			otherLibAst = append(otherLibAst, &ast.ImportSpec{Path: &ast.BasicLit{}})
		}

		sort.Slice(localLibAst, func(i, j int) bool {
			return localLibAst[i].(*ast.ImportSpec).Path.Value < localLibAst[j].(*ast.ImportSpec).Path.Value
		})

		genDecl.Specs = append(append(stdLibAst, otherLibAst...), localLibAst...)
	}

}

func generate(file *ast.File, cm ast.CommentMap) ([]string, map[int]int) {
	importStmts := make([]string, 0)
	parenMap := make(map[int]int)
	for _, d := range file.Decls {
		genDecl, ok := d.(*ast.GenDecl)
		if !ok {
			continue
		}
		if genDecl.Tok != token.IMPORT {
			continue
		}

		if genDecl.Lparen.IsValid() {
			// start from zero.
			parenMap[int(genDecl.Lparen)-1] = int(genDecl.Rparen)
		}

		buf := bytes.NewBufferString("import (\n")
		for _, s := range genDecl.Specs {
			spec := s.(*ast.ImportSpec)

			comments, ok := cm[spec]
			if ok {
				for _, comment := range comments {
					buf.WriteString("\t")
					buf.WriteString(comment.Text())
					buf.WriteString("\n")
				}
			}

			buf.WriteString("\t")
			if spec.Name != nil {
				buf.WriteString(spec.Name.String())
				buf.WriteString(" ")
			}
			buf.WriteString(spec.Path.Value)
			buf.WriteString("\n")
		}
		buf.WriteString(")\n")
		importStmts = append(importStmts, buf.String())
	}

	return importStmts, parenMap
}

func replaceImports(path string, importStmts []string, parenMap map[int]int) (string, error) {
	file, err := os.Open(path)
	if err != nil {
		return "", err
	}
	defer file.Close()

	bodyBytes, err := ioutil.ReadAll(file)
	if err != nil {
		return "", err
	}

	buf := bytes.NewBufferString("")
	for _, imp := range importStmts {
		pos := bytes.Index(bodyBytes, []byte("import"))
		buf.Write(bodyBytes[:pos])

		end := func() int {
			next := string(bodyBytes[pos+6 : pos+7])
			if next != " " && next != "\n" && next != "(" && next != "\t" {
				return -1
			}

			for i := pos + 6; i < len(bodyBytes); i++ {
				c := string(bodyBytes[i : i+1])
				if c == "(" {
					e, ok := parenMap[i]
					if !ok {
						return -1
					}
					return e
				} else if c == " " || c == "\t" || c == "\n" {
					continue
				} else {
					for j := i; j < len(bodyBytes); j++ {
						if string(bodyBytes[j:j+1]) == "\n" {
							return j + 1
						}
					}
				}
			}
			return -1
		}()

		if end < 0 {
			return "", errors.New("Failed to find import statements.")
		}

		buf.WriteString(imp)
		buf.WriteString("\n")
		bodyBytes = bytes.TrimLeft(bodyBytes[end:], " \t\n")
	}

	buf.Write(bodyBytes)

	return buf.String(), nil
}

func main() {
	args := getArgs()

	fileSet, file, err := loadFile(args.path)
	if err != nil {
		log.Println(err)
		os.Exit(1)
	}

	srcDir := filepath.Dir(args.path)
	reorderImports(args.prefix, srcDir, file)

	cm := ast.NewCommentMap(fileSet, file, file.Comments)

	importStmt, parenMap := generate(file, cm)
	replaced, err := replaceImports(args.path, importStmt, parenMap)
	if err != nil {
		log.Println(err)
		os.Exit(1)
	}

	if args.overwrite {
		err = ioutil.WriteFile(args.path, []byte(replaced), 0644)
		if err != nil {
			log.Println(err)
			os.Exit(1)
		}
	} else {
		_, err := fmt.Fprint(os.Stdout, replaced)
		if err != nil {
			log.Println(err)
			os.Exit(1)
		}
	}
}
<commit_msg>Bugfix: do not put blank on empty line.<commit_after>package main

import (
	"bytes"
	"errors"
	"flag"
	"go/ast"
	"go/build"
	"go/parser"
	"go/token"
	"io/ioutil"
	"log"
	"os"
	"path/filepath"
	"sort"
	"strings"
	"fmt"
)

type Args struct {
	path   string
	prefix string
	overwrite bool
}

func isStdLib(name, srcDir string) bool {
	pkg, err := build.Default.Import(name, srcDir, build.FindOnly)
	if err != nil {
		log.Println(err)
		os.Exit(1)
	}

	return pkg.Goroot
}

func getArgs() Args {
	f := flag.String("file", "", "path too a file.")
	prefix := flag.String("prefix", "", "prefix of the local packages.")
	overwrite := flag.Bool("w", false, "overwrite file.")
	flag.Parse()

	path := *f
	if path == "" {
		flag.Usage()
		os.Exit(1)
	}

	return Args{
		path:   path,
		prefix: *prefix,
		overwrite: *overwrite,
	}
}

func loadFile(path string) (*token.FileSet, *ast.File, error) {
	fileSet := token.NewFileSet()
	file, err := parser.ParseFile(fileSet, path, nil, parser.ImportsOnly)
	if err != nil {
		return nil, nil, err
	}

	return fileSet, file, nil
}

func reorderImports(prefix string, srcDir string, file *ast.File) {
	for _, d := range file.Decls {
		genDecl, ok := d.(*ast.GenDecl)
		if !ok {
			continue
		}
		if genDecl.Tok != token.IMPORT {
			continue
		}
		var stdLibAst []ast.Spec
		var localLibAst []ast.Spec
		var otherLibAst []ast.Spec
		for _, s := range genDecl.Specs {
			spec := s.(*ast.ImportSpec)
			name := strings.Trim(spec.Path.Value, `"`)

			if isStdLib(name, srcDir) {
				stdLibAst = append(stdLibAst, spec)
			} else if strings.HasPrefix(name, prefix) {
				localLibAst = append(localLibAst, spec)
			} else {
				otherLibAst = append(otherLibAst, spec)
			}
		}

		sort.Slice(stdLibAst, func(i, j int) bool {
			return stdLibAst[i].(*ast.ImportSpec).Path.Value < stdLibAst[j].(*ast.ImportSpec).Path.Value
		})
		if len(stdLibAst) > 0 {
			stdLibAst = append(stdLibAst, &ast.ImportSpec{Path: &ast.BasicLit{}})
		}

		sort.Slice(otherLibAst, func(i, j int) bool {
			return otherLibAst[i].(*ast.ImportSpec).Path.Value < otherLibAst[j].(*ast.ImportSpec).Path.Value
		})
		if len(otherLibAst) > 0 {
			otherLibAst = append(otherLibAst, &ast.ImportSpec{Path: &ast.BasicLit{}})
		}

		sort.Slice(localLibAst, func(i, j int) bool {
			return localLibAst[i].(*ast.ImportSpec).Path.Value < localLibAst[j].(*ast.ImportSpec).Path.Value
		})

		genDecl.Specs = append(append(stdLibAst, otherLibAst...), localLibAst...)
	}

}

func generate(file *ast.File, cm ast.CommentMap) ([]string, map[int]int) {
	importStmts := make([]string, 0)
	parenMap := make(map[int]int)
	for _, d := range file.Decls {
		genDecl, ok := d.(*ast.GenDecl)
		if !ok {
			continue
		}
		if genDecl.Tok != token.IMPORT {
			continue
		}

		if genDecl.Lparen.IsValid() {
			// start from zero.
			parenMap[int(genDecl.Lparen)-1] = int(genDecl.Rparen)
		}

		buf := bytes.NewBufferString("import (\n")
		for _, s := range genDecl.Specs {
			spec := s.(*ast.ImportSpec)

			path := spec.Path.Value
			if path == "" {
				buf.WriteString("\n")
				continue
			}

			comments, ok := cm[spec]
			if ok {
				for _, comment := range comments {
					buf.WriteString("\t")
					buf.WriteString(comment.Text())
					buf.WriteString("\n")
				}
			}

			buf.WriteString("\t")
			if spec.Name != nil {
				buf.WriteString(spec.Name.String())
				buf.WriteString(" ")
			}

			buf.WriteString(path)
			buf.WriteString("\n")
		}
		buf.WriteString(")\n")
		importStmts = append(importStmts, buf.String())
	}

	return importStmts, parenMap
}

func replaceImports(path string, importStmts []string, parenMap map[int]int) (string, error) {
	file, err := os.Open(path)
	if err != nil {
		return "", err
	}
	defer file.Close()

	bodyBytes, err := ioutil.ReadAll(file)
	if err != nil {
		return "", err
	}

	buf := bytes.NewBufferString("")
	for _, imp := range importStmts {
		pos := bytes.Index(bodyBytes, []byte("import"))
		buf.Write(bodyBytes[:pos])

		end := func() int {
			next := string(bodyBytes[pos+6 : pos+7])
			if next != " " && next != "\n" && next != "(" && next != "\t" {
				return -1
			}

			for i := pos + 6; i < len(bodyBytes); i++ {
				c := string(bodyBytes[i : i+1])
				if c == "(" {
					e, ok := parenMap[i]
					if !ok {
						return -1
					}
					return e
				} else if c == " " || c == "\t" || c == "\n" {
					continue
				} else {
					for j := i; j < len(bodyBytes); j++ {
						if string(bodyBytes[j:j+1]) == "\n" {
							return j + 1
						}
					}
				}
			}
			return -1
		}()

		if end < 0 {
			return "", errors.New("Failed to find import statements.")
		}

		buf.WriteString(imp)
		buf.WriteString("\n")
		bodyBytes = bytes.TrimLeft(bodyBytes[end:], " \t\n")
	}

	buf.Write(bodyBytes)

	return buf.String(), nil
}

func main() {
	args := getArgs()

	fileSet, file, err := loadFile(args.path)
	if err != nil {
		log.Println(err)
		os.Exit(1)
	}

	srcDir := filepath.Dir(args.path)
	reorderImports(args.prefix, srcDir, file)

	cm := ast.NewCommentMap(fileSet, file, file.Comments)

	importStmt, parenMap := generate(file, cm)
	replaced, err := replaceImports(args.path, importStmt, parenMap)
	if err != nil {
		log.Println(err)
		os.Exit(1)
	}

	if args.overwrite {
		err = ioutil.WriteFile(args.path, []byte(replaced), 0644)
		if err != nil {
			log.Println(err)
			os.Exit(1)
		}
	} else {
		_, err := fmt.Fprint(os.Stdout, replaced)
		if err != nil {
			log.Println(err)
			os.Exit(1)
		}
	}
}
<|endoftext|>


<commit_before>package gdrive2slack

import (
	"fmt"
	"github.com/optionfactory/gdrive2slack/google/drive"
	"github.com/optionfactory/gdrive2slack/slack"
)

var actionColors = []string{
	drive.Deleted:  "#ffcccc",
	drive.Created:  "#ccffcc",
	drive.Modified: "#ccccff",
	drive.Shared:   "#ccccff",
	drive.Viewed:   "#ccccff",
}

func CreateSlackAttachment(change *drive.ChangeItem) *slack.Attachment {
	var editor string
	if len(change.File.LastModifyingUser.EmailAddress) > 0 && len(change.File.LastModifyingUser.DisplayName) > 0 {
		editor = fmt.Sprintf("<mailto:%s|%s>", change.File.LastModifyingUser.EmailAddress, change.File.LastModifyingUser.DisplayName)
	} else if len(change.File.LastModifyingUser.DisplayName) > 0 {
		editor = change.File.LastModifyingUser.DisplayName
	} else {
		editor = "Unknown"
	}
	return &slack.Attachment{
		Fallback: fmt.Sprintf("Changes Detected to file <%s|%s>", change.File.AlternateLink, change.File.Title),
		Color:    actionColors[change.LastAction],
		Fields: []slack.Field{
			{
				Title: fmt.Sprintf("%s file", change.LastAction.String()),
				Value: fmt.Sprintf("<%s|%s>", change.File.AlternateLink, change.File.Title),
				Short: true,
			},
			{
				Title: "Editor",
				Value: editor,
				Short: true,
			},
		},
	}
}

func CreateSlackMessage(userState *UserState) *slack.Message {

	var attachments = make([]slack.Attachment, 0, len(userState.Gdrive.ChangeSet))

	for i := 0; i != len(userState.Gdrive.ChangeSet); i++ {
		attachments = append(attachments, *CreateSlackAttachment(&userState.Gdrive.ChangeSet[i]))
	}

	return &slack.Message{
		Channel:     userState.Channel,
		Username:    "Google Drive",
		Text:        fmt.Sprintf("hook for <mailto:%s|%s> → <@%s|%s>", userState.GoogleUserInfo.Emails[0].Value, userState.GoogleUserInfo.DisplayName, userState.SlackUserInfo.UserId, userState.SlackUserInfo.User),
		IconUrl:     "http://gdrive2slack.optionfactory.net/gdrive2slack.png",
		Attachments: attachments,
	}
}
<commit_msg>ref: slack message<commit_after>package gdrive2slack

import (
	"fmt"
	"github.com/optionfactory/gdrive2slack/google/drive"
	"github.com/optionfactory/gdrive2slack/slack"
)

var actionColors = []string{
	drive.Deleted:  "#ffcccc",
	drive.Created:  "#ccffcc",
	drive.Modified: "#ccccff",
	drive.Shared:   "#ccccff",
	drive.Viewed:   "#ccccff",
}

func CreateSlackAttachment(change *drive.ChangeItem) *slack.Attachment {
	var editor string
	if len(change.File.LastModifyingUser.EmailAddress) > 0 && len(change.File.LastModifyingUser.DisplayName) > 0 {
		editor = fmt.Sprintf("<mailto:%s|%s>", change.File.LastModifyingUser.EmailAddress, change.File.LastModifyingUser.DisplayName)
	} else if len(change.File.LastModifyingUser.DisplayName) > 0 {
		editor = change.File.LastModifyingUser.DisplayName
	} else {
		editor = "Unknown"
	}
	return &slack.Attachment{
		Fallback: fmt.Sprintf("Changes Detected to file <%s|%s>", change.File.AlternateLink, change.File.Title),
		Color:    actionColors[change.LastAction],
		Fields: []slack.Field{
			{
				Title: fmt.Sprintf("%s file", change.LastAction.String()),
				Value: fmt.Sprintf("<%s|%s>", change.File.AlternateLink, change.File.Title),
				Short: true,
			},
			{
				Title: "Editor",
				Value: editor,
				Short: true,
			},
		},
	}
}

func CreateSlackMessage(userState *UserState) *slack.Message {

	var attachments = make([]slack.Attachment, 0, len(userState.Gdrive.ChangeSet))

	for i := 0; i != len(userState.Gdrive.ChangeSet); i++ {
		attachments = append(attachments, *CreateSlackAttachment(&userState.Gdrive.ChangeSet[i]))
	}

	return &slack.Message{
		Channel:     userState.Channel,
		Username:    "Google Drive",
		Text:        fmt.Sprintf("Activity on gdrive (configured by <@%s|%s>)", userState.SlackUserInfo.UserId, userState.SlackUserInfo.User),
		IconUrl:     "http://gdrive2slack.optionfactory.net/gdrive2slack.png",
		Attachments: attachments,
	}
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"fmt"
	"os"
	"strings"

	"github.com/Cepave/open-falcon-backend/modules/f2e-api/app/controller"
	"github.com/Cepave/open-falcon-backend/modules/f2e-api/config"
	"github.com/Cepave/open-falcon-backend/modules/f2e-api/graph"
	log "github.com/Sirupsen/logrus"
	"github.com/spf13/viper"
	"github.com/gin-gonic/gin"
	yaagGin "github.com/masato25/yaag/gin"
	"github.com/masato25/yaag/yaag"
)

func initGraph() {
	graph.Start(viper.GetStringMapString("graphs.cluster"))
}

func main() {
	cfgTmp := flag.String("c", "cfg.json", "configuration file")
	version := flag.Bool("v", false, "show version")
	help := flag.Bool("h", false, "help")
	flag.Parse()
	cfg := *cfgTmp
	if *version {
		fmt.Println(config.VERSION)
		os.Exit(0)
	}

	if *help {
		flag.Usage()
		os.Exit(0)
	}

	viper.AddConfigPath(".")
	viper.AddConfigPath("/")
	viper.AddConfigPath("./config")
	viper.AddConfigPath("./api/config")
	cfg = strings.Replace(cfg, ".json", "", 1)
	viper.SetConfigName(cfg)

	viper.ReadInConfig()
	err := config.InitLog(viper.GetString("log_level"))
	if err != nil {
		log.Fatal(err)
	}
	err = config.InitDB(viper.GetBool("db.db_bug"))
	if err != nil {
		log.Fatalf("db conn failed with error %s", err.Error())
	}
	routes := gin.Default()
	if viper.GetBool("gen_doc") {
		yaag.Init(&yaag.Config{
			On:       true,
			DocTitle: "Gin",
			DocPath:  viper.GetString("gen_doc_path"),
			BaseUrls: map[string]string{"Production": "/api/v1", "Staging": "/api/v1"},
		})
		routes.Use(yaagGin.Document())
	}
	initGraph()
	//start gin server
	controller.StartGin(viper.GetString("web_port"), routes)
}
<commit_msg>main function can terminate by ^C<commit_after>package main

import (
	"flag"
	"fmt"
	"os"
	"os/signal"
	"strings"
	"syscall"

	"github.com/Cepave/open-falcon-backend/modules/f2e-api/app/controller"
	"github.com/Cepave/open-falcon-backend/modules/f2e-api/config"
	"github.com/Cepave/open-falcon-backend/modules/f2e-api/graph"
	log "github.com/Sirupsen/logrus"
	"github.com/gin-gonic/gin"
	yaagGin "github.com/masato25/yaag/gin"
	"github.com/masato25/yaag/yaag"
	"github.com/spf13/viper"
)

func initGraph() {
	graph.Start(viper.GetStringMapString("graphs.cluster"))
}

func main() {
	cfgTmp := flag.String("c", "cfg.json", "configuration file")
	version := flag.Bool("v", false, "show version")
	help := flag.Bool("h", false, "help")
	flag.Parse()
	cfg := *cfgTmp
	if *version {
		fmt.Println(config.VERSION)
		os.Exit(0)
	}

	if *help {
		flag.Usage()
		os.Exit(0)
	}

	viper.AddConfigPath(".")
	viper.AddConfigPath("/")
	viper.AddConfigPath("./config")
	viper.AddConfigPath("./api/config")
	cfg = strings.Replace(cfg, ".json", "", 1)
	viper.SetConfigName(cfg)

	viper.ReadInConfig()
	err := config.InitLog(viper.GetString("log_level"))
	if err != nil {
		log.Fatal(err)
	}
	err = config.InitDB(viper.GetBool("db.db_bug"))
	if err != nil {
		log.Fatalf("db conn failed with error %s", err.Error())
	}
	routes := gin.Default()
	if viper.GetBool("gen_doc") {
		yaag.Init(&yaag.Config{
			On:       true,
			DocTitle: "Gin",
			DocPath:  viper.GetString("gen_doc_path"),
			BaseUrls: map[string]string{"Production": "/api/v1", "Staging": "/api/v1"},
		})
		routes.Use(yaagGin.Document())
	}
	initGraph()
	//start gin server
	log.Debugf("will start with port:%v", viper.GetString("web_port"))
	go controller.StartGin(viper.GetString("web_port"), routes)
	sigs := make(chan os.Signal, 1)
	signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
	go func() {
		<-sigs
		fmt.Println()
		os.Exit(0)
	}()
	select {}
}
<|endoftext|>


<commit_before>package gateway

import (
	"io"
	"net"
	"net/http"
	"strconv"
	"time"

	"github.com/NebulousLabs/go-upnp"

	"github.com/NebulousLabs/Sia/build"
	"github.com/NebulousLabs/Sia/modules"
)

// myExternalIP discovers the gateway's external IP by querying a centralized
// service, http://myexternalip.com.
func myExternalIP() (string, error) {
	// timeout after 10 seconds
	client := http.Client{Timeout: time.Duration(10 * time.Second)}
	resp, err := client.Get("http://myexternalip.com/raw")
	if err != nil {
		return "", err
	}
	defer resp.Body.Close()
	buf := make([]byte, 64)
	n, err := resp.Body.Read(buf)
	if err != nil && err != io.EOF {
		return "", err
	}
	// trim newline
	return string(buf[:n-1]), nil
}

// learnHostname discovers the external IP of the Gateway. Once the IP has
// been discovered, it registers the ShareNodes RPC to be called on new
// connections, advertising the IP to other nodes.
func (g *Gateway) learnHostname() {
	if build.Release == "testing" {
		return
	}

	var host string

	// try UPnP first, then fallback to myexternalip.com
	d, err := upnp.Discover()
	if err == nil {
		host, err = d.ExternalIP()
	}
	if err != nil {
		host, err = myExternalIP()
	}
	if err != nil {
		g.log.Println("WARN: failed to discover external IP")
		return
	}

	id := g.mu.Lock()
	g.myAddr = modules.NetAddress(net.JoinHostPort(host, g.myAddr.Port()))
	g.mu.Unlock(id)

	g.log.Println("INFO: our address is", g.myAddr)

	// now that we know our address, we can start advertising it
	g.RegisterConnectCall("RelayNode", g.sendAddress)
}

// forwardPort adds a port mapping to the router.
func (g *Gateway) forwardPort(port string) {
	if build.Release == "testing" {
		return
	}

	d, err := upnp.Discover()
	if err != nil {
		g.log.Printf("WARN: could not automatically forward port %s: no UPnP-enabled devices found", port)
		return
	}

	portInt, _ := strconv.Atoi(port)
	err = d.Forward(uint16(portInt), "Sia RPC")
	if err != nil {
		g.log.Printf("WARN: could not automatically forward port %s: %v", port, err)
		return
	}

	g.log.Println("INFO: successfully forwarded port", port)
}

// clearPort removes a port mapping from the router.
func (g *Gateway) clearPort(port string) {
	if build.Release == "testing" {
		return
	}

	//d, err := upnp.Load("http://192.168.1.1:5000/Public_UPNP_gatedesc.xml")
	d, err := upnp.Discover()
	if err != nil {
		return
	}

	portInt, _ := strconv.Atoi(port)
	err = d.Clear(uint16(portInt))
	if err != nil {
		g.log.Printf("WARN: could not automatically unforward port %s: %v", port, err)
		return
	}

	g.log.Println("INFO: successfully unforwarded port", port)
}
<commit_msg>myExternalIP returns error on bad status code<commit_after>package gateway

import (
	"errors"
	"io"
	"io/ioutil"
	"net"
	"net/http"
	"strconv"
	"time"

	"github.com/NebulousLabs/go-upnp"

	"github.com/NebulousLabs/Sia/build"
	"github.com/NebulousLabs/Sia/modules"
)

// myExternalIP discovers the gateway's external IP by querying a centralized
// service, http://myexternalip.com.
func myExternalIP() (string, error) {
	// timeout after 10 seconds
	client := http.Client{Timeout: time.Duration(10 * time.Second)}
	resp, err := client.Get("http://myexternalip.com/raw")
	if err != nil {
		return "", err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		errResp, _ := ioutil.ReadAll(resp.Body)
		return "", errors.New(string(errResp))
	}
	buf := make([]byte, 64)
	n, err := resp.Body.Read(buf)
	if err != nil && err != io.EOF {
		return "", err
	}
	// trim newline
	return string(buf[:n-1]), nil
}

// learnHostname discovers the external IP of the Gateway. Once the IP has
// been discovered, it registers the ShareNodes RPC to be called on new
// connections, advertising the IP to other nodes.
func (g *Gateway) learnHostname() {
	if build.Release == "testing" {
		return
	}

	var host string

	// try UPnP first, then fallback to myexternalip.com
	d, err := upnp.Discover()
	if err == nil {
		host, err = d.ExternalIP()
	}
	if err != nil {
		host, err = myExternalIP()
	}
	if err != nil {
		g.log.Println("WARN: failed to discover external IP:", err)
		return
	}

	id := g.mu.Lock()
	g.myAddr = modules.NetAddress(net.JoinHostPort(host, g.myAddr.Port()))
	g.mu.Unlock(id)

	g.log.Println("INFO: our address is", g.myAddr)

	// now that we know our address, we can start advertising it
	g.RegisterConnectCall("RelayNode", g.sendAddress)
}

// forwardPort adds a port mapping to the router.
func (g *Gateway) forwardPort(port string) {
	if build.Release == "testing" {
		return
	}

	d, err := upnp.Discover()
	if err != nil {
		g.log.Printf("WARN: could not automatically forward port %s: no UPnP-enabled devices found", port)
		return
	}

	portInt, _ := strconv.Atoi(port)
	err = d.Forward(uint16(portInt), "Sia RPC")
	if err != nil {
		g.log.Printf("WARN: could not automatically forward port %s: %v", port, err)
		return
	}

	g.log.Println("INFO: successfully forwarded port", port)
}

// clearPort removes a port mapping from the router.
func (g *Gateway) clearPort(port string) {
	if build.Release == "testing" {
		return
	}

	//d, err := upnp.Load("http://192.168.1.1:5000/Public_UPNP_gatedesc.xml")
	d, err := upnp.Discover()
	if err != nil {
		return
	}

	portInt, _ := strconv.Atoi(port)
	err = d.Clear(uint16(portInt))
	if err != nil {
		g.log.Printf("WARN: could not automatically unforward port %s: %v", port, err)
		return
	}

	g.log.Println("INFO: successfully unforwarded port", port)
}
<|endoftext|>


<commit_before>package glfw

/*
// Standard OpenGL client is used on 386 and amd64 architectures, except when
// explicitly asked for gles2 or wayland.
#cgo 386,!gles2,!wayland CFLAGS: -D_GLFW_USE_OPENGL
#cgo amd64,!gles2,!wayland CFLAGS: -D_GLFW_USE_OPENGL

// Choose OpenGL ES V2 on arm, or when explicitly asked for gles2/wayland.
#cgo arm gles2 wayland CFLAGS: -D_GLFW_USE_GLESV2

// Windows Build Tags
// ----------------
// GLFW Options:
#cgo windows CFLAGS: -D_GLFW_WIN32 -D_GLFW_WGL

// Linker Options:
#cgo windows LDFLAGS: -lopengl32 -lgdi32

// Darwin Build Tags
// ----------------
// GLFW Options:
#cgo darwin CFLAGS: -D_GLFW_COCOA -D_GLFW_NSGL -D_GLFW_USE_CHDIR -D_GLFW_USE_MENUBAR -D_GLFW_USE_RETINA -Wno-deprecated-declarations

// Linker Options:
#cgo darwin LDFLAGS: -framework Cocoa -framework OpenGL -framework IOKit -framework CoreVideo

// Linux Build Tags
// ----------------
// GLFW Options:
#cgo linux,!wayland CFLAGS: -D_GLFW_X11 -D_GLFW_GLX -D_GLFW_HAS_GLXGETPROCADDRESSARB -D_GLFW_HAS_DLOPEN
#cgo linux,wayland CFLAGS: -D_GLFW_WAYLAND -D_GLFW_EGL -D_GLFW_HAS_DLOPEN

// Linker Options:
#cgo linux,!wayland LDFLAGS: -lGL -lX11 -lXrandr -lXxf86vm -lXi -lXcursor -lm -lXinerama
#cgo linux,wayland LDFLAGS: -lGL -lX11 -lXrandr -lXxf86vm -lXi -lXcursor -lm -lXinerama

// FreeBSD Build Tags
// ----------------
// GLFW Options:
#cgo freebsd,!wayland CFLAGS: -D_GLFW_X11 -D_GLFW_GLX -D_GLFW_HAS_GLXGETPROCADDRESSARB -D_GLFW_HAS_DLOPEN
#cgo freebsd,wayland CFLAGS: -D_GLFW_WAYLAND -D_GLFW_EGL -D_GLFW_HAS_DLOPEN

// Linker Options:
#cgo freebsd,!wayland LDFLAGS: -lGL -lX11 -lXrandr -lXxf86vm -lXi -lXcursor -lm -lXinerama
#cgo freebsd,wayland LDFLAGS: -lGL -lX11 -lXrandr -lXxf86vm -lXi -lXcursor -lm -lXinerama
*/
import "C"
<commit_msg>Update Linux build and linker options.<commit_after>package glfw

/*
// Standard OpenGL client is used on 386 and amd64 architectures, except when
// explicitly asked for gles2 or wayland.
#cgo 386,!gles2,!wayland CFLAGS: -D_GLFW_USE_OPENGL
#cgo amd64,!gles2,!wayland CFLAGS: -D_GLFW_USE_OPENGL

// Choose OpenGL ES V2 on arm, or when explicitly asked for gles2/wayland.
#cgo arm gles2 wayland CFLAGS: -D_GLFW_USE_GLESV2

// Windows Build Tags
// ----------------
// GLFW Options:
#cgo windows CFLAGS: -D_GLFW_WIN32 -D_GLFW_WGL

// Linker Options:
#cgo windows LDFLAGS: -lopengl32 -lgdi32

// Darwin Build Tags
// ----------------
// GLFW Options:
#cgo darwin CFLAGS: -D_GLFW_COCOA -D_GLFW_NSGL -D_GLFW_USE_CHDIR -D_GLFW_USE_MENUBAR -D_GLFW_USE_RETINA -Wno-deprecated-declarations

// Linker Options:
#cgo darwin LDFLAGS: -framework Cocoa -framework OpenGL -framework IOKit -framework CoreVideo

// Linux Build Tags
// ----------------
// GLFW Options:
#cgo linux,!wayland CFLAGS: -D_GLFW_X11 -D_GLFW_GLX
#cgo linux,wayland CFLAGS: -D_GLFW_WAYLAND -D_GLFW_EGL

// Linker Options:
#cgo linux,!wayland LDFLAGS: -lGL -lX11 -lXrandr -lXxf86vm -lXi -lXcursor -lm -lXinerama -ldl
#cgo linux,wayland LDFLAGS: -lGL -lX11 -lXrandr -lXxf86vm -lXi -lXcursor -lm -lXinerama -ldl

// FreeBSD Build Tags
// ----------------
// GLFW Options:
#cgo freebsd,!wayland CFLAGS: -D_GLFW_X11 -D_GLFW_GLX -D_GLFW_HAS_GLXGETPROCADDRESSARB -D_GLFW_HAS_DLOPEN
#cgo freebsd,wayland CFLAGS: -D_GLFW_WAYLAND -D_GLFW_EGL -D_GLFW_HAS_DLOPEN

// Linker Options:
#cgo freebsd,!wayland LDFLAGS: -lGL -lX11 -lXrandr -lXxf86vm -lXi -lXcursor -lm -lXinerama
#cgo freebsd,wayland LDFLAGS: -lGL -lX11 -lXrandr -lXxf86vm -lXi -lXcursor -lm -lXinerama
*/
import "C"
<|endoftext|>


<commit_before>package main

import (
	"bytes"
	"io"
	"io/ioutil"
	"net"
	"strconv"
	"strings"

	"github.com/flynn/strowger/types"
	. "github.com/titanous/gocheck"
)

func NewTCPTestServer(r io.Reader, w io.Writer) *TCPTestServer {
	s := &TCPTestServer{w: w, r: r}
	var err error
	s.l, err = net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		panic(err)
	}
	s.Addr = s.l.Addr().String()
	go s.Serve()
	return s
}

type TCPTestServer struct {
	Addr string
	w    io.Writer
	r    io.Reader
	l    net.Listener
}

func (s *TCPTestServer) Serve() {
	for {
		conn, err := s.l.Accept()
		if err != nil {
			return
		}
		go func() {
			defer conn.Close()
			done := make(chan struct{})
			go func() {
				io.Copy(conn, s.r)
				close(done)
			}()
			io.Copy(s.w, conn)
			<-done
		}()
	}
}

func (s *TCPTestServer) Close() error { return s.l.Close() }

func newTCPListener(etcd *fakeEtcd) (*TCPListener, *fakeDiscoverd, error) {
	discoverd := newFakeDiscoverd()
	if etcd == nil {
		etcd = newFakeEtcd()
	}
	l := NewTCPListener("127.0.0.1", NewEtcdDataStore(etcd, "/strowger/tcp/"), discoverd)
	return l, discoverd, l.Start()
}

func assertTCPConn(c *C, addr, expected string, rcvd *bytes.Buffer) {
	conn, err := net.Dial("tcp", addr)
	c.Assert(err, IsNil)
	conn.Write([]byte("asdf"))
	conn.(*net.TCPConn).CloseWrite()
	res, err := ioutil.ReadAll(conn)
	conn.Close()

	c.Assert(err, IsNil)
	c.Assert(string(res), Equals, expected)
	c.Assert(rcvd.String(), Equals, "asdf")
	rcvd.Reset()
}

func (s *S) TestAddTCPRoute(c *C) {
	const addr, port, portInt = "127.0.0.1:45000", "45000", 45000
	buf := &bytes.Buffer{}
	srv1 := NewTCPTestServer(strings.NewReader("1"), buf)
	srv2 := NewTCPTestServer(strings.NewReader("2"), buf)
	defer srv1.Close()
	defer srv2.Close()

	l, discoverd, err := newTCPListener(nil)
	c.Assert(err, IsNil)
	defer l.Close()

	discoverd.Register("test", srv1.Addr)
	defer discoverd.UnregisterAll()

	wait := waitForEvent(c, l, "add", port)
	err = l.AddRoute(&strowger.TCPRoute{Port: portInt, Service: "test"})
	c.Assert(err, IsNil)
	wait()

	assertTCPConn(c, addr, "1", buf)

	discoverd.Unregister("test", srv1.Addr)
	discoverd.Register("test", srv2.Addr)

	assertTCPConn(c, addr, "2", buf)

	wait = waitForEvent(c, l, "remove", port)
	err = l.RemoveRoute(port)
	c.Assert(err, IsNil)
	wait()

	_, err = net.Dial("tcp", addr)
	c.Assert(err, Not(IsNil))
}

func (s *S) TestInitialTCPSync(c *C) {
	const addr, port = "127.0.0.1:45000", 45000
	etcd := newFakeEtcd()
	l, _, err := newTCPListener(etcd)
	c.Assert(err, IsNil)
	wait := waitForEvent(c, l, "add", strconv.Itoa(port))
	err = l.AddRoute(&strowger.TCPRoute{Service: "test", Port: port})
	c.Assert(err, IsNil)
	wait()
	l.Close()

	buf := &bytes.Buffer{}
	srv := NewTCPTestServer(strings.NewReader("1"), buf)
	defer srv.Close()

	l, discoverd, err := newTCPListener(etcd)
	c.Assert(err, IsNil)
	defer l.Close()

	discoverd.Register("test", srv.Addr)
	defer discoverd.UnregisterAll()

	assertTCPConn(c, addr, "1", buf)
}
<commit_msg>Fix race in test, simplify<commit_after>package main

import (
	"io"
	"io/ioutil"
	"net"
	"strconv"

	"github.com/flynn/strowger/types"
	. "github.com/titanous/gocheck"
)

func NewTCPTestServer(prefix string) *TCPTestServer {
	s := &TCPTestServer{prefix: prefix}
	var err error
	s.l, err = net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		panic(err)
	}
	s.Addr = s.l.Addr().String()
	go s.Serve()
	return s
}

type TCPTestServer struct {
	Addr   string
	prefix string
	l      net.Listener
}

func (s *TCPTestServer) Serve() {
	for {
		conn, err := s.l.Accept()
		if err != nil {
			return
		}
		go func() {
			conn.Write([]byte(s.prefix))
			io.Copy(conn, conn)
			conn.Close()
		}()
	}
}

func (s *TCPTestServer) Close() error { return s.l.Close() }

func newTCPListener(etcd *fakeEtcd) (*TCPListener, *fakeDiscoverd, error) {
	discoverd := newFakeDiscoverd()
	if etcd == nil {
		etcd = newFakeEtcd()
	}
	l := NewTCPListener("127.0.0.1", NewEtcdDataStore(etcd, "/strowger/tcp/"), discoverd)
	return l, discoverd, l.Start()
}

func assertTCPConn(c *C, addr, prefix string) {
	conn, err := net.Dial("tcp", addr)
	c.Assert(err, IsNil)
	conn.Write([]byte("asdf"))
	conn.(*net.TCPConn).CloseWrite()
	res, err := ioutil.ReadAll(conn)
	conn.Close()

	c.Assert(err, IsNil)
	c.Assert(string(res), Equals, prefix+"asdf")
}

func (s *S) TestAddTCPRoute(c *C) {
	const addr, port, portInt = "127.0.0.1:45000", "45000", 45000
	srv1 := NewTCPTestServer("1")
	srv2 := NewTCPTestServer("2")
	defer srv1.Close()
	defer srv2.Close()

	l, discoverd, err := newTCPListener(nil)
	c.Assert(err, IsNil)
	defer l.Close()

	discoverd.Register("test", srv1.Addr)
	defer discoverd.UnregisterAll()

	wait := waitForEvent(c, l, "add", port)
	err = l.AddRoute(&strowger.TCPRoute{Port: portInt, Service: "test"})
	c.Assert(err, IsNil)
	wait()

	assertTCPConn(c, addr, "1")

	discoverd.Unregister("test", srv1.Addr)
	discoverd.Register("test", srv2.Addr)

	assertTCPConn(c, addr, "2")

	wait = waitForEvent(c, l, "remove", port)
	err = l.RemoveRoute(port)
	c.Assert(err, IsNil)
	wait()

	_, err = net.Dial("tcp", addr)
	c.Assert(err, Not(IsNil))
}

func (s *S) TestInitialTCPSync(c *C) {
	const addr, port = "127.0.0.1:45000", 45000
	etcd := newFakeEtcd()
	l, _, err := newTCPListener(etcd)
	c.Assert(err, IsNil)
	wait := waitForEvent(c, l, "add", strconv.Itoa(port))
	err = l.AddRoute(&strowger.TCPRoute{Service: "test", Port: port})
	c.Assert(err, IsNil)
	wait()
	l.Close()

	srv := NewTCPTestServer("1")
	defer srv.Close()

	l, discoverd, err := newTCPListener(etcd)
	c.Assert(err, IsNil)
	defer l.Close()

	discoverd.Register("test", srv.Addr)
	defer discoverd.UnregisterAll()

	assertTCPConn(c, addr, "1")
}
<|endoftext|>


<commit_before>package scripturebot

import (
	"bytes"
	"encoding/json"
	"log"
	"net/http"
	"strconv"
	"strings"
)

type TelegramSender struct {
	Id        int    `json:"id"`
	Bot       bool   `json:"is_bot"`
	Firstname string `json:"first_name"`
	Lastname  string `json:"last_name"`
	Username  string `json:"username"`
	Language  string `json:"langauge_code"`
}

type TelegramChat struct {
	Id        int    `json:"id"`
	Firstname string `json:"first_name"`
	Lastname  string `json:"last_name"`
	Username  string `json:"username"`
	Type      string `json:"type"`
}

type TelegramMessage struct {
	Sender TelegramSender `json:"from"`
	Chat   TelegramChat   `json:"chat"`
	Text   string         `json:"text"`
	Id     int            `json:"message_id"`
}

type TelegramRequest struct {
	Message TelegramMessage `json:"message"`
}

type TelegramPost struct {
	Id      string `json:"chat_id"`
	Text    string `json:"text"`
	ReplyId string `json:"reply_to_message_id"`
}

type InlineButton struct {
	Text string `json:"text"`
	Url  string `json:"url"`
}

type InlineMarkup struct {
	Keyboard [][]InlineButton `json:"inline_keyboard"`
}

type TelegramInlinePost struct {
	TelegramPost
	Markup InlineMarkup `json:"reply_markup"`
}

type KeyButton struct {
	Text string `json:"text"`
}

type ReplyMarkup struct {
	Keyboard  [][]KeyButton `json:"keyboard"`
	Resize    bool          `json:"resize_keyboard"`
	Once      bool          `json:"one_time_keyboard`
	Selective bool          `json:"selective"`
}

type TelegramReplyPost struct {
	TelegramPost
	Markup ReplyMarkup `json:"reply_markup"`
}

type RemoveMarkup struct {
	Remove    bool `json:"remove_keyboard"`
	Selective bool `json:"selective"`
}

type TelegramRemovePost struct {
	TelegramPost
	Markup RemoveMarkup `json:"reply_markup`
}

func TelegramTranslate(body []byte, env *SessionData) bool {
	log.Printf("Parsing Telegram message")

	var data TelegramRequest
	err := json.Unmarshal(body, &data)
	if err != nil {
		log.Fatalf("Failed to unmarshal request body: %v", err)
		return false
	}

	env.User.Firstname = data.Message.Sender.Firstname
	env.User.Lastname = data.Message.Sender.Lastname
	env.User.Username = data.Message.Sender.Username
	env.User.Id = strconv.Itoa(data.Message.Sender.Id)
	env.User.Type = TYPE_TELEGRAM

	log.Printf("User: %s %s | %s : %s", env.User.Firstname, env.User.Lastname, env.User.Username, env.User.Id)

	tokens := strings.Split(data.Message.Text, " ")
	if strings.Index(tokens[0], "/") == 0 {
		env.Msg.Command = string((tokens[0])[1:])
	}
	env.Msg.Message = strings.Replace(data.Message.Text, env.Msg.Command, "", 1)
	env.Msg.Id = strconv.Itoa(data.Message.Id)

	env.Channel = strconv.Itoa(data.Message.Chat.Id)

	log.Printf("Message: %s | %s", env.Msg.Command, env.Msg.Message)

	return true
}

func PostTelegram(env *SessionData) bool {
	endpoint := "https://api.telegram.org/bot" + env.Secrets.TELEGRAM_ID + "/sendMessage"
	header := "application/json;charset=utf-8"

	var base TelegramPost
	base.Id = env.User.Id
	base.ReplyId = env.Msg.Id
	base.Text = env.Res.Message

	var data []byte
	var err error

	if env.Res.Affordances != nil {
		if len(env.Res.Affordances.Options) > 0 {
			if env.Res.Affordances.Inline {
				var buttons []InlineButton
				for i := 0; i < len(env.Res.Affordances.Options); i++ {
					buttons = append(buttons, InlineButton{env.Res.Affordances.Options[i].Text, env.Res.Affordances.Options[i].Link})
				}
				var markup InlineMarkup
				markup.Keyboard = append([][]InlineButton{}, buttons)
				var message TelegramInlinePost
				message.TelegramPost = base
				message.Markup = markup
				data, err = json.Marshal(message)
			} else {
				var buttons []KeyButton
				for i := 0; i < len(env.Res.Affordances.Options); i++ {
					buttons = append(buttons, KeyButton{env.Res.Affordances.Options[i].Text})
				}
				var markup ReplyMarkup
				markup.Keyboard = append([][]KeyButton{}, buttons)
				var message TelegramReplyPost
				message.TelegramPost = base
				message.Markup = markup
				data, err = json.Marshal(message)
			}
		} else if env.Res.Affordances.Remove {
			var message TelegramRemovePost
			message.TelegramPost = base
			message.Markup.Remove = true
			message.Markup.Selective = true
			data, err = json.Marshal(message)
		}
	} else {
		data, err = json.Marshal(base)
	}

	if err != nil {
		log.Fatalf("Error occurred during conversion to JSON: %v", err)
		return false
	}

	buffer := bytes.NewBuffer(data)
	_, err = http.Post(endpoint, header, buffer)
	if err != nil {
		log.Fatalf("Error occurred during post: %v", err)
		return false
	}

	return true
}
<commit_msg>Fixing command stripping<commit_after>package scripturebot

import (
	"bytes"
	"encoding/json"
	"log"
	"net/http"
	"strconv"
	"strings"
)

type TelegramSender struct {
	Id        int    `json:"id"`
	Bot       bool   `json:"is_bot"`
	Firstname string `json:"first_name"`
	Lastname  string `json:"last_name"`
	Username  string `json:"username"`
	Language  string `json:"langauge_code"`
}

type TelegramChat struct {
	Id        int    `json:"id"`
	Firstname string `json:"first_name"`
	Lastname  string `json:"last_name"`
	Username  string `json:"username"`
	Type      string `json:"type"`
}

type TelegramMessage struct {
	Sender TelegramSender `json:"from"`
	Chat   TelegramChat   `json:"chat"`
	Text   string         `json:"text"`
	Id     int            `json:"message_id"`
}

type TelegramRequest struct {
	Message TelegramMessage `json:"message"`
}

type TelegramPost struct {
	Id      string `json:"chat_id"`
	Text    string `json:"text"`
	ReplyId string `json:"reply_to_message_id"`
}

type InlineButton struct {
	Text string `json:"text"`
	Url  string `json:"url"`
}

type InlineMarkup struct {
	Keyboard [][]InlineButton `json:"inline_keyboard"`
}

type TelegramInlinePost struct {
	TelegramPost
	Markup InlineMarkup `json:"reply_markup"`
}

type KeyButton struct {
	Text string `json:"text"`
}

type ReplyMarkup struct {
	Keyboard  [][]KeyButton `json:"keyboard"`
	Resize    bool          `json:"resize_keyboard"`
	Once      bool          `json:"one_time_keyboard`
	Selective bool          `json:"selective"`
}

type TelegramReplyPost struct {
	TelegramPost
	Markup ReplyMarkup `json:"reply_markup"`
}

type RemoveMarkup struct {
	Remove    bool `json:"remove_keyboard"`
	Selective bool `json:"selective"`
}

type TelegramRemovePost struct {
	TelegramPost
	Markup RemoveMarkup `json:"reply_markup`
}

func TelegramTranslate(body []byte, env *SessionData) bool {
	log.Printf("Parsing Telegram message")

	var data TelegramRequest
	err := json.Unmarshal(body, &data)
	if err != nil {
		log.Fatalf("Failed to unmarshal request body: %v", err)
		return false
	}

	env.User.Firstname = data.Message.Sender.Firstname
	env.User.Lastname = data.Message.Sender.Lastname
	env.User.Username = data.Message.Sender.Username
	env.User.Id = strconv.Itoa(data.Message.Sender.Id)
	env.User.Type = TYPE_TELEGRAM

	log.Printf("User: %s %s | %s : %s", env.User.Firstname, env.User.Lastname, env.User.Username, env.User.Id)

	tokens := strings.Split(data.Message.Text, " ")
	if strings.Index(tokens[0], "/") == 0 {
		env.Msg.Command = string((tokens[0])[1:])                                // Get the first token and strip off the prefix
		data.Message.Text = strings.Replace(data.Message.Text, tokens[0], "", 1) // Replace the command
	}
	env.Msg.Message = data.Message.Text
	env.Msg.Id = strconv.Itoa(data.Message.Id)

	env.Channel = strconv.Itoa(data.Message.Chat.Id)

	log.Printf("Message: %s | %s", env.Msg.Command, env.Msg.Message)

	return true
}

func PostTelegram(env *SessionData) bool {
	endpoint := "https://api.telegram.org/bot" + env.Secrets.TELEGRAM_ID + "/sendMessage"
	header := "application/json;charset=utf-8"

	var base TelegramPost
	base.Id = env.User.Id
	base.ReplyId = env.Msg.Id
	base.Text = env.Res.Message

	var data []byte
	var err error

	if env.Res.Affordances != nil {
		if len(env.Res.Affordances.Options) > 0 {
			if env.Res.Affordances.Inline {
				var buttons []InlineButton
				for i := 0; i < len(env.Res.Affordances.Options); i++ {
					buttons = append(buttons, InlineButton{env.Res.Affordances.Options[i].Text, env.Res.Affordances.Options[i].Link})
				}
				var markup InlineMarkup
				markup.Keyboard = append([][]InlineButton{}, buttons)
				var message TelegramInlinePost
				message.TelegramPost = base
				message.Markup = markup
				data, err = json.Marshal(message)
			} else {
				var buttons []KeyButton
				for i := 0; i < len(env.Res.Affordances.Options); i++ {
					buttons = append(buttons, KeyButton{env.Res.Affordances.Options[i].Text})
				}
				var markup ReplyMarkup
				markup.Keyboard = append([][]KeyButton{}, buttons)
				var message TelegramReplyPost
				message.TelegramPost = base
				message.Markup = markup
				data, err = json.Marshal(message)
			}
		} else if env.Res.Affordances.Remove {
			var message TelegramRemovePost
			message.TelegramPost = base
			message.Markup.Remove = true
			message.Markup.Selective = true
			data, err = json.Marshal(message)
		}
	} else {
		data, err = json.Marshal(base)
	}

	if err != nil {
		log.Fatalf("Error occurred during conversion to JSON: %v", err)
		return false
	}

	buffer := bytes.NewBuffer(data)
	_, err = http.Post(endpoint, header, buffer)
	if err != nil {
		log.Fatalf("Error occurred during post: %v", err)
		return false
	}

	return true
}
<|endoftext|>


<commit_before>package dev

import (
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"strconv"

	"github.com/kardianos/osext"
	"github.com/mitchellh/go-homedir"
)

func Setup() error {
	err := os.MkdirAll(etcDir, 0755)
	if err != nil {
		return err
	}

	var ok bool

	sudo := os.Getenv("SUDO_USER")
	if sudo != "" {
		uid, err1 := strconv.Atoi(os.Getenv("SUDO_UID"))
		gid, err2 := strconv.Atoi(os.Getenv("SUDO_GID"))

		if err1 == nil && err2 == nil {
			fmt.Printf("* Configuring %s to be owned by %s\n", etcDir, sudo)

			err := os.Chown(etcDir, uid, gid)
			if err != nil {
				return err
			}

			err = os.Chmod(etcDir, 0755)
			if err != nil {
				return err
			}

			ok = true
		}
	}

	if !ok {
		fmt.Printf("* Configuring %s to be world writable\n")
		err := os.Chmod(etcDir, 0777)
		if err != nil {
			return err
		}
	}

	return nil
}

func mustExpand(str string) string {
	str, err := homedir.Expand(str)
	if err != nil {
		panic(err)
	}

	return str
}

func Cleanup() {
	oldSetup := "/Library/LaunchDaemons/io.puma.devsetup.plist"

	exec.Command("launchctl", "unload", oldSetup).Run()
	os.Remove(oldSetup)
	exec.Command("pfctl", "-F", "nat", "-a", "com.apple/250.PumaDevFirewall").Run()

	fmt.Printf("* Expunged old puma dev system rules\n")

	// Fix perms of the LaunchAgent
	uid, err1 := strconv.Atoi(os.Getenv("SUDO_UID"))
	gid, err2 := strconv.Atoi(os.Getenv("SUDO_GID"))

	if err1 == nil && err2 == nil {
		plist := mustExpand("~/Library/LaunchAgents/io.puma.dev.plist")
		os.Chown(plist, uid, gid)

		fmt.Printf("* Fixed permissions of user LaunchAgent\n")
	}
}

func InstallIntoSystem(listenPort, tlsPort int, dir, domains, timeout string) error {
	err := SetupOurCert()
	if err != nil {
		return err
	}

	binPath, err := osext.Executable()
	if err != nil {
		return err
	}

	fmt.Printf("* Use '%s' as the location of puma-dev\n", binPath)

	var userTemplate = `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
   <key>Label</key>
   <string>io.puma.dev</string>
   <key>ProgramArguments</key>
   <array>
     <string>%s</string>
     <string>-launchd</string>
     <string>-dir</string>
     <string>%s</string>
     <string>-d</string>
     <string>%s</string>
     <string>-timeout</string>
     <string>%s</string>
   </array>
   <key>KeepAlive</key>
   <true/>
   <key>RunAtLoad</key>
   <true/>
   <key>Sockets</key>
   <dict>
       <key>Socket</key>
       <dict>
           <key>SockNodeName</key>
           <string>0.0.0.0</string>
           <key>SockServiceName</key>
           <string>%d</string>
       </dict>
       <key>SocketTLS</key>
       <dict>
           <key>SockNodeName</key>
           <string>0.0.0.0</string>
           <key>SockServiceName</key>
           <string>%d</string>
       </dict>
   </dict>
   <key>StandardOutPath</key>
   <string>%s</string>
   <key>StandardErrorPath</key>
   <string>%s</string>
</dict>
</plist>
`

	logPath := mustExpand("~/Library/Logs/puma-dev.log")

	plist := mustExpand("~/Library/LaunchAgents/io.puma.dev.plist")

	err = ioutil.WriteFile(
		plist,
		[]byte(fmt.Sprintf(userTemplate, binPath, dir, domains, timeout, listenPort, tlsPort, logPath, logPath)),
		0644,
	)

	if err != nil {
		return err
	}

	// Unload a previous one if need be.
	exec.Command("launchctl", "unload", plist).Run()

	err = exec.Command("launchctl", "load", plist).Run()
	if err != nil {
		return err
	}

	fmt.Printf("* Installed puma-dev on ports: http %d, https %d\n", listenPort, tlsPort)

	return nil
}

func Uninstall(domains []string) {
	plist := mustExpand("~/Library/LaunchAgents/io.puma.dev.plist")

	// Unload a previous one if need be.
	exec.Command("launchctl", "unload", plist).Run()

	os.Remove(plist)

	fmt.Printf("* Removed puma-dev from automatically running\n")

	for _, d := range domains {
		os.Remove(filepath.Join("/etc/resolver", d))
		fmt.Printf("* Removed domain '%s'\n", d)
	}
}
<commit_msg>Fix perms of /etc/resolver to allow access. Fixes #39<commit_after>package dev

import (
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"strconv"

	"github.com/kardianos/osext"
	"github.com/mitchellh/go-homedir"
)

func Setup() error {
	err := os.MkdirAll(etcDir, 0755)
	if err != nil {
		return err
	}

	var ok bool

	sudo := os.Getenv("SUDO_USER")
	if sudo != "" {
		uid, err1 := strconv.Atoi(os.Getenv("SUDO_UID"))
		gid, err2 := strconv.Atoi(os.Getenv("SUDO_GID"))

		if err1 == nil && err2 == nil {
			fmt.Printf("* Configuring %s to be owned by %s\n", etcDir, sudo)

			err := os.Chown(etcDir, uid, gid)
			if err != nil {
				return err
			}

			err = os.Chmod(etcDir, 0755)
			if err != nil {
				return err
			}

			files, err := ioutil.ReadDir(etcDir)
			if err != nil {
				return err
			}

			for _, fi := range files {
				path := filepath.Join(etcDir, fi.Name())
				fmt.Printf("* Changing '%s' to be owned by %s\n", path, sudo)

				err = os.Chown(path, uid, gid)
				if err != nil {
					return err
				}

				err = os.Chmod(path, 0644)
				if err != nil {
					return err
				}
			}

			ok = true
		}
	}

	if !ok {
		fmt.Printf("* Configuring %s to be world writable\n")
		err := os.Chmod(etcDir, 0777)
		if err != nil {
			return err
		}
	}

	return nil
}

func mustExpand(str string) string {
	str, err := homedir.Expand(str)
	if err != nil {
		panic(err)
	}

	return str
}

func Cleanup() {
	oldSetup := "/Library/LaunchDaemons/io.puma.devsetup.plist"

	exec.Command("launchctl", "unload", oldSetup).Run()
	os.Remove(oldSetup)
	exec.Command("pfctl", "-F", "nat", "-a", "com.apple/250.PumaDevFirewall").Run()

	fmt.Printf("* Expunged old puma dev system rules\n")

	// Fix perms of the LaunchAgent
	uid, err1 := strconv.Atoi(os.Getenv("SUDO_UID"))
	gid, err2 := strconv.Atoi(os.Getenv("SUDO_GID"))

	if err1 == nil && err2 == nil {
		plist := mustExpand("~/Library/LaunchAgents/io.puma.dev.plist")
		os.Chown(plist, uid, gid)

		fmt.Printf("* Fixed permissions of user LaunchAgent\n")
	}
}

func InstallIntoSystem(listenPort, tlsPort int, dir, domains, timeout string) error {
	err := SetupOurCert()
	if err != nil {
		return err
	}

	binPath, err := osext.Executable()
	if err != nil {
		return err
	}

	fmt.Printf("* Use '%s' as the location of puma-dev\n", binPath)

	var userTemplate = `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
   <key>Label</key>
   <string>io.puma.dev</string>
   <key>ProgramArguments</key>
   <array>
     <string>%s</string>
     <string>-launchd</string>
     <string>-dir</string>
     <string>%s</string>
     <string>-d</string>
     <string>%s</string>
     <string>-timeout</string>
     <string>%s</string>
   </array>
   <key>KeepAlive</key>
   <true/>
   <key>RunAtLoad</key>
   <true/>
   <key>Sockets</key>
   <dict>
       <key>Socket</key>
       <dict>
           <key>SockNodeName</key>
           <string>0.0.0.0</string>
           <key>SockServiceName</key>
           <string>%d</string>
       </dict>
       <key>SocketTLS</key>
       <dict>
           <key>SockNodeName</key>
           <string>0.0.0.0</string>
           <key>SockServiceName</key>
           <string>%d</string>
       </dict>
   </dict>
   <key>StandardOutPath</key>
   <string>%s</string>
   <key>StandardErrorPath</key>
   <string>%s</string>
</dict>
</plist>
`

	logPath := mustExpand("~/Library/Logs/puma-dev.log")

	plist := mustExpand("~/Library/LaunchAgents/io.puma.dev.plist")

	err = ioutil.WriteFile(
		plist,
		[]byte(fmt.Sprintf(userTemplate, binPath, dir, domains, timeout, listenPort, tlsPort, logPath, logPath)),
		0644,
	)

	if err != nil {
		return err
	}

	// Unload a previous one if need be.
	exec.Command("launchctl", "unload", plist).Run()

	err = exec.Command("launchctl", "load", plist).Run()
	if err != nil {
		return err
	}

	fmt.Printf("* Installed puma-dev on ports: http %d, https %d\n", listenPort, tlsPort)

	return nil
}

func Uninstall(domains []string) {
	plist := mustExpand("~/Library/LaunchAgents/io.puma.dev.plist")

	// Unload a previous one if need be.
	exec.Command("launchctl", "unload", plist).Run()

	os.Remove(plist)

	fmt.Printf("* Removed puma-dev from automatically running\n")

	for _, d := range domains {
		os.Remove(filepath.Join("/etc/resolver", d))
		fmt.Printf("* Removed domain '%s'\n", d)
	}
}
<|endoftext|>


<commit_before>package main

import (
	"eaciit/gdrj/model"
	"eaciit/gdrj/modules"
	"os"
	//"runtime"

	"sync"
	"time"

	"github.com/eaciit/dbox"
	//"github.com/eaciit/orm/v1"
	"github.com/eaciit/toolkit"

	"flag"
)

var mutex = new(sync.Mutex)
var conn dbox.IConnection
var (
	compute              string
	periodFrom, periodTo int
	dateFrom, dateTo     time.Time
)

func setinitialconnection() {
	var err error
	conn, err = modules.GetDboxIConnection("db_godrej")

	if err != nil {
		toolkit.Println("Initial connection found : ", err)
		os.Exit(1)
	}

	err = gdrj.SetDb(conn)
	if err != nil {
		toolkit.Println("Initial connection found : ", err)
		os.Exit(1)
	}
}

var masters = toolkit.M{}

/*
func getCursor(obj orm.IModel) dbox.ICursor {
	c, e := gdrj.Find(obj,
		nil, nil)
		//toolkit.M{}.Set("take", 10))
	if e != nil {
		return nil
	}
	return c
}
*/

var subchannels = toolkit.M{}

func prepMaster() {
	toolkit.Println("--> SUBCHANNEL")
	c, _ := conn.NewQuery().From("subchannels").Cursor(nil)
	defer c.Close()

	for {
		m := toolkit.M{}
		e := c.Fetch(&m, 1, false)
		if e != nil {
			break
		}
		subchannels.Set(m.GetString("_id"), m.GetString("title"))
	}
}

func makeDateFromInt(i int, endofmth bool) time.Time {
	yr := int(toolkit.ToFloat64(float64(i)/float64(100), 0, toolkit.RoundingDown))
	m := i - 100*yr
	dt := time.Date(yr, time.Month(m), 1, 0, 0, 0, 0, time.UTC)
	if endofmth {
		dt = dt.AddDate(0, 1, 0).AddDate(0, 0, -1)
	}
	return dt
}

var pldatas = map[string]*gdrj.PLDataModel{}

var t0 time.Time

func main() {
	flag.IntVar(&periodFrom, "from", 0, "YYYYMM representation of period from. Default is 0")
	flag.IntVar(&periodTo, "to", 0, "YYYYMM representation of period to. Default is 0 (equal to from)")
	flag.Parse()
	if periodFrom == 0 && periodTo == 0 {
		dateFrom = makeDateFromInt(201404, false)
	} else {
		dateFrom = makeDateFromInt(periodFrom, false)
	}
	dateTo = makeDateFromInt(periodTo, true)

	setinitialconnection()
	defer gdrj.CloseDb()

	t0 = time.Now()
	toolkit.Printfn("Model Updater v 1.0")
	toolkit.Printfn("Compute: %s", compute)

	var f *dbox.Filter
	if periodFrom == 0 && periodTo == 0 {
		toolkit.Printfn("Period: All")
	} else if periodTo == 0 {
		toolkit.Printfn("Period: %s",
			toolkit.Date2String(dateFrom, "MMM-yyyy"))
		f = dbox.Eq("date.date", dateFrom)
	} else {
		toolkit.Printfn("Period: %s to %s",
			toolkit.Date2String(dateFrom, "dd-MMM-yyyy"),
			toolkit.Date2String(dateTo, "dd-MMM-yyyy"))
		f = dbox.And(dbox.Gte("date.date", dateFrom), dbox.Lte("date.date", dateTo))
	}
	toolkit.Printfn("Run :%v", t0)

	toolkit.Println("Reading Master")
	prepMaster()

	//spl := new(gdrj.SalesPL)
	//toolkit.Println("Delete existing")
	//conn.NewQuery().From(spl.TableName()).Delete().Exec(nil)

	//f = dbox.Eq("_id", "CN/GBP/15000011_10")
	c, _ := gdrj.Find(new(gdrj.SalesPL), f, nil)
	defer c.Close()

	count := c.Count()
	jobs := make(chan *gdrj.SalesPL, count)
	result := make(chan string, count)
	for wi := 0; wi < 10; wi++ {
		go workerProc(wi, jobs, result)
	}

	toolkit.Printfn("START ... %d records", count)
	step := count / 100
	limit := step
	i := 0
	for {
		stx := new(gdrj.SalesPL)
		e := c.Fetch(stx, 1, false)
		if e != nil {
			break
		}

		if i == 1000 {
			//break
		}

		i++
		jobs <- stx
		if i >= limit {
			toolkit.Printfn("Processing %d of %d (%d pct) in %s",
				i, count, i*100/count, time.Since(t0).String())
			limit += step
		}
	}
	close(jobs)

	count = i
	step = count / 100
	limit = step
	for ri := 0; ri < i; ri++ {
		<-result

		if ri >= limit {
			toolkit.Printfn("Saving %d of %d (%d pct) in %s",
				ri, count, ri*100/count, time.Since(t0).String())
			limit += step
		}
	}
}

func workerProc(wi int, jobs <-chan *gdrj.SalesPL, result chan<- string) {
	workerConn, _ := modules.GetDboxIConnection("db_godrej")
	defer workerConn.Close()

	var spl *gdrj.SalesPL
	for spl = range jobs {

		//-- update channel and subchannel
		subchannel := subchannels.GetString(spl.Customer.CustType)
		if spl.Customer.ChannelID == "I1" {
			spl.Customer.ReportChannel = "RD"
			spl.Customer.ReportSubChannel = "RD"
		} else if spl.Customer.ChannelID == "I3" {
			spl.Customer.ReportChannel = "MT"
			if subchannel == "" {
				spl.Customer.ReportSubChannel = subchannels.GetString("M3")
			} else {
				spl.Customer.ReportSubChannel = subchannel
			}
		} else if spl.Customer.ChannelID == "I4" {
			spl.Customer.ReportChannel = "IT"
			spl.Customer.ReportSubChannel = "IT"
		} else if spl.Customer.ChannelID == "I6" {
			spl.Customer.ReportChannel = "Motoris"
			spl.Customer.ReportSubChannel = "Motoris"
		} else {
			spl.Customer.ChannelID = "I2"
			spl.Customer.ReportChannel = "GT"
			subchannel := subchannels.GetString(spl.Customer.CustType)
			if subchannel == "" {
				spl.Customer.ReportSubChannel = "R18 - Lain-lain"
			} else {
				spl.Customer.ReportSubChannel = subchannel
			}
		}

		workerConn.NewQuery().From(spl.TableName()).
			Save().Exec(toolkit.M{}.Set("data", spl))

		result <- spl.ID
	}
}
<commit_msg>fix calculate<commit_after>package main

import (
	"eaciit/gdrj/model"
	"eaciit/gdrj/modules"
	"os"
	//"runtime"

	"sync"
	"time"

	"github.com/eaciit/dbox"
	//"github.com/eaciit/orm/v1"
	"github.com/eaciit/toolkit"

	"flag"
)

var mutex = new(sync.Mutex)
var conn dbox.IConnection
var (
	compute              string
	periodFrom, periodTo int
	dateFrom, dateTo     time.Time
)

func setinitialconnection() {
	var err error
	conn, err = modules.GetDboxIConnection("db_godrej")

	if err != nil {
		toolkit.Println("Initial connection found : ", err)
		os.Exit(1)
	}

	err = gdrj.SetDb(conn)
	if err != nil {
		toolkit.Println("Initial connection found : ", err)
		os.Exit(1)
	}
}

var masters = toolkit.M{}

/*
func getCursor(obj orm.IModel) dbox.ICursor {
	c, e := gdrj.Find(obj,
		nil, nil)
		//toolkit.M{}.Set("take", 10))
	if e != nil {
		return nil
	}
	return c
}
*/

var subchannels = toolkit.M{}

func prepMaster() {
	toolkit.Println("--> SUBCHANNEL")
	c, _ := conn.NewQuery().From("subchannels").Cursor(nil)
	defer c.Close()

	for {
		m := toolkit.M{}
		e := c.Fetch(&m, 1, false)
		if e != nil {
			break
		}
		subchannels.Set(m.GetString("_id"), m.GetString("title"))
	}
}

func makeDateFromInt(i int, endofmth bool) time.Time {
	yr := int(toolkit.ToFloat64(float64(i)/float64(100), 0, toolkit.RoundingDown))
	m := i - 100*yr
	dt := time.Date(yr, time.Month(m), 1, 0, 0, 0, 0, time.UTC)
	if endofmth {
		dt = dt.AddDate(0, 1, 0).AddDate(0, 0, -1)
	}
	return dt
}

var pldatas = map[string]*gdrj.PLDataModel{}

var t0 time.Time

func main() {
	flag.IntVar(&periodFrom, "from", 0, "YYYYMM representation of period from. Default is 0")
	flag.IntVar(&periodTo, "to", 0, "YYYYMM representation of period to. Default is 0 (equal to from)")
	flag.Parse()
	if periodFrom == 0 && periodTo == 0 {
		dateFrom = makeDateFromInt(201404, false)
	} else {
		dateFrom = makeDateFromInt(periodFrom, false)
	}
	dateTo = makeDateFromInt(periodTo, true)

	setinitialconnection()
	defer gdrj.CloseDb()

	t0 = time.Now()
	toolkit.Printfn("Model Updater v 1.0")
	toolkit.Printfn("Compute: %s", compute)

	var f *dbox.Filter
	if periodFrom == 0 && periodTo == 0 {
		toolkit.Printfn("Period: All")
	} else if periodTo == 0 {
		toolkit.Printfn("Period: %s",
			toolkit.Date2String(dateFrom, "MMM-yyyy"))
		f = dbox.Eq("date.date", dateFrom)
	} else {
		toolkit.Printfn("Period: %s to %s",
			toolkit.Date2String(dateFrom, "dd-MMM-yyyy"),
			toolkit.Date2String(dateTo, "dd-MMM-yyyy"))
		f = dbox.And(dbox.Gte("date.date", dateFrom), dbox.Lte("date.date", dateTo))
	}
	toolkit.Printfn("Run :%v", t0)

	toolkit.Println("Reading Master")
	prepMaster()

	//spl := new(gdrj.SalesPL)
	//toolkit.Println("Delete existing")
	//conn.NewQuery().From(spl.TableName()).Delete().Exec(nil)

	//f = dbox.Eq("_id", "CN/GBP/15000011_10")
	c, _ := gdrj.Find(new(gdrj.SalesPL), f, nil)
	defer c.Close()

	count := c.Count()
	jobs := make(chan *gdrj.SalesPL, count)
	result := make(chan string, count)
	for wi := 0; wi < 10; wi++ {
		go workerProc(wi, jobs, result)
	}

	toolkit.Printfn("START ... %d records", count)
	step := count / 100
	limit := step
	i := 0
	for {
		stx := new(gdrj.SalesPL)
		e := c.Fetch(stx, 1, false)
		if e != nil {
			break
		}

		if i == 1000 {
			//break
		}

		i++
		jobs <- stx
		if i >= limit {
			toolkit.Printfn("Processing %d of %d (%d pct) in %s",
				i, count, i*100/count, time.Since(t0).String())
			limit += step
		}
	}
	close(jobs)

	count = i
	step = count / 100
	limit = step
	for ri := 0; ri < i; ri++ {
		<-result

		if ri >= limit {
			toolkit.Printfn("Saving %d of %d (%d pct) in %s",
				ri, count, ri*100/count, time.Since(t0).String())
			limit += step
		}
	}
}

func workerProc(wi int, jobs <-chan *gdrj.SalesPL, result chan<- string) {
	workerConn, _ := modules.GetDboxIConnection("db_godrej")
	defer workerConn.Close()

	var spl *gdrj.SalesPL
	for spl = range jobs {

		//-- update channel and subchannel
		subchannel := subchannels.GetString(spl.Customer.CustType)
		if spl.Customer.ChannelID == "I1" {
			spl.Customer.ReportChannel = "RD"
			spl.Customer.ReportSubChannel = "RD"
		} else if spl.Customer.ChannelID == "I3" {
			spl.Customer.ReportChannel = "MT"
			if subchannel == "" {
				spl.Customer.ReportSubChannel = subchannels.GetString("M3")
			} else {
				spl.Customer.ReportSubChannel = subchannel
			}
		} else if spl.Customer.ChannelID == "I4" {
			spl.Customer.ReportChannel = "IT"
			spl.Customer.ReportSubChannel = "IT"
		} else if spl.Customer.ChannelID == "I6" {
			spl.Customer.ReportChannel = "Motoris"
			spl.Customer.ReportSubChannel = "Motoris"
		} else {
			spl.Customer.ChannelID = "I2"
			spl.Customer.ReportChannel = "GT"
			subchannel := subchannels.GetString(spl.Customer.CustType)
			if subchannel == "" {
				spl.Customer.ReportSubChannel = "R18 - Lain-lain"
			} else {
				spl.Customer.ReportSubChannel = subchannel
			}
		}

		spl.CalcSum(masters)

		workerConn.NewQuery().From(spl.TableName()).
			Save().Exec(toolkit.M{}.Set("data", spl))

		result <- spl.ID
	}
}
<|endoftext|>