<commit_before>// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// This file contains the handlers that serve go-import redirects for Go
// sub-repositories. It specifies the mapping from import paths like
// "golang.org/x/tools" to the actual repository locations.

package main

import (
	"html/template"
	"log"
	"net/http"
	"strings"
)

const xPrefix = "/x/"

type xRepo struct {
	URL, VCS string
}

var xMap = map[string]xRepo{
	"benchmarks": {"https://code.google.com/p/go.benchmarks", "hg"},
	"blog":       {"https://code.google.com/p/go.blog", "hg"},
	"codereview": {"https://code.google.com/p/go.codereview", "hg"},
	"crypto":     {"https://code.google.com/p/go.crypto", "hg"},
	"exp":        {"https://code.google.com/p/go.exp", "hg"},
	"image":      {"https://code.google.com/p/go.image", "hg"},
	"mobile":     {"https://code.google.com/p/go.mobile", "hg"},
	"net":        {"https://code.google.com/p/go.net", "hg"},
	"sys":        {"https://code.google.com/p/go.sys", "hg"},
	"talks":      {"https://code.google.com/p/go.talks", "hg"},
	"text":       {"https://code.google.com/p/go.text", "hg"},
	"tools":      {"https://code.google.com/p/go.tools", "hg"},

	"oauth2": {"https://go.googlesource.com/oauth2", "git"},
	"review": {"https://go.googlesource.com/review", "git"},
}

func init() {
	http.HandleFunc(xPrefix, xHandler)
}

func xHandler(w http.ResponseWriter, r *http.Request) {
	head, tail := strings.TrimPrefix(r.URL.Path, xPrefix), ""
	if i := strings.Index(head, "/"); i != -1 {
		head, tail = head[:i], head[i:]
	}
	repo, ok := xMap[head]
	if !ok {
		http.NotFound(w, r)
		return
	}
	data := struct {
		Prefix, Head, Tail string
		Repo               xRepo
	}{xPrefix, head, tail, repo}
	if err := xTemplate.Execute(w, data); err != nil {
		log.Println("xHandler:", err)
	}
}

var xTemplate = template.Must(template.New("x").Parse(`<!DOCTYPE html>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="go-import" content="golang.org{{.Prefix}}{{.Head}} {{.Repo.VCS}} {{.Repo.URL}}">
<meta http-equiv="refresh" content="0; url=https://godoc.org/golang.org{{.Prefix}}{{.Head}}{{.Tail}}">

Nothing to see here; move along.

`))
<commit_msg>x/tools/cmd/godoc: update golang.org/x/ redirects to point to git repos<commit_after>// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// This file contains the handlers that serve go-import redirects for Go
// sub-repositories. It specifies the mapping from import paths like
// "golang.org/x/tools" to the actual repository locations.

package main

import (
	"html/template"
	"log"
	"net/http"
	"strings"
)

const xPrefix = "/x/"

type xRepo struct {
	URL, VCS string
}

var xMap = map[string]xRepo{
	"codereview": {"https://code.google.com/p/go.codereview", "hg"},

	"benchmarks": {"https://go.googlesource.com/benchmarks", "git"},
	"blog":       {"https://go.googlesource.com/blog", "git"},
	"crypto":     {"https://go.googlesource.com/crypto", "git"},
	"exp":        {"https://go.googlesource.com/exp", "git"},
	"image":      {"https://go.googlesource.com/image", "git"},
	"mobile":     {"https://go.googlesource.com/mobile", "git"},
	"net":        {"https://go.googlesource.com/net", "git"},
	"oauth2":     {"https://go.googlesource.com/oauth2", "git"},
	"review":     {"https://go.googlesource.com/review", "git"},
	"sys":        {"https://go.googlesource.com/sys", "git"},
	"talks":      {"https://go.googlesource.com/talks", "git"},
	"text":       {"https://go.googlesource.com/text", "git"},
	"tools":      {"https://go.googlesource.com/tools", "git"},
}

func init() {
	http.HandleFunc(xPrefix, xHandler)
}

func xHandler(w http.ResponseWriter, r *http.Request) {
	head, tail := strings.TrimPrefix(r.URL.Path, xPrefix), ""
	if i := strings.Index(head, "/"); i != -1 {
		head, tail = head[:i], head[i:]
	}
	repo, ok := xMap[head]
	if !ok {
		http.NotFound(w, r)
		return
	}
	data := struct {
		Prefix, Head, Tail string
		Repo               xRepo
	}{xPrefix, head, tail, repo}
	if err := xTemplate.Execute(w, data); err != nil {
		log.Println("xHandler:", err)
	}
}

var xTemplate = template.Must(template.New("x").Parse(`<!DOCTYPE html>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="go-import" content="golang.org{{.Prefix}}{{.Head}} {{.Repo.VCS}} {{.Repo.URL}}">
<meta http-equiv="refresh" content="0; url=https://godoc.org/golang.org{{.Prefix}}{{.Head}}{{.Tail}}">

Nothing to see here; move along.

`))
<|endoftext|>


<commit_before>package cmd

import (
	"fmt"
	"os"
	"os/exec"

	"github.com/urfave/cli"
)

func KubectlCommand() cli.Command {
	return cli.Command{
		Name:            "kubectl",
		Usage:           "Run kubectl commands",
		Description:     "Use the current kubectl context to run commands",
		Action:          runKubectl,
		SkipFlagParsing: true,
	}
}

func runKubectl(ctx *cli.Context) error {
	args := ctx.Args()
	if len(args) > 0 && (args[0] == "-h" || args[0] == "--help") {
		return cli.ShowCommandHelp(ctx, "kubectl")
	}

	path, err := exec.LookPath("kubectl")
	if nil != err {
		return fmt.Errorf("kubectl is required to use this command: %s", err.Error())
	}

	cmd := exec.Command(path, ctx.Args()...)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	cmd.Stdin = os.Stdin
	err = cmd.Run()
	if nil != err {
		return err
	}
	return nil
}
<commit_msg>Update kubectl to use cluster kubeconfig<commit_after>package cmd

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

	"github.com/urfave/cli"
)

func KubectlCommand() cli.Command {
	return cli.Command{
		Name:            "kubectl",
		Usage:           "Run kubectl commands",
		Description:     "Use the current cluster context to run kubectl commands in the cluster",
		Action:          runKubectl,
		SkipFlagParsing: true,
	}
}

func runKubectl(ctx *cli.Context) error {
	args := ctx.Args()
	if len(args) > 0 && (args[0] == "-h" || args[0] == "--help") {
		return cli.ShowCommandHelp(ctx, "kubectl")
	}

	path, err := exec.LookPath("kubectl")
	if nil != err {
		return fmt.Errorf("kubectl is required to be set in your path to use this "+
			"command. See https://kubernetes.io/docs/tasks/tools/install-kubectl/ "+
			"for more info. Error: %s", err.Error())
	}

	c, err := GetClient(ctx)
	if err != nil {
		return err
	}

	cluster, err := getClusterByID(c, c.UserConfig.FocusedCluster())
	if nil != err {
		return err
	}

	config, err := c.ManagementClient.Cluster.ActionGenerateKubeconfig(cluster)
	if nil != err {
		return err
	}

	tmpfile, err := ioutil.TempFile("", "rancher-")
	if nil != err {
		return err
	}
	defer os.Remove(tmpfile.Name())

	_, err = tmpfile.Write([]byte(config.Config))
	if nil != err {
		return err
	}

	err = tmpfile.Close()
	if nil != err {
		return err
	}

	kubeLocationFlag := "--kubeconfig=" + tmpfile.Name()

	combinedArgs := []string{kubeLocationFlag}
	combinedArgs = append(combinedArgs, ctx.Args()...)

	cmd := exec.Command(path, combinedArgs...)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	cmd.Stdin = os.Stdin
	err = cmd.Run()
	if nil != err {
		return err
	}
	return nil
}
<|endoftext|>


<commit_before>package main

import (
	"bytes"
	"crypto/md5"
	"encoding/hex"
	"errors"
	"flag"
	"fmt"
	"io"
	"net"
	"net/url"
	"os"
	"path/filepath"
	"strings"
	"sync"
	"time"
)

const defaultBufferSize = 1024

//Common error types.
var (
	//ErrPoolClosed is returned when trying to acquire/release a net.Conn from
	//a closed pool.
	ErrPoolClosed = errors.New("pool closed")

	//ErrInvalidTag is returned when trying to acquire/release a net.Conn from a
	//pool with a tag that is not known by the pool. This error can also be
	//returned when creating a pool if not tag is supplied or the given tag is
	//not hex encoded.
	ErrInvalidTag = errors.New("invalid tag")
)

type pool struct {
	mu     sync.Mutex
	urls   map[byte]url.URL
	conns  map[byte]chan net.Conn
	closed bool
}

//New creates a new pool from urls setting for each urls a channel of net.Conn
//with cap of size.
func New(urls []string, size int) (*pool, error) {
	if size <= 0 {
		return nil, fmt.Errorf("invalid size")
	}
	infos := make(map[byte]url.URL)
	conns := make(map[byte]chan net.Conn)

	for _, u := range urls {
		uri, err := url.Parse(u)
		if err != nil {
			return nil, err
		}
		raw, err := hex.DecodeString(uri.Query().Get("tag"))
		if err != nil || len(raw) == 0 {
			return nil, ErrInvalidTag
		}
		tag := raw[0]
		infos[tag] = *uri
		conns[tag] = make(chan net.Conn, size)
	}
	return &pool{urls: infos, conns: conns}, nil
}

//Acquire returns a net.Conn for the given tag. ErrPoolClosed is returned if
//the pool is closed and ErrInvalidErr if the tag can not be found. An net.Error
//is returned when a new net.Conn fails to be created.
func (p *pool) Acquire(t byte) (net.Conn, error) {
	q, ok := p.conns[t]
	if !ok {
		return nil, ErrInvalidTag
	}
	var client net.Conn
	select {
	case c, ok := <-q:
		if !ok {
			return nil, ErrPoolClosed
		}
		client = c
	default:
		uri := p.urls[t]
		c, err := net.Dial(uri.Scheme, uri.Host)
		if err != nil {
			return nil, err
		}
		client = c
	}
	return client, nil
}

func (p *pool) Release(t byte, c net.Conn) error {
	p.mu.Lock()
	defer p.mu.Unlock()

	if p.closed {
		return c.Close()
	}
	q, ok := p.conns[t]
	if !ok {
		return ErrInvalidTag
	}
	select {
	case q <- c:
		return nil
	default:
		return c.Close()
	}
}

func (p *pool) Close() error {
	p.mu.Lock()
	defer p.mu.Unlock()
	if p.closed {
		return ErrPoolClosed
	}
	p.closed = true

	var err error
	for t, q := range p.conns {
		close(q)
		for c := range q {
			err = c.Close()
		}
		delete(p.conns, t)
		delete(p.urls, t)
	}
	return err
}

func init() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "usage: %s [-c] [-d] [-v] [-s] <addr> <remote>\n", os.Args[0])
		os.Exit(1)
	}
}

func main() {
	config := struct {
		Verbose bool
		Size    int
		Count   int
		Datadir string
	}{}
	flag.StringVar(&config.Datadir, "d", config.Datadir, "data dir")
	flag.BoolVar(&config.Verbose, "v", config.Verbose, "verbose")
	flag.IntVar(&config.Size, "s", config.Size, "size")
	flag.IntVar(&config.Count, "c", config.Count, "count")
	flag.Parse()

	switch flag.NArg() {
	case 0:
		flag.Usage()
		return
	case 1:
		queue, err := accept(flag.Arg(0), config.Size)
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			os.Exit(1)
		}
		for buf := range queue {
			if len(buf) == 0 {
				continue
			}
			fmt.Fprintln(os.Stdout, hex.Dump(buf))
		}
	default:
		queue, err := accept(flag.Arg(0), config.Size)
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			os.Exit(1)
		}
		p, err := New(flag.Args()[1:], config.Count)
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			os.Exit(1)
		}
		defer p.Close()
		transmit(p, queue, config.Datadir, config.Verbose)
	}
}

func transmit(p *pool, queue <-chan []byte, d string, v bool) {
	var counter uint64
	Loop:
	for buf := range queue {
		if len(buf) == 0 {
			continue
		}
		c, err := p.Acquire(buf[0])
		counter++
		if v {
			go func(b []byte, c uint64) {
				fmt.Fprintf(os.Stderr, "%s | %8d | %8d | %x\n", time.Now().Format(time.RFC3339), c, len(b), md5.Sum(b))
			}(buf, counter)
		}
		switch {
		case err == ErrPoolClosed:
			break Loop
		case err == ErrInvalidTag && d != "":
			go func(chunk []byte) {
				if len(chunk) == 0 {
					return
				}
				t := time.Now().Format("20060102_150405")
				f, err := os.Create(filepath.Join(d, t+".dat"))
				if err != nil {
					return
				}
				defer f.Close()
				f.Write(chunk)
			}(buf[:])
		case err == nil:
			if _, err := c.Write(buf); err == nil {
				p.Release(buf[0], c)
			}
		default:
			continue
		}

	}
}

func accept(addr string, z int) (<-chan []byte, error) {
	uri, err := url.Parse(addr)
	if err != nil {
		return nil, err
	}

	switch s := strings.ToLower(uri.Scheme); s {
	case "tcp", "tcp4", "tcp6", "udp", "udp4", "udp6":
		addr = uri.Host
	case "unix":
		addr = uri.Path
	}
	serv, err := net.Listen(uri.Scheme, addr)
	if err != nil {
		return nil, err
	}
	queue := make(chan []byte)
	go func() {
		defer func() {
			close(queue)
			serv.Close()
		}()
		var wg sync.WaitGroup
		for {
			c, err := serv.Accept()
			if err != nil {
				return
			}
			wg.Add(1)
			go recv(c, queue, z, &wg)
		}
		wg.Wait()
	}()
	return queue, nil
}

func recv(c net.Conn, queue chan []byte, z int, wg *sync.WaitGroup) {
	defer func() {
		c.Close()
		wg.Done()
	}()
	if z <= 0 {
		z = defaultBufferSize
	}

	var buf bytes.Buffer
	for {
		var abort bool
	Loop:
		for {
			chunk := make([]byte, z)
			r, err := c.Read(chunk)
			switch {
			case err == io.EOF:
				abort = true
			case err != nil:
				buf.Reset()
				break Loop
			}
			buf.Write(chunk[:r])
			if r < z {
				break
			}
		}
		queue <- buf.Bytes()
		if abort {
			return
		}
		buf.Reset()
	}
}
<commit_msg>used default on config<commit_after>package main

import (
	"bytes"
	"crypto/md5"
	"encoding/hex"
	"errors"
	"flag"
	"fmt"
	"io"
	"net"
	"net/url"
	"os"
	"path/filepath"
	"strings"
	"sync"
	"time"
)

const defaultBufferSize = 1024

//Common error types.
var (
	//ErrPoolClosed is returned when trying to acquire/release a net.Conn from
	//a closed pool.
	ErrPoolClosed = errors.New("pool closed")

	//ErrInvalidTag is returned when trying to acquire/release a net.Conn from a
	//pool with a tag that is not known by the pool. This error can also be
	//returned when creating a pool if not tag is supplied or the given tag is
	//not hex encoded.
	ErrInvalidTag = errors.New("invalid tag")
)

type pool struct {
	mu     sync.Mutex
	urls   map[byte]url.URL
	conns  map[byte]chan net.Conn
	closed bool
}

//New creates a new pool from urls setting for each urls a channel of net.Conn
//with cap of size.
func New(urls []string, size int) (*pool, error) {
	if size <= 0 {
		return nil, fmt.Errorf("invalid size")
	}
	infos := make(map[byte]url.URL)
	conns := make(map[byte]chan net.Conn)

	for _, u := range urls {
		uri, err := url.Parse(u)
		if err != nil {
			return nil, err
		}
		raw, err := hex.DecodeString(uri.Query().Get("tag"))
		if err != nil || len(raw) == 0 {
			return nil, ErrInvalidTag
		}
		tag := raw[0]
		infos[tag] = *uri
		conns[tag] = make(chan net.Conn, size)
	}
	return &pool{urls: infos, conns: conns}, nil
}

//Acquire returns a net.Conn for the given tag. ErrPoolClosed is returned if
//the pool is closed and ErrInvalidErr if the tag can not be found. An net.Error
//is returned when a new net.Conn fails to be created.
func (p *pool) Acquire(t byte) (net.Conn, error) {
	q, ok := p.conns[t]
	if !ok {
		return nil, ErrInvalidTag
	}
	var client net.Conn
	select {
	case c, ok := <-q:
		if !ok {
			return nil, ErrPoolClosed
		}
		client = c
	default:
		uri := p.urls[t]
		c, err := net.Dial(uri.Scheme, uri.Host)
		if err != nil {
			return nil, err
		}
		client = c
	}
	return client, nil
}

func (p *pool) Release(t byte, c net.Conn) error {
	p.mu.Lock()
	defer p.mu.Unlock()

	if p.closed {
		return c.Close()
	}
	q, ok := p.conns[t]
	if !ok {
		return ErrInvalidTag
	}
	select {
	case q <- c:
		return nil
	default:
		return c.Close()
	}
}

func (p *pool) Close() error {
	p.mu.Lock()
	defer p.mu.Unlock()
	if p.closed {
		return ErrPoolClosed
	}
	p.closed = true

	var err error
	for t, q := range p.conns {
		close(q)
		for c := range q {
			err = c.Close()
		}
		delete(p.conns, t)
		delete(p.urls, t)
	}
	return err
}

func init() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "usage: %s [-c] [-d] [-v] [-s] <addr> <remote>\n", os.Args[0])
		os.Exit(1)
	}
}

func main() {
	config := struct {
		Verbose bool
		Size    int
		Count   int
		Datadir string
	}{Size: defaultBufferSize, Count: 5}

	flag.StringVar(&config.Datadir, "d", config.Datadir, "data dir")
	flag.BoolVar(&config.Verbose, "v", config.Verbose, "verbose")
	flag.IntVar(&config.Size, "s", config.Size, "size")
	flag.IntVar(&config.Count, "c", config.Count, "count")
	flag.Parse()

	switch flag.NArg() {
	case 0:
		flag.Usage()
		return
	case 1:
		queue, err := accept(flag.Arg(0), config.Size)
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			os.Exit(1)
		}
		for buf := range queue {
			if len(buf) == 0 {
				continue
			}
			fmt.Fprintln(os.Stdout, hex.Dump(buf))
		}
	default:
		queue, err := accept(flag.Arg(0), config.Size)
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			os.Exit(1)
		}
		p, err := New(flag.Args()[1:], config.Count)
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			os.Exit(1)
		}
		defer p.Close()
		transmit(p, queue, config.Datadir, config.Verbose)
	}
}

func transmit(p *pool, queue <-chan []byte, d string, v bool) {
	var counter uint64
Loop:
	for buf := range queue {
		if len(buf) == 0 {
			continue
		}
		c, err := p.Acquire(buf[0])
		counter++
		if v {
			go func(b []byte, c uint64) {
				fmt.Fprintf(os.Stderr, "%s | %8d | %8d | %x\n", time.Now().Format(time.RFC3339), c, len(b), md5.Sum(b))
			}(buf, counter)
		}
		switch {
		case err == ErrPoolClosed:
			break Loop
		case err == ErrInvalidTag && d != "":
			go func(chunk []byte) {
				if len(chunk) == 0 {
					return
				}
				t := time.Now().Format("20060102_150405")
				f, err := os.Create(filepath.Join(d, t+".dat"))
				if err != nil {
					return
				}
				defer f.Close()
				f.Write(chunk)
			}(buf[:])
		case err == nil:
			if _, err := c.Write(buf); err == nil {
				p.Release(buf[0], c)
			}
		default:
			continue
		}

	}
}

func accept(addr string, z int) (<-chan []byte, error) {
	uri, err := url.Parse(addr)
	if err != nil {
		return nil, err
	}

	switch s := strings.ToLower(uri.Scheme); s {
	case "tcp", "tcp4", "tcp6", "udp", "udp4", "udp6":
		addr = uri.Host
	case "unix":
		addr = uri.Path
	}
	serv, err := net.Listen(uri.Scheme, addr)
	if err != nil {
		return nil, err
	}
	queue := make(chan []byte)
	go func() {
		defer func() {
			close(queue)
			serv.Close()
		}()
		var wg sync.WaitGroup
		for {
			c, err := serv.Accept()
			if err != nil {
				return
			}
			wg.Add(1)
			go recv(c, queue, z, &wg)
		}
		wg.Wait()
	}()
	return queue, nil
}

func recv(c net.Conn, queue chan []byte, z int, wg *sync.WaitGroup) {
	defer func() {
		c.Close()
		wg.Done()
	}()
	if z <= 0 {
		z = defaultBufferSize
	}

	var buf bytes.Buffer
	for {
		var abort bool
	Loop:
		for {
			chunk := make([]byte, z)
			r, err := c.Read(chunk)
			switch {
			case err == io.EOF:
				abort = true
			case err != nil:
				buf.Reset()
				break Loop
			}
			buf.Write(chunk[:r])
			if r < z {
				break
			}
		}
		queue <- buf.Bytes()
		if abort {
			return
		}
		buf.Reset()
	}
}
<|endoftext|>


<commit_before>package pod

import (
	"gopkg.in/urfave/cli.v2"
)

var GetPodAction = &cli.Command{
	Name: "pod",
}
<commit_msg>add get pod command<commit_after>package clipod

import (
	"fmt"

	"github.com/containerum/chkit/cmd/util"
	"github.com/containerum/chkit/pkg/model"
	"github.com/containerum/chkit/pkg/model/pod"
	"gopkg.in/urfave/cli.v2"
)

var GetPodAction = &cli.Command{
	Name: "pod",
	Action: func(ctx *cli.Context) error {
		log := util.GetLog(ctx)
		client := util.GetClient(ctx)
		var showItem model.Renderer
		var err error
		switch ctx.NArg() {
		case 0:
			fmt.Println(ctx.Command.UsageText)
			return nil
		case 1:
			namespaceLabel := ctx.Args().First()
			log.Debugf("getting pod list from %q", namespaceLabel)
			showItem, err = client.GetPodList(namespaceLabel)
			if err != nil {
				return err
			}
		default:
			var list pod.PodList
			var gainedPod pod.Pod
			namespaceLabel := ctx.Args().First()
			log.Debugf("getting pods")
			for _, podName := range ctx.Args().Tail() {
				log.Debugf("getting %q", podName)
				gainedPod, err = client.GetPod(namespaceLabel, podName)
				if err != nil {
					return err
				}
				list = append(list, gainedPod)
			}
			showItem = list
		}
		err = util.WriteData(ctx, showItem)
		if err != nil {
			log.Debugf("fatal error: %v", err)
		}
		return err
	},
	Flags: []cli.Flag{
		&cli.StringFlag{
			Name: "json",
		},
		&cli.StringFlag{
			Name: "yaml",
		},
	},
}
<|endoftext|>


<commit_before>// Copyright (c) 2015-2021 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package cmd

import (
	"context"
	"os"
	"os/exec"
	"syscall"
)

// Type of service signals currently supported.
type serviceSignal int

const (
	serviceRestart       serviceSignal = iota // Restarts the server.
	serviceStop                               // Stops the server.
	serviceReloadDynamic                      // Reload dynamic config values.
	// Add new service requests here.
)

// Global service signal channel.
var globalServiceSignalCh chan serviceSignal

// GlobalServiceDoneCh - Global service done channel.
var GlobalServiceDoneCh <-chan struct{}

// GlobalContext context that is canceled when server is requested to shut down.
var GlobalContext context.Context

// cancelGlobalContext can be used to indicate server shutdown.
var cancelGlobalContext context.CancelFunc

func initGlobalContext() {
	GlobalContext, cancelGlobalContext = context.WithCancel(context.Background())
	GlobalServiceDoneCh = GlobalContext.Done()
	globalServiceSignalCh = make(chan serviceSignal)
}

// restartProcess starts a new process passing it the active fd's. It
// doesn't fork, but starts a new process using the same environment and
// arguments as when it was originally started. This allows for a newly
// deployed binary to be started. It returns the pid of the newly started
// process when successful.
func restartProcess() error {
	// Use the original binary location. This works with symlinks such that if
	// the file it points to has been changed we will use the updated symlink.
	argv0, err := exec.LookPath(os.Args[0])
	if err != nil {
		return err
	}

	// Invokes the execve system call.
	// Re-uses the same pid. This preserves the pid over multiple server-respawns.
	return syscall.Exec(argv0, os.Args, os.Environ())
}
<commit_msg>Remove unused GlobalServiceDoneCh (#13578)<commit_after>// Copyright (c) 2015-2021 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package cmd

import (
	"context"
	"os"
	"os/exec"
	"syscall"
)

// Type of service signals currently supported.
type serviceSignal int

const (
	serviceRestart       serviceSignal = iota // Restarts the server.
	serviceStop                               // Stops the server.
	serviceReloadDynamic                      // Reload dynamic config values.
	// Add new service requests here.
)

// Global service signal channel.
var globalServiceSignalCh chan serviceSignal

// GlobalContext context that is canceled when server is requested to shut down.
var GlobalContext context.Context

// cancelGlobalContext can be used to indicate server shutdown.
var cancelGlobalContext context.CancelFunc

func initGlobalContext() {
	GlobalContext, cancelGlobalContext = context.WithCancel(context.Background())
	globalServiceSignalCh = make(chan serviceSignal)
}

// restartProcess starts a new process passing it the active fd's. It
// doesn't fork, but starts a new process using the same environment and
// arguments as when it was originally started. This allows for a newly
// deployed binary to be started. It returns the pid of the newly started
// process when successful.
func restartProcess() error {
	// Use the original binary location. This works with symlinks such that if
	// the file it points to has been changed we will use the updated symlink.
	argv0, err := exec.LookPath(os.Args[0])
	if err != nil {
		return err
	}

	// Invokes the execve system call.
	// Re-uses the same pid. This preserves the pid over multiple server-respawns.
	return syscall.Exec(argv0, os.Args, os.Environ())
}
<|endoftext|>


<commit_before>package cmd

import (
	"errors"
	"fmt"
	"log"

	"path/filepath"

	"github.com/bitrise-io/go-utils/cmdex"
	"github.com/bitrise-io/go-utils/colorstring"
	"github.com/bitrise-io/go-utils/fileutil"
	"github.com/bitrise-io/go-utils/pathutil"
	"github.com/spf13/cobra"
)

const (
	vagrantBoxName = "bitrise-replica-macos"
)

// vagrantCmd represents the vagrant command
var vagrantCmd = &cobra.Command{
	Use:   "vagrant DESTINATION_DIR_PATH VAGRANT_BOX_PATH",
	Short: `Create a vagrant VM, using the vagrant box`,
	Long: `Create a vagrant VM, using the vagrant box.

NOTE: You can create the vagrant box with: replica create box`,
	RunE: func(cmd *cobra.Command, args []string) error {
		if len(args) < 2 {
			return errors.New("No vagrant box or destination directory path provided")
		}
		destinationDirPath := args[0]
		vagrantBoxPath := args[1]

		return createVagrantVM(vagrantBoxPath, destinationDirPath)
	},
}

func init() {
	createCmd.AddCommand(vagrantCmd)
}

func createVagrantVM(vagrantBoxPath, destinationDirPath string) error {
	fmt.Println()
	log.Println(colorstring.Green(" => Registering the vagrant box:"), vagrantBoxPath)
	printFreeDiskSpace()

	{
		cmd := cmdex.NewCommandWithStandardOuts("vagrant",
			"box", "add",
			"--force",
			"--name", vagrantBoxName,
			vagrantBoxPath,
		)

		fmt.Println()
		log.Printf("$ %s", cmd.PrintableCommandArgs())
		fmt.Println()
		if err := cmd.Run(); err != nil {
			return fmt.Errorf("Failed to run command, error: %s", err)
		}
	}

	fmt.Println()
	printFreeDiskSpace()
	log.Println(colorstring.Green(" => vagrant box registered! [OK]"))

	fmt.Println()
	log.Println(colorstring.Green(" => Creating and booting vagrant VM at path"), destinationDirPath)

	if err := pathutil.EnsureDirExist(destinationDirPath); err != nil {
		return fmt.Errorf("Failed to create vagrant VM destination directory (path: %s), error: %s", destinationDirPath, err)
	}

	const vagrantFileContent = `# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.box = "bitrise-replica-macos"
  config.vm.synced_folder ".", "/vagrant", :disabled => true
  config.ssh.insert_key = false
end
`

	if err := fileutil.WriteStringToFile(filepath.Join(destinationDirPath, "Vagrantfile"), vagrantFileContent); err != nil {
		return fmt.Errorf("Failed to write Vagrantfile into the destination directory, error: %s", err)
	}

	{
		cmd := cmdex.NewCommandWithStandardOuts("vagrant",
			"up",
		).SetDir(destinationDirPath)

		fmt.Println()
		log.Printf("$ %s", cmd.PrintableCommandArgs())
		fmt.Println()
		if err := cmd.Run(); err != nil {
			return fmt.Errorf("Failed to run command, error: %s", err)
		}
	}

	printFreeDiskSpace()
	log.Println(colorstring.Green(" => vagrant VM created & ready! [OK]"))
	return nil
}
<commit_msg>replica create vagrant - auto create a snapshot (#3)<commit_after>package cmd

import (
	"errors"
	"fmt"
	"log"

	"path/filepath"

	"github.com/bitrise-io/go-utils/cmdex"
	"github.com/bitrise-io/go-utils/colorstring"
	"github.com/bitrise-io/go-utils/fileutil"
	"github.com/bitrise-io/go-utils/pathutil"
	"github.com/spf13/cobra"
)

const (
	vagrantBoxName           = "bitrise-replica-macos"
	vagrantInitialSnapshotID = "bitrise-replica-initial"
)

// vagrantCmd represents the vagrant command
var vagrantCmd = &cobra.Command{
	Use:   "vagrant DESTINATION_DIR_PATH VAGRANT_BOX_PATH",
	Short: `Create a vagrant VM, using the vagrant box`,
	Long: `Create a vagrant VM, using the vagrant box.

NOTE: You can create the vagrant box with: replica create box`,
	RunE: func(cmd *cobra.Command, args []string) error {
		if len(args) < 2 {
			return errors.New("No vagrant box or destination directory path provided")
		}
		destinationDirPath := args[0]
		vagrantBoxPath := args[1]

		return createVagrantVM(vagrantBoxPath, destinationDirPath)
	},
}

func init() {
	createCmd.AddCommand(vagrantCmd)
}

func createVagrantVM(vagrantBoxPath, destinationDirPath string) error {
	fmt.Println()
	log.Println(colorstring.Green(" => Registering the vagrant box:"), vagrantBoxPath)
	printFreeDiskSpace()

	{
		cmd := cmdex.NewCommandWithStandardOuts("vagrant",
			"box", "add",
			"--force",
			"--name", vagrantBoxName,
			vagrantBoxPath,
		)

		fmt.Println()
		log.Printf("$ %s", cmd.PrintableCommandArgs())
		fmt.Println()
		if err := cmd.Run(); err != nil {
			return fmt.Errorf("Failed to run command, error: %s", err)
		}
	}

	fmt.Println()
	printFreeDiskSpace()
	log.Println(colorstring.Green(" => vagrant box registered! [OK]"))

	fmt.Println()
	log.Println(colorstring.Green(" => Creating and booting vagrant VM at path"), destinationDirPath)

	if err := pathutil.EnsureDirExist(destinationDirPath); err != nil {
		return fmt.Errorf("Failed to create vagrant VM destination directory (path: %s), error: %s", destinationDirPath, err)
	}

	const vagrantFileContent = `# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.box = "bitrise-replica-macos"
  config.vm.synced_folder ".", "/vagrant", :disabled => true
  config.ssh.insert_key = false
end
`

	if err := fileutil.WriteStringToFile(filepath.Join(destinationDirPath, "Vagrantfile"), vagrantFileContent); err != nil {
		return fmt.Errorf("Failed to write Vagrantfile into the destination directory, error: %s", err)
	}

	{
		cmd := cmdex.NewCommandWithStandardOuts("vagrant",
			"up",
		).SetDir(destinationDirPath)

		fmt.Println()
		log.Printf("$ %s", cmd.PrintableCommandArgs())
		fmt.Println()
		if err := cmd.Run(); err != nil {
			return fmt.Errorf("Failed to run command, error: %s", err)
		}
	}

	printFreeDiskSpace()
	log.Println(colorstring.Green(" => vagrant VM created & ready! [OK]"))

	fmt.Println()
	log.Println(colorstring.Green(" => Creating an initial snapshot ..."))

	{
		cmd := cmdex.NewCommandWithStandardOuts("vagrant",
			"snapshot",
			"save", vagrantInitialSnapshotID,
		).SetDir(destinationDirPath)

		fmt.Println()
		log.Printf("$ %s", cmd.PrintableCommandArgs())
		fmt.Println()
		if err := cmd.Run(); err != nil {
			return fmt.Errorf("Failed to run command, error: %s", err)
		}
	}

	printFreeDiskSpace()
	log.Println(colorstring.Green(" => Snapshot created! [OK]"))
	fmt.Println(colorstring.Yellow(" NOTE: you can restore this saved snapshot state of the virtual machine with:"))
	fmt.Println(" $ vagrant snapshot restore " + vagrantInitialSnapshotID)
	fmt.Println()

	return nil
}
<|endoftext|>


<commit_before>package cmd

import (
	"fmt"

	"github.com/manabu/dockerlayer/config"
	"github.com/spf13/cobra"
)

func init() {
	RootCmd.AddCommand(versionCmd)

}

var versionCmd = &cobra.Command{
	Use:   "version",
	Short: "Print the version number of dockerlayer",
	Long:  `Print the version number of dockerlayer`,
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Printf("dockerlayer version %s (%s)\n", config.VersionString, config.CommitID)
	},
}
<commit_msg>Improve version string<commit_after>package cmd

import (
	"fmt"

	"github.com/manabu/dockerlayer/config"
	"github.com/spf13/cobra"
)

func init() {
	RootCmd.AddCommand(versionCmd)

}

var versionCmd = &cobra.Command{
	Use:   "version",
	Short: "Print the version number of dockerlayer",
	Long:  `Print the version number of dockerlayer`,
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Printf("dockerlayer Version %s Git Commit ID(%s)\n", config.VersionString, config.CommitID)
	},
}
<|endoftext|>


<commit_before>package cmd

import (
	"fmt"

	"github.com/spf13/cobra"
)

func VersionCmd() *cobra.Command {
	return &cobra.Command{
		Use:   "version",
		Short: "print the version",
		Long:  `Print the version.`,
		Run: func(cmd *cobra.Command, args []string) {
			fmt.Println("v0.2.10-beta")
		},
	}
}
<commit_msg>Version up to 1st release.<commit_after>package cmd

import (
	"fmt"

	"github.com/spf13/cobra"
)

func VersionCmd() *cobra.Command {
	return &cobra.Command{
		Use:   "version",
		Short: "print the version",
		Long:  `Print the version.`,
		Run: func(cmd *cobra.Command, args []string) {
			fmt.Println("v1.0.0")
		},
	}
}
<|endoftext|>


<commit_before>package bos

import (
	"fmt"
	"io"
	"net/http"
	"sort"
	"strconv"
	"strings"
	"time"

	"github.com/guoyao/baidubce-sdk-go/bce"
	"github.com/guoyao/baidubce-sdk-go/util"
)

const MIN_PART_NUMBER int = 1
const MAX_PART_NUMBER int = 10000

var UserDefinedMetadataPrefix = "x-bce-meta-"

var CannedAccessControlList = map[string]string{
	"Private":         "private",
	"PublicRead":      "public-read",
	"PublicReadWrite": "public-read-write",
}

// Location is a struct for bucket location info.
type Location struct {
	LocationConstraint string
}

type BucketOwner struct {
	Id          string `json:"id"`
	DisplayName string `json:"displayName,omitempty"`
}

// Bucket is a struct for bucket info.
type Bucket struct {
	Name, Location string
	CreationDate   time.Time
}

// BucketSummary is a struct for bucket summary.
type BucketSummary struct {
	Owner   BucketOwner
	Buckets []Bucket
}

type BucketAcl struct {
	Owner             BucketOwner `json:"owner"`
	AccessControlList []Grant     `json:"accessControlList"`
}

type Grant struct {
	Grantee    []BucketGrantee `json:"grantee"`
	Permission []string        `json:"permission"`
}

type BucketGrantee struct {
	Id string `json:"id"`
}

type ObjectMetadata struct {
	CacheControl       string
	ContentDisposition string
	ContentLength      int
	ContentMD5         string
	ContentType        string
	Expires            string
	ContentSha256      string

	ContentRange string
	ETag         string
	UserMetadata map[string]string
}

func NewObjectMetadataFromHeader(h http.Header) *ObjectMetadata {
	objectMetadata := &ObjectMetadata{}

	for key, _ := range h {
		key = strings.ToLower(key)

		if key == "cache-control" {
			objectMetadata.CacheControl = h.Get(key)
		} else if key == "content-disposition" {
			objectMetadata.ContentDisposition = h.Get(key)
		} else if key == "content-length" {
			length, err := strconv.Atoi(h.Get(key))

			if err == nil {
				objectMetadata.ContentLength = length
			}
		} else if key == "content-range" {
			objectMetadata.ContentRange = h.Get(key)
		} else if key == "content-type" {
			objectMetadata.ContentType = h.Get(key)
		} else if key == "expires" {
			objectMetadata.Expires = h.Get(key)
		} else if key == "etag" {
			objectMetadata.ETag = strings.Replace(h.Get(key), "\"", "", -1)
		} else if IsUserDefinedMetadata(key) {
			objectMetadata.UserMetadata[key] = h.Get(key)
		}
	}

	return objectMetadata
}

func (metadata *ObjectMetadata) AddUserMetadata(key, value string) {
	if metadata.UserMetadata == nil {
		metadata.UserMetadata = make(map[string]string)
	}

	metadata.UserMetadata[key] = value
}

func (metadata *ObjectMetadata) mergeToSignOption(option *bce.SignOption) {
	if metadata.CacheControl != "" {
		option.AddHeader("Cache-Control", metadata.CacheControl)
	}

	if metadata.ContentDisposition != "" {
		option.AddHeader("Content-Disposition", metadata.ContentDisposition)
	}

	if metadata.ContentLength != 0 {
		option.AddHeader("Content-Length", strconv.Itoa(metadata.ContentLength))
	}

	if metadata.ContentMD5 != "" {
		option.AddHeader("Content-MD5", metadata.ContentMD5)
	}

	if metadata.ContentType != "" {
		option.AddHeader("Content-Type", metadata.ContentType)
	}

	if metadata.Expires != "" {
		option.AddHeader("Expires", metadata.Expires)
	}

	if metadata.ContentSha256 != "" {
		option.AddHeader("x-bce-content-sha256", metadata.ContentSha256)
	}

	for key, value := range metadata.UserMetadata {
		option.AddHeader(ToUserDefinedMetadata(key), value)
	}
}

type PutObjectResponse http.Header

func NewPutObjectResponse(h http.Header) PutObjectResponse {
	return PutObjectResponse(h)
}

func (res PutObjectResponse) Get(key string) string {
	return http.Header(res).Get(key)
}

func (res PutObjectResponse) GetETag() string {
	return strings.Replace(res.Get("Etag"), "\"", "", -1)
}

type AppendObjectResponse http.Header

func NewAppendObjectResponse(h http.Header) AppendObjectResponse {
	return AppendObjectResponse(h)
}

func (res AppendObjectResponse) Get(key string) string {
	return http.Header(res).Get(key)
}

func (res AppendObjectResponse) GetETag() string {
	return strings.Replace(res.Get("Etag"), "\"", "", -1)
}

func (res AppendObjectResponse) GetMD5() string {
	return res.Get("Content-MD5")
}

func (res AppendObjectResponse) GetNextAppendOffset() string {
	return res.Get("x-bce-next-append-offset")
}

type ObjectSummary struct {
	Key          string
	LastModified string
	ETag         string
	Size         int64
	Owner        BucketOwner
}

type ListObjectsRequest struct {
	BucketName, Delimiter, Marker, Prefix string
	MaxKeys                               int
}

type ListObjectsResponse struct {
	Name           string
	Prefix         string
	Delimiter      string
	Marker         string
	NextMarker     string
	MaxKeys        uint
	IsTruncated    bool
	Contents       []ObjectSummary
	CommonPrefixes []map[string]string
}

func (listObjectsResponse *ListObjectsResponse) GetCommonPrefixes() []string {
	prefixes := make([]string, 0, len(listObjectsResponse.CommonPrefixes))

	for _, commonPrefix := range listObjectsResponse.CommonPrefixes {
		prefixes = append(prefixes, commonPrefix["prefix"])
	}

	return prefixes
}

type CopyObjectResponse struct {
	ETag         string
	LastModified time.Time
}

type CopyObjectRequest struct {
	SrcBucketName         string          `json:"-"`
	SrcKey                string          `json:"-"`
	DestBucketName        string          `json:"-"`
	DestKey               string          `json:"-"`
	ObjectMetadata        *ObjectMetadata `json:"-"`
	SourceMatch           string          `json:"x-bce-copy-source-if-match,omitempty"`
	SourceNoneMatch       string          `json:"x-bce-copy-source-if-none-match,omitempty"`
	SourceModifiedSince   string          `json:"x-bce-copy-source-if-modified-since,omitempty"`
	SourceUnmodifiedSince string          `json:"x-bce-copy-source-if-unmodified-since,omitempty"`
}

func (copyObjectRequest CopyObjectRequest) mergeToSignOption(option *bce.SignOption) {
	m, err := util.ToMap(copyObjectRequest)

	if err != nil {
		return
	}

	headerMap := make(map[string]string)

	for key, value := range m {
		if str, ok := value.(string); ok {
			headerMap[key] = str
		}
	}

	option.AddHeaders(headerMap)

	if copyObjectRequest.ObjectMetadata != nil {
		option.AddHeader("x-bce-metadata-directive", "replace")
		copyObjectRequest.ObjectMetadata.mergeToSignOption(option)
	} else {
		option.AddHeader("x-bce-metadata-directive", "copy")
	}
}

type Object struct {
	ObjectMetadata *ObjectMetadata
	ObjectContent  io.ReadCloser
}

type GetObjectRequest struct {
	BucketName string
	ObjectKey  string
	Range      string
}

func (getObjectRequest *GetObjectRequest) MergeToSignOption(option *bce.SignOption) {
	if getObjectRequest.Range != "" {
		option.AddHeader("Range", "bytes="+getObjectRequest.Range)
	}
}

func (getObjectRequest *GetObjectRequest) SetRange(start uint, end uint) {
	getObjectRequest.Range = fmt.Sprintf("%v-%v", start, end)
}

type DeleteMultipleObjectsResponse struct {
	Errors []DeleteMultipleObjectsError
}

type DeleteMultipleObjectsError struct {
	Key, Code, Message string
}

func (deleteMultipleObjectsError *DeleteMultipleObjectsError) Error() string {
	if deleteMultipleObjectsError.Message != "" {
		return deleteMultipleObjectsError.Message
	}

	return deleteMultipleObjectsError.Code
}

type InitiateMultipartUploadRequest struct {
	BucketName, ObjectKey string
	ObjectMetadata        *ObjectMetadata
}

type InitiateMultipartUploadResponse struct {
	Bucket, Key, UploadId string
}

type UploadPartRequest struct {
	BucketName, ObjectKey, UploadId string
	PartSize                        int64
	PartNumber                      int
	PartData                        io.Reader
}

type UploadPartResponse http.Header

func NewUploadPartResponse(h http.Header) UploadPartResponse {
	return UploadPartResponse(h)
}

func (res UploadPartResponse) Get(key string) string {
	return http.Header(res).Get(key)
}

func (res UploadPartResponse) GetETag() string {
	return strings.Replace(res.Get("Etag"), "\"", "", -1)
}

type PartSummarySlice []PartSummary

func (partSummarySlice PartSummarySlice) Len() int {
	return len(partSummarySlice)
}

func (partSummarySlice PartSummarySlice) Swap(i, j int) {
	partSummarySlice[i], partSummarySlice[j] = partSummarySlice[j], partSummarySlice[i]
}

func (partSummarySlice PartSummarySlice) Less(i, j int) bool {
	return partSummarySlice[i].PartNumber < partSummarySlice[j].PartNumber
}

type CompleteMultipartUploadRequest struct {
	BucketName, ObjectKey, UploadId string
	Parts                           []PartSummary `json:"parts"`
}

func (completeMultipartUploadRequest *CompleteMultipartUploadRequest) sort() {
	if len(completeMultipartUploadRequest.Parts) > 1 {
		sort.Sort(PartSummarySlice(completeMultipartUploadRequest.Parts))
	}
}

type CompleteMultipartUploadResponse struct {
	Location, Bucket, Key, ETag string
}

type AbortMultipartUploadRequest struct {
	BucketName, ObjectKey, UploadId string
}

type ListMultipartUploadsRequest struct {
	BucketName, Delimiter, KeyMarker, Prefix string
	MaxUploads                               int
}

type MultipartUploadSummary struct {
	Key, UploadId string
	Initiated     time.Time
	NextKeyMarker string
	Owner         BucketOwner
}

type ListMultipartUploadsResponse struct {
	Bucket         string
	Prefix         string
	Delimiter      string
	KeyMarker      string
	NextKeyMarker  string
	MaxUploads     int
	IsTruncated    bool
	Uploads        []MultipartUploadSummary
	CommonPrefixes []map[string]string
}

func (listMultipartUploadsResponse *ListMultipartUploadsResponse) GetCommonPrefixes() []string {
	prefixes := make([]string, 0, len(listMultipartUploadsResponse.CommonPrefixes))

	for _, commonPrefix := range listMultipartUploadsResponse.CommonPrefixes {
		prefixes = append(prefixes, commonPrefix["prefix"])
	}

	return prefixes
}

type ListPartsRequest struct {
	BucketName, ObjectKey, UploadId, PartNumberMarker string
	MaxParts                                          int
}

type PartSummary struct {
	PartNumber   int    `json:"partNumber"`
	ETag         string `json:"eTag"`
	LastModified time.Time
	Size         int64
}

type ListPartsResponse struct {
	Bucket               string
	Key                  string
	UploadId             string
	Initiated            time.Time
	PartNumberMarker     int
	NextPartNumberMarker int
	MaxParts             int
	IsTruncated          bool
	Owner                BucketOwner
	Parts                []PartSummary
}

func IsUserDefinedMetadata(metadata string) bool {
	return strings.Index(metadata, UserDefinedMetadataPrefix) == 0
}

func ToUserDefinedMetadata(metadata string) string {
	if IsUserDefinedMetadata(metadata) {
		return metadata
	}

	return UserDefinedMetadataPrefix + metadata
}
<commit_msg>use int64 as the type for content length<commit_after>package bos

import (
	"fmt"
	"io"
	"net/http"
	"sort"
	"strconv"
	"strings"
	"time"

	"github.com/guoyao/baidubce-sdk-go/bce"
	"github.com/guoyao/baidubce-sdk-go/util"
)

const MIN_PART_NUMBER int = 1
const MAX_PART_NUMBER int = 10000

var UserDefinedMetadataPrefix = "x-bce-meta-"

var CannedAccessControlList = map[string]string{
	"Private":         "private",
	"PublicRead":      "public-read",
	"PublicReadWrite": "public-read-write",
}

// Location is a struct for bucket location info.
type Location struct {
	LocationConstraint string
}

type BucketOwner struct {
	Id          string `json:"id"`
	DisplayName string `json:"displayName,omitempty"`
}

// Bucket is a struct for bucket info.
type Bucket struct {
	Name, Location string
	CreationDate   time.Time
}

// BucketSummary is a struct for bucket summary.
type BucketSummary struct {
	Owner   BucketOwner
	Buckets []Bucket
}

type BucketAcl struct {
	Owner             BucketOwner `json:"owner"`
	AccessControlList []Grant     `json:"accessControlList"`
}

type Grant struct {
	Grantee    []BucketGrantee `json:"grantee"`
	Permission []string        `json:"permission"`
}

type BucketGrantee struct {
	Id string `json:"id"`
}

type ObjectMetadata struct {
	CacheControl       string
	ContentDisposition string
	ContentLength      int64
	ContentMD5         string
	ContentType        string
	Expires            string
	ContentSha256      string

	ContentRange string
	ETag         string
	UserMetadata map[string]string
}

func NewObjectMetadataFromHeader(h http.Header) *ObjectMetadata {
	objectMetadata := &ObjectMetadata{}

	for key, _ := range h {
		key = strings.ToLower(key)

		if key == "cache-control" {
			objectMetadata.CacheControl = h.Get(key)
		} else if key == "content-disposition" {
			objectMetadata.ContentDisposition = h.Get(key)
		} else if key == "content-length" {
			length, err := strconv.ParseInt(h.Get(key), 10, 64)

			if err == nil {
				objectMetadata.ContentLength = length
			}
		} else if key == "content-range" {
			objectMetadata.ContentRange = h.Get(key)
		} else if key == "content-type" {
			objectMetadata.ContentType = h.Get(key)
		} else if key == "expires" {
			objectMetadata.Expires = h.Get(key)
		} else if key == "etag" {
			objectMetadata.ETag = strings.Replace(h.Get(key), "\"", "", -1)
		} else if IsUserDefinedMetadata(key) {
			objectMetadata.UserMetadata[key] = h.Get(key)
		}
	}

	return objectMetadata
}

func (metadata *ObjectMetadata) AddUserMetadata(key, value string) {
	if metadata.UserMetadata == nil {
		metadata.UserMetadata = make(map[string]string)
	}

	metadata.UserMetadata[key] = value
}

func (metadata *ObjectMetadata) mergeToSignOption(option *bce.SignOption) {
	if metadata.CacheControl != "" {
		option.AddHeader("Cache-Control", metadata.CacheControl)
	}

	if metadata.ContentDisposition != "" {
		option.AddHeader("Content-Disposition", metadata.ContentDisposition)
	}

	if metadata.ContentLength != 0 {
		option.AddHeader("Content-Length", strconv.FormatInt(metadata.ContentLength, 10))
	}

	if metadata.ContentMD5 != "" {
		option.AddHeader("Content-MD5", metadata.ContentMD5)
	}

	if metadata.ContentType != "" {
		option.AddHeader("Content-Type", metadata.ContentType)
	}

	if metadata.Expires != "" {
		option.AddHeader("Expires", metadata.Expires)
	}

	if metadata.ContentSha256 != "" {
		option.AddHeader("x-bce-content-sha256", metadata.ContentSha256)
	}

	for key, value := range metadata.UserMetadata {
		option.AddHeader(ToUserDefinedMetadata(key), value)
	}
}

type PutObjectResponse http.Header

func NewPutObjectResponse(h http.Header) PutObjectResponse {
	return PutObjectResponse(h)
}

func (res PutObjectResponse) Get(key string) string {
	return http.Header(res).Get(key)
}

func (res PutObjectResponse) GetETag() string {
	return strings.Replace(res.Get("Etag"), "\"", "", -1)
}

type AppendObjectResponse http.Header

func NewAppendObjectResponse(h http.Header) AppendObjectResponse {
	return AppendObjectResponse(h)
}

func (res AppendObjectResponse) Get(key string) string {
	return http.Header(res).Get(key)
}

func (res AppendObjectResponse) GetETag() string {
	return strings.Replace(res.Get("Etag"), "\"", "", -1)
}

func (res AppendObjectResponse) GetMD5() string {
	return res.Get("Content-MD5")
}

func (res AppendObjectResponse) GetNextAppendOffset() string {
	return res.Get("x-bce-next-append-offset")
}

type ObjectSummary struct {
	Key          string
	LastModified string
	ETag         string
	Size         int64
	Owner        BucketOwner
}

type ListObjectsRequest struct {
	BucketName, Delimiter, Marker, Prefix string
	MaxKeys                               int
}

type ListObjectsResponse struct {
	Name           string
	Prefix         string
	Delimiter      string
	Marker         string
	NextMarker     string
	MaxKeys        uint
	IsTruncated    bool
	Contents       []ObjectSummary
	CommonPrefixes []map[string]string
}

func (listObjectsResponse *ListObjectsResponse) GetCommonPrefixes() []string {
	prefixes := make([]string, 0, len(listObjectsResponse.CommonPrefixes))

	for _, commonPrefix := range listObjectsResponse.CommonPrefixes {
		prefixes = append(prefixes, commonPrefix["prefix"])
	}

	return prefixes
}

type CopyObjectResponse struct {
	ETag         string
	LastModified time.Time
}

type CopyObjectRequest struct {
	SrcBucketName         string          `json:"-"`
	SrcKey                string          `json:"-"`
	DestBucketName        string          `json:"-"`
	DestKey               string          `json:"-"`
	ObjectMetadata        *ObjectMetadata `json:"-"`
	SourceMatch           string          `json:"x-bce-copy-source-if-match,omitempty"`
	SourceNoneMatch       string          `json:"x-bce-copy-source-if-none-match,omitempty"`
	SourceModifiedSince   string          `json:"x-bce-copy-source-if-modified-since,omitempty"`
	SourceUnmodifiedSince string          `json:"x-bce-copy-source-if-unmodified-since,omitempty"`
}

func (copyObjectRequest CopyObjectRequest) mergeToSignOption(option *bce.SignOption) {
	m, err := util.ToMap(copyObjectRequest)

	if err != nil {
		return
	}

	headerMap := make(map[string]string)

	for key, value := range m {
		if str, ok := value.(string); ok {
			headerMap[key] = str
		}
	}

	option.AddHeaders(headerMap)

	if copyObjectRequest.ObjectMetadata != nil {
		option.AddHeader("x-bce-metadata-directive", "replace")
		copyObjectRequest.ObjectMetadata.mergeToSignOption(option)
	} else {
		option.AddHeader("x-bce-metadata-directive", "copy")
	}
}

type Object struct {
	ObjectMetadata *ObjectMetadata
	ObjectContent  io.ReadCloser
}

type GetObjectRequest struct {
	BucketName string
	ObjectKey  string
	Range      string
}

func (getObjectRequest *GetObjectRequest) MergeToSignOption(option *bce.SignOption) {
	if getObjectRequest.Range != "" {
		option.AddHeader("Range", "bytes="+getObjectRequest.Range)
	}
}

func (getObjectRequest *GetObjectRequest) SetRange(start uint, end uint) {
	getObjectRequest.Range = fmt.Sprintf("%v-%v", start, end)
}

type DeleteMultipleObjectsResponse struct {
	Errors []DeleteMultipleObjectsError
}

type DeleteMultipleObjectsError struct {
	Key, Code, Message string
}

func (deleteMultipleObjectsError *DeleteMultipleObjectsError) Error() string {
	if deleteMultipleObjectsError.Message != "" {
		return deleteMultipleObjectsError.Message
	}

	return deleteMultipleObjectsError.Code
}

type InitiateMultipartUploadRequest struct {
	BucketName, ObjectKey string
	ObjectMetadata        *ObjectMetadata
}

type InitiateMultipartUploadResponse struct {
	Bucket, Key, UploadId string
}

type UploadPartRequest struct {
	BucketName, ObjectKey, UploadId string
	PartSize                        int64
	PartNumber                      int
	PartData                        io.Reader
}

type UploadPartResponse http.Header

func NewUploadPartResponse(h http.Header) UploadPartResponse {
	return UploadPartResponse(h)
}

func (res UploadPartResponse) Get(key string) string {
	return http.Header(res).Get(key)
}

func (res UploadPartResponse) GetETag() string {
	return strings.Replace(res.Get("Etag"), "\"", "", -1)
}

type PartSummarySlice []PartSummary

func (partSummarySlice PartSummarySlice) Len() int {
	return len(partSummarySlice)
}

func (partSummarySlice PartSummarySlice) Swap(i, j int) {
	partSummarySlice[i], partSummarySlice[j] = partSummarySlice[j], partSummarySlice[i]
}

func (partSummarySlice PartSummarySlice) Less(i, j int) bool {
	return partSummarySlice[i].PartNumber < partSummarySlice[j].PartNumber
}

type CompleteMultipartUploadRequest struct {
	BucketName, ObjectKey, UploadId string
	Parts                           []PartSummary `json:"parts"`
}

func (completeMultipartUploadRequest *CompleteMultipartUploadRequest) sort() {
	if len(completeMultipartUploadRequest.Parts) > 1 {
		sort.Sort(PartSummarySlice(completeMultipartUploadRequest.Parts))
	}
}

type CompleteMultipartUploadResponse struct {
	Location, Bucket, Key, ETag string
}

type AbortMultipartUploadRequest struct {
	BucketName, ObjectKey, UploadId string
}

type ListMultipartUploadsRequest struct {
	BucketName, Delimiter, KeyMarker, Prefix string
	MaxUploads                               int
}

type MultipartUploadSummary struct {
	Key, UploadId string
	Initiated     time.Time
	NextKeyMarker string
	Owner         BucketOwner
}

type ListMultipartUploadsResponse struct {
	Bucket         string
	Prefix         string
	Delimiter      string
	KeyMarker      string
	NextKeyMarker  string
	MaxUploads     int
	IsTruncated    bool
	Uploads        []MultipartUploadSummary
	CommonPrefixes []map[string]string
}

func (listMultipartUploadsResponse *ListMultipartUploadsResponse) GetCommonPrefixes() []string {
	prefixes := make([]string, 0, len(listMultipartUploadsResponse.CommonPrefixes))

	for _, commonPrefix := range listMultipartUploadsResponse.CommonPrefixes {
		prefixes = append(prefixes, commonPrefix["prefix"])
	}

	return prefixes
}

type ListPartsRequest struct {
	BucketName, ObjectKey, UploadId, PartNumberMarker string
	MaxParts                                          int
}

type PartSummary struct {
	PartNumber   int    `json:"partNumber"`
	ETag         string `json:"eTag"`
	LastModified time.Time
	Size         int64
}

type ListPartsResponse struct {
	Bucket               string
	Key                  string
	UploadId             string
	Initiated            time.Time
	PartNumberMarker     int
	NextPartNumberMarker int
	MaxParts             int
	IsTruncated          bool
	Owner                BucketOwner
	Parts                []PartSummary
}

func IsUserDefinedMetadata(metadata string) bool {
	return strings.Index(metadata, UserDefinedMetadataPrefix) == 0
}

func ToUserDefinedMetadata(metadata string) string {
	if IsUserDefinedMetadata(metadata) {
		return metadata
	}

	return UserDefinedMetadataPrefix + metadata
}
<|endoftext|>


<commit_before>package bufit

import (
	"io"
	"os"
	"sync"
	"time"
)

func ExampleBuffer() {
	// Start a new buffer
	buf := New()

	// Create two readers
	r1, r2 := buf.NextReader(), buf.NextReader()

	// Broadcast a message
	io.WriteString(buf, "Hello World\n")

	// Wait
	var grp sync.WaitGroup
	grp.Add(4)

	// Read fast
	go func() {
		defer grp.Done()
		io.Copy(os.Stdout, r1) // "Hello World\n"
	}()

	// Read slow
	go func() {
		defer grp.Done()
		io.CopyN(os.Stdout, r2, 5) // "Hello"
		<-time.After(time.Second)
		io.Copy(os.Stdout, r2) // "World\n"
	}()

	// Both readers will read the entire buffer! The slow reader
	// won't block the fast one from reading ahead either.

	// Late reader
	// Since this reader joins after all existing readers have Read "Hello"
	// "Hello" has already been cleared from the Buffer, this Reader will only see
	// "World\n" and beyond.
	go func() {
		defer grp.Done()
		<-time.After(500 * time.Millisecond)
		r3 := buf.NextReader()
		io.Copy(os.Stdout, r3) // "World\n"
	}()

	// Short Reader
	// **Important!** if your reader isn't going to read until the buffer is empty
	// you'll need to call Close() when you are done with it to tell the buffer
	// it's done reading data.
	go func() {
		defer grp.Done()
		r4 := buf.NextReader()
		io.CopyN(os.Stdout, r4, 5) // "Hello"
		r4.Close()                 // tell the buffer you're done reading
	}()

	// **Important!** mark close so that readers can ret. io.EOF
	buf.Close()

	grp.Wait()
	// Output:
	// Hello World
	// HelloHelloHello World
	//  World
}
<commit_msg>working on flaky test<commit_after>package bufit

import (
	"io"
	"os"
	"sync"
	"time"
)

func ExampleBuffer() {
	// Start a new buffer
	buf := New()

	// Create two readers
	r1, r2 := buf.NextReader(), buf.NextReader()

	// Broadcast a message
	io.WriteString(buf, "Hello World\n")

	// Wait
	var grp sync.WaitGroup
	grp.Add(4)

	// Read fast
	go func() {
		defer grp.Done()
		io.Copy(os.Stdout, r1) // "Hello World\n"
	}()

	// Read slow
	go func() {
		defer grp.Done()
		<-time.After(100 * time.Millisecond)
		io.CopyN(os.Stdout, r2, 5) // "Hello"
		<-time.After(time.Second)
		io.Copy(os.Stdout, r2) // "World\n"
	}()

	// Both readers will read the entire buffer! The slow reader
	// won't block the fast one from reading ahead either.

	// Late reader
	// Since this reader joins after all existing readers have Read "Hello"
	// "Hello" has already been cleared from the Buffer, this Reader will only see
	// "World\n" and beyond.
	go func() {
		defer grp.Done()
		<-time.After(500 * time.Millisecond)
		r3 := buf.NextReader()
		io.Copy(os.Stdout, r3) // "World\n"
	}()

	// Short Reader
	// **Important!** if your reader isn't going to read until the buffer is empty
	// you'll need to call Close() when you are done with it to tell the buffer
	// it's done reading data.
	go func() {
		defer grp.Done()
		<-time.After(100 * time.Millisecond)
		r4 := buf.NextReader()
		io.CopyN(os.Stdout, r4, 5) // "Hello"
		r4.Close()                 // tell the buffer you're done reading
	}()

	// **Important!** mark close so that readers can ret. io.EOF
	buf.Close()

	grp.Wait()
	// Output:
	// Hello World
	// HelloHelloHello World
	//  World
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"net/url"
	"os"
	"os/exec"
	"path/filepath"
	"strings"

	"github.com/gonuts/commander"
	"github.com/gonuts/flag"
	"github.com/hwaf/hwaf/vcs"
	//gocfg "github.com/gonuts/config"
)

func hwaf_make_cmd_pkg_add() *commander.Command {
	cmd := &commander.Command{
		Run:       hwaf_run_cmd_pkg_add,
		UsageLine: "co [options] <pkg-uri> [<local-pkg-name>]",
		Short:     "add a package to the current workarea",
		Long: `
co adds a package to the current workarea.

ex:
 $ hwaf pkg co /foo/pkg
 $ hwaf pkg co Control/AthenaKernel
 $ hwaf pkg co git://github.com/mana-fwk/mana-core-athenakernel
 $ hwaf pkg co git://github.com/mana-fwk/mana-core-athenakernel Control/AthenaKernel
 $ hwaf pkg co -b=rel/mana git://github.com/mana-fwk/mana-core-athenakernel Control/AthenaKernel
 $ hwaf pkg co -b=AthenaKernel-00-00-01 svn+ssh://svn.cern.ch/reps/atlasoff/Control/AthenaKernel Control/AthenaKernel
`,
		Flag: *flag.NewFlagSet("hwaf-pkg-co", flag.ExitOnError),
	}
	cmd.Flag.Bool("q", true, "only print error and warning messages, all other output will be suppressed")
	cmd.Flag.String("b", "", "branch to checkout (default=master)")

	return cmd
}

func hwaf_run_cmd_pkg_add(cmd *commander.Command, args []string) {
	var err error
	n := "hwaf-pkg-" + cmd.Name()
	pkguri := ""
	pkgname := ""
	switch len(args) {
	case 1:
		pkguri = args[0]
		pkgname = ""
	case 2:
		pkguri = args[0]
		pkgname = args[1]
	default:
		err = fmt.Errorf("%s: you need to give a package URL", n)
		handle_err(err)
	}

	pkguri = os.ExpandEnv(pkguri)

	// FIXME: shouldn't this be refactorized ?
	if strings.HasPrefix(pkguri, "git@github.com:") {
		pkguri = strings.Replace(
			pkguri,
			"git@github.com:",
			"git+ssh://git@github.com/",
			1)
	}

	//pkguri = filepath.Clean(pkguri)

	uri, err := url.Parse(pkguri)
	handle_err(err)
	// fmt.Printf(">>> [%v]\n", uri)
	// fmt.Printf("    Scheme: %q\n", uri.Scheme)
	// fmt.Printf("    Opaque: %q\n", uri.Opaque)
	// fmt.Printf("    Host:   %q\n", uri.Host)
	// fmt.Printf("    Path:   %q\n", uri.Path)
	// fmt.Printf("    Fragm:  %q\n", uri.Fragment)

	switch pkgname {
	case "":
		pkgname = uri.Path
	default:
		pkgname = os.ExpandEnv(pkgname)
		pkgname = filepath.Clean(pkgname)
	}

	// FIXME: hack. we need a better "plugin architecture" for this...
	if uri.Scheme == "" && !path_exists(uri.Path) {
		if svnroot := os.Getenv("SVNROOT"); svnroot != "" {
			pkguri = svnroot + "/" + pkguri
			pkguri = os.ExpandEnv(pkguri)
			uri, err = url.Parse(pkguri)
			handle_err(err)
			// fmt.Printf(">>> [%v]\n", uri)
			// fmt.Printf("    Scheme: %q\n", uri.Scheme)
			// fmt.Printf("    Opaque: %q\n", uri.Opaque)
			// fmt.Printf("    Host:   %q\n", uri.Host)
			// fmt.Printf("    Path:   %q\n", uri.Path)
			// fmt.Printf("    Fragm:  %q\n", uri.Fragment)
		}
	}

	quiet := cmd.Flag.Lookup("q").Value.Get().(bool)
	bname := cmd.Flag.Lookup("b").Value.Get().(string)

	if !quiet {
		fmt.Printf("%s: checkout package [%s]...\n", n, pkguri)
	}

	cfg, err := g_ctx.LocalCfg()
	handle_err(err)

	pkgdir := "src"
	if cfg.HasOption("hwaf-cfg", "cmtpkgs") {
		pkgdir, err = cfg.String("hwaf-cfg", "cmtpkgs")
		handle_err(err)
	}

	dir := filepath.Join(pkgdir, pkgname)
	if filepath.IsAbs(pkgname) {
		dir = pkgname
	}

	switch uri.Scheme {
	case "svn", "svn+ssh":
		if !path_exists(filepath.Dir(dir)) {
			err = os.MkdirAll(filepath.Dir(dir), 0755)
			handle_err(err)
		}
		repo := pkguri
		if bname != "" {
			// can't use filepath.Join as it may mess-up the uri.Scheme
			repo = strings.Join([]string{pkguri, "tags", bname}, "/")
		} else {
			// can't use filepath.Join as it may mess-up the uri.Scheme
			repo = strings.Join([]string{pkguri, "trunk"}, "/")
		}

		if g_ctx.PkgDb.HasPkg(dir) {
			err = fmt.Errorf("%s: package [%s] already in db.\ndid you forget to run 'hwaf pkg rm %s' ?", n, dir, dir)
			handle_err(err)
		}

		err = vcs.Svn.Create(dir, repo)
		handle_err(err)

		err = g_ctx.PkgDb.Add("svn", repo, dir)
		handle_err(err)
		return
	}

	if g_ctx.PkgDb.HasPkg(dir) {
		err = fmt.Errorf("%s: package [%s] already in db.\ndid you forget to run 'hwaf pkg rm %s' ?", n, dir, dir)
		handle_err(err)
	}

	err = vcs.Git.Create(dir, pkguri)
	handle_err(err)

	if bname != "" {
		git := exec.Command(
			"git", "checkout", bname,
		)
		git.Dir = dir
		if !quiet {
			git.Stdout = os.Stdout
			git.Stderr = os.Stderr
		}
		err = git.Run()
		handle_err(err)
	}

	err = g_ctx.PkgDb.Add("git", pkguri, dir)
	handle_err(err)

	if !quiet {
		fmt.Printf("%s: checkout package [%s]... [ok]\n", n, pkguri)
	}
}

// EOF
<commit_msg>pkg-co: add checkout from a pkglist file<commit_after>package main

import (
	"bufio"
	"fmt"
	"io"
	"net/url"
	"os"
	"os/exec"
	"path/filepath"
	"strings"

	"github.com/gonuts/commander"
	"github.com/gonuts/flag"
	"github.com/hwaf/hwaf/vcs"
	//gocfg "github.com/gonuts/config"
)

func hwaf_make_cmd_pkg_add() *commander.Command {
	cmd := &commander.Command{
		Run:       hwaf_run_cmd_pkg_add,
		UsageLine: "co [options] <pkg-uri> [<local-pkg-name>]",
		Short:     "add a package to the current workarea",
		Long: `
co adds a package to the current workarea.

ex:
 $ hwaf pkg co /foo/pkg
 $ hwaf pkg co Control/AthenaKernel
 $ hwaf pkg co git://github.com/mana-fwk/mana-core-athenakernel
 $ hwaf pkg co git://github.com/mana-fwk/mana-core-athenakernel Control/AthenaKernel
 $ hwaf pkg co -b=rel/mana git://github.com/mana-fwk/mana-core-athenakernel Control/AthenaKernel
 $ hwaf pkg co -b=AthenaKernel-00-00-01 svn+ssh://svn.cern.ch/reps/atlasoff/Control/AthenaKernel Control/AthenaKernel
 $ hwaf pkg co -f=list.of.pkgs.txt
`,
		Flag: *flag.NewFlagSet("hwaf-pkg-co", flag.ExitOnError),
	}
	cmd.Flag.Bool("q", true, "only print error and warning messages, all other output will be suppressed")
	cmd.Flag.String("b", "", "branch to checkout (default=master)")
	cmd.Flag.String("f", "", "path to a file holding a list of packages to retrieve")

	return cmd
}

func hwaf_run_cmd_pkg_add(cmd *commander.Command, args []string) {
	var err error
	n := "hwaf-pkg-" + cmd.Name()
	pkguri := ""
	pkgname := ""
	switch len(args) {
	case 1:
		pkguri = args[0]
		pkgname = ""
	case 2:
		pkguri = args[0]
		pkgname = args[1]
	default:
		fname := cmd.Flag.Lookup("f").Value.Get().(string)
		if fname != "" {
			f, err := os.Open(fname)
			if err != nil {
				handle_err(err)
			}
			pkgs := [][]string{}
			scnr := bufio.NewScanner(f)
			for scnr.Scan() {
				line := scnr.Text()
				tokens := strings.Split(line, " ")
				pkg := []string{}
				for _, tok := range tokens {
					tok = strings.Trim(tok, " \t")
					if tok != "" {
						pkg = append(pkg, tok)
					}
				}
				if len(pkg) > 0 {
					pkgs = append(pkgs, pkg)
				}
			}
			err = scnr.Err()
			if err != nil && err != io.EOF {
				handle_err(err)
			}
			quiet := cmd.Flag.Lookup("q").Value.Get().(bool)
			for _, pkg := range pkgs {
				args := []string{"pkg", "co"}
				if !quiet {
					args = append(args, "-q=0")
				}
				switch len(pkg) {
				case 1:
					args = append(args, pkg[0])
				case 2:
					args = append(args, "-b="+pkg[1], pkg[0])
				case 3:
					args = append(args, "-b="+pkg[1], pkg[0], pkg[2])
				default:
					err = fmt.Errorf("%s: invalid number of pkg-co arguments (expected [1-3], got=%d) args=%v", n, len(pkg), pkg)
					handle_err(err)
				}
				cmd := exec.Command("hwaf", args...)
				cmd.Stdout = os.Stdout
				cmd.Stderr = os.Stderr
				cmd.Stdin = os.Stdin
				err = cmd.Run()
				handle_err(err)
			}
			return
		}

		err = fmt.Errorf("%s: you need to give a package URL", n)
		handle_err(err)
	}

	pkguri = os.ExpandEnv(pkguri)

	// FIXME: shouldn't this be refactorized ?
	if strings.HasPrefix(pkguri, "git@github.com:") {
		pkguri = strings.Replace(
			pkguri,
			"git@github.com:",
			"git+ssh://git@github.com/",
			1)
	}

	//pkguri = filepath.Clean(pkguri)

	uri, err := url.Parse(pkguri)
	handle_err(err)
	// fmt.Printf(">>> [%v]\n", uri)
	// fmt.Printf("    Scheme: %q\n", uri.Scheme)
	// fmt.Printf("    Opaque: %q\n", uri.Opaque)
	// fmt.Printf("    Host:   %q\n", uri.Host)
	// fmt.Printf("    Path:   %q\n", uri.Path)
	// fmt.Printf("    Fragm:  %q\n", uri.Fragment)

	switch pkgname {
	case "":
		pkgname = uri.Path
	default:
		pkgname = os.ExpandEnv(pkgname)
		pkgname = filepath.Clean(pkgname)
	}

	// FIXME: hack. we need a better "plugin architecture" for this...
	if uri.Scheme == "" && !path_exists(uri.Path) {
		if svnroot := os.Getenv("SVNROOT"); svnroot != "" {
			pkguri = svnroot + "/" + pkguri
			pkguri = os.ExpandEnv(pkguri)
			uri, err = url.Parse(pkguri)
			handle_err(err)
			// fmt.Printf(">>> [%v]\n", uri)
			// fmt.Printf("    Scheme: %q\n", uri.Scheme)
			// fmt.Printf("    Opaque: %q\n", uri.Opaque)
			// fmt.Printf("    Host:   %q\n", uri.Host)
			// fmt.Printf("    Path:   %q\n", uri.Path)
			// fmt.Printf("    Fragm:  %q\n", uri.Fragment)
		}
	}

	quiet := cmd.Flag.Lookup("q").Value.Get().(bool)
	bname := cmd.Flag.Lookup("b").Value.Get().(string)

	if !quiet {
		fmt.Printf("%s: checkout package [%s]...\n", n, pkguri)
	}

	cfg, err := g_ctx.LocalCfg()
	handle_err(err)

	pkgdir := "src"
	if cfg.HasOption("hwaf-cfg", "cmtpkgs") {
		pkgdir, err = cfg.String("hwaf-cfg", "cmtpkgs")
		handle_err(err)
	}

	dir := filepath.Join(pkgdir, pkgname)
	if filepath.IsAbs(pkgname) {
		dir = pkgname
	}

	switch uri.Scheme {
	case "svn", "svn+ssh":
		if !path_exists(filepath.Dir(dir)) {
			err = os.MkdirAll(filepath.Dir(dir), 0755)
			handle_err(err)
		}
		repo := pkguri
		if bname != "" {
			// can't use filepath.Join as it may mess-up the uri.Scheme
			repo = strings.Join([]string{pkguri, "tags", bname}, "/")
		} else {
			// can't use filepath.Join as it may mess-up the uri.Scheme
			repo = strings.Join([]string{pkguri, "trunk"}, "/")
		}

		if g_ctx.PkgDb.HasPkg(dir) {
			err = fmt.Errorf("%s: package [%s] already in db.\ndid you forget to run 'hwaf pkg rm %s' ?", n, dir, dir)
			handle_err(err)
		}

		err = vcs.Svn.Create(dir, repo)
		handle_err(err)

		err = g_ctx.PkgDb.Add("svn", repo, dir)
		handle_err(err)
		return
	}

	if g_ctx.PkgDb.HasPkg(dir) {
		err = fmt.Errorf("%s: package [%s] already in db.\ndid you forget to run 'hwaf pkg rm %s' ?", n, dir, dir)
		handle_err(err)
	}

	err = vcs.Git.Create(dir, pkguri)
	handle_err(err)

	if bname != "" {
		git := exec.Command(
			"git", "checkout", bname,
		)
		git.Dir = dir
		if !quiet {
			git.Stdout = os.Stdout
			git.Stderr = os.Stderr
		}
		err = git.Run()
		handle_err(err)
	}

	err = g_ctx.PkgDb.Add("git", pkguri, dir)
	handle_err(err)

	if !quiet {
		fmt.Printf("%s: checkout package [%s]... [ok]\n", n, pkguri)
	}
}

// EOF
<|endoftext|>


<commit_before>package main

import (
	"fmt"
)

var cmdVersion = &Command{
	Run:       runVersion,
	UsageLine: "version",
	Short:     "Print spwd version",
	Long:      `Print spwd version`,
}

// Version of spwd
const Version = "v1.1.0"

func runVersion(ctx context, args []string) error {
	fmt.Fprintln(ctx.out, fmt.Sprintf("spwd %s", Version))
	return nil
}
<commit_msg>Fix version<commit_after>package main

import (
	"fmt"
)

var cmdVersion = &Command{
	Run:       runVersion,
	UsageLine: "version",
	Short:     "Print spwd version",
	Long:      `Print spwd version`,
}

// Version of spwd
const Version = "v1.2.1"

func runVersion(ctx context, args []string) error {
	fmt.Fprintln(ctx.out, fmt.Sprintf("spwd %s", Version))
	return nil
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"

	"github.com/gonuts/commander"
	"github.com/gonuts/flag"
)

func hwaf_make_cmd_version() *commander.Command {
	cmd := &commander.Command{
		Run:       hwaf_run_cmd_version,
		UsageLine: "version",
		Short:     "print version and exit",
		Long: `
print version and exit.

ex:
 $ hwaf version
 hwaf-20121212
`,
		Flag: *flag.NewFlagSet("hwaf-version", flag.ExitOnError),
	}
	return cmd
}

func hwaf_run_cmd_version(cmd *commander.Command, args []string) {
	fmt.Printf("hwaf-20130628\n")
}

// EOF
<commit_msg>tagging 20130704<commit_after>package main

import (
	"fmt"

	"github.com/gonuts/commander"
	"github.com/gonuts/flag"
)

func hwaf_make_cmd_version() *commander.Command {
	cmd := &commander.Command{
		Run:       hwaf_run_cmd_version,
		UsageLine: "version",
		Short:     "print version and exit",
		Long: `
print version and exit.

ex:
 $ hwaf version
 hwaf-20121212
`,
		Flag: *flag.NewFlagSet("hwaf-version", flag.ExitOnError),
	}
	return cmd
}

func hwaf_run_cmd_version(cmd *commander.Command, args []string) {
	fmt.Printf("hwaf-20130704\n")
}

// EOF
<|endoftext|>


<commit_before>package coal

import (
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"go.mongodb.org/mongo-driver/mongo/options"
)

// A Tester provides facilities to the test a fire API.
type Tester struct {
	// The store to use for cleaning the database.
	Store *Store

	// The registered models.
	Models []Model
}

// NewTester returns a new tester.
func NewTester(store *Store, models ...Model) *Tester {
	return &Tester{
		Store:  store,
		Models: models,
	}
}

// Clean will remove the collections of models that have been registered and
// reset the header map.
func (t *Tester) Clean() {
	for _, model := range t.Models {
		// remove all is faster than dropping the collection
		_, err := t.Store.C(model).DeleteMany(nil, bson.M{})
		if err != nil {
			panic(err)
		}
	}
}

// Save will save the specified model.
func (t *Tester) Save(model Model) Model {
	// initialize model
	model = Init(model)

	// insert to collection
	_, err := t.Store.C(model).InsertOne(nil, model)
	if err != nil {
		panic(err)
	}

	return model
}

// FindAll will return all saved models.
func (t *Tester) FindAll(model Model) interface{} {
	// initialize model
	model = Init(model)

	// find all documents
	list := model.Meta().MakeSlice()
	cursor, err := t.Store.C(model).Find(nil, nil, options.Find().SetSort(Sort("_id")))
	if err != nil {
		panic(err)
	}

	// get all results
	err = cursor.All(nil, list)
	if err != nil {
		panic(err)
	}

	// initialize list
	InitSlice(list)

	return list
}

// FindLast will return the last saved model.
func (t *Tester) FindLast(model Model) Model {
	// find last document
	err := t.Store.C(model).FindOne(nil, nil, options.FindOne().SetSort(Sort("_id"))).Decode(model)
	if err != nil {
		panic(err)
	}

	// initialize model
	Init(model)

	return model
}

// Fetch will return the saved model.
func (t *Tester) Fetch(model Model, id primitive.ObjectID) Model {
	// find specific document
	err := t.Store.C(model).FindOne(nil, bson.M{
		"_id": id,
	}).Decode(model)
	if err != nil {
		panic(err)
	}

	// initialize model
	Init(model)

	return model
}

// Update will update the specified model.
func (t *Tester) Update(model Model) Model {
	// initialize model
	model = Init(model)

	// insert to collection
	_, err := t.Store.C(model).ReplaceOne(nil, bson.M{
		"_id": model.ID(),
	}, model)
	if err != nil {
		panic(err)
	}

	return model
}

// Delete will delete the specified model.
func (t *Tester) Delete(model Model) {
	// initialize model
	model = Init(model)

	// insert to collection
	_, err := t.Store.C(model).DeleteOne(nil, bson.M{
		"_id": model.ID(),
	})
	if err != nil {
		panic(err)
	}
}
<commit_msg>fix bug<commit_after>package coal

import (
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"go.mongodb.org/mongo-driver/mongo/options"
)

// A Tester provides facilities to the test a fire API.
type Tester struct {
	// The store to use for cleaning the database.
	Store *Store

	// The registered models.
	Models []Model
}

// NewTester returns a new tester.
func NewTester(store *Store, models ...Model) *Tester {
	return &Tester{
		Store:  store,
		Models: models,
	}
}

// Clean will remove the collections of models that have been registered and
// reset the header map.
func (t *Tester) Clean() {
	for _, model := range t.Models {
		// remove all is faster than dropping the collection
		_, err := t.Store.C(model).DeleteMany(nil, bson.M{})
		if err != nil {
			panic(err)
		}
	}
}

// Save will save the specified model.
func (t *Tester) Save(model Model) Model {
	// initialize model
	model = Init(model)

	// insert to collection
	_, err := t.Store.C(model).InsertOne(nil, model)
	if err != nil {
		panic(err)
	}

	return model
}

// FindAll will return all saved models.
func (t *Tester) FindAll(model Model) interface{} {
	// initialize model
	model = Init(model)

	// find all documents
	list := model.Meta().MakeSlice()
	cursor, err := t.Store.C(model).Find(nil, bson.M{}, options.Find().SetSort(Sort("_id")))
	if err != nil {
		panic(err)
	}

	// get all results
	err = cursor.All(nil, list)
	if err != nil {
		panic(err)
	}

	// close cursor
	err = cursor.Close(nil)
	if err != nil {
		panic(err)
	}

	// initialize list
	InitSlice(list)

	return list
}

// FindLast will return the last saved model.
func (t *Tester) FindLast(model Model) Model {
	// find last document
	err := t.Store.C(model).FindOne(nil, bson.M{}, options.FindOne().SetSort(Sort("-_id"))).Decode(model)
	if err != nil {
		panic(err)
	}

	// initialize model
	Init(model)

	return model
}

// Fetch will return the saved model.
func (t *Tester) Fetch(model Model, id primitive.ObjectID) Model {
	// find specific document
	err := t.Store.C(model).FindOne(nil, bson.M{
		"_id": id,
	}).Decode(model)
	if err != nil {
		panic(err)
	}

	// initialize model
	Init(model)

	return model
}

// Update will update the specified model.
func (t *Tester) Update(model Model) Model {
	// initialize model
	model = Init(model)

	// insert to collection
	_, err := t.Store.C(model).ReplaceOne(nil, bson.M{
		"_id": model.ID(),
	}, model)
	if err != nil {
		panic(err)
	}

	return model
}

// Delete will delete the specified model.
func (t *Tester) Delete(model Model) {
	// initialize model
	model = Init(model)

	// insert to collection
	_, err := t.Store.C(model).DeleteOne(nil, bson.M{
		"_id": model.ID(),
	})
	if err != nil {
		panic(err)
	}
}
<|endoftext|>


<commit_before>/*
Copyright 2019 Google Inc. All rights reserved.

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.
*/

// Command websockets launches a reverse proxy that can be used to
// manually test changes to the code in the agent/websockets package
//
// Example usage:
//   go build -o ~/bin/test-websocket-server testing/websockets/main.go
//   ~/bin/test-websocket-server --port 8081 --backend http://localhost:8082
package main

import (
	"context"
	"flag"
	"fmt"
	"log"
	"net/http"
	"net/http/httputil"
	"net/url"

	"github.com/google/inverting-proxy/agent/websockets"
)

var (
	port     = flag.Int("port", 0, "Port on which to listen")
	backend  = flag.String("backend", "", "URL of the backend HTTP server to proxy")
	shimPath = flag.String("shim-path", "websocket-shim", "Path under which to handle websocket shim requests")
)

func main() {
	flag.Parse()

	if *backend == "" {
		log.Fatal("You must specify the address of the backend server to proxy")
	}
	if *port == 0 {
		log.Fatal("You must specify a local port number on which to listen")
	}
	backendURL, err := url.Parse(*backend)
	if err != nil {
		log.Fatalf("Failure parsing the address of the backend server: %v", err)
	}
	backendProxy := httputil.NewSingleHostReverseProxy(backendURL)
	shimmingProxy, err := websockets.Proxy(context.Background(), backendProxy, backendURL.Host, *shimPath, true, func(h http.Handler) http.Handler { return h })
	if err != nil {
		log.Fatal("Failure starting the websocket-shimming proxy: %v", err)
	}
	shimFunc, err := websockets.ShimBody(*shimPath)
	if err != nil {
		log.Fatal("Failure setting up shim injection code: %v", err)
	}
	backendProxy.ModifyResponse = shimFunc

	http.Handle("/", shimmingProxy)
	log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil))
}
<commit_msg>Update main.go<commit_after>/*
Copyright 2019 Google Inc. All rights reserved.

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.
*/

// Command websockets launches a reverse proxy that can be used to
// manually test changes to the code in the agent/websockets package
//
// Example usage:
//   go build -o ~/bin/test-websocket-server testing/websockets/main.go
//   ~/bin/test-websocket-server --port 8081 --backend http://localhost:8082
package main

import (
	"context"
	"flag"
	"fmt"
	"log"
	"net/http"
	"net/http/httputil"
	"net/url"

	"github.com/google/inverting-proxy/agent/websockets"
)

var (
	port     = flag.Int("port", 0, "Port on which to listen")
	backend  = flag.String("backend", "", "URL of the backend HTTP server to proxy")
	shimPath = flag.String("shim-path", "websocket-shim", "Path under which to handle websocket shim requests")
)

func main() {
	flag.Parse()

	if *backend == "" {
		log.Fatal("You must specify the address of the backend server to proxy")
	}
	if *port == 0 {
		log.Fatal("You must specify a local port number on which to listen")
	}
	backendURL, err := url.Parse(*backend)
	if err != nil {
		log.Fatalf("Failure parsing the address of the backend server: %v", err)
	}
	backendProxy := httputil.NewSingleHostReverseProxy(backendURL)
	shimmingProxy, err := websockets.Proxy(context.Background(), backendProxy, backendURL.Host, *shimPath, true, func(h http.Handler) http.Handler { return h })
	if err != nil {
		log.Fatalf("Failure starting the websocket-shimming proxy: %v", err)
	}
	shimFunc, err := websockets.ShimBody(*shimPath)
	if err != nil {
		log.Fatalf("Failure setting up shim injection code: %v", err)
	}
	backendProxy.ModifyResponse = shimFunc

	http.Handle("/", shimmingProxy)
	log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil))
}
<|endoftext|>