<commit_before>// Copyright (c) 2012 - Cloud Instruments Co., Ltd.
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
//    list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
//    this list of conditions and the following disclaimer in the documentation
//    and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

package seelog

import (
	"crypto/tls"
	"crypto/x509"
	"errors"
	"fmt"
	"io/ioutil"
	"net/smtp"
)

const (
	subjectPhrase = "Diagnostic message from server: "
	// Message subject pattern composed according to RFC 5321.
	rfc5321SubjectPattern = "From: %s <%s>\nSubject: %s\n"
)

// smtpWriter is used to send emails via given SMTP-server.
type smtpWriter struct {
	auth               smtp.Auth
	hostName           string
	hostPort           string
	hostNameWithPort   string
	senderAddress      string
	senderName         string
	recipientAddresses []string
	caCertificatePaths []string
}

// newSmtpWriter returns a new SMTP-writer.
func newSmtpWriter(senderAddress, senderName string, recipientAddresses []string, hostName, hostPort, userName, password string, caCertificatePaths []string) (writer *smtpWriter, err error) {
	// TODO: Check PEM file paths here?
	writer = &smtpWriter{
		smtp.PlainAuth("", userName, password, hostName),
		hostName,
		hostPort,
		fmt.Sprintf("%s:%s", hostName, hostPort),
		senderAddress,
		senderName,
		recipientAddresses,
		caCertificatePaths,
	}
	return
}

func prepareMessage(senderAddr, senderName, subject string, body []byte) []byte {
	h := []byte(fmt.Sprintf(rfc5321SubjectPattern, senderName, senderAddr, subject))
	return append(h, body...)
}

// getTLSConfig gets paths of PEM files with certificates,
// host server name and tries to create an appropriate TLS.Config.
func getTLSConfig(pemFilePaths []string, hostName string) (config *tls.Config, err error) {
	if pemFilePaths == nil || len(pemFilePaths) == 0 {
		err = errors.New("Invalid PEM file paths")
		return
	}
	pemEncodedContent := []byte{}
	var (
		e     error
		bytes []byte
	)
	// Put together all the PEM files to decode them as a whole byte slice.
	for _, pfp := range pemFilePaths {
		if bytes, e = ioutil.ReadFile(pfp); e == nil {
			pemEncodedContent = append(pemEncodedContent, bytes...)
		} else {
			err = fmt.Errorf("Cannot read file: %s", pfp)
			return
		}
	}
	config = &tls.Config{RootCAs: x509.NewCertPool(), ServerName: hostName}
	isAppended := config.RootCAs.AppendCertsFromPEM(pemEncodedContent)
	if !isAppended {
		// Extract this into a separate error.
		err = errors.New("Invalid PEM content")
		return
	}
	return
}

// SendMail accepts TLS configuration, connects to the server at addr,
// switches to TLS if possible, authenticates with mechanism a if possible,
// and then sends an email from address from, to addresses to, with message msg.
func sendMailWithTLSConfig(config *tls.Config, addr string, a smtp.Auth, from string, to []string, msg []byte) error {
	c, err := smtp.Dial(addr)
	if err != nil {
		return err
	}
	// Check if the server supports STARTTLS extension.
	if ok, _ := c.Extension("STARTTLS"); ok {
		if err = c.StartTLS(config); err != nil {
			return err
		}
	}
	// Check if the server supports AUTH extension and use given smtp.Auth.
	if a != nil {
		if isSupported, _ := c.Extension("AUTH"); isSupported {
			if err = c.Auth(a); err != nil {
				return err
			}
		}
	}
	// Portion of code from the official smtp.SendMail function,
	// see http://golang.org/src/pkg/net/smtp/smtp.go.
	if err = c.Mail(from); err != nil {
		return err
	}
	for _, addr := range to {
		if err = c.Rcpt(addr); err != nil {
			return err
		}
	}
	w, err := c.Data()
	if err != nil {
		return err
	}
	_, err = w.Write(msg)
	if err != nil {
		return err
	}
	err = w.Close()
	if err != nil {
		return err
	}
	return c.Quit()
}

// Write pushes a text message properly composed according to RFC 5321
// to a post server, which sends it to the recipients.
func (smtpw *smtpWriter) Write(data []byte) (int, error) {
	var err error
	if smtpw.caCertificatePaths == nil {
		err = smtp.SendMail(
			smtpw.hostNameWithPort,
			smtpw.auth,
			smtpw.senderAddress,
			smtpw.recipientAddresses,
			prepareMessage(smtpw.senderAddress, smtpw.senderName, subjectPhrase, data),
		)
	} else {
		config, e := getTLSConfig(smtpw.caCertificatePaths, smtpw.hostName)
		if e != nil {
			return 0, e
		}
		err = sendMailWithTLSConfig(
			config,
			smtpw.hostNameWithPort,
			smtpw.auth,
			smtpw.senderAddress,
			smtpw.recipientAddresses,
			prepareMessage(smtpw.senderAddress, smtpw.senderName, subjectPhrase, data),
		)
	}
	if err != nil {
		return 0, err
	}
	return len(data), nil
}

// Close closes down SMTP-connection.
func (smtpWriter *smtpWriter) Close() error {
	// Do nothing as Write method opens and closes connection automatically
	return nil
}
<commit_msg>Changed the meaning of the input CA certificate file paths - now this are paths of directories with PEM files.<commit_after>// Copyright (c) 2012 - Cloud Instruments Co., Ltd.
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
//    list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
//    this list of conditions and the following disclaimer in the documentation
//    and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

package seelog

import (
	"crypto/tls"
	"crypto/x509"
	"errors"
	"fmt"
	"io/ioutil"
	"net/smtp"
	"path/filepath"
)

const (
	subjectPhrase = "Diagnostic message from server: "
	// Message subject pattern composed according to RFC 5321.
	rfc5321SubjectPattern = "From: %s <%s>\nSubject: %s\n"
)

// smtpWriter is used to send emails via given SMTP-server.
type smtpWriter struct {
	auth               smtp.Auth
	hostName           string
	hostPort           string
	hostNameWithPort   string
	senderAddress      string
	senderName         string
	recipientAddresses []string
	caCertDirPaths     []string
}

// newSmtpWriter returns a new SMTP-writer.
func newSmtpWriter(sa, sn string, ras []string, hn, hp, un, pwd string, cacdps []string) *smtpWriter {
	return &smtpWriter{
		auth:               smtp.PlainAuth("", un, pwd, hn),
		hostName:           hn,
		hostPort:           hp,
		hostNameWithPort:   fmt.Sprintf("%s:%s", hn, hp),
		senderAddress:      sa,
		senderName:         sn,
		recipientAddresses: ras,
		caCertDirPaths:     cacdps,
	}
}

func prepareMessage(senderAddr, senderName, subject string, body []byte) []byte {
	h := []byte(fmt.Sprintf(rfc5321SubjectPattern, senderName, senderAddr, subject))
	return append(h, body...)
}

// getTLSConfig gets paths of PEM files with certificates,
// host server name and tries to create an appropriate TLS.Config.
func getTLSConfig(pemFileDirPaths []string, hostName string) (config *tls.Config, err error) {
	if pemFileDirPaths == nil || len(pemFileDirPaths) == 0 {
		err = errors.New("Invalid PEM file paths")
		return
	}
	pemEncodedContent := []byte{}
	var (
		e     error
		bytes []byte
	)
	// Create a file-filter-by-extension, set aside non-pem files.
	pemFilePathFilter := func(fp string) bool {
		if filepath.Ext(fp) == ".pem" {
			return true
		}
		return false
	}

	for _, pemFileDirPath := range pemFileDirPaths {
		pemFilePaths, err := getDirFilePaths(pemFileDirPath, pemFilePathFilter)
		if err != nil {
			return nil, err
		}

		// Put together all the PEM files to decode them as a whole byte slice.
		for _, pfp := range pemFilePaths {
			if bytes, e = ioutil.ReadFile(pfp); e == nil {
				pemEncodedContent = append(pemEncodedContent, bytes...)
			} else {
				return nil, fmt.Errorf("Cannot read file: %s: %s", pfp, e.Error())
			}
		}
	}

	config = &tls.Config{RootCAs: x509.NewCertPool(), ServerName: hostName}
	isAppended := config.RootCAs.AppendCertsFromPEM(pemEncodedContent)
	if !isAppended {
		// Extract this into a separate error.
		err = errors.New("Invalid PEM content")
		return
	}
	return
}

// SendMail accepts TLS configuration, connects to the server at addr,
// switches to TLS if possible, authenticates with mechanism a if possible,
// and then sends an email from address from, to addresses to, with message msg.
func sendMailWithTLSConfig(config *tls.Config, addr string, a smtp.Auth, from string, to []string, msg []byte) error {
	c, err := smtp.Dial(addr)
	if err != nil {
		return err
	}
	// Check if the server supports STARTTLS extension.
	if ok, _ := c.Extension("STARTTLS"); ok {
		if err = c.StartTLS(config); err != nil {
			return err
		}
	}
	// Check if the server supports AUTH extension and use given smtp.Auth.
	if a != nil {
		if isSupported, _ := c.Extension("AUTH"); isSupported {
			if err = c.Auth(a); err != nil {
				return err
			}
		}
	}
	// Portion of code from the official smtp.SendMail function,
	// see http://golang.org/src/pkg/net/smtp/smtp.go.
	if err = c.Mail(from); err != nil {
		return err
	}
	for _, addr := range to {
		if err = c.Rcpt(addr); err != nil {
			return err
		}
	}
	w, err := c.Data()
	if err != nil {
		return err
	}
	_, err = w.Write(msg)
	if err != nil {
		return err
	}
	err = w.Close()
	if err != nil {
		return err
	}
	return c.Quit()
}

// Write pushes a text message properly composed according to RFC 5321
// to a post server, which sends it to the recipients.
func (smtpw *smtpWriter) Write(data []byte) (int, error) {
	var err error
	if smtpw.caCertDirPaths == nil {
		err = smtp.SendMail(
			smtpw.hostNameWithPort,
			smtpw.auth,
			smtpw.senderAddress,
			smtpw.recipientAddresses,
			prepareMessage(smtpw.senderAddress, smtpw.senderName, subjectPhrase, data),
		)
	} else {
		config, e := getTLSConfig(smtpw.caCertDirPaths, smtpw.hostName)
		if e != nil {
			return 0, e
		}
		err = sendMailWithTLSConfig(
			config,
			smtpw.hostNameWithPort,
			smtpw.auth,
			smtpw.senderAddress,
			smtpw.recipientAddresses,
			prepareMessage(smtpw.senderAddress, smtpw.senderName, subjectPhrase, data),
		)
	}
	if err != nil {
		return 0, err
	}
	return len(data), nil
}

// Close closes down SMTP-connection.
func (smtpWriter *smtpWriter) Close() error {
	// Do nothing as Write method opens and closes connection automatically
	return nil
}
<|endoftext|>


<commit_before>/*
Package linda is a background manager to poll jobs from broker and dispatch them to multi workers.

Linda Broker provides a unified API across different broker (queue) services.

Brokers allow you to defer the processing of a time consuming task.

Use ReleaseWithDelay func, you can implement a cron job service.

Simple Usage:

	 package main

	 import (
	      "fmt"
	      "github.com/amlun/linda"
	 )

	 func init() {
		settings := linda.Settings{
			Queue:         "scheduler",
			Connection:      "redis://localhost:6379/",
			Timeout:       60,
			IntervalFloat: 5.0,
			Concurrency:   1,
		}
		linda.SetSettings(settings)
		linda.RegisterWorkers("DispatcherSeed", DispatcherSeed)
	 }

	 func main() {
		if err := linda.Run(); err != nil {
			fmt.Println("Error:", err)
		}
	 }

	 func DispatcherSeed(job *linda.Job) error {
		broker := linda.GetBroker()
		// get seed info
		// do seed job
		// release job with delay (like a cron job)
		broker.ReleaseWithDelay("scheduler", job, 60)
		return nil
	 }
*/
package linda
<commit_msg>modify doc<commit_after>/*
Package linda is a background manager to poll jobs from broker and dispatch them to multi workers.

Linda Broker provides a unified API across different broker (queue) services.

Brokers allow you to defer the processing of a time consuming task.

Use ReleaseWithDelay func, you can implement a cron job service.

Simple Usage:

		package main

		import (
			"fmt"
			"github.com/amlun/linda"
			"github.com/sirupsen/logrus"
			"os"
			"os/signal"
			"syscall"
			"time"
		)

		func init() {
			linda.RegisterWorkers("printArgs", PrintArgs)
		}

		func main() {
			logrus.SetLevel(logrus.DebugLevel)
			// broker
			b, _ := linda.NewBroker("redis://localhost:6379/")
			// saver
			s, _ := linda.NewSaver("redis://localhost:6379/")
			// config
			c := linda.Config{
				Queue:     "test",
				Timeout:   60,
				Interval:  time.Second,
				WorkerNum: 4,
			}
			quit := signals()
			linda.Init(c, b, s)
			go func() {
				defer func() {
					linda.Quit()
				}()
				<-quit
			}()

			if err := linda.Run(); err != nil {
				fmt.Println("Error:", err)
			}
		}

		func PrintArgs(args ...interface{}) error {
			fmt.Println(args)
			return nil
		}

		// Signal Handling
		func signals() <-chan bool {
			quit := make(chan bool)
			go func() {
				signals := make(chan os.Signal)
				defer close(signals)
				signal.Notify(signals, syscall.SIGQUIT, syscall.SIGTERM, os.Interrupt)
				defer signalStop(signals)
				<-signals
				quit <- true
			}()
			return quit
		}

		// Stops signals channel.
		func signalStop(c chan<- os.Signal) {
			signal.Stop(c)
		}

*/
package linda
<|endoftext|>


<commit_before>/*
Package color wraps fmt.Printf with verbs for producing colored output.

Printing

Highlight Verbs:

	%h[attr...]	replaced with a SGR code that sets all of the attributes in []
			multiple attributes are + separated
	%r		an abbreviation for %h[reset]

Errors:

If an error occurs, the generated string will contain a description of the problem, as in these examples.

	No attributes in the highlight verb:
		Printf("%h[]%s", "hi"):			%!h(MISSING)
	Invalid character in the highlight verb:
		Printf("%h[%&:*!]%s", "hi"):		%!h(INVALID)
	Unknown attribute in the highlight verb:
		Printf("%h[fgOrange]%s", "hi"):	%!h(BADATTR)

Everything else is handled by the fmt package. You should read its documentation.

Attributes Reference

Standard Colors:
	%h[fgBlack]
	%h[fgRed]
	%h[fgGreen]
	%h[fgYellow]
	%h[fgBlue]
	%h[fgMagenta]
	%h[fgCyan]
	%h[fgWhite]
	%h[fgDefault]
	%h[bgBlack]
	%h[bgRed]
	%h[bgGreen]
	%h[bgYellow]
	%h[bgBlue]
	%h[bgMagenta]
	%h[bgCyan]
	%h[bgWhite]
	%h[bgDefault]

Bright Colors:
	%h[fgBrightBlack]
	%h[fgBrightRed]
	%h[fgBrightGreen]
	%h[fgBrightYellow]
	%h[fgBrightBlue]
	%h[fgBrightMagenta]
	%h[fgBrightCyan]
	%h[fgBrightWhite]
	%h[bgBrightBlack]
	%h[bgBrightRed]
	%h[bgBrightGreen]
	%h[bgBrightYellow]
	%h[bgBrightBlue]
	%h[bgBrightMagenta]
	%h[bgBrightCyan]
	%h[bgBrightWhite]

256 Colors:
	%h[fgx]
	%h[bgx]
Where x is any number from 0-255

Other:
	%h[reset] or the %r verb
	%h[bold]
	%h[faint]
	%h[italic]
	%h[underline]
	%h[blink]
	%h[inverse]
	%h[invisible]
	%h[crossedOut]
	%h[doubleUnderline]
	%h[normal]
	%h[notItalic]
	%h[notUnderlined]
	%h[steady]
	%h[positive]
	%h[visible]
	%h[notCrossedOut]

See http://goo.gl/LRLA7o for a more in depth explanation of the attributes. Scroll down till you see the SGR section.

See http://goo.gl/fvtHLs and according to the above document, ISO-8613-3, for more information on 256 colors.
*/
package color
<commit_msg>fixed indention<commit_after>/*
Package color wraps fmt.Printf with verbs for producing colored output.

Printing

Highlight Verbs:

	%h[attr...]	replaced with a SGR code that sets all of the attributes in []
			multiple attributes are + separated
	%r		an abbreviation for %h[reset]

Errors:

If an error occurs, the generated string will contain a description of the problem, as in these examples.

	No attributes in the highlight verb:
		Printf("%h[]%s", "hi"):			%!h(MISSING)
	Invalid character in the highlight verb:
		Printf("%h[%&:*!]%s", "hi"):		%!h(INVALID)
	Unknown attribute in the highlight verb:
		Printf("%h[fgOrange]%s", "hi"):		%!h(BADATTR)

Everything else is handled by the fmt package. You should read its documentation.

Attributes Reference

Standard Colors:
	%h[fgBlack]
	%h[fgRed]
	%h[fgGreen]
	%h[fgYellow]
	%h[fgBlue]
	%h[fgMagenta]
	%h[fgCyan]
	%h[fgWhite]
	%h[fgDefault]
	%h[bgBlack]
	%h[bgRed]
	%h[bgGreen]
	%h[bgYellow]
	%h[bgBlue]
	%h[bgMagenta]
	%h[bgCyan]
	%h[bgWhite]
	%h[bgDefault]

Bright Colors:
	%h[fgBrightBlack]
	%h[fgBrightRed]
	%h[fgBrightGreen]
	%h[fgBrightYellow]
	%h[fgBrightBlue]
	%h[fgBrightMagenta]
	%h[fgBrightCyan]
	%h[fgBrightWhite]
	%h[bgBrightBlack]
	%h[bgBrightRed]
	%h[bgBrightGreen]
	%h[bgBrightYellow]
	%h[bgBrightBlue]
	%h[bgBrightMagenta]
	%h[bgBrightCyan]
	%h[bgBrightWhite]

256 Colors:
	%h[fgx]
	%h[bgx]
Where x is any number from 0-255

Other:
	%h[reset] or the %r verb
	%h[bold]
	%h[faint]
	%h[italic]
	%h[underline]
	%h[blink]
	%h[inverse]
	%h[invisible]
	%h[crossedOut]
	%h[doubleUnderline]
	%h[normal]
	%h[notItalic]
	%h[notUnderlined]
	%h[steady]
	%h[positive]
	%h[visible]
	%h[notCrossedOut]

See http://goo.gl/LRLA7o for a more in depth explanation of the attributes. Scroll down till you see the SGR section.

See http://goo.gl/fvtHLs and according to the above document, ISO-8613-3, for more information on 256 colors.
*/
package color
<|endoftext|>


<commit_before>// Package couch implements a client for a CouchDB database. Version 0.1
// focuses on basic interactions, replication, and conflict management.
//
// Setup
//
//  cred := NewCredentials("much", "safe")
//  s := NewServer("http://127.0.0.1:5984", cred)
//  db := s.Database("MyDatabase")
//  if !db.Exists() {
//    db.Create()
//  }
//
// Basics
//
// Every document in CouchDB has to be identifiable by an id and a revision id.
// Two datatypes implement the interface Identifiable, Doc and DynamicDoc. Doc can
// be used as an anonymous field in your own struct. DynamicDoc is a type alias
// for map[string]interface{}, use it when your documents have no implicit schema at all.
// To make code examples easier to follow, there will be no explicit error handling.
//
//  type Person struct {
//    couch.Doc
//    Name string
//  }
//

// Managing conflicting document revisions
// See http://docs.couchdb.org/en/latest/replication/conflicts.html

//  p := &DynamicDoc  // REaLLY?
//
//  p := &Person{Name : "Peter"}
//
//  // Because Id from couch.Doc is not set, insert will create a new document.
//  // After the insert, p will contain the id and revision id assigned to it.
//  db.Insert(p)
//  p.Name = "A new name"
//  db.Insert(p)    // Each insert writes the resulting id and revision into the given struct.
//
//  q := &Person{}
//  db.Retrieve(p.Id, q)
//
// Conflicts
//
//  count, _ := db.ConflictsCount()
//  docIds, _ := db.Conflicts()  // BAD NAME
//  conflict, _ := db.ConflictFor(docId)
//
//  var p []person
//  conflict.Revisions(&p)    // UnmarshalRevisions
//
//  conflict.SolveWith(anotherDoc)
//
// Continuous replication
//
//  repl, _ := db.ReplicateTo(anotherDb, true)
//  repl.Cancel()
//
package couch
<commit_msg>Complete documentation<commit_after>// Package couch implements a client for a CouchDB database.
//
// Version 0.1 focuses on basic operations, proper conflict management, error handling and
// replication. Not part of this version are attachment handling, general
// statistics and optimizations, change detection and creating views. Most of
// the features are accessible using the generic Do() function, though.
//
//
// Getting started:
//
//  cred := couch.NewCredentials("user_notsosafe", "password_withoutssl")
//  s := couch.NewServer("http://127.0.0.1:5984", cred)
//  db := s.Database("MyDatabase")
//
//  if !db.Exists() {
//    db.Create()
//  }
//
// Basics
//
// Every document in CouchDB has to be identifiable by an id and a revision id.
// Two types already implement this interface called Identifiable: Doc and DynamicDoc. Doc can
// be used as an anonymous field in your own struct. DynamicDoc is a type alias
// for map[string]interface{}, use it when your documents have no implicit schema at all.
// To make code examples easier to follow, there will be no explicit error handling in
// these examples even though it's fully supported throughout the API.
//
//  type Person struct {
//    couch.Doc
//    Name string
//  }
//
// Insert() will create a new document if it doesn't have an id yet:
//
//  p := &Person{Name : "Peter"}
//  db.Insert(p)
//
// After the operation the final id and revision id will be written back to p. That's
// why you can now just edit something and call Insert() again which will save the same document.
//
//  p.Name = "Anna"
//  db.Insert(p)
//
// After this edit, p will contain the latest revision id. Note that it is possible that this
// second edit fails because someone else edited and saved the same document in the meantime.
// You will be notified of this in form of an error and you should then first retrieve the
// latest document revision to see the changes of this lost update:
//
//  db.Retrieve(p.Id, p)
//
// CouchDB doesn't edit documents in-place but adds a complete revision for each edit. That's
// why you will be correctly informed of any lost update.
//
// Conflicts
//
// Because CouchDB supports master-master replication of databases, it is possible that conflicts
// like the one described above can't be avoided. CouchDB is not going to interrupt replication
// because of a lost update.
//
// Let's say you have two instances running,
// maybe a central one and a mobile one and both are kept in sync by replication. Now let's assume
// you edit a document on your mobile DB and someone else edits the same document on the central DB.
// After you've come online again, you use bi-directional replication to sync the databases.
// CouchDB will now create a branch structure for your document, similar to version control systems.
// Your document has two conflicting revisions and in this case they can't necessarily be resolved
// automatically. This client helps you with a number of methods to resolve such an issue quickly.
// Read more about the conflict model http://docs.couchdb.org/en/latest/replication/conflicts.html
//
// Continuing with above example, replicate the database:
//
//  anotherDB := s.Database("sharedBackup")
//  db.ReplicateTo(anotherDB, false)
//
// Now, on the other database, edit the document (note that it has the same id there):
//
//  anotherDB.Retrieve(p.Id, p)
//  p.Name = "AnotherAnna"
//  anotherDB.Insert(p)
//
// Now edit the document on the first database. Retrieve it first to make sure it
// has the correct revision id:
//
//  db.Retrieve(p.Id, p)
//  p.Name = "LatestAnna"
//  db.Insert(p)
//
// Now replicate anotherDB back to our first database:
//
//  anotherDB.ReplicateTo(db, false)
//
// Now we have two conflicting versions of a document. Only you as the editor can decide
// whether "LatestAnna" or "AnotherAnna" are correct. To detect this conflict there are a number
// of methods. First, you can just ask a document:
//
//  conflict, _ := db.ConflictFor(p.Id)
//
// You probably want to have a 'look' at the revisions in your preferred format:
//
//  var revs []Person
//  conflict.Revisions(&revs)
//
// And then pick one or create a new document to solve the conflict:
//
//  solution := &Person{Name:"Anna"}
//  conflict.SolveWith(solution)
//
// That's all. You can detect conflicts throughout your database like this:
//
//  num := db.ConflictsCount()
//  docIds := db.Conflicts()
//
// This client is still work in progress and probably also has a heavy bias towards
// what I'm using the database for (and for what I don't). Just let me know, I'm open
// to any suggestions.
package couch
<|endoftext|>


<commit_before>/*
Package goon provides an autocaching interface to the app engine datastore
similar to the python NDB package.

Goon differs from the datastore package in varios ways: it remembers the
appengine Context, which need only be specified once at creation time; kinds
need not be specified as they are computed, by default, from a type's name;
keys are inferred from specially named fields on types, removing the need to
pass key objects around.

In general, the difference is that Goon's API is identical to the datastore API,
it's just shorter.

Keys in Goon are stored in the structs themselves. Below is an example struct
with a field to specify the id (see the Key Specifications section below for
full documentation).
	type User struct {
		Id    string `datastore:"-" goon:"id"`
		Name  string
	}

Thus, to get a User with id 2:
	userid := 2
	g := goon.NewGoon(r)
	u := User{Id: userid}
	g.Get(&u)

Key Specifications

For both the Key and KeyError functions, src must be a S or *S for some
struct type S. The key is extracted based on various fields of S. If a field
of type int64 or string has a struct tag named goon with value "id", it is
used as the key's id. If a field of type *datastore.Key has a struct tag
named goon with value "parent", it is used as the key's parent. If a field
of type string has a struct tag named goon with value "kind", it is used
as the key's kind. The "kind" field supports an optional second parameter
which is the default kind name. If no kind field exists, the struct's name
is used. These fields should all have their datastore field marked as "-".

Example, with kind User:
	type User struct {
		Id    string `datastore:"-" goon:"id"`
		Read  time.Time
	}

Example, with kind U if _kind is the empty string:
	type User struct {
		_kind string `goon:"kind,U"`
		Id    string `datastore:"-" goon:"id"`
		Read  time.Time
	}

To override kind of a single entity to UserKind:
	u := User{_kind: "UserKind"}

An example with both parent and kind:
	type UserData struct {
		Id     string         `datastore:"-" goon:"id"`
		_kind  string         `goon:"kind,UD"`
		Parent *datastore.Key `datastore:"-" goon:"parent"`
		Data   []byte
	}

Features

Datastore interaction with: Get, GetMulti, Put, PutMulti, Delete, DeleteMulti, Queries.

All key-based operations backed by memory and memcache.

Per-request, in-memory cache: fetch the same key twice, the second request is served from local memory.

Intelligent multi support: running GetMulti on correctly fetches from memory, then memcache, then the datastore; each tier only sends keys off to the next one if they were missing.

Transactions use a separate context, but locally cache any results on success.

Automatic kind naming: struct names are inferred by reflection, removing the need to manually specify key kinds.

Simpler api than appengine/datastore.

API comparison between goon and datastore

put with incomplete key

datastore:

	type Group struct {
		Name string
	}
	c := appengine.NewContext(r)
	g := &Group{Name: "name"}
	k := datastore.NewIncompleteKey(c, "Group", nil)
	err := datastore.Put(c, k, g)

goon:

	type Group struct {
		Id   int64 `datastore:"-" goon:"id"`
		Name string
	}
	n := goon.NewGoon(r)
	g := &Group{Name: "name"}
	err := n.Put(g)

get with known key

datastore:

	type Group struct {
		Name string
	}
	c := appengine.NewContext(r)
	g := &Group{}
	k := datastore.NewKey(c, "Group", "", 1, nil)
	err := datastore.Get(c, k, g)

goon:

	type Group struct {
		Id   int64 `datastore:"-" goon:"id"`
		Name string
	}
	n := goon.NewGoon(r)
	g := &Group{Id: 1}
	err := n.Get(g)

*/
package goon
<commit_msg>Spelling; tweaks<commit_after>/*
Package goon provides an autocaching interface to the app engine datastore
similar to the python NDB package.

Goon differs from the datastore package in various ways: it remembers the
appengine Context, which need only be specified once at creation time; kinds
need not be specified as they are computed, by default, from a type's name;
keys are inferred from specially-tagged fields on types, removing the need to
pass key objects around.

In general, the difference is that Goon's API is identical to the datastore API,
it's just shorter.

Keys in Goon are stored in the structs themselves. Below is an example struct
with a field to specify the id (see the Key Specifications section below for
full documentation).
	type User struct {
		Id    string `datastore:"-" goon:"id"`
		Name  string
	}

Thus, to get a User with id 2:
	userid := 2
	g := goon.NewGoon(r)
	u := User{Id: userid}
	g.Get(&u)

Key Specifications

For both the Key and KeyError functions, src must be a S or *S for some
struct type S. The key is extracted based on various fields of S. If a field
of type int64 or string has a struct tag named goon with value "id", it is
used as the key's id. If a field of type *datastore.Key has a struct tag
named goon with value "parent", it is used as the key's parent. If a field
of type string has a struct tag named goon with value "kind", it is used
as the key's kind. The "kind" field supports an optional second parameter
which is the default kind name. If no kind field exists, the struct's name
is used. These fields should all have their datastore field marked as "-".

Example, with kind User:
	type User struct {
		Id    string `datastore:"-" goon:"id"`
		Read  time.Time
	}

Example, with kind U if _kind is the empty string:
	type User struct {
		_kind string `goon:"kind,U"`
		Id    string `datastore:"-" goon:"id"`
		Read  time.Time
	}

To override kind of a single entity to UserKind:
	u := User{_kind: "UserKind"}

An example with both parent and kind:
	type UserData struct {
		Id     string         `datastore:"-" goon:"id"`
		_kind  string         `goon:"kind,UD"`
		Parent *datastore.Key `datastore:"-" goon:"parent"`
		Data   []byte
	}

Features

Datastore interaction with: Get, GetMulti, Put, PutMulti, Delete, DeleteMulti, Queries.

All key-based operations backed by memory and memcache.

Per-request, in-memory cache: fetch the same key twice, the second request is served from local memory.

Intelligent multi support: running GetMulti on correctly fetches from memory, then memcache, then the datastore; each tier only sends keys off to the next one if they were missing.

Transactions use a separate context, but locally cache any results on success.

Automatic kind naming: struct names are inferred by reflection, removing the need to manually specify key kinds.

Simpler api than appengine/datastore.

API comparison between goon and datastore

put with incomplete key

datastore:

	type Group struct {
		Name string
	}
	c := appengine.NewContext(r)
	g := &Group{Name: "name"}
	k := datastore.NewIncompleteKey(c, "Group", nil)
	err := datastore.Put(c, k, g)

goon:

	type Group struct {
		Id   int64 `datastore:"-" goon:"id"`
		Name string
	}
	n := goon.NewGoon(r)
	g := &Group{Name: "name"}
	err := n.Put(g)

get with known key

datastore:

	type Group struct {
		Name string
	}
	c := appengine.NewContext(r)
	g := &Group{}
	k := datastore.NewKey(c, "Group", "", 1, nil)
	err := datastore.Get(c, k, g)

goon:

	type Group struct {
		Id   int64 `datastore:"-" goon:"id"`
		Name string
	}
	n := goon.NewGoon(r)
	g := &Group{Id: 1}
	err := n.Get(g)

*/
package goon
<|endoftext|>


<commit_before>// Package slog provides concurrent levelled logging capabilities
// with parent/child loggers.
//
// * Concurrent safe
// * Three levels; `slog.Err`, `slog.Warn`, and `slog.Info`
// * Children loggers for sub-processes
// * Built-in zero-memory `slog.NilLogger` to easily logging off without changing calling code
// * Custom reporters to send logs anywhere
// * `Reporters` function for reporting to many places
//
//     // make a logger to output to stdout
//     l := slog.New("parent", slog.Warn)
//     l.SetReporter(slog.Stdout)
//     if l.Info() {
//       l.Info("This is some information")
//     }
//     if l.Err() {
//       l.Err("failed to do something:", err)
//     }
//
//     // start a sub process
//     cl := l.New("child")
//     StartSubProcess(cl)
//     // reports as parent>child
//
//     // stop the logger
//     l.Stop()
//     <-l.StopChan() // wait for it to stop
package slog
<commit_msg>ditto<commit_after>// Package slog provides concurrent levelled logging capabilities
// with parent/child loggers.
//
// * Concurrent safe
// * Three levels; `slog.Err`, `slog.Warn`, and `slog.Info`
// * Children loggers for sub-processes
// * Built-in zero-memory `slog.NilLogger` to easily logging off without changing calling code
// * Custom reporters to send logs anywhere
// * `Reporters` function for reporting to many places
//
// Usage
//
// Using slog.Logger is very simple:
//
//     // make a logger to output to stdout
//     l := slog.New("parent", slog.Warn)
//     l.SetReporter(slog.Stdout)
//     if l.Info() {
//       l.Info("This is some information")
//     }
//     if l.Err() {
//       l.Err("failed to do something:", err)
//     }
//
//     // start a sub process
//     cl := l.New("child")
//     StartSubProcess(cl)
//     // reports as parent>child
//
//     // stop the logger
//     l.Stop()
//     <-l.StopChan() // wait for it to stop
package slog
<|endoftext|>


<commit_before>//Package utron is a lightweight MVC framework for building fast, scalable and robust web applications
package utron
<commit_msg>Add godoc<commit_after>//Package utron is a lightweight MVC framework for building fast, scalable and robust web applications
//
// Utron is built with focus of composability. This means, you can reap all the
// benefit of using utron while maintaining harmony with existing libraries and
// frameworks.
//
// Core feaures of utron
//
// * Session management
//
// * Middleware support
//
// * Data acess( Relational database)
//
// * Logging
//
// * Templates (views)
//
// * Configuration
//
// * Static content serving
//
// Why utron
//
// There are many frameworks out there, you might be wondering why do we need
// yet another framework?. We probably don't.. Utron is just a summary of the
// tools, and techinques I use to develop web  applications in Go.
//
// This includes the best libraries, and the best organization of the code base.
// Utron has one of the very handy Controller( you will see more details in the
// controller section)
//
// These are the common libraries I use
//
// * gorilla/mux: for http routing.
//
// * gorilla/session: for session management
//
// * gorm: for object relationl mapping.
//
// In all of my web application I noticed that, keeping global state brings a
// lot of complexities to the application, so uttron avoids this. utron
// applicatio is a simple struct that you can play around with in whatever way
// that you find suits you.
package utron
<|endoftext|>


<commit_before>// Copyright (c) 2013-2016 The btcsuite developers
// Copyright (c) 2015-2021 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

/*
dcrd is a full-node Decred implementation written in Go.

The default options are sane for most users.  This means dcrd will work 'out of
the box' for most users.  However, there are also a wide variety of flags that
can be used to control it.

The following section provides a usage overview which enumerates the flags.  An
interesting point to note is that the long form of all of these options
(except -C) can be specified in a configuration file that is automatically
parsed when dcrd starts up.  By default, the configuration file is located at
~/.dcrd/dcrd.conf on POSIX-style operating systems and %LOCALAPPDATA%\dcrd\dcrd.conf
on Windows.  The -C (--configfile) flag, as shown below, can be used to override
this location.

Usage:
  dcrd [OPTIONS]

Application Options:
  -V, --version                Display version information and exit
  -A, --appdata=               Path to application home directory
  -C, --configfile=            Path to configuration file
  -b, --datadir=               Directory to store data
      --logdir=                Directory to log output
      --nofilelogging=         Disable file logging
      --dbtype=                Database backend to use for the block chain
                               (default: ffldb)
      --profile=               Enable HTTP profiling on given [addr:]port --
                               NOTE: port must be between 1024 and 65536
      --cpuprofile=            Write CPU profile to the specified file
      --memprofile=            Write mem profile to the specified file
      --testnet                Use the test network
      --simnet                 Use the simulation test network
      --regnet                 Use the regression test network
  -d, --debuglevel=            Logging level for all subsystems {trace, debug,
                               info, warn, error, critical} -- You may also
                               specify
                               <subsystem>=<level>,<subsystem2>=<level>,... to
                               set the log level for individual subsystems --
                               Use show to list available subsystems (info)
      --sigcachemaxsize=       The maximum number of entries in the signature
                               verification cache (default: 100000)
      --utxocachemaxsize=      The maximum size in MiB of the utxo cache
                               (default: 150, minimum: 25, maximum: 32768)
      --norpc                  Disable built-in RPC server -- NOTE: The RPC
                               server is disabled by default if no
                               rpcuser/rpcpass or rpclimituser/rpclimitpass is
                               specified
      --rpclisten=             Add an interface/port to listen for RPC
                               connections (default port: 9109, testnet: 19109)
  -u, --rpcuser=               Username for RPC connections
  -P, --rpcpass=               Password for RPC connections
      --rpclimituser=          Username for limited RPC connections
      --rpclimitpass=          Password for limited RPC connections
      --rpccert=               File containing the certificate file
      --rpckey=                File containing the certificate key
      --tlscurve=              Curve to use when generating the TLS keypair
                               (default: P-256)
      --altdnsnames            Specify additional dns names to use when
                               generating the rpc server certificate
                               [supports DCRD_ALT_DNSNAMES environment variable]
      --notls                  Disable TLS for the RPC server -- NOTE: This is
                               only allowed if the RPC server is bound to
                               localhost
      --rpcmaxclients=         Max number of RPC clients for standard
                               connections (default: 10)
      --rpcmaxwebsockets=      Max number of RPC websocket connections (default:
                               25)
      --rpcmaxconcurrentreqs=  Max number of concurrent RPC requests that may be
                               processed concurrently (default: 20)
      --proxy=                 Connect via SOCKS5 proxy (eg. 127.0.0.1:9050)
      --proxyuser=             Username for proxy server
      --proxypass=             Password for proxy server
      --onion=                 Connect to tor hidden services via SOCKS5 proxy
                               (eg. 127.0.0.1:9050)
      --onionuser=             Username for onion proxy server
      --onionpass=             Password for onion proxy server
      --noonion                Disable connecting to tor hidden services
      --torisolation           Enable Tor stream isolation by randomizing user
                               credentials for each connection
  -a, --addpeer=               Add a peer to connect with at startup
      --connect=               Connect only to the specified peers at startup
      --nolisten               Disable listening for incoming connections --
                               NOTE: Listening is automatically disabled if the
                               --connect or --proxy options are used without
                               also specifying listen interfaces via --listen
      --listen=                Add an interface/port to listen for connections
                               (default all interfaces port: 9108, testnet:
                               19108)
      --maxsameip=             Max number of connections with the same IP -- 0
                               to disable (default: 5)
      --maxpeers=              Max number of inbound and outbound peers
                               (default: 125)
      --dialtimeout=           How long to wait for TCP connection completion
                               Valid time units are {s, m, h}  Minimum 1 second
                               (default: 30s)
      --peeridletimeout        The duration of inactivity before a peer is timed
                               out. Valid time units are {s,m,h}. Minimum 15
                               seconds (default: 2m0s)
      --noseeders              Disable seeding for peer discovery
      --nodnsseed              DEPRECATED: use --noseeders
      --externalip=            Add an ip to the list of local addresses we claim
                               to listen on to peers
      --nodiscoverip           Disable automatic network address discovery of
                               local external IPs
      --upnp                   Use UPnP to map our listening port outside of NAT
      --nobanning              Disable banning of misbehaving peers
      --banduration=           How long to ban misbehaving peers.  Valid time
                               units are {s, m, h}.  Minimum 1 second (default:
                               24h0m0s)
      --banthreshold=          Maximum allowed ban score before disconnecting
                               and banning misbehaving peers (default: 100)
      --whitelist=             Add an IP network or IP that will not be banned.
                               (eg. 192.168.1.0/24 or ::1)
      --nocheckpoints          Disable built-in checkpoints.  Don't do this
                               unless you know what you're doing
      --dumpblockchain=        Write blockchain as a flat file of blocks for use
                               with addblock, to the specified filename
      --minrelaytxfee=         The minimum transaction fee in DCR/kB to be
                               considered a non-zero fee (default: 0.0001)
      --limitfreerelay=        Limit relay of transactions with no transaction
                               fee to the given amount in thousands of bytes per
                               minute (default: 15)
      --norelaypriority        Do not require free or low-fee transactions to
                               have high priority for relaying
      --maxorphantx=           Max number of orphan transactions to keep in
                               memory (default: 100)
      --blocksonly             Do not accept transactions from remote peers
      --acceptnonstd           Accept and relay non-standard transactions to
                               the network regardless of the default settings
                               for the active network
      --rejectnonstd           Reject non-standard transactions regardless of
                               the default settings for the active network
      --allowoldvotes          Enable the addition of very old votes to the
                               mempool
      --generate               Generate (mine) bitcoins using the CPU
      --miningaddr=            Add the specified payment address to the list of
                               addresses to use for generated blocks -- At least
                               one address is required if the generate option is
                               set
      --blockminsize=          Minimum block size in bytes to be used when
                               creating a block
      --blockmaxsize=          Maximum block size in bytes to be used when
                               creating a block (default: 375000)
      --blockprioritysize=     Size in bytes for high-priority/low-fee
                               transactions when creating a block (default:
                               20000)
      --miningtimeoffset=      Offset the mining timestamp of a block by this
                               many seconds (positive values are in the past)
      --nonaggressive          Disable mining off of the parent block of the
                               blockchain if there aren't enough voters
      --nominingstatesync      Disable synchronizing the mining state with other
                               nodes
      --allowunsyncedmining    Allow block templates to be generated even when
                               the chain is not considered synced on networks
                               other than the main network.  This is
                               automatically enabled when the simnet option is
                               set.  Don't do this unless you know what you're
                               doing
      --txindex                Maintain a full hash-based transaction index
                               which makes all transactions available via the
                               getrawtransaction RPC
      --droptxindex            Deletes the hash-based transaction index from the
                               database on start up and then exits
      --addrindex              Maintain a full address-based transaction index
                               which makes the searchrawtransactions RPC
                               available
      --dropaddrindex          Deletes the address-based transaction index from
                               the database on start up and then exits
      --noexistsaddrindex      Disable the exists address index, which tracks
                               whether or not an address has even been used
      --dropexistsaddrindex    Deletes the exists address index from the
                               database on start up and then exits
      --piperx=                File descriptor of read end pipe to enable parent
                               -> child process communication
      --pipetx=                File descriptor of write end pipe to enable
                               parent <- child process communication
      --lifetimeevents         Send lifetime notifications over the TX pipe

Help Options:
  -h, --help                   Show this help message

*/
package main
<commit_msg>docs: Correct generate description.<commit_after>// Copyright (c) 2013-2016 The btcsuite developers
// Copyright (c) 2015-2021 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

/*
dcrd is a full-node Decred implementation written in Go.

The default options are sane for most users.  This means dcrd will work 'out of
the box' for most users.  However, there are also a wide variety of flags that
can be used to control it.

The following section provides a usage overview which enumerates the flags.  An
interesting point to note is that the long form of all of these options
(except -C) can be specified in a configuration file that is automatically
parsed when dcrd starts up.  By default, the configuration file is located at
~/.dcrd/dcrd.conf on POSIX-style operating systems and %LOCALAPPDATA%\dcrd\dcrd.conf
on Windows.  The -C (--configfile) flag, as shown below, can be used to override
this location.

Usage:
  dcrd [OPTIONS]

Application Options:
  -V, --version                Display version information and exit
  -A, --appdata=               Path to application home directory
  -C, --configfile=            Path to configuration file
  -b, --datadir=               Directory to store data
      --logdir=                Directory to log output
      --nofilelogging=         Disable file logging
      --dbtype=                Database backend to use for the block chain
                               (default: ffldb)
      --profile=               Enable HTTP profiling on given [addr:]port --
                               NOTE: port must be between 1024 and 65536
      --cpuprofile=            Write CPU profile to the specified file
      --memprofile=            Write mem profile to the specified file
      --testnet                Use the test network
      --simnet                 Use the simulation test network
      --regnet                 Use the regression test network
  -d, --debuglevel=            Logging level for all subsystems {trace, debug,
                               info, warn, error, critical} -- You may also
                               specify
                               <subsystem>=<level>,<subsystem2>=<level>,... to
                               set the log level for individual subsystems --
                               Use show to list available subsystems (info)
      --sigcachemaxsize=       The maximum number of entries in the signature
                               verification cache (default: 100000)
      --utxocachemaxsize=      The maximum size in MiB of the utxo cache
                               (default: 150, minimum: 25, maximum: 32768)
      --norpc                  Disable built-in RPC server -- NOTE: The RPC
                               server is disabled by default if no
                               rpcuser/rpcpass or rpclimituser/rpclimitpass is
                               specified
      --rpclisten=             Add an interface/port to listen for RPC
                               connections (default port: 9109, testnet: 19109)
  -u, --rpcuser=               Username for RPC connections
  -P, --rpcpass=               Password for RPC connections
      --rpclimituser=          Username for limited RPC connections
      --rpclimitpass=          Password for limited RPC connections
      --rpccert=               File containing the certificate file
      --rpckey=                File containing the certificate key
      --tlscurve=              Curve to use when generating the TLS keypair
                               (default: P-256)
      --altdnsnames            Specify additional dns names to use when
                               generating the rpc server certificate
                               [supports DCRD_ALT_DNSNAMES environment variable]
      --notls                  Disable TLS for the RPC server -- NOTE: This is
                               only allowed if the RPC server is bound to
                               localhost
      --rpcmaxclients=         Max number of RPC clients for standard
                               connections (default: 10)
      --rpcmaxwebsockets=      Max number of RPC websocket connections (default:
                               25)
      --rpcmaxconcurrentreqs=  Max number of concurrent RPC requests that may be
                               processed concurrently (default: 20)
      --proxy=                 Connect via SOCKS5 proxy (eg. 127.0.0.1:9050)
      --proxyuser=             Username for proxy server
      --proxypass=             Password for proxy server
      --onion=                 Connect to tor hidden services via SOCKS5 proxy
                               (eg. 127.0.0.1:9050)
      --onionuser=             Username for onion proxy server
      --onionpass=             Password for onion proxy server
      --noonion                Disable connecting to tor hidden services
      --torisolation           Enable Tor stream isolation by randomizing user
                               credentials for each connection
  -a, --addpeer=               Add a peer to connect with at startup
      --connect=               Connect only to the specified peers at startup
      --nolisten               Disable listening for incoming connections --
                               NOTE: Listening is automatically disabled if the
                               --connect or --proxy options are used without
                               also specifying listen interfaces via --listen
      --listen=                Add an interface/port to listen for connections
                               (default all interfaces port: 9108, testnet:
                               19108)
      --maxsameip=             Max number of connections with the same IP -- 0
                               to disable (default: 5)
      --maxpeers=              Max number of inbound and outbound peers
                               (default: 125)
      --dialtimeout=           How long to wait for TCP connection completion
                               Valid time units are {s, m, h}  Minimum 1 second
                               (default: 30s)
      --peeridletimeout        The duration of inactivity before a peer is timed
                               out. Valid time units are {s,m,h}. Minimum 15
                               seconds (default: 2m0s)
      --noseeders              Disable seeding for peer discovery
      --nodnsseed              DEPRECATED: use --noseeders
      --externalip=            Add an ip to the list of local addresses we claim
                               to listen on to peers
      --nodiscoverip           Disable automatic network address discovery of
                               local external IPs
      --upnp                   Use UPnP to map our listening port outside of NAT
      --nobanning              Disable banning of misbehaving peers
      --banduration=           How long to ban misbehaving peers.  Valid time
                               units are {s, m, h}.  Minimum 1 second (default:
                               24h0m0s)
      --banthreshold=          Maximum allowed ban score before disconnecting
                               and banning misbehaving peers (default: 100)
      --whitelist=             Add an IP network or IP that will not be banned.
                               (eg. 192.168.1.0/24 or ::1)
      --nocheckpoints          Disable built-in checkpoints.  Don't do this
                               unless you know what you're doing
      --dumpblockchain=        Write blockchain as a flat file of blocks for use
                               with addblock, to the specified filename
      --minrelaytxfee=         The minimum transaction fee in DCR/kB to be
                               considered a non-zero fee (default: 0.0001)
      --limitfreerelay=        Limit relay of transactions with no transaction
                               fee to the given amount in thousands of bytes per
                               minute (default: 15)
      --norelaypriority        Do not require free or low-fee transactions to
                               have high priority for relaying
      --maxorphantx=           Max number of orphan transactions to keep in
                               memory (default: 100)
      --blocksonly             Do not accept transactions from remote peers
      --acceptnonstd           Accept and relay non-standard transactions to
                               the network regardless of the default settings
                               for the active network
      --rejectnonstd           Reject non-standard transactions regardless of
                               the default settings for the active network
      --allowoldvotes          Enable the addition of very old votes to the
                               mempool
      --generate               Generate (mine) coins using the CPU
      --miningaddr=            Add the specified payment address to the list of
                               addresses to use for generated blocks -- At least
                               one address is required if the generate option is
                               set
      --blockminsize=          Minimum block size in bytes to be used when
                               creating a block
      --blockmaxsize=          Maximum block size in bytes to be used when
                               creating a block (default: 375000)
      --blockprioritysize=     Size in bytes for high-priority/low-fee
                               transactions when creating a block (default:
                               20000)
      --miningtimeoffset=      Offset the mining timestamp of a block by this
                               many seconds (positive values are in the past)
      --nonaggressive          Disable mining off of the parent block of the
                               blockchain if there aren't enough voters
      --nominingstatesync      Disable synchronizing the mining state with other
                               nodes
      --allowunsyncedmining    Allow block templates to be generated even when
                               the chain is not considered synced on networks
                               other than the main network.  This is
                               automatically enabled when the simnet option is
                               set.  Don't do this unless you know what you're
                               doing
      --txindex                Maintain a full hash-based transaction index
                               which makes all transactions available via the
                               getrawtransaction RPC
      --droptxindex            Deletes the hash-based transaction index from the
                               database on start up and then exits
      --addrindex              Maintain a full address-based transaction index
                               which makes the searchrawtransactions RPC
                               available
      --dropaddrindex          Deletes the address-based transaction index from
                               the database on start up and then exits
      --noexistsaddrindex      Disable the exists address index, which tracks
                               whether or not an address has even been used
      --dropexistsaddrindex    Deletes the exists address index from the
                               database on start up and then exits
      --piperx=                File descriptor of read end pipe to enable parent
                               -> child process communication
      --pipetx=                File descriptor of write end pipe to enable
                               parent <- child process communication
      --lifetimeevents         Send lifetime notifications over the TX pipe

Help Options:
  -h, --help                   Show this help message

*/
package main
<|endoftext|>


<commit_before>package main

import (
	"bufio"
	"fmt"
	"os"
	"os/exec"
	"strings"

	"github.com/codegangsta/cli"
)

func fix(c *cli.Context) {
	repo := c.String("repo")

	if fixErr := fixExistingSubmodules(repo); fixErr != nil {
		println("failed to fix existing submodules: " + fixErr.Error())
		os.Exit(1)
	}

}

// Convert any "semi-submodules" into first class submodules.
// See http://stackoverflow.com/questions/4161022/git-how-to-track-untracked-content/4162672#4162672
func fixExistingSubmodules(repo string) error {
	submodules, err := getSubmodules(repo)
	if err != nil {
		return err
	}

	var lastErr error
	for _, submodule := range submodules {
		err = fixSubmodule(submodule)
		if err != nil {
			fmt.Printf("fixExistingSubmodules failed to fix submodule %s\n", submodule)
			lastErr = err
		}
	}

	return lastErr
}

func fixSubmodule(submodule string) error {
	fmt.Printf("\x1b[31mFixing submodule %s .", submodule)
	defer fmt.Println("\x1b[0m")
	rm := exec.Command("git", "rm", "--cached", "-f", submodule)
	rm.Stderr = os.Stderr
	err := rm.Run()
	if err != nil {
		return fmt.Errorf("fixSubmodule failed to remove submodule path %s from the index: %s", submodule, err)
	}
	fmt.Printf(".")

	url, err := submoduleUrl(submodule)
	if err != nil {
		return fmt.Errorf("fixSubmodule failed to determine URL of submodule %s: %s", submodule, err)
	}

	submoduleAdd := exec.Command("git", "submodule", "add", url, submodule)
	submoduleAdd.Stderr = os.Stderr
	err = submoduleAdd.Run()
	if err != nil {
		return fmt.Errorf("fixSubmodule failed to add submodule %s: %s", submodule, err)
	}
	fmt.Printf(".")

	fmt.Printf(". done.")
	return nil
}

func submoduleUrl(submodule string) (string, error) {
	submoduleQuery := exec.Command("git", "remote", "show", "origin")
	submoduleQuery.Dir = submodule
	submoduleQuery.Stderr = os.Stderr

	lsFileOut, err := submoduleQuery.StdoutPipe()
	if err != nil {
		fmt.Printf("submoduleUrl failed to get StdoutPipe: %s\n", err)
		return "", err
	}

	lineScanner := bufio.NewScanner(lsFileOut)

	err = submoduleQuery.Start()
	if err != nil {
		fmt.Printf("submoduleUrl failed to start git remote show origin: %s\n", err)
		return "", err
	}

	var url string
	for lineScanner.Scan() {
		segments := strings.Fields(lineScanner.Text())

		if len(segments) < 3 {
			continue
		}

		if segments[0] == "Fetch" && segments[1] == "URL:" {
			url = segments[2]
		}
	}

	if url == "" {
		return "", fmt.Errorf("submoduleUrl failed to find the URL of %s\n", submodule)
	}

	err = submoduleQuery.Wait()
	if err != nil {
		fmt.Printf("submoduleUrl failed to wait for git remote show origin: %s\n", err)
		return "", err
	}

	return url, nil
}

func getSubmodules(repo string) ([]string, error) {
	lsFiles := exec.Command("git", "ls-files", "--stage")
	lsFiles.Dir = repo
	lsFiles.Stderr = os.Stderr

	lsFileOut, err := lsFiles.StdoutPipe()
	if err != nil {
		fmt.Printf("getSubmodules failed to get StdoutPipe: %s\n", err)
		return nil, err
	}

	lineScanner := bufio.NewScanner(lsFileOut)

	err = lsFiles.Start()
	if err != nil {
		fmt.Printf("getSubmodules failed to start git ls-files --stage: %s\n", err)
		return nil, err
	}

	submodules := []string{}
	for lineScanner.Scan() {
		segments := strings.Fields(lineScanner.Text())

		if len(segments) < 4 {
			return nil, fmt.Errorf("invalid git ls-files output: %q", lineScanner.Text())
		}

		if segments[0] == "160000" {
			submodules = append(submodules, segments[3])
		}
	}

	err = lsFiles.Wait()
	if err != nil {
		fmt.Printf("getSubmodules failed to wait for git ls-files --stage: %s\n", err)
		return nil, err
	}

	return submodules, nil
}
<commit_msg>Only fix submodules which are absent from .gitmodules<commit_after>package main

import (
	"bufio"
	"fmt"
	"os"
	"os/exec"
	"strings"

	"io/ioutil"
	"path/filepath"

	"github.com/codegangsta/cli"
)

func fix(c *cli.Context) {
	repo := c.String("repo")

	if fixErr := fixExistingSubmodules(repo); fixErr != nil {
		println("failed to fix existing submodules: " + fixErr.Error())
		os.Exit(1)
	}

}

// Convert any "semi-submodules" into first class submodules.
// See http://stackoverflow.com/questions/4161022/git-how-to-track-untracked-content/4162672#4162672
func fixExistingSubmodules(repo string) error {
	submodules, err := getSubmodules(repo)
	if err != nil {
		return err
	}

	gitmodules, err := getGitModules(repo)
	if err != nil {
		return err
	}

	var lastErr error
	for _, submodule := range submodules {
		if !gitmodules[submodule] {
			err = fixSubmodule(submodule)
			if err != nil {
				fmt.Printf("fixExistingSubmodules failed to fix submodule %s\n", submodule)
				lastErr = err
			}
		}
	}

	return lastErr
}

func fixSubmodule(submodule string) error {
	fmt.Printf("\x1b[32mFixing submodule %s .", submodule)
	defer fmt.Println("\x1b[0m")
	rm := exec.Command("git", "rm", "--cached", "-f", submodule)
	rm.Stderr = os.Stderr
	err := rm.Run()
	if err != nil {
		return fmt.Errorf("fixSubmodule failed to remove submodule path %s from the index: %s", submodule, err)
	}
	fmt.Printf(".")

	url, err := submoduleUrl(submodule)
	if err != nil {
		return fmt.Errorf("fixSubmodule failed to determine URL of submodule %s: %s", submodule, err)
	}
	fmt.Printf(".")

	submoduleAdd := exec.Command("git", "submodule", "add", url, submodule)
	submoduleAdd.Stderr = os.Stderr
	err = submoduleAdd.Run()
	if err != nil {
		return fmt.Errorf("fixSubmodule failed to add submodule %s: %s", submodule, err)
	}
	fmt.Printf(".")

	fmt.Printf(". done.")
	return nil
}

func submoduleUrl(submodule string) (string, error) {
	submoduleQuery := exec.Command("git", "remote", "show", "origin")
	submoduleQuery.Dir = submodule
	submoduleQuery.Stderr = os.Stderr

	lsFileOut, err := submoduleQuery.StdoutPipe()
	if err != nil {
		fmt.Printf("submoduleUrl failed to get StdoutPipe: %s\n", err)
		return "", err
	}

	lineScanner := bufio.NewScanner(lsFileOut)

	err = submoduleQuery.Start()
	if err != nil {
		fmt.Printf("submoduleUrl failed to start git remote show origin: %s\n", err)
		return "", err
	}

	var url string
	for lineScanner.Scan() {
		segments := strings.Fields(lineScanner.Text())

		if len(segments) < 3 {
			continue
		}

		if segments[0] == "Fetch" && segments[1] == "URL:" {
			url = segments[2]
		}
	}

	if url == "" {
		return "", fmt.Errorf("submoduleUrl failed to find the URL of %s\n", submodule)
	}

	err = submoduleQuery.Wait()
	if err != nil {
		fmt.Printf("submoduleUrl failed to wait for git remote show origin: %s\n", err)
		return "", err
	}

	return url, nil
}

func getSubmodules(repo string) ([]string, error) {
	lsFiles := exec.Command("git", "ls-files", "--stage")
	lsFiles.Dir = repo
	lsFiles.Stderr = os.Stderr

	lsFileOut, err := lsFiles.StdoutPipe()
	if err != nil {
		fmt.Printf("getSubmodules failed to get StdoutPipe: %s\n", err)
		return nil, err
	}

	lineScanner := bufio.NewScanner(lsFileOut)

	err = lsFiles.Start()
	if err != nil {
		fmt.Printf("getSubmodules failed to start git ls-files --stage: %s\n", err)
		return nil, err
	}

	submodules := []string{}
	for lineScanner.Scan() {
		segments := strings.Fields(lineScanner.Text())

		if len(segments) < 4 {
			return nil, fmt.Errorf("invalid git ls-files output: %q", lineScanner.Text())
		}

		if segments[0] == "160000" {
			submodules = append(submodules, segments[3])
		}
	}

	err = lsFiles.Wait()
	if err != nil {
		fmt.Printf("getSubmodules failed to wait for git ls-files --stage: %s\n", err)
		return nil, err
	}

	return submodules, nil
}

func getGitModules(repo string) (map[string]bool, error) {
	gitmodules := make(map[string]bool)

	dotGitModules, err := ioutil.ReadFile(filepath.Join(repo, ".gitmodules"))
	if err != nil {
		return nil, fmt.Errorf("Failed to read .gitmodules file: %s", err)
	}

	lineScanner := bufio.NewScanner(strings.NewReader(string(dotGitModules)))
	for lineScanner.Scan() {
		segments := strings.Fields(lineScanner.Text())
		if len(segments) == 3 && segments[0] == "path" && segments[1] == "=" {
			gitmodules[segments[2]] = true
		}
	}

	return gitmodules, nil
}
<|endoftext|>


<commit_before>package venom

import (
	"bufio"
	"fmt"
	"os"
	"strings"

	"github.com/spf13/pflag"
	"github.com/spf13/viper"
)

func parseEnvLine(line string) (string, string, error) {
	pair := strings.SplitN(line, "=", 2)
	if len(pair) != 2 {
		return "", "", fmt.Errorf("Invalid format, must be kv-pair separated with '=': %s", line)
	}
	return pair[0], pair[1], nil
}

func readEnvFile(p string) (map[string]string, error) {
	f, err := os.Open(p)
	if err != nil {
		return nil, err
	}

	s := bufio.NewScanner(f)
	envMap := make(map[string]string)
	for s.Scan() {
		line := s.Text()
		line = strings.TrimSpace(line)
		if len(line) == 0 {
			continue // skip empty lines
		}

		k, v, err := parseEnvLine(line)
		if err != nil {
			return nil, err
		}
		envMap[k] = v
	}
	return envMap, nil
}

type envConfig struct {
	Envs      []string `mapstructure:"env"`
	EnvFiles  []string `mapstructure:"env-file"`
	EnvPrefix string   `mapstructure:"env-prefix"`
}

func initEnvFlags(flags *pflag.FlagSet, envprefix string) error {
	flags.String("env-prefix", envprefix, "Set environment variables prefix")
	flags.StringSliceP("env", "e", nil, "Set environment variables")
	flags.StringSlice("env-file", nil, "Read environment variables from a text file")
	return nil
}

func readEnv(v *viper.Viper) error {
	var cfg envConfig
	err := Unmarshal(&cfg, v)
	if err != nil {
		return err
	}

	envMap := make(map[string]string)

	// read --env-prefix
	if cfg.EnvPrefix != "" {
		v.SetEnvPrefix(cfg.EnvPrefix)
	}

	// read --env-file
	for _, f := range cfg.EnvFiles {
		env, err := readEnvFile(f)
		if err != nil {
			return err
		}
		for k, v := range env {
			envMap[k] = v
		}
	}

	// read --env
	for _, kv := range cfg.Envs {
		k, v, err := parseEnvLine(kv)
		if err != nil {
			return err
		}
		envMap[k] = v
	}

	// apply env
	for k, v := range envMap {
		err = os.Setenv(k, v)
		if err != nil {
			return err
		}
	}
	return nil
}

func lookupEnvPrefix(flags *pflag.FlagSet) (string, bool) {
	envprefix := ""
	flags.VisitAll(func(f *pflag.Flag) {
		if f.Name == "env-prefix" {
			envprefix = f.Value.String()
		}
	})
	return envprefix, envprefix != ""
}
<commit_msg>Update help<commit_after>package venom

import (
	"bufio"
	"fmt"
	"os"
	"strings"

	"github.com/spf13/pflag"
	"github.com/spf13/viper"
)

func parseEnvLine(line string) (string, string, error) {
	pair := strings.SplitN(line, "=", 2)
	if len(pair) != 2 {
		return "", "", fmt.Errorf("Invalid format, must be kv-pair separated with '=': %s", line)
	}
	return pair[0], pair[1], nil
}

func readEnvFile(p string) (map[string]string, error) {
	f, err := os.Open(p)
	if err != nil {
		return nil, err
	}

	s := bufio.NewScanner(f)
	envMap := make(map[string]string)
	for s.Scan() {
		line := s.Text()
		line = strings.TrimSpace(line)
		if len(line) == 0 {
			continue // skip empty lines
		}

		k, v, err := parseEnvLine(line)
		if err != nil {
			return nil, err
		}
		envMap[k] = v
	}
	return envMap, nil
}

type envConfig struct {
	Envs      []string `mapstructure:"env"`
	EnvFiles  []string `mapstructure:"env-file"`
	EnvPrefix string   `mapstructure:"env-prefix"`
}

func initEnvFlags(flags *pflag.FlagSet, envprefix string) error {
	flags.String("env-prefix", envprefix, "Set environment variables prefix")
	flags.StringSliceP("env", "e", nil, "Set environment variables")
	flags.StringSlice("env-file", nil, "Read environment variables from a file")
	return nil
}

func readEnv(v *viper.Viper) error {
	var cfg envConfig
	err := Unmarshal(&cfg, v)
	if err != nil {
		return err
	}

	envMap := make(map[string]string)

	// read --env-prefix
	if cfg.EnvPrefix != "" {
		v.SetEnvPrefix(cfg.EnvPrefix)
	}

	// read --env-file
	for _, f := range cfg.EnvFiles {
		env, err := readEnvFile(f)
		if err != nil {
			return err
		}
		for k, v := range env {
			envMap[k] = v
		}
	}

	// read --env
	for _, kv := range cfg.Envs {
		k, v, err := parseEnvLine(kv)
		if err != nil {
			return err
		}
		envMap[k] = v
	}

	// apply env
	for k, v := range envMap {
		err = os.Setenv(k, v)
		if err != nil {
			return err
		}
	}
	return nil
}

func lookupEnvPrefix(flags *pflag.FlagSet) (string, bool) {
	envprefix := ""
	flags.VisitAll(func(f *pflag.Flag) {
		if f.Name == "env-prefix" {
			envprefix = f.Value.String()
		}
	})
	return envprefix, envprefix != ""
}
<|endoftext|>


<commit_before>package gonfig

import (
	"os"
	"strings"
)

// EnvConfig can be used to read values from the environment
// into the underlaying Configurable
type EnvConfig struct {
	Configurable
	Prefix string
}

// Creates a new Env config backed by a memory config
func NewEnvConfig(prefix string) ReadableConfig {
	cfg := &EnvConfig{NewMemoryConfig(), prefix}
	cfg.Load()
	return cfg
}

// Get a key from the underlaying configuration
func (self *EnvConfig) Get(key string) interface{} {
	return self.Configurable.Get(self.Prefix + key)
}

// Loads the data from os.Environ() to the underlaying Configurable.
// if a Prefix is set then variables are imported with self.Prefix removed from the name
// so MYAPP_test=1 exported in env and read from ENV by EnvConfig{Prefix:"MYAPP_"} can be found from
// EnvConfig.Get("test")
func (self *EnvConfig) Load() (err error) {
	env := os.Environ()
	for _, pair := range env {
		kv := strings.Split(pair, "=")
		if kv != nil && len(kv) >= 2 {
			self.Set(strings.Replace(kv[0], self.Prefix, "", 1), kv[1])
		}
	}
	return nil
}
<commit_msg>fix prefix Get, prefix already applied in Load<commit_after>package gonfig

import (
	"os"
	"strings"
)

// EnvConfig can be used to read values from the environment
// into the underlaying Configurable
type EnvConfig struct {
	Configurable
	Prefix string
}

// Creates a new Env config backed by a memory config
func NewEnvConfig(prefix string) ReadableConfig {
	cfg := &EnvConfig{NewMemoryConfig(), prefix}
	cfg.Load()
	return cfg
}

// Loads the data from os.Environ() to the underlaying Configurable.
// if a Prefix is set then variables are imported with self.Prefix removed from the name
// so MYAPP_test=1 exported in env and read from ENV by EnvConfig{Prefix:"MYAPP_"} can be found from
// EnvConfig.Get("test")
func (self *EnvConfig) Load() (err error) {
	env := os.Environ()
	for _, pair := range env {
		kv := strings.Split(pair, "=")
		if kv != nil && len(kv) >= 2 {
			self.Set(strings.Replace(kv[0], self.Prefix, "", 1), kv[1])
		}
	}
	return nil
}
<|endoftext|>


<commit_before>package env

import (
	"bufio"
	"fmt"
	"os"
	"path/filepath"
	"reflect"
	"regexp"
	"strconv"
	"strings"

	log "github.com/Sirupsen/logrus"
	"github.com/goanywhere/x/fs"
)

const tag string = "env"

var pattern = regexp.MustCompile(`(export\s*)?(?P<key>\w+)\s*(=)\s*(?P<value>("|')?[\w\s-$,:/\.\+]+)("|')?`)

// findKeyValue finds '=' separated key/value pair from the given string.
func findKeyValue(line string) (key, value string) {
	// Intentionally insert a linebreak here to avoid non-stop characters on [[:graph:]].
	line = fmt.Sprintf("%s\n", line)
	match := pattern.FindStringSubmatch(line)
	if len(match) != 0 {
		result := make(map[string]string)
		for index, name := range pattern.SubexpNames() {
			result[name] = match[index]
		}
		// preserve those quoted spaces for value.
		key, value = result["key"], strings.TrimSpace(result["value"])
		value = strings.Trim(value, "'")
		value = strings.Trim(value, "\"")
	}
	return
}

// Load parses & set the values in the given files into os environment.
func Load(dotenv string) {
	if fs.Exists(dotenv) {
		dotenv, _ = filepath.Abs(dotenv)

		if file, err := os.Open(dotenv); err == nil {
			defer file.Close()
			scanner := bufio.NewScanner(file)
			for scanner.Scan() {
				k, v := findKeyValue(scanner.Text())
				if k != "" && v != "" {
					Set(k, v)
				} else {
					continue
				}
			}
		}
	}
}

// Get retrieves the string value of the environment variable named by the key.
// It returns the value, which will be empty if the variable is not present.
func Get(key string) (value string, exists bool) {
	if v := os.Getenv(key); v != "" {
		value, exists = v, true
	}
	return
}

// Set stores the value of the environment variable named by the key. It returns an error, if any.
func Set(key string, value interface{}) error {
	var sv string

	switch T := value.(type) {
	case bool:
		sv = strconv.FormatBool(T)

	case float32, float64:
		sv = strconv.FormatFloat(reflect.ValueOf(value).Float(), 'g', -1, 64)

	case int, int8, int16, int32, int64:
		sv = strconv.FormatInt(reflect.ValueOf(value).Int(), 10)

	case uint, uint8, uint16, uint32, uint64:
		sv = strconv.FormatUint(reflect.ValueOf(value).Uint(), 10)

	case string:
		sv = value.(string)

	case []string:
		sv = strings.Join(value.([]string), ",")

	default:
		return fmt.Errorf("Unsupported type: %v", T)
	}
	return os.Setenv(key, sv)
}

// String retrieves the string value from environment named by the key.
func String(key string, fallback ...string) (value string) {
	if str, exists := Get(key); exists {
		value = str
	} else if len(fallback) > 0 {
		value = fallback[0]
	}
	return
}

// Strings retrieves the string values separated by comma from the environment.
func Strings(key string, fallback ...[]string) (value []string) {
	if v, exists := Get(key); exists {
		for _, item := range strings.Split(strings.TrimSpace(v), ",") {
			value = append(value, strings.TrimSpace(item))
		}
	} else if len(fallback) > 0 {
		value = fallback[0]
	}
	return
}

// Int retrieves the integer values separated by comma from the environment.
func Int(key string, fallback ...int) (value int) {
	if str, exists := Get(key); exists {
		if v, e := strconv.ParseInt(str, 10, 0); e == nil {
			value = int(v)
		}
	} else if len(fallback) > 0 {
		value = fallback[0]
	}
	return
}

// Int retrieves the 64-bit integer values separated by comma from the environment.
func Int64(key string, fallback ...int64) (value int64) {
	if str, exists := Get(key); exists {
		if v, e := strconv.ParseInt(str, 10, 64); e == nil {
			value = v
		}
	} else if len(fallback) > 0 {
		value = fallback[0]
	}
	return
}

// Uint retrieves the unsigned integer values separated by comma from the environment.
func Uint(key string, fallback ...uint) (value uint) {
	if str, exists := Get(key); exists {
		if v, e := strconv.ParseUint(str, 10, 0); e == nil {
			value = uint(v)
		}
	} else if len(fallback) > 0 {
		value = fallback[0]
	}
	return
}

// Uint64 retrieves the 64-bit unsigned integer values separated by comma from the environment.
func Uint64(key string, fallback ...uint64) (value uint64) {
	if str, exists := Get(key); exists {
		if v, e := strconv.ParseUint(str, 10, 64); e == nil {
			value = v
		}
	} else if len(fallback) > 0 {
		value = fallback[0]
	}
	return
}

// Bool retrieves boolean value from the environment.
func Bool(key string, fallback ...bool) (value bool) {
	if str, exists := Get(key); exists {
		if v, e := strconv.ParseBool(str); e == nil {
			value = v
		}
	} else if len(fallback) > 0 {
		value = fallback[0]
	}
	return
}

// Float retrieves float (64) value from the environment.
func Float(key string, fallback ...float64) (value float64) {
	if str, exists := Get(key); exists {
		if v, e := strconv.ParseFloat(str, 64); e == nil {
			value = v
		}
	} else if len(fallback) > 0 {
		value = fallback[0]
	}
	return
}

// Map fetches the key/value pair from os.Environ into the given spec.
// Tags are supported via `env:ACTUAL_OS_KEY`.
func Map(spec interface{}) error {
	value := reflect.ValueOf(spec)
	s := value.Elem()

	var stype reflect.Type = s.Type()
	var field reflect.Value

	for index := 0; index < s.NumField(); index++ {
		field = s.Field(index)
		if field.CanSet() {

			key := stype.Field(index).Tag.Get(tag)
			if key == "" {
				key = stype.Field(index).Name
			}

			value, exists := Get(key)
			if !exists {
				continue
			}
			// converts the environmental value from string to its real type.
			// Supports: String | Strings | Bool | Float | Integer | Unsiged Integer
			switch field.Kind() {
			case reflect.String:
				field.SetString(value)

			case reflect.Bool:
				field.SetBool(Bool(key))

			case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
				field.SetInt(Int64(key))

			case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
				field.SetUint(Uint64(key))

			case reflect.Float32, reflect.Float64:
				field.SetFloat(Float(key))

			case reflect.Slice:
				switch field.Interface().(type) {
				case []string:
					field.Set(reflect.ValueOf(Strings(key)))

				default:
					log.Fatalf("Only string slice is supported")
				}
			}
		}
	}
	return nil
}
<commit_msg>x/fs => fs<commit_after>package env

import (
	"bufio"
	"fmt"
	"os"
	"path/filepath"
	"reflect"
	"regexp"
	"strconv"
	"strings"

	log "github.com/Sirupsen/logrus"
	"github.com/goanywhere/fs"
)

const tag string = "env"

var pattern = regexp.MustCompile(`(export\s*)?(?P<key>\w+)\s*(=)\s*(?P<value>("|')?[\w\s-$,:/\.\+]+)("|')?`)

// findKeyValue finds '=' separated key/value pair from the given string.
func findKeyValue(line string) (key, value string) {
	// Intentionally insert a linebreak here to avoid non-stop characters on [[:graph:]].
	line = fmt.Sprintf("%s\n", line)
	match := pattern.FindStringSubmatch(line)
	if len(match) != 0 {
		result := make(map[string]string)
		for index, name := range pattern.SubexpNames() {
			result[name] = match[index]
		}
		// preserve those quoted spaces for value.
		key, value = result["key"], strings.TrimSpace(result["value"])
		value = strings.Trim(value, "'")
		value = strings.Trim(value, "\"")
	}
	return
}

// Load parses & set the values in the given files into os environment.
func Load(dotenv string) {
	if fs.Exists(dotenv) {
		dotenv, _ = filepath.Abs(dotenv)

		if file, err := os.Open(dotenv); err == nil {
			defer file.Close()
			scanner := bufio.NewScanner(file)
			for scanner.Scan() {
				k, v := findKeyValue(scanner.Text())
				if k != "" && v != "" {
					Set(k, v)
				} else {
					continue
				}
			}
		}
	}
}

// Get retrieves the string value of the environment variable named by the key.
// It returns the value, which will be empty if the variable is not present.
func Get(key string) (value string, exists bool) {
	if v := os.Getenv(key); v != "" {
		value, exists = v, true
	}
	return
}

// Set stores the value of the environment variable named by the key. It returns an error, if any.
func Set(key string, value interface{}) error {
	var sv string

	switch T := value.(type) {
	case bool:
		sv = strconv.FormatBool(T)

	case float32, float64:
		sv = strconv.FormatFloat(reflect.ValueOf(value).Float(), 'g', -1, 64)

	case int, int8, int16, int32, int64:
		sv = strconv.FormatInt(reflect.ValueOf(value).Int(), 10)

	case uint, uint8, uint16, uint32, uint64:
		sv = strconv.FormatUint(reflect.ValueOf(value).Uint(), 10)

	case string:
		sv = value.(string)

	case []string:
		sv = strings.Join(value.([]string), ",")

	default:
		return fmt.Errorf("Unsupported type: %v", T)
	}
	return os.Setenv(key, sv)
}

// String retrieves the string value from environment named by the key.
func String(key string, fallback ...string) (value string) {
	if str, exists := Get(key); exists {
		value = str
	} else if len(fallback) > 0 {
		value = fallback[0]
	}
	return
}

// Strings retrieves the string values separated by comma from the environment.
func Strings(key string, fallback ...[]string) (value []string) {
	if v, exists := Get(key); exists {
		for _, item := range strings.Split(strings.TrimSpace(v), ",") {
			value = append(value, strings.TrimSpace(item))
		}
	} else if len(fallback) > 0 {
		value = fallback[0]
	}
	return
}

// Int retrieves the integer values separated by comma from the environment.
func Int(key string, fallback ...int) (value int) {
	if str, exists := Get(key); exists {
		if v, e := strconv.ParseInt(str, 10, 0); e == nil {
			value = int(v)
		}
	} else if len(fallback) > 0 {
		value = fallback[0]
	}
	return
}

// Int retrieves the 64-bit integer values separated by comma from the environment.
func Int64(key string, fallback ...int64) (value int64) {
	if str, exists := Get(key); exists {
		if v, e := strconv.ParseInt(str, 10, 64); e == nil {
			value = v
		}
	} else if len(fallback) > 0 {
		value = fallback[0]
	}
	return
}

// Uint retrieves the unsigned integer values separated by comma from the environment.
func Uint(key string, fallback ...uint) (value uint) {
	if str, exists := Get(key); exists {
		if v, e := strconv.ParseUint(str, 10, 0); e == nil {
			value = uint(v)
		}
	} else if len(fallback) > 0 {
		value = fallback[0]
	}
	return
}

// Uint64 retrieves the 64-bit unsigned integer values separated by comma from the environment.
func Uint64(key string, fallback ...uint64) (value uint64) {
	if str, exists := Get(key); exists {
		if v, e := strconv.ParseUint(str, 10, 64); e == nil {
			value = v
		}
	} else if len(fallback) > 0 {
		value = fallback[0]
	}
	return
}

// Bool retrieves boolean value from the environment.
func Bool(key string, fallback ...bool) (value bool) {
	if str, exists := Get(key); exists {
		if v, e := strconv.ParseBool(str); e == nil {
			value = v
		}
	} else if len(fallback) > 0 {
		value = fallback[0]
	}
	return
}

// Float retrieves float (64) value from the environment.
func Float(key string, fallback ...float64) (value float64) {
	if str, exists := Get(key); exists {
		if v, e := strconv.ParseFloat(str, 64); e == nil {
			value = v
		}
	} else if len(fallback) > 0 {
		value = fallback[0]
	}
	return
}

// Map fetches the key/value pair from os.Environ into the given spec.
// Tags are supported via `env:ACTUAL_OS_KEY`.
func Map(spec interface{}) error {
	value := reflect.ValueOf(spec)
	s := value.Elem()

	var stype reflect.Type = s.Type()
	var field reflect.Value

	for index := 0; index < s.NumField(); index++ {
		field = s.Field(index)
		if field.CanSet() {

			key := stype.Field(index).Tag.Get(tag)
			if key == "" {
				key = stype.Field(index).Name
			}

			value, exists := Get(key)
			if !exists {
				continue
			}
			// converts the environmental value from string to its real type.
			// Supports: String | Strings | Bool | Float | Integer | Unsiged Integer
			switch field.Kind() {
			case reflect.String:
				field.SetString(value)

			case reflect.Bool:
				field.SetBool(Bool(key))

			case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
				field.SetInt(Int64(key))

			case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
				field.SetUint(Uint64(key))

			case reflect.Float32, reflect.Float64:
				field.SetFloat(Float(key))

			case reflect.Slice:
				switch field.Interface().(type) {
				case []string:
					field.Set(reflect.ValueOf(Strings(key)))

				default:
					log.Fatalf("Only string slice is supported")
				}
			}
		}
	}
	return nil
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"strings"
	"path/filepath"
)

func main() {
	sourceFile, err := ioutil.ReadFile(os.Args[1])
	var s string
	var cfile bool = (filepath.Ext(os.Args[1]) == "c")

	if err == nil {
		s = string(sourceFile)
		fmt.Println(s)
	}
	splitFile := strings.Split(s, "\n")
	splitFile = removeNewlines(splitFile)
	lineSlice := createLines(splitFile)
	debugFile, _ := os.Create("testfiles/c++/debug.cpp")

	for i := 0; i < len(lineSlice); i++ {
		debugFile.WriteString(lineSlice[i].s + "\n")
		if cfile {
			AddPrintc(debugFile, lineSlice[i].s)
		}else{
			AddPrint(debugFile, lineSlice[i])
		}
	}
}

type lineType struct {
	s    string
	code int
}

func AddPrint(file *os.File, line lineType) {
	if line.code == 0 {
		file.WriteString("std::cout << \"" + line.s + "\" std::endl;\n")
	}
}

func AddPrintc(file *os.File, s string) {
	file.WriteString("printf(\"" + s + "\n\");\n");
}

func createLines(stringSlice []string) []lineType {
	var lineSlice []lineType
	for i := 0; i < len(stringSlice); i++ {
		lineSlice = append(lineSlice, lineType{stringSlice[i], 0})
	}
	return lineSlice
}

func removeNewlines(stringSlice []string) []string {
	var newSlice []string
	for i := 0; i < len(stringSlice); i++ {
		if stringSlice[i] != "" && stringSlice[i][0] != 10 {
			newSlice = append(newSlice, stringSlice[i])
		}
	}
	return newSlice
}
<commit_msg>Compiles and runs new cpp file<commit_after>package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
)

func main() {
	sourceFile, err := ioutil.ReadFile(os.Args[1])
	var s string
	var cfile bool = (filepath.Ext(os.Args[1]) == "c")

	if err == nil {
		s = string(sourceFile)
		fmt.Println(s)
	}
	splitFile := strings.Split(s, "\n")
	splitFile = RemoveNewlines(splitFile)
	lineSlice := CreateLines(splitFile)
	debugFile, _ := os.Create("testfiles/c++/debug.cpp")

	for i := 0; i < len(lineSlice); i++ {
		debugFile.WriteString(lineSlice[i].s + "\n")
		if cfile {
			AddPrintc(debugFile, lineSlice[i].s)
		} else {
			AddPrint(debugFile, lineSlice[i])
		}
	}
	debugFile.Close()
	CompileAndRun()
}

type lineType struct {
	s    string
	code int
}

func AddPrint(file *os.File, line lineType) {
	if line.code == 0 {
		file.WriteString("std::cout << \"" + line.s + "\" std::endl;\n")
	}
}

func AddPrintc(file *os.File, s string) {
	file.WriteString("printf(\"" + s + "\n\");\n")
}

func CreateLines(stringSlice []string) []lineType {
	var lineSlice []lineType
	for i := 0; i < len(stringSlice); i++ {
		lineSlice = append(lineSlice, lineType{stringSlice[i], 0})
	}
	return lineSlice
}

func RemoveNewlines(stringSlice []string) []string {
	var newSlice []string
	for i := 0; i < len(stringSlice); i++ {
		if stringSlice[i] != "" && stringSlice[i][0] != 10 {
			newSlice = append(newSlice, stringSlice[i])
		}
	}
	return newSlice
}

func CompileAndRun() {
	clangCmd := exec.Command("clang++", "-Wall", "testfiles/c++/debug.cpp", "-o", "testfiles/c++/out")
	gccCmd := exec.Command("g++", "-Wall", "testfiles/c++/debug.cpp", "-o", "testfiles/c++/out")
	err := clangCmd.Run()
	fmt.Println(err)
	if err != nil {
		gccCmd.Run()
	}
	runCmd := exec.Command("./testfiles/c++/out")
	err = runCmd.Run()
	fmt.Println(err)
}
<|endoftext|>