<commit_before>// Copyright 2014 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.

// Package storage contains a Google Cloud Storage client.
package storage // import "google.golang.org/cloud/storage"

import (
	"crypto"
	"crypto/rand"
	"crypto/rsa"
	"crypto/sha256"
	"crypto/x509"
	"encoding/base64"
	"encoding/pem"
	"errors"
	"fmt"
	"io"
	"net/http"
	"net/url"
	"strings"
	"time"

	"google.golang.org/cloud/internal"

	"golang.org/x/net/context"
	"google.golang.org/api/googleapi"
	raw "google.golang.org/api/storage/v1"
)

var (
	ErrBucketNotExist = errors.New("storage: bucket doesn't exist")
	ErrObjectNotExist = errors.New("storage: object doesn't exist")
)

const (
	// ScopeFullControl grants permissions to manage your
	// data and permissions in Google Cloud Storage.
	ScopeFullControl = raw.DevstorageFull_controlScope

	// ScopeReadOnly grants permissions to
	// view your data in Google Cloud Storage.
	ScopeReadOnly = raw.DevstorageRead_onlyScope

	// ScopeReadWrite grants permissions to manage your
	// data in Google Cloud Storage.
	ScopeReadWrite = raw.DevstorageRead_writeScope
)

// TODO(jbd): Add storage.buckets.list.
// TODO(jbd): Add storage.buckets.insert.
// TODO(jbd): Add storage.buckets.update.
// TODO(jbd): Add storage.buckets.delete.

// TODO(jbd): Add storage.objects.watch.

// BucketInfo returns the metadata for the specified bucket.
func BucketInfo(ctx context.Context, name string) (*Bucket, error) {
	resp, err := rawService(ctx).Buckets.Get(name).Projection("full").Do()
	if e, ok := err.(*googleapi.Error); ok && e.Code == http.StatusNotFound {
		return nil, ErrBucketNotExist
	}
	if err != nil {
		return nil, err
	}
	return newBucket(resp), nil
}

// ListObjects lists objects from the bucket. You can specify a query
// to filter the results. If q is nil, no filtering is applied.
func ListObjects(ctx context.Context, bucket string, q *Query) (*Objects, error) {
	c := rawService(ctx).Objects.List(bucket)
	if q != nil {
		c.Projection("full")
		c.Delimiter(q.Delimiter)
		c.Prefix(q.Prefix)
		c.Versions(q.Versions)
		c.PageToken(q.Cursor)
		if q.MaxResults > 0 {
			c.MaxResults(int64(q.MaxResults))
		}
	}
	resp, err := c.Do()
	if err != nil {
		return nil, err
	}
	objects := &Objects{
		Results:  make([]*Object, len(resp.Items)),
		Prefixes: make([]string, len(resp.Prefixes)),
	}
	for i, item := range resp.Items {
		objects.Results[i] = newObject(item)
	}
	for i, prefix := range resp.Prefixes {
		objects.Prefixes[i] = prefix
	}
	if resp.NextPageToken != "" {
		next := Query{}
		if q != nil {
			// keep the other filtering
			// criteria if there is a query
			next = *q
		}
		next.Cursor = resp.NextPageToken
		objects.Next = &next
	}
	return objects, nil
}

// SignedURLOptions allows you to restrict the access to the signed URL.
type SignedURLOptions struct {
	// GoogleAccessID represents the authorizer of the signed URL generation.
	// It is typically the Google service account client email address from
	// the Google Developers Console in the form of "xxx@developer.gserviceaccount.com".
	// Required.
	GoogleAccessID string

	// PrivateKey is the Google service account private key. It is obtainable
	// from the Google Developers Console.
	// At https://console.developers.google.com/project/<your-project-id>/apiui/credential,
	// create a service account client ID or reuse one of your existing service account
	// credentials. Click on the "Generate new P12 key" to generate and download
	// a new private key. Once you download the P12 file, use the following command
	// to convert it into a PEM file.
	//
	//    $ openssl pkcs12 -in key.p12 -passin pass:notasecret -out key.pem -nodes
	//
	// Provide the contents of the PEM file as a byte slice.
	// Required.
	PrivateKey []byte

	// Method is the HTTP method to be used with the signed URL.
	// Signed URLs can be used with GET, HEAD, PUT, and DELETE requests.
	// Required.
	Method string

	// Expires is the expiration time on the signed URL. It must be
	// a datetime in the future.
	// Required.
	Expires time.Time

	// ContentType is the content type header the client must provide
	// to use the generated signed URL.
	// Optional.
	ContentType string

	// Headers is a list of extention headers the client must provide
	// in order to use the generated signed URL.
	// Optional.
	Headers []string

	// MD5 is the base64 encoded MD5 checksum of the file.
	// If provided, the client should provide the exact value on the request
	// header in order to use the signed URL.
	// Optional.
	MD5 []byte
}

// SignedURL returns a URL for the specified object. Signed URLs allow
// the users access to a restricted resource for a limited time without having a
// Google account or signing in. For more information about the signed
// URLs, see https://cloud.google.com/storage/docs/accesscontrol#Signed-URLs.
func SignedURL(bucket, name string, opts *SignedURLOptions) (string, error) {
	if opts.GoogleAccessID == "" || opts.PrivateKey == nil {
		return "", errors.New("storage: missing required credentials to generate a signed URL")
	}
	if opts.Method == "" {
		return "", errors.New("storage: missing required method option")
	}
	if opts.Expires.IsZero() {
		return "", errors.New("storage: missing required expires option")
	}
	key, err := parseKey(opts.PrivateKey)
	if err != nil {
		return "", err
	}
	h := sha256.New()
	fmt.Fprintf(h, "%s\n", opts.Method)
	fmt.Fprintf(h, "%s\n", opts.MD5)
	fmt.Fprintf(h, "%s\n", opts.ContentType)
	fmt.Fprintf(h, "%d\n", opts.Expires.Unix())
	fmt.Fprintf(h, "%s", strings.Join(opts.Headers, "\n"))
	fmt.Fprintf(h, "/%s/%s", bucket, name)
	b, err := rsa.SignPKCS1v15(
		rand.Reader,
		key,
		crypto.SHA256,
		h.Sum(nil),
	)
	if err != nil {
		return "", err
	}
	encoded := base64.StdEncoding.EncodeToString(b)
	u, err := url.Parse(fmt.Sprintf("https://storage.googleapis.com/%s/%s", bucket, name))
	if err != nil {
		return "", err
	}
	q := u.Query()
	q.Set("GoogleAccessId", opts.GoogleAccessID)
	q.Set("Expires", fmt.Sprintf("%d", opts.Expires.Unix()))
	q.Set("Signature", string(encoded))
	u.RawQuery = q.Encode()
	return u.String(), nil
}

// StatObject returns meta information about the specified object.
func StatObject(ctx context.Context, bucket, name string) (*Object, error) {
	o, err := rawService(ctx).Objects.Get(bucket, name).Projection("full").Do()
	if e, ok := err.(*googleapi.Error); ok && e.Code == http.StatusNotFound {
		return nil, ErrObjectNotExist
	}
	if err != nil {
		return nil, err
	}
	return newObject(o), nil
}

// UpdateAttrs updates an object with the provided attributes.
// All zero-value attributes are ignored.
func UpdateAttrs(ctx context.Context, bucket, name string, attrs ObjectAttrs) (*Object, error) {
	o, err := rawService(ctx).Objects.Patch(bucket, name, attrs.toRawObject(bucket)).Projection("full").Do()
	if e, ok := err.(*googleapi.Error); ok && e.Code == http.StatusNotFound {
		return nil, ErrObjectNotExist
	}
	if err != nil {
		return nil, err
	}
	return newObject(o), nil
}

// DeleteObject deletes the specified object.
func DeleteObject(ctx context.Context, bucket, name string) error {
	return rawService(ctx).Objects.Delete(bucket, name).Do()
}

// CopyObject copies the source object to the destination.
// The copied object's attributes are overwritten by those given.
func CopyObject(ctx context.Context, bucket, name string, destBucket string, attrs ObjectAttrs) (*Object, error) {
	destName := name
	if attrs.Name != "" {
		destName = attrs.Name
	}
	o, err := rawService(ctx).Objects.Copy(
		bucket, name, destBucket, destName, attrs.toRawObject(destBucket)).Projection("full").Do()
	if err != nil {
		return nil, err
	}
	return newObject(o), nil
}

// NewReader creates a new io.ReadCloser to read the contents
// of the object.
func NewReader(ctx context.Context, bucket, name string) (io.ReadCloser, error) {
	hc := internal.HTTPClient(ctx)
	res, err := hc.Get(fmt.Sprintf("https://storage.googleapis.com/%s/%s", bucket, name))
	if err != nil {
		return nil, err
	}
	if res.StatusCode == http.StatusNotFound {
		res.Body.Close()
		return nil, ErrObjectNotExist
	}
	if res.StatusCode < 200 || res.StatusCode > 299 {
		res.Body.Close()
		return res.Body, fmt.Errorf("storage: can't read object %v/%v, status code: %v", bucket, name, res.Status)
	}
	return res.Body, nil
}

// NewWriter returns a storage Writer that writes to the GCS object
// identified by the specified name.
// If such an object doesn't exist, it creates one.
// Attributes can be set on the object by modifying the returned Writer's
// ObjectAttrs field before the first call to Write. The name parameter to this
// function is ignored if the Name field of the ObjectAttrs field is set to a
// non-empty string.
//
// It is the caller's responsibility to call Close when writing is done.
//
// The object is not available and any previous object with the same
// name is not replaced on Cloud Storage until Close is called.
func NewWriter(ctx context.Context, bucket, name string) *Writer {
	return &Writer{
		ctx:    ctx,
		bucket: bucket,
		name:   name,
		donec:  make(chan struct{}),
	}
}

func rawService(ctx context.Context) *raw.Service {
	return internal.Service(ctx, "storage", func(hc *http.Client) interface{} {
		svc, _ := raw.New(hc)
		return svc
	}).(*raw.Service)
}

// parseKey converts the binary contents of a private key file
// to an *rsa.PrivateKey. It detects whether the private key is in a
// PEM container or not. If so, it extracts the the private key
// from PEM container before conversion. It only supports PEM
// containers with no passphrase.
func parseKey(key []byte) (*rsa.PrivateKey, error) {
	if block, _ := pem.Decode(key); block != nil {
		key = block.Bytes
	}
	parsedKey, err := x509.ParsePKCS8PrivateKey(key)
	if err != nil {
		parsedKey, err = x509.ParsePKCS1PrivateKey(key)
		if err != nil {
			return nil, err
		}
	}
	parsed, ok := parsedKey.(*rsa.PrivateKey)
	if !ok {
		return nil, errors.New("oauth2: private key is invalid")
	}
	return parsed, nil
}
<commit_msg>cloud/storage: Use full projection when listing regardless of query.<commit_after>// Copyright 2014 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.

// Package storage contains a Google Cloud Storage client.
package storage // import "google.golang.org/cloud/storage"

import (
	"crypto"
	"crypto/rand"
	"crypto/rsa"
	"crypto/sha256"
	"crypto/x509"
	"encoding/base64"
	"encoding/pem"
	"errors"
	"fmt"
	"io"
	"net/http"
	"net/url"
	"strings"
	"time"

	"google.golang.org/cloud/internal"

	"golang.org/x/net/context"
	"google.golang.org/api/googleapi"
	raw "google.golang.org/api/storage/v1"
)

var (
	ErrBucketNotExist = errors.New("storage: bucket doesn't exist")
	ErrObjectNotExist = errors.New("storage: object doesn't exist")
)

const (
	// ScopeFullControl grants permissions to manage your
	// data and permissions in Google Cloud Storage.
	ScopeFullControl = raw.DevstorageFull_controlScope

	// ScopeReadOnly grants permissions to
	// view your data in Google Cloud Storage.
	ScopeReadOnly = raw.DevstorageRead_onlyScope

	// ScopeReadWrite grants permissions to manage your
	// data in Google Cloud Storage.
	ScopeReadWrite = raw.DevstorageRead_writeScope
)

// TODO(jbd): Add storage.buckets.list.
// TODO(jbd): Add storage.buckets.insert.
// TODO(jbd): Add storage.buckets.update.
// TODO(jbd): Add storage.buckets.delete.

// TODO(jbd): Add storage.objects.watch.

// BucketInfo returns the metadata for the specified bucket.
func BucketInfo(ctx context.Context, name string) (*Bucket, error) {
	resp, err := rawService(ctx).Buckets.Get(name).Projection("full").Do()
	if e, ok := err.(*googleapi.Error); ok && e.Code == http.StatusNotFound {
		return nil, ErrBucketNotExist
	}
	if err != nil {
		return nil, err
	}
	return newBucket(resp), nil
}

// ListObjects lists objects from the bucket. You can specify a query
// to filter the results. If q is nil, no filtering is applied.
func ListObjects(ctx context.Context, bucket string, q *Query) (*Objects, error) {
	c := rawService(ctx).Objects.List(bucket)
	c.Projection("full")
	if q != nil {
		c.Delimiter(q.Delimiter)
		c.Prefix(q.Prefix)
		c.Versions(q.Versions)
		c.PageToken(q.Cursor)
		if q.MaxResults > 0 {
			c.MaxResults(int64(q.MaxResults))
		}
	}
	resp, err := c.Do()
	if err != nil {
		return nil, err
	}
	objects := &Objects{
		Results:  make([]*Object, len(resp.Items)),
		Prefixes: make([]string, len(resp.Prefixes)),
	}
	for i, item := range resp.Items {
		objects.Results[i] = newObject(item)
	}
	for i, prefix := range resp.Prefixes {
		objects.Prefixes[i] = prefix
	}
	if resp.NextPageToken != "" {
		next := Query{}
		if q != nil {
			// keep the other filtering
			// criteria if there is a query
			next = *q
		}
		next.Cursor = resp.NextPageToken
		objects.Next = &next
	}
	return objects, nil
}

// SignedURLOptions allows you to restrict the access to the signed URL.
type SignedURLOptions struct {
	// GoogleAccessID represents the authorizer of the signed URL generation.
	// It is typically the Google service account client email address from
	// the Google Developers Console in the form of "xxx@developer.gserviceaccount.com".
	// Required.
	GoogleAccessID string

	// PrivateKey is the Google service account private key. It is obtainable
	// from the Google Developers Console.
	// At https://console.developers.google.com/project/<your-project-id>/apiui/credential,
	// create a service account client ID or reuse one of your existing service account
	// credentials. Click on the "Generate new P12 key" to generate and download
	// a new private key. Once you download the P12 file, use the following command
	// to convert it into a PEM file.
	//
	//    $ openssl pkcs12 -in key.p12 -passin pass:notasecret -out key.pem -nodes
	//
	// Provide the contents of the PEM file as a byte slice.
	// Required.
	PrivateKey []byte

	// Method is the HTTP method to be used with the signed URL.
	// Signed URLs can be used with GET, HEAD, PUT, and DELETE requests.
	// Required.
	Method string

	// Expires is the expiration time on the signed URL. It must be
	// a datetime in the future.
	// Required.
	Expires time.Time

	// ContentType is the content type header the client must provide
	// to use the generated signed URL.
	// Optional.
	ContentType string

	// Headers is a list of extention headers the client must provide
	// in order to use the generated signed URL.
	// Optional.
	Headers []string

	// MD5 is the base64 encoded MD5 checksum of the file.
	// If provided, the client should provide the exact value on the request
	// header in order to use the signed URL.
	// Optional.
	MD5 []byte
}

// SignedURL returns a URL for the specified object. Signed URLs allow
// the users access to a restricted resource for a limited time without having a
// Google account or signing in. For more information about the signed
// URLs, see https://cloud.google.com/storage/docs/accesscontrol#Signed-URLs.
func SignedURL(bucket, name string, opts *SignedURLOptions) (string, error) {
	if opts.GoogleAccessID == "" || opts.PrivateKey == nil {
		return "", errors.New("storage: missing required credentials to generate a signed URL")
	}
	if opts.Method == "" {
		return "", errors.New("storage: missing required method option")
	}
	if opts.Expires.IsZero() {
		return "", errors.New("storage: missing required expires option")
	}
	key, err := parseKey(opts.PrivateKey)
	if err != nil {
		return "", err
	}
	h := sha256.New()
	fmt.Fprintf(h, "%s\n", opts.Method)
	fmt.Fprintf(h, "%s\n", opts.MD5)
	fmt.Fprintf(h, "%s\n", opts.ContentType)
	fmt.Fprintf(h, "%d\n", opts.Expires.Unix())
	fmt.Fprintf(h, "%s", strings.Join(opts.Headers, "\n"))
	fmt.Fprintf(h, "/%s/%s", bucket, name)
	b, err := rsa.SignPKCS1v15(
		rand.Reader,
		key,
		crypto.SHA256,
		h.Sum(nil),
	)
	if err != nil {
		return "", err
	}
	encoded := base64.StdEncoding.EncodeToString(b)
	u, err := url.Parse(fmt.Sprintf("https://storage.googleapis.com/%s/%s", bucket, name))
	if err != nil {
		return "", err
	}
	q := u.Query()
	q.Set("GoogleAccessId", opts.GoogleAccessID)
	q.Set("Expires", fmt.Sprintf("%d", opts.Expires.Unix()))
	q.Set("Signature", string(encoded))
	u.RawQuery = q.Encode()
	return u.String(), nil
}

// StatObject returns meta information about the specified object.
func StatObject(ctx context.Context, bucket, name string) (*Object, error) {
	o, err := rawService(ctx).Objects.Get(bucket, name).Projection("full").Do()
	if e, ok := err.(*googleapi.Error); ok && e.Code == http.StatusNotFound {
		return nil, ErrObjectNotExist
	}
	if err != nil {
		return nil, err
	}
	return newObject(o), nil
}

// UpdateAttrs updates an object with the provided attributes.
// All zero-value attributes are ignored.
func UpdateAttrs(ctx context.Context, bucket, name string, attrs ObjectAttrs) (*Object, error) {
	o, err := rawService(ctx).Objects.Patch(bucket, name, attrs.toRawObject(bucket)).Projection("full").Do()
	if e, ok := err.(*googleapi.Error); ok && e.Code == http.StatusNotFound {
		return nil, ErrObjectNotExist
	}
	if err != nil {
		return nil, err
	}
	return newObject(o), nil
}

// DeleteObject deletes the specified object.
func DeleteObject(ctx context.Context, bucket, name string) error {
	return rawService(ctx).Objects.Delete(bucket, name).Do()
}

// CopyObject copies the source object to the destination.
// The copied object's attributes are overwritten by those given.
func CopyObject(ctx context.Context, bucket, name string, destBucket string, attrs ObjectAttrs) (*Object, error) {
	destName := name
	if attrs.Name != "" {
		destName = attrs.Name
	}
	o, err := rawService(ctx).Objects.Copy(
		bucket, name, destBucket, destName, attrs.toRawObject(destBucket)).Projection("full").Do()
	if err != nil {
		return nil, err
	}
	return newObject(o), nil
}

// NewReader creates a new io.ReadCloser to read the contents
// of the object.
func NewReader(ctx context.Context, bucket, name string) (io.ReadCloser, error) {
	hc := internal.HTTPClient(ctx)
	res, err := hc.Get(fmt.Sprintf("https://storage.googleapis.com/%s/%s", bucket, name))
	if err != nil {
		return nil, err
	}
	if res.StatusCode == http.StatusNotFound {
		res.Body.Close()
		return nil, ErrObjectNotExist
	}
	if res.StatusCode < 200 || res.StatusCode > 299 {
		res.Body.Close()
		return res.Body, fmt.Errorf("storage: can't read object %v/%v, status code: %v", bucket, name, res.Status)
	}
	return res.Body, nil
}

// NewWriter returns a storage Writer that writes to the GCS object
// identified by the specified name.
// If such an object doesn't exist, it creates one.
// Attributes can be set on the object by modifying the returned Writer's
// ObjectAttrs field before the first call to Write. The name parameter to this
// function is ignored if the Name field of the ObjectAttrs field is set to a
// non-empty string.
//
// It is the caller's responsibility to call Close when writing is done.
//
// The object is not available and any previous object with the same
// name is not replaced on Cloud Storage until Close is called.
func NewWriter(ctx context.Context, bucket, name string) *Writer {
	return &Writer{
		ctx:    ctx,
		bucket: bucket,
		name:   name,
		donec:  make(chan struct{}),
	}
}

func rawService(ctx context.Context) *raw.Service {
	return internal.Service(ctx, "storage", func(hc *http.Client) interface{} {
		svc, _ := raw.New(hc)
		return svc
	}).(*raw.Service)
}

// parseKey converts the binary contents of a private key file
// to an *rsa.PrivateKey. It detects whether the private key is in a
// PEM container or not. If so, it extracts the the private key
// from PEM container before conversion. It only supports PEM
// containers with no passphrase.
func parseKey(key []byte) (*rsa.PrivateKey, error) {
	if block, _ := pem.Decode(key); block != nil {
		key = block.Bytes
	}
	parsedKey, err := x509.ParsePKCS8PrivateKey(key)
	if err != nil {
		parsedKey, err = x509.ParsePKCS1PrivateKey(key)
		if err != nil {
			return nil, err
		}
	}
	parsed, ok := parsedKey.(*rsa.PrivateKey)
	if !ok {
		return nil, errors.New("oauth2: private key is invalid")
	}
	return parsed, nil
}
<|endoftext|>


<commit_before>package root

import (
	"fmt"
	"sort"
	"strings"

	"github.com/justwatchcom/gopass/store"
	"github.com/justwatchcom/gopass/utils/tree"
	"github.com/justwatchcom/gopass/utils/tree/simple"
	"github.com/pkg/errors"
)

// List will return a flattened list of all tree entries
func (r *Store) List(maxDepth int) ([]string, error) {
	t, err := r.Tree()
	if err != nil {
		return []string{}, err
	}
	return t.List(maxDepth), nil
}

// Tree returns the tree representation of the entries
func (r *Store) Tree() (tree.Tree, error) {
	root := simple.New("gopass")
	addFileFunc := func(in ...string) {
		for _, f := range in {
			ct := "text/plain"
			if strings.HasSuffix(f, ".yaml") {
				ct = "text/yaml"
				f = strings.TrimSuffix(f, ".yaml")
			} else if strings.HasSuffix(f, ".b64") {
				ct = "application/octet-stream"
				f = strings.TrimSuffix(f, ".b64")
			}
			if err := root.AddFile(f, ct); err != nil {
				fmt.Printf("Failed to add file %s to tree: %s\n", f, err)
				continue
			}
		}
	}
	addTplFunc := func(in ...string) {
		for _, f := range in {
			if err := root.AddTemplate(f); err != nil {
				fmt.Printf("Failed to add template %s to tree: %s\n", f, err)
				continue
			}
		}
	}

	sf, err := r.store.List("")
	if err != nil {
		return nil, err
	}
	addFileFunc(sf...)
	addTplFunc(r.store.ListTemplates("")...)

	mps := r.MountPoints()
	sort.Sort(store.ByPathLen(mps))
	for _, alias := range mps {
		substore := r.mounts[alias]
		if substore == nil {
			continue
		}
		if err := root.AddMount(alias, substore.Path()); err != nil {
			return nil, errors.Errorf("failed to add mount: %s", err)
		}
		sf, err := substore.List(alias)
		if err != nil {
			return nil, errors.Errorf("failed to add file: %s", err)
		}
		addFileFunc(sf...)
		addTplFunc(substore.ListTemplates(alias)...)
	}

	return root, nil
}

// Format will pretty print all entries in this store and all substores
func (r *Store) Format(maxDepth int) (string, error) {
	t, err := r.Tree()
	if err != nil {
		return "", err
	}
	return t.Format(maxDepth), nil
}
<commit_msg>Remove deprecated special case for .yaml files (#362)<commit_after>package root

import (
	"fmt"
	"sort"
	"strings"

	"github.com/justwatchcom/gopass/store"
	"github.com/justwatchcom/gopass/utils/tree"
	"github.com/justwatchcom/gopass/utils/tree/simple"
	"github.com/pkg/errors"
)

// List will return a flattened list of all tree entries
func (r *Store) List(maxDepth int) ([]string, error) {
	t, err := r.Tree()
	if err != nil {
		return []string{}, err
	}
	return t.List(maxDepth), nil
}

// Tree returns the tree representation of the entries
func (r *Store) Tree() (tree.Tree, error) {
	root := simple.New("gopass")
	addFileFunc := func(in ...string) {
		for _, f := range in {
			ct := "text/plain"
			if strings.HasSuffix(f, ".b64") {
				ct = "application/octet-stream"
				f = strings.TrimSuffix(f, ".b64")
			}
			if err := root.AddFile(f, ct); err != nil {
				fmt.Printf("Failed to add file %s to tree: %s\n", f, err)
				continue
			}
		}
	}
	addTplFunc := func(in ...string) {
		for _, f := range in {
			if err := root.AddTemplate(f); err != nil {
				fmt.Printf("Failed to add template %s to tree: %s\n", f, err)
				continue
			}
		}
	}

	sf, err := r.store.List("")
	if err != nil {
		return nil, err
	}
	addFileFunc(sf...)
	addTplFunc(r.store.ListTemplates("")...)

	mps := r.MountPoints()
	sort.Sort(store.ByPathLen(mps))
	for _, alias := range mps {
		substore := r.mounts[alias]
		if substore == nil {
			continue
		}
		if err := root.AddMount(alias, substore.Path()); err != nil {
			return nil, errors.Errorf("failed to add mount: %s", err)
		}
		sf, err := substore.List(alias)
		if err != nil {
			return nil, errors.Errorf("failed to add file: %s", err)
		}
		addFileFunc(sf...)
		addTplFunc(substore.ListTemplates(alias)...)
	}

	return root, nil
}

// Format will pretty print all entries in this store and all substores
func (r *Store) Format(maxDepth int) (string, error) {
	t, err := r.Tree()
	if err != nil {
		return "", err
	}
	return t.Format(maxDepth), nil
}
<|endoftext|>


<commit_before>/*

Command goimports updates your Go import lines,
adding missing ones and removing unreferenced ones.

     $ go get code.google.com/p/go.tools/cmd/goimports

It's a drop-in replacement for your editor's gofmt-on-save hook.
It has the same command-line interface as gofmt and formats
your code in the same way.

For emacs, make sure you have the latest (Go 1.2+) go-mode.el:
   https://go.googlecode.com/hg/misc/emacs/go-mode.el
Then in your .emacs file:
   (setq gofmt-command "goimports")
   (add-to-list 'load-path "/home/you/goroot/misc/emacs/")
   (require 'go-mode-load)
   (add-hook 'before-save-hook 'gofmt-before-save)

For vim, set "gofmt_command" to "goimports":
    https://code.google.com/p/go/source/detail?r=39c724dd7f252
    https://code.google.com/p/go/source/browse#hg%2Fmisc%2Fvim
    etc

For GoSublime, follow the steps described here:
    http://michaelwhatcott.com/gosublime-goimports/

For other editors, you probably know what to do.

Happy hacking!

*/
package main
<commit_msg>go.tools/cmd/goimports: update doc.go to new emacs instructions<commit_after>/*

Command goimports updates your Go import lines,
adding missing ones and removing unreferenced ones.

     $ go get code.google.com/p/go.tools/cmd/goimports

It's a drop-in replacement for your editor's gofmt-on-save hook.
It has the same command-line interface as gofmt and formats
your code in the same way.

For emacs, make sure you have the latest go-mode.el:
   https://github.com/dominikh/go-mode.el
Then in your .emacs file:
   (setq gofmt-command "goimports")
   (add-to-list 'load-path "/home/you/somewhere/emacs/")
   (require 'go-mode-load)
   (add-hook 'before-save-hook 'gofmt-before-save)

For vim, set "gofmt_command" to "goimports":
    https://code.google.com/p/go/source/detail?r=39c724dd7f252
    https://code.google.com/p/go/source/browse#hg%2Fmisc%2Fvim
    etc

For GoSublime, follow the steps described here:
    http://michaelwhatcott.com/gosublime-goimports/

For other editors, you probably know what to do.

Happy hacking!

*/
package main
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"time"

	"github.com/LiuPai/himawari"
)

var (
	level = flag.Int("level", 4,
		"Image quality and size choose one of [4, 8, 16, 20]")
	cache = flag.String("cache", os.TempDir(),
		"Path to the cache file directory")
	defaultImageFile = fmt.Sprintf("%s/himawari.png", os.TempDir())
	output           = flag.String("output", defaultImageFile,
		"The link of current himawari image")
	daemon = flag.Bool("daemon", false,
		"Run himawari as daemon")
	tick = flag.Uint("tick", 300,
		"Duration to check himawari latest timestamp in seconds")
	pidFile = flag.String("pid", "",
		"Himawari unix like system pid file")
	latestTimestamp *time.Time
)

func checkLatestImage() (err error) {
	latestTimestamp, err = himawari.LatestTimestamp()
	if err != nil {
		return err
	}

	imageFile, err := himawari.FetchImage(*level,
		latestTimestamp,
		*cache)
	if err != nil {
		return err
	}
	_ = os.Remove(*output)
	err = os.Symlink(imageFile, *output)
	if err != nil {
		return err
	}
	return nil
}

func main() {
	flag.Parse()
	switch *level {
	case 4, 8, 16, 20:
	default:
		log.Fatalf("unsupport level value: %d", *level)
	}

	// oneshot fetch current himawari image
	if !*daemon {
		err := checkLatestImage()
		if err != nil {
			log.Fatal(err)
		}
		return
	}

	// store pid file
	if *pidFile != "" {
		err := ioutil.WriteFile(*pidFile,
			[]byte(fmt.Sprintf("%d", os.Getpid())),
			0644)
		if err != nil {
			log.Fatalf("failed to write pid file %s, err: %v",
				*pidFile, err)
		}
	}

	// daemon ticker
	ticker := time.NewTicker(time.Second * time.Duration(*tick))
	log.Printf("level: %d", *level)
	log.Printf("tick: %d", *tick)
	log.Printf("cache: %s", *cache)
	log.Printf("output: %s", *output)

	err := checkLatestImage()
	if err != nil {
		log.Print(err)
	}
	// main loop
	for _ = range ticker.C {
		timestamp, err := himawari.LatestTimestamp()
		if err != nil {
			log.Print(err)
		}
		// check latest timestamp
		if timestamp.Unix() != latestTimestamp.Unix() {
			latestTimestamp = timestamp
			imageFile, err := himawari.FetchImage(*level,
				latestTimestamp,
				*output)
			if err != nil {
				log.Print(err)
			}
			_ = os.Remove(*output)
			err = os.Symlink(imageFile, *output)
			if err != nil {
				log.Print(err)
			}
		}
	}
}
<commit_msg>Bug fix: variable typo error<commit_after>package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"time"

	"github.com/LiuPai/himawari"
)

var (
	level = flag.Int("level", 4,
		"Image quality and size choose one of [4, 8, 16, 20]")
	cache = flag.String("cache", os.TempDir(),
		"Path to the cache file directory")
	defaultImageFile = fmt.Sprintf("%s/himawari.png", os.TempDir())
	output           = flag.String("output", defaultImageFile,
		"The link of current himawari image")
	daemon = flag.Bool("daemon", false,
		"Run himawari as daemon")
	tick = flag.Uint("tick", 300,
		"Duration to check himawari latest timestamp in seconds")
	pidFile = flag.String("pid", "",
		"Himawari unix like system pid file")
	latestTimestamp *time.Time
)

func checkLatestImage() (err error) {
	latestTimestamp, err = himawari.LatestTimestamp()
	if err != nil {
		return err
	}

	imageFile, err := himawari.FetchImage(*level,
		latestTimestamp,
		*cache)
	if err != nil {
		return err
	}
	_ = os.Remove(*output)
	err = os.Symlink(imageFile, *output)
	if err != nil {
		return err
	}
	return nil
}

func main() {
	flag.Parse()
	switch *level {
	case 4, 8, 16, 20:
	default:
		log.Fatalf("unsupport level value: %d", *level)
	}

	// oneshot fetch current himawari image
	if !*daemon {
		err := checkLatestImage()
		if err != nil {
			log.Fatal(err)
		}
		return
	}

	// store pid file
	if *pidFile != "" {
		err := ioutil.WriteFile(*pidFile,
			[]byte(fmt.Sprintf("%d", os.Getpid())),
			0644)
		if err != nil {
			log.Fatalf("failed to write pid file %s, err: %v",
				*pidFile, err)
		}
	}

	// daemon ticker
	ticker := time.NewTicker(time.Second * time.Duration(*tick))
	log.Printf("level: %d", *level)
	log.Printf("tick: %d", *tick)
	log.Printf("cache: %s", *cache)
	log.Printf("output: %s", *output)

	err := checkLatestImage()
	if err != nil {
		log.Print(err)
	}
	// main loop
	for _ = range ticker.C {
		timestamp, err := himawari.LatestTimestamp()
		if err != nil {
			log.Print(err)
		}
		// check latest timestamp
		if timestamp.Unix() != latestTimestamp.Unix() {
			latestTimestamp = timestamp
			imageFile, err := himawari.FetchImage(*level,
				latestTimestamp,
				*cache)
			if err != nil {
				log.Print(err)
			}
			_ = os.Remove(*output)
			err = os.Symlink(imageFile, *output)
			if err != nil {
				log.Print(err)
			}
		}
	}
}
<|endoftext|>


<commit_before>/*
i-sudoku is an interactive command-line sudoku tool. Although its main
puropse is debugging the sudoku package, it's a reasonably full-featured
command-line sudoku game in its own right.
*/

package main

import (
	"fmt"
	"github.com/jkomoros/sudoku"
	"github.com/nsf/termbox-go"
	"log"
	"strconv"
	"strings"
	"time"
	"unicode/utf8"
)

//A debug override; if true will print a color palette to the screen, wait for
//a keypress, and then quit. Useful for seeing what different colors are
//available to use.
const DRAW_PALETTE = false

var tickCount int

func main() {

	//TODO: should be possible to run it and pass in a puzzle to use.

	if err := termbox.Init(); err != nil {
		log.Fatal("Termbox initialization failed:", err)
	}
	defer termbox.Close()

	termbox.SetOutputMode(termbox.Output256)

	model := newModel()

	width, _ := termbox.Size()
	model.outputWidth = width

	if DRAW_PALETTE {
		drawColorPalette()
		//Wait until something happens, generally a key is pressed.
		termbox.PollEvent()
		return
	}

	draw(model)

	eventChan := make(chan termbox.Event, 1)
	go func() {
		for {
			eventChan <- termbox.PollEvent()
		}
	}()

	timeTick := time.Tick(time.Millisecond * 750)

mainloop:
	for {
		select {
		case evt := <-eventChan:
			switch evt.Type {
			case termbox.EventKey:
				model.WillProcessEvent()
				model.state.handleInput(model, evt)
			}
		case <-timeTick:
			tickCount++
		}

		draw(model)
		if model.exitNow {
			break mainloop
		}
	}
}

func clearScreen() {
	width, height := termbox.Size()
	for x := 0; x < width; x++ {
		for y := 0; y < height; y++ {
			termbox.SetCell(x, y, ' ', termbox.ColorDefault, termbox.ColorDefault)
		}
	}
}

func drawColorPalette() {
	clearScreen()
	x := 0
	y := 0

	for i := 0x00; i <= 0xFF; i++ {
		numToPrint := "  " + fmt.Sprintf("%02X", i) + "  "
		for _, ch := range numToPrint {
			termbox.SetCell(x, y, ch, termbox.ColorBlack, termbox.Attribute(i))
			x++
		}
		//Fit 8 print outs on a line before creating a new one
		if i%8 == 0 {
			x = 0
			y++
		}
	}

	termbox.Flush()
}

func drawGrid(y int, model *mainModel) (endY int) {
	var x int
	grid := model.grid

	fg := termbox.ColorBlack
	bg := termbox.ColorGreen

	var toggleBackgrounds []termbox.Attribute

	//Figure out which possible colors we should paint the grid in.
	//Iterate through toggles backwards, since earlier ones have higher preference
	for i := len(model.toggles) - 1; i >= 0; i-- {
		toggle := model.toggles[i]
		if toggle.Value() {
			toggleBackgrounds = append(toggleBackgrounds, toggle.GridColor)
		}
	}
	//Now, as long as there's one possibiltiy, select a grid color to paint.
	//Otherwise, just leave it as default.
	if len(toggleBackgrounds) > 0 {
		bg = toggleBackgrounds[tickCount%len(toggleBackgrounds)]
	}

	//The column where the grid starts
	gridLeft := 1
	gridTop := 1

	termbox.SetCell(0, y, ' ', fg, bg)
	x++

	for i := 0; i < sudoku.DIM; i++ {

		cellLeft, _, _, _ := grid.Cell(0, i).DiagramExtents()
		cellLeft += gridLeft
		//Pad until we get to the start of this cell area
		for x < cellLeft {
			termbox.SetCell(x, y, '|', fg, bg)
			x++
		}
		for _, ch := range " " + strconv.Itoa(i) + " " {
			termbox.SetCell(x, y, ch, fg, bg)
			x++
		}
	}

	y++

	//Draw diagram down left rail
	x = 0
	tempY := y
	for i := 0; i < sudoku.DIM; i++ {

		_, cellTop, _, _ := grid.Cell(i, 0).DiagramExtents()
		cellTop += gridTop
		//Pad until we get to the start of this cell area
		for tempY < cellTop {
			termbox.SetCell(x, tempY, '-', fg, bg)
			tempY++
		}
		for _, ch := range " " + strconv.Itoa(i) + " " {
			termbox.SetCell(x, tempY, ch, fg, bg)
			tempY++
		}
	}

	fg, bg = bg, fg

	//TODO: I'm pretty sure top/left are reversed
	selectedTop, selectedLeft, selectedHeight, selectedWidth := model.Selected().DiagramExtents()
	//Correct the selected coordinate for the offset of the grid from the top.
	selectedLeft += gridTop
	selectedTop += gridLeft
	for _, line := range strings.Split(grid.Diagram(true), "\n") {
		//Grid starts at 1 cell over from left edge
		x = gridLeft
		//The first number in range will be byte offset, but for some items like the bullet, it's two bytes.
		//But what we care about is that each item is a character.
		for _, ch := range line {

			defaultColor := fg

			numberRune, _ := utf8.DecodeRuneInString(sudoku.DIAGRAM_NUMBER)
			lockedRune, _ := utf8.DecodeRuneInString(sudoku.DIAGRAM_LOCKED)

			if ch == numberRune {
				defaultColor = 0x12
			} else if ch == lockedRune {
				defaultColor = 0x35
			} else if runeIsNum(ch) {
				defaultColor = termbox.ColorWhite | termbox.AttrBold
			}

			backgroundColor := bg
			if x >= selectedTop && x < (selectedTop+selectedHeight) && y >= selectedLeft && y < (selectedLeft+selectedWidth) {
				//We're on the selected cell
				backgroundColor = 0xf0
			}

			termbox.SetCell(x, y, ch, defaultColor, backgroundColor)
			x++
		}
		y++
	}
	//The last loop added one extra to y.
	y--
	return y
}

func drawToggleLine(y int, model *mainModel) (newY int) {
	x := 0

	for _, toggle := range model.toggles {
		msg := toggle.OffText
		fg := toggle.GridColor
		bg := termbox.ColorBlack
		if toggle.Value() {
			msg = toggle.OnText
			fg, bg = bg, fg
		}
		for _, ch := range msg {
			termbox.SetCell(x, y, ch, fg, bg)
			x++
		}
	}
	return y
}

func drawStatusLine(y int, model *mainModel) (newY int) {
	x := 0
	var fg termbox.Attribute
	underlined := false
	for _, ch := range ">>> " + model.StatusLine() {
		//The ( and ) are non-printing control characters
		if ch == '{' {
			underlined = true
			continue
		} else if ch == '}' {
			underlined = false
			continue
		}
		fg = termbox.ColorBlack
		if underlined {
			fg = fg | termbox.AttrUnderline | termbox.AttrBold
		}

		termbox.SetCell(x, y, ch, fg, termbox.ColorWhite)
		x++
	}

	width, _ := termbox.Size()

	for x < width {
		termbox.SetCell(x, y, ' ', fg, termbox.ColorWhite)
		x++
	}
	return y
}

func drawConsole(y int, model *mainModel) (newY int) {

	x := 0

	underlined := false

	splitMessage := strings.Split(model.consoleMessage, "\n")

	for _, line := range splitMessage {

		x = 0
		for _, ch := range line {
			if ch == '{' {
				underlined = true
				continue
			} else if ch == '}' {
				underlined = false
				continue
			}
			fg := termbox.Attribute(0xf6)
			if underlined {
				fg = termbox.Attribute(0xFC) | termbox.AttrBold
			}
			termbox.SetCell(x, y, ch, fg, termbox.ColorBlack)
			x++
		}
		y++
	}
	//y is one too many
	y--
	return y
}

func draw(model *mainModel) {

	clearScreen()

	y := 0

	y = drawGrid(y, model)
	y++
	y = drawToggleLine(y, model)
	y++
	y = drawStatusLine(y, model)
	y++
	y = drawConsole(y, model)

	termbox.Flush()
}
<commit_msg>Factor main loop of i-sudoku into mainLoop function<commit_after>/*
i-sudoku is an interactive command-line sudoku tool. Although its main
puropse is debugging the sudoku package, it's a reasonably full-featured
command-line sudoku game in its own right.
*/

package main

import (
	"fmt"
	"github.com/jkomoros/sudoku"
	"github.com/nsf/termbox-go"
	"log"
	"strconv"
	"strings"
	"time"
	"unicode/utf8"
)

//A debug override; if true will print a color palette to the screen, wait for
//a keypress, and then quit. Useful for seeing what different colors are
//available to use.
const DRAW_PALETTE = false

var tickCount int

func main() {

	//TODO: should be possible to run it and pass in a puzzle to use.

	if err := termbox.Init(); err != nil {
		log.Fatal("Termbox initialization failed:", err)
	}
	defer termbox.Close()

	termbox.SetOutputMode(termbox.Output256)

	model := newModel()

	width, _ := termbox.Size()
	model.outputWidth = width

	if DRAW_PALETTE {
		drawColorPalette()
		//Wait until something happens, generally a key is pressed.
		termbox.PollEvent()
		return
	}

	mainLoop(model)

}

func mainLoop(model *mainModel) {
	draw(model)

	eventChan := make(chan termbox.Event, 1)
	go func() {
		for {
			eventChan <- termbox.PollEvent()
		}
	}()

	timeTick := time.Tick(time.Millisecond * 750)

mainloop:
	for {
		select {
		case evt := <-eventChan:
			switch evt.Type {
			case termbox.EventKey:
				model.WillProcessEvent()
				model.state.handleInput(model, evt)
			}
		case <-timeTick:
			tickCount++
		}

		draw(model)
		if model.exitNow {
			break mainloop
		}
	}
}

func clearScreen() {
	width, height := termbox.Size()
	for x := 0; x < width; x++ {
		for y := 0; y < height; y++ {
			termbox.SetCell(x, y, ' ', termbox.ColorDefault, termbox.ColorDefault)
		}
	}
}

func drawColorPalette() {
	clearScreen()
	x := 0
	y := 0

	for i := 0x00; i <= 0xFF; i++ {
		numToPrint := "  " + fmt.Sprintf("%02X", i) + "  "
		for _, ch := range numToPrint {
			termbox.SetCell(x, y, ch, termbox.ColorBlack, termbox.Attribute(i))
			x++
		}
		//Fit 8 print outs on a line before creating a new one
		if i%8 == 0 {
			x = 0
			y++
		}
	}

	termbox.Flush()
}

func drawGrid(y int, model *mainModel) (endY int) {
	var x int
	grid := model.grid

	fg := termbox.ColorBlack
	bg := termbox.ColorGreen

	var toggleBackgrounds []termbox.Attribute

	//Figure out which possible colors we should paint the grid in.
	//Iterate through toggles backwards, since earlier ones have higher preference
	for i := len(model.toggles) - 1; i >= 0; i-- {
		toggle := model.toggles[i]
		if toggle.Value() {
			toggleBackgrounds = append(toggleBackgrounds, toggle.GridColor)
		}
	}
	//Now, as long as there's one possibiltiy, select a grid color to paint.
	//Otherwise, just leave it as default.
	if len(toggleBackgrounds) > 0 {
		bg = toggleBackgrounds[tickCount%len(toggleBackgrounds)]
	}

	//The column where the grid starts
	gridLeft := 1
	gridTop := 1

	termbox.SetCell(0, y, ' ', fg, bg)
	x++

	for i := 0; i < sudoku.DIM; i++ {

		cellLeft, _, _, _ := grid.Cell(0, i).DiagramExtents()
		cellLeft += gridLeft
		//Pad until we get to the start of this cell area
		for x < cellLeft {
			termbox.SetCell(x, y, '|', fg, bg)
			x++
		}
		for _, ch := range " " + strconv.Itoa(i) + " " {
			termbox.SetCell(x, y, ch, fg, bg)
			x++
		}
	}

	y++

	//Draw diagram down left rail
	x = 0
	tempY := y
	for i := 0; i < sudoku.DIM; i++ {

		_, cellTop, _, _ := grid.Cell(i, 0).DiagramExtents()
		cellTop += gridTop
		//Pad until we get to the start of this cell area
		for tempY < cellTop {
			termbox.SetCell(x, tempY, '-', fg, bg)
			tempY++
		}
		for _, ch := range " " + strconv.Itoa(i) + " " {
			termbox.SetCell(x, tempY, ch, fg, bg)
			tempY++
		}
	}

	fg, bg = bg, fg

	//TODO: I'm pretty sure top/left are reversed
	selectedTop, selectedLeft, selectedHeight, selectedWidth := model.Selected().DiagramExtents()
	//Correct the selected coordinate for the offset of the grid from the top.
	selectedLeft += gridTop
	selectedTop += gridLeft
	for _, line := range strings.Split(grid.Diagram(true), "\n") {
		//Grid starts at 1 cell over from left edge
		x = gridLeft
		//The first number in range will be byte offset, but for some items like the bullet, it's two bytes.
		//But what we care about is that each item is a character.
		for _, ch := range line {

			defaultColor := fg

			numberRune, _ := utf8.DecodeRuneInString(sudoku.DIAGRAM_NUMBER)
			lockedRune, _ := utf8.DecodeRuneInString(sudoku.DIAGRAM_LOCKED)

			if ch == numberRune {
				defaultColor = 0x12
			} else if ch == lockedRune {
				defaultColor = 0x35
			} else if runeIsNum(ch) {
				defaultColor = termbox.ColorWhite | termbox.AttrBold
			}

			backgroundColor := bg
			if x >= selectedTop && x < (selectedTop+selectedHeight) && y >= selectedLeft && y < (selectedLeft+selectedWidth) {
				//We're on the selected cell
				backgroundColor = 0xf0
			}

			termbox.SetCell(x, y, ch, defaultColor, backgroundColor)
			x++
		}
		y++
	}
	//The last loop added one extra to y.
	y--
	return y
}

func drawToggleLine(y int, model *mainModel) (newY int) {
	x := 0

	for _, toggle := range model.toggles {
		msg := toggle.OffText
		fg := toggle.GridColor
		bg := termbox.ColorBlack
		if toggle.Value() {
			msg = toggle.OnText
			fg, bg = bg, fg
		}
		for _, ch := range msg {
			termbox.SetCell(x, y, ch, fg, bg)
			x++
		}
	}
	return y
}

func drawStatusLine(y int, model *mainModel) (newY int) {
	x := 0
	var fg termbox.Attribute
	underlined := false
	for _, ch := range ">>> " + model.StatusLine() {
		//The ( and ) are non-printing control characters
		if ch == '{' {
			underlined = true
			continue
		} else if ch == '}' {
			underlined = false
			continue
		}
		fg = termbox.ColorBlack
		if underlined {
			fg = fg | termbox.AttrUnderline | termbox.AttrBold
		}

		termbox.SetCell(x, y, ch, fg, termbox.ColorWhite)
		x++
	}

	width, _ := termbox.Size()

	for x < width {
		termbox.SetCell(x, y, ' ', fg, termbox.ColorWhite)
		x++
	}
	return y
}

func drawConsole(y int, model *mainModel) (newY int) {

	x := 0

	underlined := false

	splitMessage := strings.Split(model.consoleMessage, "\n")

	for _, line := range splitMessage {

		x = 0
		for _, ch := range line {
			if ch == '{' {
				underlined = true
				continue
			} else if ch == '}' {
				underlined = false
				continue
			}
			fg := termbox.Attribute(0xf6)
			if underlined {
				fg = termbox.Attribute(0xFC) | termbox.AttrBold
			}
			termbox.SetCell(x, y, ch, fg, termbox.ColorBlack)
			x++
		}
		y++
	}
	//y is one too many
	y--
	return y
}

func draw(model *mainModel) {

	clearScreen()

	y := 0

	y = drawGrid(y, model)
	y++
	y = drawToggleLine(y, model)
	y++
	y = drawStatusLine(y, model)
	y++
	y = drawConsole(y, model)

	termbox.Flush()
}
<|endoftext|>


<commit_before>// Copyright 2016 Red Hat, Inc, and individual contributors.
//
// 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 (
	"fmt"
	"io"
	"os"
	"path"
	"strings"

	"github.com/bbrowning/readline"

	"k8s.io/kubernetes/pkg/kubectl/cmd"
	cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
	"k8s.io/kubernetes/pkg/util/homedir"
)

type InternalCommand func(*kubesh, []string) error

type kubesh struct {
	finder           ResourceFinder
	context          []string
	rl               *readline.Instance
	internalCommands map[string]InternalCommand
}

func main() {
	cmdutil.BehaviorOnFatal(func(msg string, code int) {
		fmt.Println(msg)
	})

	factory := cmdutil.NewFactory(nil)
	finder := Resourceful{factory}
	kubectl := cmd.NewKubectlCommand(factory, os.Stdin, os.Stdout, os.Stderr)
	rl, err := readline.NewEx(&readline.Config{
		Prompt:       "> ",
		AutoComplete: &CommandCompleter{kubectl, finder},
		HistoryFile:  path.Join(homedir.HomeDir(), ".kubesh_history"),
	})
	if err != nil {
		panic(err)
	}

	defer rl.Close()

	sh := kubesh{
		finder: finder,
		rl:     rl,
		internalCommands: map[string]InternalCommand{
			"exit": func(_ *kubesh, _ []string) error {
				fmt.Println("Bye!")
				os.Exit(0)

				return nil
			},

			"pin": setContextCommand,
		},
	}

	for {
		line, err := sh.rl.Readline()
		if err == readline.ErrInterrupt {
			if len(line) == 0 {
				break
			} else {
				continue
			}
		} else if err == io.EOF {
			break
		}
		line = strings.TrimSpace(line)
		args := strings.Split(line, " ")
		internal, err := sh.runInternalCommand(args)
		if err != nil {
			panic(err)
		}

		if !internal {
			kubectl := cmd.NewKubectlCommand(factory, os.Stdin, os.Stdout, os.Stderr)
			// TODO: what do we do with an error here? do we care?
			args, _ = applyContext(sh.context, args, kubectl)
			kubectl.SetArgs(args)
			kubectl.Execute()
		}
	}
}

func (sh *kubesh) runInternalCommand(args []string) (bool, error) {
	if len(args) > 0 {
		if f := sh.internalCommands[args[0]]; f != nil {

			return true, f(sh, args)
		}
	}

	return false, nil
}
<commit_msg>Taking a stab at fixing #12<commit_after>// Copyright 2016 Red Hat, Inc, and individual contributors.
//
// 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 (
	"fmt"
	"io"
	"os"
	"path"
	"strings"

	"github.com/bbrowning/readline"

	"k8s.io/kubernetes/pkg/kubectl/cmd"
	cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
	"k8s.io/kubernetes/pkg/util/homedir"
)

type InternalCommand func(*kubesh, []string) error

type kubesh struct {
	finder           ResourceFinder
	context          []string
	rl               *readline.Instance
	internalCommands map[string]InternalCommand
}

func main() {
	cmdutil.BehaviorOnFatal(func(msg string, code int) {
		fmt.Println(msg)
	})

	factory := cmdutil.NewFactory(nil)
	finder := Resourceful{factory}
	kubectl := cmd.NewKubectlCommand(factory, os.Stdin, os.Stdout, os.Stderr)
	rl, err := readline.NewEx(&readline.Config{
		Prompt:       "> ",
		AutoComplete: &CommandCompleter{kubectl, finder},
		HistoryFile:  path.Join(homedir.HomeDir(), ".kubesh_history"),
	})
	if err != nil {
		panic(err)
	}

	defer rl.Close()

	sh := kubesh{
		finder: finder,
		rl:     rl,
		internalCommands: map[string]InternalCommand{
			"exit": func(_ *kubesh, _ []string) error {
				fmt.Println("Bye!")
				os.Exit(0)

				return nil
			},

			"pin": setContextCommand,
		},
	}

	fmt.Println("Welcome to kubesh, the kubectl shell!")
	fmt.Println("Type 'help' or TAB to see available commands")
	fmt.Println("For options/flags, tab complete a dash, '--<TAB>'")
	fmt.Println("Use 'pin' when multiple commands apply to same resource")
	fmt.Println("Use GNU readline key bindings for editing and history")
	for {
		line, err := sh.rl.Readline()
		if err == readline.ErrInterrupt {
			if len(line) == 0 {
				break
			} else {
				continue
			}
		} else if err == io.EOF {
			break
		}
		line = strings.TrimSpace(line)
		args := strings.Split(line, " ")
		internal, err := sh.runInternalCommand(args)
		if err != nil {
			panic(err)
		}

		if !internal {
			kubectl := cmd.NewKubectlCommand(factory, os.Stdin, os.Stdout, os.Stderr)
			// TODO: what do we do with an error here? do we care?
			args, _ = applyContext(sh.context, args, kubectl)
			kubectl.SetArgs(args)
			kubectl.Execute()
		}
	}
}

func (sh *kubesh) runInternalCommand(args []string) (bool, error) {
	if len(args) > 0 {
		if f := sh.internalCommands[args[0]]; f != nil {

			return true, f(sh, args)
		}
	}

	return false, nil
}
<|endoftext|>


<commit_before>/*
Copyright 2016 The Kubernetes Authors 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.
*/

package main

import (
	"bytes"
	"flag"
	"fmt"
	"log"
	"os"
	"path/filepath"
	"regexp"
	"strconv"

	"github.com/spf13/pflag"
	"k8s.io/klog/v2"

	// Register drivers
	"k8s.io/minikube/pkg/minikube/localpath"
	_ "k8s.io/minikube/pkg/minikube/registry/drvs"

	// Force exp dependency
	_ "golang.org/x/exp/ebnf"

	mlog "github.com/docker/machine/libmachine/log"

	"github.com/google/slowjam/pkg/stacklog"
	"github.com/pkg/profile"

	"k8s.io/minikube/cmd/minikube/cmd"
	"k8s.io/minikube/pkg/minikube/constants"
	"k8s.io/minikube/pkg/minikube/machine"
	"k8s.io/minikube/pkg/minikube/out"
	_ "k8s.io/minikube/pkg/provision"
)

const minikubeEnableProfile = "MINIKUBE_ENABLE_PROFILING"

var (
	// This regex is intentionally very specific, it's supposed to surface
	// unexpected errors from libmachine to the user.
	machineLogErrorRe   = regexp.MustCompile(`VirtualizationException`)
	machineLogWarningRe = regexp.MustCompile(`(?i)warning`)
)

func main() {
	bridgeLogMessages()
	defer klog.Flush()

	setFlags()

	s := stacklog.MustStartFromEnv("STACKLOG_PATH")
	defer s.Stop()

	if os.Getenv(minikubeEnableProfile) == "1" {
		defer profile.Start(profile.TraceProfile).Stop()
	}
	if os.Getenv(constants.IsMinikubeChildProcess) == "" {
		machine.StartDriver()
	}
	out.SetOutFile(os.Stdout)
	out.SetErrFile(os.Stderr)
	cmd.Execute()
}

// bridgeLogMessages bridges non-glog logs into klog
func bridgeLogMessages() {
	log.SetFlags(log.Lshortfile)
	log.SetOutput(stdLogBridge{})
	mlog.SetErrWriter(machineLogBridge{})
	mlog.SetOutWriter(machineLogBridge{})
	mlog.SetDebug(true)
}

type stdLogBridge struct{}

// Write parses the standard logging line and passes its components to klog
func (lb stdLogBridge) Write(b []byte) (n int, err error) {
	// Split "d.go:23: message" into "d.go", "23", and "message".
	parts := bytes.SplitN(b, []byte{':'}, 3)
	if len(parts) != 3 || len(parts[0]) < 1 || len(parts[2]) < 1 {
		klog.Errorf("bad log format: %s", b)
		return
	}

	file := string(parts[0])
	text := string(parts[2][1:]) // skip leading space
	line, err := strconv.Atoi(string(parts[1]))
	if err != nil {
		text = fmt.Sprintf("bad line number: %s", b)
		line = 0
	}
	klog.Infof("stdlog: %s:%d %s", file, line, text)
	return len(b), nil
}

// libmachine log bridge
type machineLogBridge struct{}

// Write passes machine driver logs to klog
func (lb machineLogBridge) Write(b []byte) (n int, err error) {
	if machineLogErrorRe.Match(b) {
		klog.Errorf("libmachine: %s", b)
	} else if machineLogWarningRe.Match(b) {
		klog.Warningf("libmachine: %s", b)
	} else {
		klog.Infof("libmachine: %s", b)
	}
	return len(b), nil
}

// setFlags sets the flags
func setFlags() {
	// parse flags beyond subcommand - get aroung go flag 'limitations':
	// "Flag parsing stops just before the first non-flag argument" (ref: https://pkg.go.dev/flag#hdr-Command_line_flag_syntax)
	pflag.CommandLine.ParseErrorsWhitelist.UnknownFlags = true
	pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
	// avoid 'pflag: help requested' error, as help will be defined later by cobra cmd.Execute()
	pflag.BoolP("help", "h", false, "")
	pflag.Parse()

	// set default flag value for logtostderr and alsologtostderr but don't override user's preferences
	if !pflag.CommandLine.Changed("logtostderr") {
		if err := pflag.Set("logtostderr", "false"); err != nil {
			klog.Warningf("Unable to set default flag value for logtostderr: %v", err)
		}
	}
	if !pflag.CommandLine.Changed("alsologtostderr") {
		if err := pflag.Set("alsologtostderr", "false"); err != nil {
			klog.Warningf("Unable to set default flag value for alsologtostderr: %v", err)
		}
	}
	setLastStartFlags()

	// make sure log_dir exists if log_file is not also set - the log_dir is mutually exclusive with the log_file option
	// ref: https://github.com/kubernetes/klog/blob/52c62e3b70a9a46101f33ebaf0b100ec55099975/klog.go#L491
	if pflag.Lookup("log_file") != nil && pflag.Lookup("log_file").Value.String() == "" &&
		pflag.Lookup("log_dir") != nil && pflag.Lookup("log_dir").Value.String() != "" {
		if err := os.MkdirAll(pflag.Lookup("log_dir").Value.String(), 0755); err != nil {
			klog.Warningf("unable to create log directory: %v", err)
		}
	}
}

// setLastStartFlags sets the log_file & log_dir flags if start command and no flags provided.
func setLastStartFlags() {
	if os.Args[1] != "start" {
		return
	}
	if pflag.CommandLine.Changed("log_file") || pflag.CommandLine.Changed("log_dir") {
		return
	}
	fp := localpath.LastStartLog()
	if err := os.Remove(fp); err != nil && !os.IsNotExist(err) {
		klog.Warningf("Unable to delete file %s: %v", err)
	}
	dp := filepath.Dir(fp)
	if _, err := os.Stat(dp); err != nil {
		if !os.IsNotExist(err) {
			klog.Warningf("Unable to get log folder %s: %v", dp, err)
		}
		if err := os.MkdirAll(dp, 0755); err != nil {
			klog.Warningf("Unable to make log folder %s: %v", dp, err)
		}
	}
	if !pflag.CommandLine.Changed("log_file") && !pflag.CommandLine.Changed("log_dir") {
		if err := pflag.Set("log_file", fp); err != nil {
			klog.Warningf("Unable to set default flag value for log_file: %v", err)
		}
	}
}
<commit_msg>removed duplicate flag check<commit_after>/*
Copyright 2016 The Kubernetes Authors 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.
*/

package main

import (
	"bytes"
	"flag"
	"fmt"
	"log"
	"os"
	"path/filepath"
	"regexp"
	"strconv"

	"github.com/spf13/pflag"
	"k8s.io/klog/v2"

	// Register drivers
	"k8s.io/minikube/pkg/minikube/localpath"
	_ "k8s.io/minikube/pkg/minikube/registry/drvs"

	// Force exp dependency
	_ "golang.org/x/exp/ebnf"

	mlog "github.com/docker/machine/libmachine/log"

	"github.com/google/slowjam/pkg/stacklog"
	"github.com/pkg/profile"

	"k8s.io/minikube/cmd/minikube/cmd"
	"k8s.io/minikube/pkg/minikube/constants"
	"k8s.io/minikube/pkg/minikube/machine"
	"k8s.io/minikube/pkg/minikube/out"
	_ "k8s.io/minikube/pkg/provision"
)

const minikubeEnableProfile = "MINIKUBE_ENABLE_PROFILING"

var (
	// This regex is intentionally very specific, it's supposed to surface
	// unexpected errors from libmachine to the user.
	machineLogErrorRe   = regexp.MustCompile(`VirtualizationException`)
	machineLogWarningRe = regexp.MustCompile(`(?i)warning`)
)

func main() {
	bridgeLogMessages()
	defer klog.Flush()

	setFlags()

	s := stacklog.MustStartFromEnv("STACKLOG_PATH")
	defer s.Stop()

	if os.Getenv(minikubeEnableProfile) == "1" {
		defer profile.Start(profile.TraceProfile).Stop()
	}
	if os.Getenv(constants.IsMinikubeChildProcess) == "" {
		machine.StartDriver()
	}
	out.SetOutFile(os.Stdout)
	out.SetErrFile(os.Stderr)
	cmd.Execute()
}

// bridgeLogMessages bridges non-glog logs into klog
func bridgeLogMessages() {
	log.SetFlags(log.Lshortfile)
	log.SetOutput(stdLogBridge{})
	mlog.SetErrWriter(machineLogBridge{})
	mlog.SetOutWriter(machineLogBridge{})
	mlog.SetDebug(true)
}

type stdLogBridge struct{}

// Write parses the standard logging line and passes its components to klog
func (lb stdLogBridge) Write(b []byte) (n int, err error) {
	// Split "d.go:23: message" into "d.go", "23", and "message".
	parts := bytes.SplitN(b, []byte{':'}, 3)
	if len(parts) != 3 || len(parts[0]) < 1 || len(parts[2]) < 1 {
		klog.Errorf("bad log format: %s", b)
		return
	}

	file := string(parts[0])
	text := string(parts[2][1:]) // skip leading space
	line, err := strconv.Atoi(string(parts[1]))
	if err != nil {
		text = fmt.Sprintf("bad line number: %s", b)
		line = 0
	}
	klog.Infof("stdlog: %s:%d %s", file, line, text)
	return len(b), nil
}

// libmachine log bridge
type machineLogBridge struct{}

// Write passes machine driver logs to klog
func (lb machineLogBridge) Write(b []byte) (n int, err error) {
	if machineLogErrorRe.Match(b) {
		klog.Errorf("libmachine: %s", b)
	} else if machineLogWarningRe.Match(b) {
		klog.Warningf("libmachine: %s", b)
	} else {
		klog.Infof("libmachine: %s", b)
	}
	return len(b), nil
}

// setFlags sets the flags
func setFlags() {
	// parse flags beyond subcommand - get aroung go flag 'limitations':
	// "Flag parsing stops just before the first non-flag argument" (ref: https://pkg.go.dev/flag#hdr-Command_line_flag_syntax)
	pflag.CommandLine.ParseErrorsWhitelist.UnknownFlags = true
	pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
	// avoid 'pflag: help requested' error, as help will be defined later by cobra cmd.Execute()
	pflag.BoolP("help", "h", false, "")
	pflag.Parse()

	// set default flag value for logtostderr and alsologtostderr but don't override user's preferences
	if !pflag.CommandLine.Changed("logtostderr") {
		if err := pflag.Set("logtostderr", "false"); err != nil {
			klog.Warningf("Unable to set default flag value for logtostderr: %v", err)
		}
	}
	if !pflag.CommandLine.Changed("alsologtostderr") {
		if err := pflag.Set("alsologtostderr", "false"); err != nil {
			klog.Warningf("Unable to set default flag value for alsologtostderr: %v", err)
		}
	}
	setLastStartFlags()

	// make sure log_dir exists if log_file is not also set - the log_dir is mutually exclusive with the log_file option
	// ref: https://github.com/kubernetes/klog/blob/52c62e3b70a9a46101f33ebaf0b100ec55099975/klog.go#L491
	if pflag.Lookup("log_file") != nil && pflag.Lookup("log_file").Value.String() == "" &&
		pflag.Lookup("log_dir") != nil && pflag.Lookup("log_dir").Value.String() != "" {
		if err := os.MkdirAll(pflag.Lookup("log_dir").Value.String(), 0755); err != nil {
			klog.Warningf("unable to create log directory: %v", err)
		}
	}
}

// setLastStartFlags sets the log_file & log_dir flags if start command and no flags provided.
func setLastStartFlags() {
	if os.Args[1] != "start" {
		return
	}
	if pflag.CommandLine.Changed("log_file") || pflag.CommandLine.Changed("log_dir") {
		return
	}
	fp := localpath.LastStartLog()
	if err := os.Remove(fp); err != nil && !os.IsNotExist(err) {
		klog.Warningf("Unable to delete file %s: %v", err)
	}
	dp := filepath.Dir(fp)
	if _, err := os.Stat(dp); err != nil {
		if !os.IsNotExist(err) {
			klog.Warningf("Unable to get log folder %s: %v", dp, err)
		}
		if err := os.MkdirAll(dp, 0755); err != nil {
			klog.Warningf("Unable to make log folder %s: %v", dp, err)
		}
	}
	if err := pflag.Set("log_file", fp); err != nil {
		klog.Warningf("Unable to set default flag value for log_file: %v", err)
	}
}
<|endoftext|>


<commit_before>package main

import (
	"time"

	"github.com/deshboard/boilerplate-crondaemon/app"
	"github.com/goph/serverz"
)

// newDaemonServer creates a new daemon server.
func newDaemonServer(appCtx *application) serverz.Server {
	var ticker *time.Ticker

	if appCtx.config.Daemon {
		ticker = time.NewTicker(appCtx.config.DaemonSchedule)
	}

	return &serverz.AppServer{
		Server: &serverz.DaemonServer{
			Daemon: &serverz.CronDaemon{
				Job: app.NewService(
					app.Logger(appCtx.logger),
					app.ErrorHandler(appCtx.errorHandler),
				),
				Ticker: ticker,
			},
		},
		Name:   "daemon",
		Logger: appCtx.logger,
	}
}
<commit_msg>Format code<commit_after>package main

import (
	"time"

	. "github.com/deshboard/boilerplate-crondaemon/app"
	"github.com/goph/serverz"
)

// newDaemonServer creates a new daemon server.
func newDaemonServer(app *application) serverz.Server {
	var ticker *time.Ticker

	if app.config.Daemon {
		ticker = time.NewTicker(app.config.DaemonSchedule)
	}

	return &serverz.AppServer{
		Server: &serverz.DaemonServer{
			Daemon: &serverz.CronDaemon{
				Job: NewService(
					Logger(app.logger),
					ErrorHandler(app.errorHandler),
				),
				Ticker: ticker,
			},
		},
		Name:   "daemon",
		Logger: app.logger,
	}
}
<|endoftext|>


<commit_before>package checkersbot

import (
	"encoding/json"
	"github.com/couchbaselabs/go.assert"
	"github.com/couchbaselabs/logg"
	"log"
	"testing"
)

func init() {

	logg.LogKeys["NODE_SEND"] = true
	logg.LogKeys["NODE_RECV"] = true
	logg.LogKeys["TEST"] = true
	logg.LogKeys["DEBUG"] = true

}

func TestIsOurTurn(t *testing.T) {

	jsonString := `{"_id":"game:checkers","_rev":"3773-aa8a4c5a30b49e1eec65dff6df05561f","activeTeam":0,"channels":["game"],"moveDeadline":"2013-09-20T21:13:35Z","moveInterval":30,"moves":[{"game":153563,"locations":[10,14],"piece":9,"team":0,"turn":1},{"game":153563,"locations":[23,19],"piece":2,"team":1,"turn":2}],"number":153563,"startTime":"2013-09-20T17:11:53Z","teams":[{"participantCount":1,"pieces":[{"location":1},{"location":2},{"location":3},{"location":4},{"location":5},{"location":6,"validMoves":[{"captures":[],"king":false,"locations":[10]}]},{"location":7,"validMoves":[{"captures":[],"king":false,"locations":[10]}]},{"location":8},{"location":9,"validMoves":[{"captures":[],"king":false,"locations":[13]}]},{"location":14,"validMoves":[{"captures":[],"king":false,"locations":[17]},{"captures":[],"king":false,"locations":[18]}]},{"location":11,"validMoves":[{"captures":[],"king":false,"locations":[15]},{"captures":[],"king":false,"locations":[16]}]},{"location":12,"validMoves":[{"captures":[],"king":false,"locations":[16]}]}]},{"participantCount":0,"pieces":[{"location":21},{"location":22},{"location":19},{"location":24},{"location":25},{"location":26},{"location":27},{"location":28},{"location":29},{"location":30},{"location":31},{"location":32}]}],"turn":3,"votesDoc":"votes:checkers"}`

	gameState := NewGameStateFromString(jsonString)

	game := &Game{ourTeamId: 0}
	result := game.isOurTurn(gameState)
	assert.True(t, result)

	game.ourTeamId = 1
	assert.False(t, game.isOurTurn(gameState))

}

func TestCheckGameDocInChanges(t *testing.T) {

	jsonString := `{"results":[{"seq":"*:3408","id":"user:6213C1A1-4E5F-429E-91C9-CDC2BF1537C3","changes":[{"rev":"3-783b9cda9b7b9e6faac2d8bda9e16535"}]},{"seq":"*:3409","id":"vote:6213C1A1-4E5F-429E-91C9-CDC2BF1537C3","changes":[{"rev":"1-393aaf8f37404c4a0159d9ec8dc1e0ee"}]},{"seq":"*:3440","id":"votes:checkers","changes":[{"rev":"16-ebaa86d97e63940fddfdbd11a219e9e6"}]},{"seq":"*:3641","id":"game:checkers","changes":[{"rev":"3586-09a232e6b524940185b0b268483981ea"}]}],"last_seq":"*:3641"}`
	jsonBytes := []byte(jsonString)
	changes := new(Changes)
	err := json.Unmarshal(jsonBytes, changes)
	if err != nil {
		log.Fatal(err)
	}

	game := &Game{}
	result := game.hasGameDocChanged(*changes)
	assert.True(t, result)

}

func TestCalculatePreMoveSleepSeconds(t *testing.T) {
	game := &Game{}
	game.gameState.MoveInterval = 30
	preMoveSleepSeconds := game.calculatePreMoveSleepSeconds()
	assert.True(t, preMoveSleepSeconds > 0)
	assert.True(t, preMoveSleepSeconds <= 30)
}
<commit_msg>add TestFinished()<commit_after>package checkersbot

import (
	"encoding/json"
	"github.com/couchbaselabs/go.assert"
	"github.com/couchbaselabs/logg"
	"log"
	"testing"
)

func init() {

	logg.LogKeys["NODE_SEND"] = true
	logg.LogKeys["NODE_RECV"] = true
	logg.LogKeys["TEST"] = true
	logg.LogKeys["DEBUG"] = true

}

func TestIsOurTurn(t *testing.T) {

	jsonString := `{"_id":"game:checkers","_rev":"3773-aa8a4c5a30b49e1eec65dff6df05561f","activeTeam":0,"channels":["game"],"moveDeadline":"2013-09-20T21:13:35Z","moveInterval":30,"moves":[{"game":153563,"locations":[10,14],"piece":9,"team":0,"turn":1},{"game":153563,"locations":[23,19],"piece":2,"team":1,"turn":2}],"number":153563,"startTime":"2013-09-20T17:11:53Z","teams":[{"participantCount":1,"pieces":[{"location":1},{"location":2},{"location":3},{"location":4},{"location":5},{"location":6,"validMoves":[{"captures":[],"king":false,"locations":[10]}]},{"location":7,"validMoves":[{"captures":[],"king":false,"locations":[10]}]},{"location":8},{"location":9,"validMoves":[{"captures":[],"king":false,"locations":[13]}]},{"location":14,"validMoves":[{"captures":[],"king":false,"locations":[17]},{"captures":[],"king":false,"locations":[18]}]},{"location":11,"validMoves":[{"captures":[],"king":false,"locations":[15]},{"captures":[],"king":false,"locations":[16]}]},{"location":12,"validMoves":[{"captures":[],"king":false,"locations":[16]}]}]},{"participantCount":0,"pieces":[{"location":21},{"location":22},{"location":19},{"location":24},{"location":25},{"location":26},{"location":27},{"location":28},{"location":29},{"location":30},{"location":31},{"location":32}]}],"turn":3,"votesDoc":"votes:checkers"}`

	gameState := NewGameStateFromString(jsonString)

	game := &Game{ourTeamId: 0}
	result := game.isOurTurn(gameState)
	assert.True(t, result)

	game.ourTeamId = 1
	assert.False(t, game.isOurTurn(gameState))

}

func TestFinished(t *testing.T) {

	jsonString := `{"_id":"game:checkers","_rev":"3773-aa8a4c5a30b49e1eec65dff6df05561f","activeTeam":0,"channels":["game"],"moveDeadline":"2013-09-20T21:13:35Z","moveInterval":30,"moves":[{"game":153563,"locations":[10,14],"piece":9,"team":0,"turn":1},{"game":153563,"locations":[23,19],"piece":2,"team":1,"turn":2}],"number":153563,"startTime":"2013-09-20T17:11:53Z","teams":[{"participantCount":1,"pieces":[{"location":1},{"location":2},{"location":3},{"location":4},{"location":5},{"location":6,"validMoves":[{"captures":[],"king":false,"locations":[10]}]},{"location":7,"validMoves":[{"captures":[],"king":false,"locations":[10]}]},{"location":8},{"location":9,"validMoves":[{"captures":[],"king":false,"locations":[13]}]},{"location":14,"validMoves":[{"captures":[],"king":false,"locations":[17]},{"captures":[],"king":false,"locations":[18]}]},{"location":11,"validMoves":[{"captures":[],"king":false,"locations":[15]},{"captures":[],"king":false,"locations":[16]}]},{"location":12,"validMoves":[{"captures":[],"king":false,"locations":[16]}]}]},{"participantCount":0,"pieces":[{"location":21},{"location":22},{"location":19},{"location":24},{"location":25},{"location":26},{"location":27},{"location":28},{"location":29},{"location":30},{"location":31},{"location":32}]}],"turn":3,"votesDoc":"votes:checkers"}`

	gameState := NewGameStateFromString(jsonString)

	game := &Game{ourTeamId: 0}
	game.gameState = gameState
	assert.False(t, game.finished(gameState))

	gameStatePrime := gameState
	gameStatePrime.WinningTeam = RED_TEAM
	gameStatePrime.Number = gameState.Number + 1
	assert.NotEquals(t, gameState.Number, gameStatePrime.Number)

	result := game.finished(gameStatePrime)
	assert.True(t, result)

}

func TestCheckGameDocInChanges(t *testing.T) {

	jsonString := `{"results":[{"seq":"*:3408","id":"user:6213C1A1-4E5F-429E-91C9-CDC2BF1537C3","changes":[{"rev":"3-783b9cda9b7b9e6faac2d8bda9e16535"}]},{"seq":"*:3409","id":"vote:6213C1A1-4E5F-429E-91C9-CDC2BF1537C3","changes":[{"rev":"1-393aaf8f37404c4a0159d9ec8dc1e0ee"}]},{"seq":"*:3440","id":"votes:checkers","changes":[{"rev":"16-ebaa86d97e63940fddfdbd11a219e9e6"}]},{"seq":"*:3641","id":"game:checkers","changes":[{"rev":"3586-09a232e6b524940185b0b268483981ea"}]}],"last_seq":"*:3641"}`
	jsonBytes := []byte(jsonString)
	changes := new(Changes)
	err := json.Unmarshal(jsonBytes, changes)
	if err != nil {
		log.Fatal(err)
	}

	game := &Game{}
	result := game.hasGameDocChanged(*changes)
	assert.True(t, result)

}

func TestCalculatePreMoveSleepSeconds(t *testing.T) {
	game := &Game{}
	game.gameState.MoveInterval = 30
	preMoveSleepSeconds := game.calculatePreMoveSleepSeconds()
	assert.True(t, preMoveSleepSeconds > 0)
	assert.True(t, preMoveSleepSeconds <= 30)
}
<|endoftext|>


<commit_before>// generated by go-import-subtree -- DO NOT EDIT
package main

import _ "github.com/taskcluster/taskcluster-worker/engines/enginetest"
import _ "github.com/taskcluster/taskcluster-worker/engines/mock"
import _ "github.com/taskcluster/taskcluster-worker/engines/osxnative"
import _ "github.com/taskcluster/taskcluster-worker/engines/qemu"
import _ "github.com/taskcluster/taskcluster-worker/engines/script"
import _ "github.com/taskcluster/taskcluster-worker/plugins/artifacts"
import _ "github.com/taskcluster/taskcluster-worker/plugins/env"
import _ "github.com/taskcluster/taskcluster-worker/plugins/interactive"
import _ "github.com/taskcluster/taskcluster-worker/plugins/livelog"
import _ "github.com/taskcluster/taskcluster-worker/plugins/plugintest"
import _ "github.com/taskcluster/taskcluster-worker/plugins/success"
import _ "github.com/taskcluster/taskcluster-worker/commands/daemon"
import _ "github.com/taskcluster/taskcluster-worker/commands/help"
import _ "github.com/taskcluster/taskcluster-worker/commands/qemu-build"
import _ "github.com/taskcluster/taskcluster-worker/commands/qemu-guest-tools"
import _ "github.com/taskcluster/taskcluster-worker/commands/qemu-run"
import _ "github.com/taskcluster/taskcluster-worker/commands/schema"
import _ "github.com/taskcluster/taskcluster-worker/commands/shell"
import _ "github.com/taskcluster/taskcluster-worker/commands/shell-server"
import _ "github.com/taskcluster/taskcluster-worker/commands/work"
import _ "github.com/taskcluster/taskcluster-worker/config/abs"
import _ "github.com/taskcluster/taskcluster-worker/config/configtest"
import _ "github.com/taskcluster/taskcluster-worker/config/env"
import _ "github.com/taskcluster/taskcluster-worker/config/packet"
import _ "github.com/taskcluster/taskcluster-worker/config/secrets"
<commit_msg>Run make generate<commit_after>// generated by go-import-subtree -- DO NOT EDIT
package main

import _ "github.com/taskcluster/taskcluster-worker/engines/enginetest"
import _ "github.com/taskcluster/taskcluster-worker/engines/mock"
import _ "github.com/taskcluster/taskcluster-worker/engines/native"
import _ "github.com/taskcluster/taskcluster-worker/engines/osxnative"
import _ "github.com/taskcluster/taskcluster-worker/engines/qemu"
import _ "github.com/taskcluster/taskcluster-worker/engines/script"
import _ "github.com/taskcluster/taskcluster-worker/plugins/artifacts"
import _ "github.com/taskcluster/taskcluster-worker/plugins/env"
import _ "github.com/taskcluster/taskcluster-worker/plugins/interactive"
import _ "github.com/taskcluster/taskcluster-worker/plugins/livelog"
import _ "github.com/taskcluster/taskcluster-worker/plugins/plugintest"
import _ "github.com/taskcluster/taskcluster-worker/plugins/success"
import _ "github.com/taskcluster/taskcluster-worker/commands/daemon"
import _ "github.com/taskcluster/taskcluster-worker/commands/help"
import _ "github.com/taskcluster/taskcluster-worker/commands/qemu-build"
import _ "github.com/taskcluster/taskcluster-worker/commands/qemu-guest-tools"
import _ "github.com/taskcluster/taskcluster-worker/commands/qemu-run"
import _ "github.com/taskcluster/taskcluster-worker/commands/schema"
import _ "github.com/taskcluster/taskcluster-worker/commands/shell"
import _ "github.com/taskcluster/taskcluster-worker/commands/shell-server"
import _ "github.com/taskcluster/taskcluster-worker/commands/work"
import _ "github.com/taskcluster/taskcluster-worker/config/abs"
import _ "github.com/taskcluster/taskcluster-worker/config/configtest"
import _ "github.com/taskcluster/taskcluster-worker/config/env"
import _ "github.com/taskcluster/taskcluster-worker/config/packet"
import _ "github.com/taskcluster/taskcluster-worker/config/secrets"
<|endoftext|>


<commit_before>package summarize

import (
	"strings"

	"github.com/jdkato/prose/internal/util"
	"github.com/montanaflynn/stats"
)

// WordDensity returns a map of each word and its density.
func (d *Document) WordDensity() map[string]float64 {
	density := make(map[string]float64)
	for word, freq := range d.WordFrequency {
		val, _ := stats.Round(float64(freq)/d.NumWords, 3)
		density[word] = val
	}
	return density
}

// Keywords returns the top-n most used words in the Document, omitting stop
// words and normalizing case.
func (d *Document) Keywords() map[string]int {
	scores := map[string]int{}
	for word, freq := range d.WordFrequency {
		normalized := strings.ToLower(word)
		if util.StringInSlice(normalized, stopWords) {
			continue
		}
		if _, found := scores[normalized]; found {
			scores[normalized] += freq
		} else {
			scores[normalized] = freq
		}
	}
	return scores
}

// MeanWordLength returns the mean number of characters per word.
func (d *Document) MeanWordLength() float64 {
	val, _ := stats.Round(d.NumCharacters/d.NumWords, 3)
	return val
}
<commit_msg>Update old GoDoc comment<commit_after>package summarize

import (
	"strings"

	"github.com/jdkato/prose/internal/util"
	"github.com/montanaflynn/stats"
)

// WordDensity returns a map of each word and its density.
func (d *Document) WordDensity() map[string]float64 {
	density := make(map[string]float64)
	for word, freq := range d.WordFrequency {
		val, _ := stats.Round(float64(freq)/d.NumWords, 3)
		density[word] = val
	}
	return density
}

// Keywords returns a Document's words in the form
//
//    map[word]count
//
// omitting stop words and normalizing case.
func (d *Document) Keywords() map[string]int {
	scores := map[string]int{}
	for word, freq := range d.WordFrequency {
		normalized := strings.ToLower(word)
		if util.StringInSlice(normalized, stopWords) {
			continue
		}
		if _, found := scores[normalized]; found {
			scores[normalized] += freq
		} else {
			scores[normalized] = freq
		}
	}
	return scores
}

// MeanWordLength returns the mean number of characters per word.
func (d *Document) MeanWordLength() float64 {
	val, _ := stats.Round(d.NumCharacters/d.NumWords, 3)
	return val
}
<|endoftext|>


<commit_before>package immortal

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

func TestReadPidFileNonexistent(t *testing.T) {
	sup := new(Sup)
	i, e := sup.ReadPidFile("nonexistent")
	if i != 0 {
		t.Errorf("Expecting: 0 got: %v", i)
	}
	if e == nil {
		t.Errorf("Expecting: no such file or directory")
	}
}

func TestReadPidFileBadContent(t *testing.T) {
	sup := new(Sup)
	i, e := sup.ReadPidFile("funcs.go")
	if i != 0 {
		t.Errorf("Expecting: 0 got: %v", i)
	}
	if e == nil {
		t.Errorf("Expecting: no such file or directory")
	}
}

func TestReadPidFile(t *testing.T) {
	content := []byte("1234")
	tmpfile, err := ioutil.TempFile("", "TestReadPidfile")
	if err != nil {
		t.Error(err)
	}

	defer os.Remove(tmpfile.Name()) // clean up

	if _, err := tmpfile.Write(content); err != nil {
		t.Error(err)
	}
	if err := tmpfile.Close(); err != nil {
		t.Error(err)
	}
	sup := new(Sup)
	i, e := sup.ReadPidFile(tmpfile.Name())
	if i != 1234 {
		t.Errorf("Expecting: 1234 got: %v", i)
	}
	if e != nil {
		t.Error(e)
	}
}
<commit_msg>	modified:   supervisor_test.go<commit_after>package immortal

import (
	"io/ioutil"
	"os"
	"os/signal"
	"path/filepath"
	"testing"
	"time"
)

func TestReadPidFileNonexistent(t *testing.T) {
	sup := new(Sup)
	i, e := sup.ReadPidFile("nonexistent")
	if i != 0 {
		t.Errorf("Expecting: 0 got: %v", i)
	}
	if e == nil {
		t.Errorf("Expecting: no such file or directory")
	}
}

func TestReadPidFileBadContent(t *testing.T) {
	sup := new(Sup)
	i, e := sup.ReadPidFile("funcs.go")
	if i != 0 {
		t.Errorf("Expecting: 0 got: %v", i)
	}
	if e == nil {
		t.Errorf("Expecting: no such file or directory")
	}
}

func TestReadPidFile(t *testing.T) {
	content := []byte("1234")
	tmpfile, err := ioutil.TempFile("", "TestReadPidfile")
	if err != nil {
		t.Error(err)
	}

	defer os.Remove(tmpfile.Name()) // clean up

	if _, err := tmpfile.Write(content); err != nil {
		t.Error(err)
	}
	if err := tmpfile.Close(); err != nil {
		t.Error(err)
	}
	sup := new(Sup)
	i, e := sup.ReadPidFile(tmpfile.Name())
	if i != 1234 {
		t.Errorf("Expecting: 1234 got: %v", i)
	}
	if e != nil {
		t.Error(e)
	}
}

func TestHelperProcessSup(*testing.T) {
	if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
		return
	}
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt, os.Kill)
	select {
	case <-c:
		os.Exit(1)
	case <-time.After(10 * time.Second):
		os.Exit(0)
	}
}

func TestSupervise(t *testing.T) {
	//log.SetOutput(ioutil.Discard)
	base := filepath.Base(os.Args[0]) // "exec.test"
	dir := filepath.Dir(os.Args[0])   // "/tmp/go-buildNNNN/os/exec/_test"
	if dir == "." {
		t.Skip("skipping; running test at root somehow")
	}
	parentDir := filepath.Dir(dir) // "/tmp/go-buildNNNN/os/exec"
	dirBase := filepath.Base(dir)  // "_test"
	if dirBase == "." {
		t.Skipf("skipping; unexpected shallow dir of %q", dir)
	}
	cfg := &Config{
		Env:     map[string]string{"GO_WANT_HELPER_PROCESS": "1"},
		command: []string{filepath.Join(dirBase, base), "-test.run=TestHelperProcessSup"},
		Cwd:     parentDir,
		Pid: Pid{
			Parent: filepath.Join(parentDir, "parent.pid"),
			Child:  filepath.Join(parentDir, "child.pid"),
		},
	}
	d, err := New(cfg)
	if err != nil {
		t.Fatal(err)
	}
	d.Run()
	sup := new(Sup)
	go Supervise(sup, d)

	// check pids
	if pid, err := sup.ReadPidFile(filepath.Join(parentDir, "parent.pid")); err != nil {
		t.Error(err)
	} else {
		expect(t, os.Getpid(), pid)
	}
	if pid, err := sup.ReadPidFile(filepath.Join(parentDir, "child.pid")); err != nil {
		t.Error(err)
	} else {
		expect(t, d.Process().Pid, pid)
	}

	select {
	case <-time.After(1 * time.Second):
		d.Control.fifo <- Return{err: nil, msg: "x"}
	}
}
<|endoftext|>


<commit_before>package connector

import (
	"context"
	"fmt"
	"io"
	"os"
	"os/user"
	"strings"
	"sync"
	"sync/atomic"
	"time"

	"github.com/datawire/dlib/dgroup"
	"github.com/datawire/dlib/dlog"
	"github.com/pkg/errors"
	"google.golang.org/grpc"

	"github.com/datawire/telepresence2/pkg/client"
	"github.com/datawire/telepresence2/pkg/rpc/manager"
)

// trafficManager is a handle to access the Traffic Manager in a
// cluster.
type trafficManager struct {
	aiListener      aiListener
	iiListener      iiListener
	conn            *grpc.ClientConn
	grpc            manager.ManagerClient
	startup         chan bool
	apiPort         int32
	sshPort         int32
	userAndHost     string
	installID       string // telepresence's install ID
	sessionID       string // sessionID returned by the traffic-manager
	apiErr          error  // holds the latest traffic-manager API error
	connectCI       bool   // whether --ci was passed to connect
	installer       *installer
	myIntercept     string
	cancelIntercept context.CancelFunc
	// previewHost string // hostname to use for preview URLs, if enabled
}

// newTrafficManager returns a TrafficManager resource for the given
// cluster if it has a Traffic Manager service.
func newTrafficManager(c context.Context, cluster *k8sCluster, installID string, isCI bool) (*trafficManager, error) {
	name, err := user.Current()
	if err != nil {
		return nil, errors.Wrap(err, "user.Current()")
	}
	host, err := os.Hostname()
	if err != nil {
		return nil, errors.Wrap(err, "os.Hostname()")
	}

	// Ensure that we have a traffic-manager to talk to.
	ti, err := newTrafficManagerInstaller(cluster)
	if err != nil {
		return nil, errors.Wrap(err, "new installer")
	}
	localAPIPort, err := getFreePort()
	if err != nil {
		return nil, errors.Wrap(err, "get free port for API")
	}
	localSSHPort, err := getFreePort()
	if err != nil {
		return nil, errors.Wrap(err, "get free port for ssh")
	}
	tm := &trafficManager{
		installer:   ti,
		apiPort:     localAPIPort,
		sshPort:     localSSHPort,
		installID:   installID,
		connectCI:   isCI,
		startup:     make(chan bool),
		userAndHost: fmt.Sprintf("%s@%s", name, host)}

	dgroup.ParentGroup(c).Go("traffic-manager", tm.start)
	return tm, nil
}

func (tm *trafficManager) waitUntilStarted() error {
	<-tm.startup
	return tm.apiErr
}

func (tm *trafficManager) start(c context.Context) error {
	remoteSSHPort, remoteAPIPort, err := tm.installer.ensureManager(c)
	if err != nil {
		tm.apiErr = err
		close(tm.startup)
		return err
	}
	kpfArgs := []string{
		"port-forward",
		"svc/traffic-manager",
		fmt.Sprintf("%d:%d", tm.sshPort, remoteSSHPort),
		fmt.Sprintf("%d:%d", tm.apiPort, remoteAPIPort)}

	return client.Retry(c, func(c context.Context) error {
		return tm.installer.portForwardAndThen(c, kpfArgs, "init-grpc", tm.initGrpc)
	}, time.Second, 15*time.Second)
}

func (tm *trafficManager) initGrpc(c context.Context) (err error) {
	defer func() {
		tm.apiErr = err
		close(tm.startup)
	}()

	// First check. Establish connection
	tc, cancel := context.WithTimeout(c, connectTimeout)
	defer cancel()

	var conn *grpc.ClientConn
	conn, err = grpc.DialContext(tc, fmt.Sprintf("127.0.0.1:%d", tm.apiPort),
		grpc.WithInsecure(),
		grpc.WithNoProxy(),
		grpc.WithBlock())
	if err != nil {
		dlog.Errorf(c, "error when dialing traffic-manager: %s", err.Error())
		return err
	}

	mClient := manager.NewManagerClient(conn)
	si, err := mClient.ArriveAsClient(c, &manager.ClientInfo{
		Name:      tm.userAndHost,
		InstallId: tm.installID,
		Product:   "telepresence",
		Version:   client.Version(),
	})

	if err != nil {
		dlog.Errorf(c, "ArriveAsClient: %s", err.Error())
		conn.Close()
		return err
	}
	tm.conn = conn
	tm.grpc = mClient
	tm.sessionID = si.SessionId

	g := dgroup.ParentGroup(c)
	g.Go("remain", tm.remain)
	g.Go("watch-agents", tm.watchAgents)
	g.Go("watch-intercepts", tm.watchIntercepts)
	return nil
}

func (tm *trafficManager) watchAgents(c context.Context) error {
	ac, err := tm.grpc.WatchAgents(c, tm.session())
	if err != nil {
		return err
	}
	return tm.aiListener.start(c, ac)
}

func (tm *trafficManager) watchIntercepts(c context.Context) error {
	ic, err := tm.grpc.WatchIntercepts(c, tm.session())
	if err != nil {
		return err
	}
	return tm.iiListener.start(c, ic)
}

func (tm *trafficManager) session() *manager.SessionInfo {
	return &manager.SessionInfo{SessionId: tm.sessionID}
}

func (tm *trafficManager) agentInfoSnapshot() *manager.AgentInfoSnapshot {
	return tm.aiListener.getData()
}

func (tm *trafficManager) interceptInfoSnapshot() *manager.InterceptInfoSnapshot {
	return tm.iiListener.getData()
}

func (tm *trafficManager) remain(c context.Context) error {
	ticker := time.NewTicker(5 * time.Second)
	for {
		select {
		case <-c.Done():
			return nil
		case <-ticker.C:
			_, err := tm.grpc.Remain(c, tm.session())
			if err != nil {
				return err
			}
		}
	}
}

// Close implements io.Closer
func (tm *trafficManager) Close() error {
	if tm.conn != nil {
		_ = tm.conn.Close()
		tm.conn = nil
		tm.grpc = nil
	}
	return nil
}

// A watcher listens on a grpc.ClientStream and notifies listeners when
// something arrives.
type watcher struct {
	entryMaker    func() interface{} // returns an instance of the type produced by the stream
	listeners     []listener
	listenersLock sync.RWMutex
	stream        grpc.ClientStream
}

// watch reads messages from the stream and passes them onto registered listeners. The
// function terminates when the context used when the stream was acquired is cancelled,
// when io.EOF is encountered, or an error occurs during read.
func (r *watcher) watch(c context.Context) error {
	dataChan := make(chan interface{}, 1000)
	go func() {
		for {
			select {
			case <-c.Done():
				return
			case data := <-dataChan:
				if data == nil {
					return
				}

				r.listenersLock.RLock()
				lc := make([]listener, len(r.listeners))
				copy(lc, r.listeners)
				r.listenersLock.RUnlock()

				for _, l := range lc {
					l.onData(data)
				}
			}
		}
	}()

	for {
		data := r.entryMaker()
		if err := r.stream.RecvMsg(data); err != nil {
			if err == io.EOF || strings.HasSuffix(err.Error(), " is closing") {
				err = nil
			}
			close(dataChan)
			return err
		}
		dataChan <- data
	}
}

func (r *watcher) addListener(l listener) {
	r.listenersLock.Lock()
	r.listeners = append(r.listeners, l)
	r.listenersLock.Unlock()
}

func (r *watcher) removeListener(l listener) {
	r.listenersLock.Lock()
	ls := r.listeners
	for i, x := range ls {
		if l == x {
			last := len(ls) - 1
			ls[i] = ls[last]
			ls[last] = nil
			r.listeners = ls[:last]
			break
		}
	}
	r.listenersLock.Unlock()
}

// A listener gets notified by a watcher when something arrives on the stream
type listener interface {
	onData(data interface{})
}

// An aiListener keeps track of the latest received AgentInfoSnapshot and provides the
// watcher needed to register other listeners.
type aiListener struct {
	watcher
	data atomic.Value
}

func (al *aiListener) getData() *manager.AgentInfoSnapshot {
	v := al.data.Load()
	if v == nil {
		return nil
	}
	return v.(*manager.AgentInfoSnapshot)
}

func (al *aiListener) onData(d interface{}) {
	al.data.Store(d)
}

func (al *aiListener) start(c context.Context, stream grpc.ClientStream) error {
	al.stream = stream
	al.listeners = []listener{al}
	al.entryMaker = func() interface{} { return new(manager.AgentInfoSnapshot) }
	return al.watch(c)
}

func (il *iiListener) onData(d interface{}) {
	il.data.Store(d)
}

func (il *iiListener) start(c context.Context, stream grpc.ClientStream) error {
	il.stream = stream
	il.listeners = []listener{il}
	il.entryMaker = func() interface{} { return new(manager.InterceptInfoSnapshot) }
	return il.watch(c)
}

// iiActive is a listener that waits for an intercept with a given id to become active
type iiActive struct {
	id   string
	done chan *manager.InterceptInfo
}

func (ia *iiActive) onData(d interface{}) {
	if iis, ok := d.(*manager.InterceptInfoSnapshot); ok {
		for _, ii := range iis.Intercepts {
			if ii.Id == ia.id && ii.Disposition != manager.InterceptDispositionType_WAITING {
				ia.done <- ii
				break
			}
		}
	}
}

// aiPresent is a listener that waits for an agent with a given name to be present
type aiPresent struct {
	name string
	done chan *manager.AgentInfo
}

func (ap *aiPresent) onData(d interface{}) {
	if ais, ok := d.(*manager.AgentInfoSnapshot); ok {
		for _, ai := range ais.Agents {
			if ai.Name == ap.name {
				ap.done <- ai
				break
			}
		}
	}
}
<commit_msg>Make listener for active intercept and present agent, one-shot only<commit_after>package connector

import (
	"context"
	"fmt"
	"io"
	"os"
	"os/user"
	"strings"
	"sync"
	"sync/atomic"
	"time"

	"github.com/datawire/dlib/dgroup"
	"github.com/datawire/dlib/dlog"
	"github.com/pkg/errors"
	"google.golang.org/grpc"

	"github.com/datawire/telepresence2/pkg/client"
	"github.com/datawire/telepresence2/pkg/rpc/manager"
)

// trafficManager is a handle to access the Traffic Manager in a
// cluster.
type trafficManager struct {
	aiListener      aiListener
	iiListener      iiListener
	conn            *grpc.ClientConn
	grpc            manager.ManagerClient
	startup         chan bool
	apiPort         int32
	sshPort         int32
	userAndHost     string
	installID       string // telepresence's install ID
	sessionID       string // sessionID returned by the traffic-manager
	apiErr          error  // holds the latest traffic-manager API error
	connectCI       bool   // whether --ci was passed to connect
	installer       *installer
	myIntercept     string
	cancelIntercept context.CancelFunc
	// previewHost string // hostname to use for preview URLs, if enabled
}

// newTrafficManager returns a TrafficManager resource for the given
// cluster if it has a Traffic Manager service.
func newTrafficManager(c context.Context, cluster *k8sCluster, installID string, isCI bool) (*trafficManager, error) {
	name, err := user.Current()
	if err != nil {
		return nil, errors.Wrap(err, "user.Current()")
	}
	host, err := os.Hostname()
	if err != nil {
		return nil, errors.Wrap(err, "os.Hostname()")
	}

	// Ensure that we have a traffic-manager to talk to.
	ti, err := newTrafficManagerInstaller(cluster)
	if err != nil {
		return nil, errors.Wrap(err, "new installer")
	}
	localAPIPort, err := getFreePort()
	if err != nil {
		return nil, errors.Wrap(err, "get free port for API")
	}
	localSSHPort, err := getFreePort()
	if err != nil {
		return nil, errors.Wrap(err, "get free port for ssh")
	}
	tm := &trafficManager{
		installer:   ti,
		apiPort:     localAPIPort,
		sshPort:     localSSHPort,
		installID:   installID,
		connectCI:   isCI,
		startup:     make(chan bool),
		userAndHost: fmt.Sprintf("%s@%s", name, host)}

	dgroup.ParentGroup(c).Go("traffic-manager", tm.start)
	return tm, nil
}

func (tm *trafficManager) waitUntilStarted() error {
	<-tm.startup
	return tm.apiErr
}

func (tm *trafficManager) start(c context.Context) error {
	remoteSSHPort, remoteAPIPort, err := tm.installer.ensureManager(c)
	if err != nil {
		tm.apiErr = err
		close(tm.startup)
		return err
	}
	kpfArgs := []string{
		"port-forward",
		"svc/traffic-manager",
		fmt.Sprintf("%d:%d", tm.sshPort, remoteSSHPort),
		fmt.Sprintf("%d:%d", tm.apiPort, remoteAPIPort)}

	return client.Retry(c, func(c context.Context) error {
		return tm.installer.portForwardAndThen(c, kpfArgs, "init-grpc", tm.initGrpc)
	}, time.Second, 15*time.Second)
}

func (tm *trafficManager) initGrpc(c context.Context) (err error) {
	defer func() {
		tm.apiErr = err
		close(tm.startup)
	}()

	// First check. Establish connection
	tc, cancel := context.WithTimeout(c, connectTimeout)
	defer cancel()

	var conn *grpc.ClientConn
	conn, err = grpc.DialContext(tc, fmt.Sprintf("127.0.0.1:%d", tm.apiPort),
		grpc.WithInsecure(),
		grpc.WithNoProxy(),
		grpc.WithBlock())
	if err != nil {
		dlog.Errorf(c, "error when dialing traffic-manager: %s", err.Error())
		return err
	}

	mClient := manager.NewManagerClient(conn)
	si, err := mClient.ArriveAsClient(c, &manager.ClientInfo{
		Name:      tm.userAndHost,
		InstallId: tm.installID,
		Product:   "telepresence",
		Version:   client.Version(),
	})

	if err != nil {
		dlog.Errorf(c, "ArriveAsClient: %s", err.Error())
		conn.Close()
		return err
	}
	tm.conn = conn
	tm.grpc = mClient
	tm.sessionID = si.SessionId

	g := dgroup.ParentGroup(c)
	g.Go("remain", tm.remain)
	g.Go("watch-agents", tm.watchAgents)
	g.Go("watch-intercepts", tm.watchIntercepts)
	return nil
}

func (tm *trafficManager) watchAgents(c context.Context) error {
	ac, err := tm.grpc.WatchAgents(c, tm.session())
	if err != nil {
		return err
	}
	return tm.aiListener.start(c, ac)
}

func (tm *trafficManager) watchIntercepts(c context.Context) error {
	ic, err := tm.grpc.WatchIntercepts(c, tm.session())
	if err != nil {
		return err
	}
	return tm.iiListener.start(c, ic)
}

func (tm *trafficManager) session() *manager.SessionInfo {
	return &manager.SessionInfo{SessionId: tm.sessionID}
}

func (tm *trafficManager) agentInfoSnapshot() *manager.AgentInfoSnapshot {
	return tm.aiListener.getData()
}

func (tm *trafficManager) interceptInfoSnapshot() *manager.InterceptInfoSnapshot {
	return tm.iiListener.getData()
}

func (tm *trafficManager) remain(c context.Context) error {
	ticker := time.NewTicker(5 * time.Second)
	for {
		select {
		case <-c.Done():
			return nil
		case <-ticker.C:
			_, err := tm.grpc.Remain(c, tm.session())
			if err != nil {
				return err
			}
		}
	}
}

// Close implements io.Closer
func (tm *trafficManager) Close() error {
	if tm.conn != nil {
		_ = tm.conn.Close()
		tm.conn = nil
		tm.grpc = nil
	}
	return nil
}

// A watcher listens on a grpc.ClientStream and notifies listeners when
// something arrives.
type watcher struct {
	entryMaker    func() interface{} // returns an instance of the type produced by the stream
	listeners     []listener
	listenersLock sync.RWMutex
	stream        grpc.ClientStream
}

// watch reads messages from the stream and passes them onto registered listeners. The
// function terminates when the context used when the stream was acquired is cancelled,
// when io.EOF is encountered, or an error occurs during read.
func (r *watcher) watch(c context.Context) error {
	dataChan := make(chan interface{}, 1000)
	go func() {
		for {
			select {
			case <-c.Done():
				return
			case data := <-dataChan:
				if data == nil {
					return
				}

				r.listenersLock.RLock()
				lc := make([]listener, len(r.listeners))
				copy(lc, r.listeners)
				r.listenersLock.RUnlock()

				for _, l := range lc {
					l.onData(data)
				}
			}
		}
	}()

	for {
		data := r.entryMaker()
		if err := r.stream.RecvMsg(data); err != nil {
			if err == io.EOF || strings.HasSuffix(err.Error(), " is closing") {
				err = nil
			}
			close(dataChan)
			return err
		}
		dataChan <- data
	}
}

func (r *watcher) addListener(l listener) {
	r.listenersLock.Lock()
	r.listeners = append(r.listeners, l)
	r.listenersLock.Unlock()
}

func (r *watcher) removeListener(l listener) {
	r.listenersLock.Lock()
	ls := r.listeners
	for i, x := range ls {
		if l == x {
			last := len(ls) - 1
			ls[i] = ls[last]
			ls[last] = nil
			r.listeners = ls[:last]
			break
		}
	}
	r.listenersLock.Unlock()
}

// A listener gets notified by a watcher when something arrives on the stream
type listener interface {
	onData(data interface{})
}

// An aiListener keeps track of the latest received AgentInfoSnapshot and provides the
// watcher needed to register other listeners.
type aiListener struct {
	watcher
	data atomic.Value
}

func (al *aiListener) getData() *manager.AgentInfoSnapshot {
	v := al.data.Load()
	if v == nil {
		return nil
	}
	return v.(*manager.AgentInfoSnapshot)
}

func (al *aiListener) onData(d interface{}) {
	al.data.Store(d)
}

func (al *aiListener) start(c context.Context, stream grpc.ClientStream) error {
	al.stream = stream
	al.listeners = []listener{al}
	al.entryMaker = func() interface{} { return new(manager.AgentInfoSnapshot) }
	return al.watch(c)
}

func (il *iiListener) onData(d interface{}) {
	il.data.Store(d)
}

func (il *iiListener) start(c context.Context, stream grpc.ClientStream) error {
	il.stream = stream
	il.listeners = []listener{il}
	il.entryMaker = func() interface{} { return new(manager.InterceptInfoSnapshot) }
	return il.watch(c)
}

// iiActive is a listener that waits for an intercept with a given id to become active
type iiActive struct {
	id   string
	done chan *manager.InterceptInfo
}

func (ia *iiActive) onData(d interface{}) {
	if iis, ok := d.(*manager.InterceptInfoSnapshot); ok {
		for _, ii := range iis.Intercepts {
			if ii.Id == ia.id && ii.Disposition != manager.InterceptDispositionType_WAITING {
				done := ia.done
				ia.done = nil
				if done != nil {
					done <- ii
					close(done)
				}
				break
			}
		}
	}
}

// aiPresent is a listener that waits for an agent with a given name to be present
type aiPresent struct {
	name string
	done chan *manager.AgentInfo
}

func (ap *aiPresent) onData(d interface{}) {
	if ais, ok := d.(*manager.AgentInfoSnapshot); ok {
		for _, ai := range ais.Agents {
			if ai.Name == ap.name {
				done := ap.done
				ap.done = nil
				if done != nil {
					done <- ai
					close(done)
				}
				break
			}
		}
	}
}
<|endoftext|>