本文整理汇总了Golang中github.com/pkg/errors.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了New函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: parseCommand
// Returns the executable path and arguments
// TODO: Clean this up
func parseCommand(cmd string) (string, []string, error) {
var args []string
state := "start"
current := ""
quote := "\""
for i := 0; i < len(cmd); i++ {
c := cmd[i]
if state == "quotes" {
if string(c) != quote {
current += string(c)
} else {
args = append(args, current)
current = ""
state = "start"
}
continue
}
if c == '"' || c == '\'' {
state = "quotes"
quote = string(c)
continue
}
if state == "arg" {
if c == ' ' || c == '\t' {
args = append(args, current)
current = ""
state = "start"
} else {
current += string(c)
}
continue
}
if c != ' ' && c != '\t' {
state = "arg"
current += string(c)
}
}
if state == "quotes" {
return "", []string{}, errors.New(fmt.Sprintf("Unclosed quote in command line: %s", cmd))
}
if current != "" {
args = append(args, current)
}
if len(args) <= 0 {
return "", []string{}, errors.New("Empty command line")
}
if len(args) == 1 {
return args[0], []string{}, nil
}
return args[0], args[1:], nil
}
开发者ID:yext,项目名称:edward,代码行数:62,代码来源:runner.go
示例2: doUpdate
func (rancherClient *RancherBaseClientImpl) doUpdate(schemaType string, existing *Resource, updates interface{}, respObject interface{}) error {
if existing == nil {
return errors.New("Existing object is nil")
}
selfUrl, ok := existing.Links[SELF]
if !ok {
return errors.New(fmt.Sprintf("Failed to find self URL of [%v]", existing))
}
if updates == nil {
updates = map[string]string{}
}
if respObject == nil {
respObject = &map[string]interface{}{}
}
schema, ok := rancherClient.Types[schemaType]
if !ok {
return errors.New("Unknown schema type [" + schemaType + "]")
}
if !contains(schema.ResourceMethods, "PUT") {
return errors.New("Resource type [" + schemaType + "] is not updatable")
}
return rancherClient.doModify("PUT", selfUrl, updates, respObject)
}
开发者ID:vdemeester,项目名称:rancher-compose,代码行数:29,代码来源:common.go
示例3: StatVFS
// StatVFS retrieves VFS statistics from a remote host.
//
// It implements the [email protected] SSH_FXP_EXTENDED feature
// from http://www.opensource.apple.com/source/OpenSSH/OpenSSH-175/openssh/PROTOCOL?txt.
func (c *Client) StatVFS(path string) (*StatVFS, error) {
// send the StatVFS packet to the server
id := c.nextID()
typ, data, err := c.sendPacket(sshFxpStatvfsPacket{
ID: id,
Path: path,
})
if err != nil {
return nil, err
}
switch typ {
// server responded with valid data
case ssh_FXP_EXTENDED_REPLY:
var response StatVFS
err = binary.Read(bytes.NewReader(data), binary.BigEndian, &response)
if err != nil {
return nil, errors.New("can not parse reply")
}
return &response, nil
// the resquest failed
case ssh_FXP_STATUS:
return nil, errors.New(fxp(ssh_FXP_STATUS).String())
default:
return nil, unimplementedPacketErr(typ)
}
}
开发者ID:kubernetes,项目名称:kubernetes,代码行数:34,代码来源:client.go
示例4: WaitForCluster
// WaitForCluster waits until leader will be one of specified nodes
func WaitForCluster(t *testing.T, clockSource *fakeclock.FakeClock, nodes map[uint64]*TestNode) {
err := PollFunc(clockSource, func() error {
var prev *etcdraft.Status
nodeLoop:
for _, n := range nodes {
if prev == nil {
prev = new(etcdraft.Status)
*prev = n.Status()
for _, n2 := range nodes {
if n2.Config.ID == prev.Lead && n2.ReadyForProposals() {
continue nodeLoop
}
}
return errors.New("did not find a ready leader in member list")
}
cur := n.Status()
for _, n2 := range nodes {
if n2.Config.ID == cur.Lead {
if cur.Lead != prev.Lead || cur.Term != prev.Term || cur.Applied != prev.Applied {
return errors.New("state does not match on all nodes")
}
continue nodeLoop
}
}
return errors.New("did not find leader in member list")
}
return nil
})
require.NoError(t, err)
}
开发者ID:yongtang,项目名称:swarmkit,代码行数:32,代码来源:testutils.go
示例5: UnlockSwarm
// UnlockSwarm provides a key to decrypt data that is encrypted at rest.
func (c *Cluster) UnlockSwarm(req types.UnlockRequest) error {
key, err := encryption.ParseHumanReadableKey(req.UnlockKey)
if err != nil {
return err
}
c.Lock()
if c.node != nil || c.locked != true {
c.Unlock()
return errors.New("swarm is not locked")
}
config := *c.lastNodeConfig
config.lockKey = key
n, err := c.startNewNode(config)
if err != nil {
c.Unlock()
return err
}
c.Unlock()
select {
case <-n.Ready():
case <-n.done:
if errors.Cause(c.err) == ErrSwarmLocked {
return errors.New("swarm could not be unlocked: invalid key provided")
}
return fmt.Errorf("swarm component could not be started: %v", c.err)
}
go c.reconnectOnFailure(n)
return nil
}
开发者ID:djs55,项目名称:docker,代码行数:32,代码来源:cluster.go
示例6: FindLeaf
// FindLeaf finds a directory of name leaf in the folder with ID pathID
func (f *Fs) FindLeaf(pathID, leaf string) (pathIDOut string, found bool, err error) {
// fs.Debug(f, "FindLeaf(%q, %q)", pathID, leaf)
parent, ok := f.dirCache.GetInv(pathID)
if !ok {
return "", false, errors.New("couldn't find parent ID")
}
path := leaf
if parent != "" {
path = parent + "/" + path
}
if f.dirCache.FoundRoot() {
path = f.rootSlash() + path
}
info, resp, err := f.readMetaDataForPath(path)
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
return "", false, nil
}
return "", false, err
}
if info.Folder == nil {
return "", false, errors.New("found file when looking for folder")
}
return info.ID, true, nil
}
开发者ID:pombredanne,项目名称:rclone,代码行数:26,代码来源:onedrive.go
示例7: RenewTLSConfig
// RenewTLSConfig will continuously monitor for the necessity of renewing the local certificates, either by
// issuing them locally if key-material is available, or requesting them from a remote CA.
func RenewTLSConfig(ctx context.Context, s *SecurityConfig, remotes remotes.Remotes, renew <-chan struct{}) <-chan CertificateUpdate {
updates := make(chan CertificateUpdate)
go func() {
var retry time.Duration
defer close(updates)
for {
ctx = log.WithModule(ctx, "tls")
log := log.G(ctx).WithFields(logrus.Fields{
"node.id": s.ClientTLSCreds.NodeID(),
"node.role": s.ClientTLSCreds.Role(),
})
// Our starting default will be 5 minutes
retry = 5 * time.Minute
// Since the expiration of the certificate is managed remotely we should update our
// retry timer on every iteration of this loop.
// Retrieve the current certificate expiration information.
validFrom, validUntil, err := readCertValidity(s.KeyReader())
if err != nil {
// We failed to read the expiration, let's stick with the starting default
log.Errorf("failed to read the expiration of the TLS certificate in: %s", s.KeyReader().Target())
updates <- CertificateUpdate{Err: errors.New("failed to read certificate expiration")}
} else {
// If we have an expired certificate, we let's stick with the starting default in
// the hope that this is a temporary clock skew.
if validUntil.Before(time.Now()) {
log.WithError(err).Errorf("failed to create a new client TLS config")
updates <- CertificateUpdate{Err: errors.New("TLS certificate is expired")}
} else {
// Random retry time between 50% and 80% of the total time to expiration
retry = calculateRandomExpiry(validFrom, validUntil)
}
}
log.WithFields(logrus.Fields{
"time": time.Now().Add(retry),
}).Debugf("next certificate renewal scheduled")
select {
case <-time.After(retry):
log.Infof("renewing certificate")
case <-renew:
log.Infof("forced certificate renewal")
case <-ctx.Done():
log.Infof("shuting down certificate renewal routine")
return
}
// ignore errors - it will just try again laster
if err := RenewTLSConfigNow(ctx, s, remotes); err != nil {
updates <- CertificateUpdate{Err: err}
} else {
updates <- CertificateUpdate{Role: s.ClientTLSCreds.Role()}
}
}
}()
return updates
}
开发者ID:ollie314,项目名称:docker,代码行数:62,代码来源:config.go
示例8: Set
// Parse string of the form "db"."rp" where the quotes are optional but can include escaped quotes
// within the strings.
func (d *dbrps) Set(value string) error {
dbrp := client.DBRP{}
if len(value) == 0 {
return errors.New("dbrp cannot be empty")
}
var n int
if value[0] == '"' {
dbrp.Database, n = parseQuotedStr(value)
} else {
n = strings.IndexRune(value, '.')
if n == -1 {
return errors.New("does not contain a '.', it must be in the form \"dbname\".\"rpname\" where the quotes are optional.")
}
dbrp.Database = value[:n]
}
if value[n] != '.' {
return errors.New("dbrp must specify retention policy, do you have a missing or extra '.'?")
}
value = value[n+1:]
if value[0] == '"' {
dbrp.RetentionPolicy, n = parseQuotedStr(value)
} else {
dbrp.RetentionPolicy = value
}
*d = append(*d, dbrp)
return nil
}
开发者ID:wutaizeng,项目名称:kapacitor,代码行数:29,代码来源:main.go
示例9: write
func (av *Writer) write(updates []parser.UpdateData) error {
av.availableUpdates = updates
// write temporary header (we need to know the size before storing in tar)
if err := av.WriteHeader(); err != nil {
return err
}
// archive info
info := av.getInfo()
ia := archiver.NewMetadataArchiver(&info, "info")
if err := ia.Archive(av.aArchiver); err != nil {
return errors.Wrapf(err, "writer: error archiving info")
}
// archive header
ha := archiver.NewFileArchiver(av.hTmpFile.Name(), "header.tar.gz")
if err := ha.Archive(av.aArchiver); err != nil {
return errors.Wrapf(err, "writer: error archiving header")
}
// archive data
if err := av.WriteData(); err != nil {
return err
}
// we've been storing everything in temporary file
if err := av.aArchiver.Close(); err != nil {
return errors.New("writer: error closing archive")
}
// prevent from closing archiver twice
av.aArchiver = nil
if err := av.aTmpFile.Close(); err != nil {
return errors.New("writer: error closing archive temporary file")
}
return os.Rename(av.aTmpFile.Name(), av.aName)
}
开发者ID:pasinskim,项目名称:mender,代码行数:35,代码来源:writer.go
示例10: FetchUpdate
// Returns a byte stream which is a download of the given link.
func (u *UpdateClient) FetchUpdate(api ApiRequester, url string) (io.ReadCloser, int64, error) {
req, err := makeUpdateFetchRequest(url)
if err != nil {
return nil, -1, errors.Wrapf(err, "failed to create update fetch request")
}
r, err := api.Do(req)
if err != nil {
log.Error("Can not fetch update image: ", err)
return nil, -1, errors.Wrapf(err, "update fetch request failed")
}
log.Debugf("Received fetch update response %v+", r)
if r.StatusCode != http.StatusOK {
r.Body.Close()
log.Errorf("Error fetching shcheduled update info: code (%d)", r.StatusCode)
return nil, -1, errors.New("Error receiving scheduled update information.")
}
if r.ContentLength < 0 {
r.Body.Close()
return nil, -1, errors.New("Will not continue with unknown image size.")
} else if r.ContentLength < u.minImageSize {
r.Body.Close()
log.Errorf("Image smaller than expected. Expected: %d, received: %d", u.minImageSize, r.ContentLength)
return nil, -1, errors.New("Image size is smaller than expected. Aborting.")
}
return r.Body, r.ContentLength, nil
}
开发者ID:pasinskim,项目名称:mender,代码行数:32,代码来源:client_update.go
示例11: purgeCheck
// purgeCheck removes the root directory, if check is set then it
// refuses to do so if it has anything in
func (f *Fs) purgeCheck(dir string, check bool) error {
root := path.Join(f.root, dir)
if root == "" {
return errors.New("can't purge root directory")
}
dc := f.dirCache
err := dc.FindRoot(false)
if err != nil {
return err
}
rootID, err := dc.FindDir(dir, false)
if err != nil {
return err
}
item, _, err := f.readMetaDataForPath(root)
if err != nil {
return err
}
if item.Folder == nil {
return errors.New("not a folder")
}
if check && item.Folder.ChildCount != 0 {
return errors.New("folder not empty")
}
err = f.deleteObject(rootID)
if err != nil {
return err
}
f.dirCache.FlushDir(dir)
if err != nil {
return err
}
return nil
}
开发者ID:ncw,项目名称:rclone,代码行数:36,代码来源:onedrive.go
示例12: execQuery
func (s *Service) execQuery(q, cluster string) (kapacitor.DBRP, *influxdb.Response, error) {
// Parse query to determine dbrp
dbrp := kapacitor.DBRP{}
stmt, err := influxql.ParseStatement(q)
if err != nil {
return dbrp, nil, err
}
if slct, ok := stmt.(*influxql.SelectStatement); ok && len(slct.Sources) == 1 {
if m, ok := slct.Sources[0].(*influxql.Measurement); ok {
dbrp.Database = m.Database
dbrp.RetentionPolicy = m.RetentionPolicy
}
}
if dbrp.Database == "" || dbrp.RetentionPolicy == "" {
return dbrp, nil, errors.New("could not determine database and retention policy. Is the query fully qualified?")
}
if s.InfluxDBService == nil {
return dbrp, nil, errors.New("InfluxDB not configured, cannot record query")
}
// Query InfluxDB
con, err := s.InfluxDBService.NewNamedClient(cluster)
if err != nil {
return dbrp, nil, errors.Wrap(err, "failed to get InfluxDB client")
}
query := influxdb.Query{
Command: q,
}
resp, err := con.Query(query)
if err != nil {
return dbrp, nil, errors.Wrap(err, "InfluxDB query failed")
}
return dbrp, resp, nil
}
开发者ID:influxdata,项目名称:kapacitor,代码行数:33,代码来源:service.go
示例13: commonInit
func commonInit(config *menderConfig, opts *runOptionsType) (*MenderPieces, error) {
tentok, err := loadTenantToken(*opts.dataStore)
if err != nil {
return nil, errors.Wrapf(err, "failed to load tenant token")
}
ks := getKeyStore(*opts.dataStore, config.DeviceKey)
if ks == nil {
return nil, errors.New("failed to setup key storage")
}
dbstore := NewDBStore(*opts.dataStore)
if dbstore == nil {
return nil, errors.New("failed to initialize DB store")
}
authmgr := NewAuthManager(AuthManagerConfig{
AuthDataStore: dbstore,
KeyStore: ks,
IdentitySource: NewIdentityDataGetter(),
TenantToken: tentok,
})
if authmgr == nil {
// close DB store explicitly
dbstore.Close()
return nil, errors.New("error initializing authentication manager")
}
mp := MenderPieces{
store: dbstore,
authMgr: authmgr,
}
return &mp, nil
}
开发者ID:pasinskim,项目名称:mender,代码行数:34,代码来源:main.go
示例14: newSyncCopyMove
func newSyncCopyMove(fdst, fsrc Fs, Delete bool, DoMove bool) (*syncCopyMove, error) {
s := &syncCopyMove{
fdst: fdst,
fsrc: fsrc,
Delete: Delete,
DoMove: DoMove,
dir: "",
srcFilesChan: make(chan Object, Config.Checkers+Config.Transfers),
srcFilesResult: make(chan error, 1),
dstFilesResult: make(chan error, 1),
noTraverse: Config.NoTraverse,
abort: make(chan struct{}),
toBeChecked: make(ObjectPairChan, Config.Transfers),
toBeUploaded: make(ObjectPairChan, Config.Transfers),
deleteBefore: Delete && Config.DeleteBefore,
trackRenames: Config.TrackRenames,
commonHash: fsrc.Hashes().Overlap(fdst.Hashes()).GetOne(),
toBeRenamed: make(ObjectPairChan, Config.Transfers),
}
if s.noTraverse && s.Delete {
Debug(s.fdst, "Ignoring --no-traverse with sync")
s.noTraverse = false
}
if s.trackRenames {
// Don't track renames for remotes without server-side move support.
if !CanServerSideMove(fdst) {
ErrorLog(fdst, "Ignoring --track-renames as the destination does not support server-side move or copy")
s.trackRenames = false
}
if s.commonHash == HashNone {
ErrorLog(fdst, "Ignoring --track-renames as the source and destination do not have a common hash")
s.trackRenames = false
}
}
if s.noTraverse && s.trackRenames {
Debug(s.fdst, "Ignoring --no-traverse with --track-renames")
s.noTraverse = false
}
// Make Fs for --backup-dir if required
if Config.BackupDir != "" {
var err error
s.backupDir, err = NewFs(Config.BackupDir)
if err != nil {
return nil, FatalError(errors.Errorf("Failed to make fs for --backup-dir %q: %v", Config.BackupDir, err))
}
if !CanServerSideMove(s.backupDir) {
return nil, FatalError(errors.New("can't use --backup-dir on a remote which doesn't support server side move or copy"))
}
if !SameConfig(fdst, s.backupDir) {
return nil, FatalError(errors.New("parameter to --backup-dir has to be on the same remote as destination"))
}
if Overlapping(fdst, s.backupDir) {
return nil, FatalError(errors.New("destination and parameter to --backup-dir mustn't overlap"))
}
if Overlapping(fsrc, s.backupDir) {
return nil, FatalError(errors.New("source and parameter to --backup-dir mustn't overlap"))
}
}
return s, nil
}
开发者ID:ncw,项目名称:rclone,代码行数:60,代码来源:sync.go
示例15: Run
func (t *targetSet) Run(ctx *Context, client *Client) error {
if len(ctx.Args) != 1 {
return errors.New("Invalid arguments")
}
targetLabelToSet := strings.TrimSpace(ctx.Args[0])
labelExist, err := CheckIfTargetLabelExists(targetLabelToSet)
if err != nil {
return err
}
if !labelExist {
return errors.New("Target not found")
}
targets, err := getTargets()
if err != nil {
return err
}
for label, target := range targets {
if label == targetLabelToSet {
err = WriteTarget(target)
if err != nil {
return err
}
fmt.Fprintf(ctx.Stdout, "New target is %s -> %s\n", label, target)
}
}
return nil
}
开发者ID:tsuru,项目名称:tsuru,代码行数:27,代码来源:target.go
示例16: FindRangeLeaseHolder
// FindRangeLeaseHolder is part of TestClusterInterface.
func (tc *TestCluster) FindRangeLeaseHolder(
rangeDesc roachpb.RangeDescriptor, hint *base.ReplicationTarget,
) (base.ReplicationTarget, error) {
lease, now, err := tc.FindRangeLease(rangeDesc, hint)
if err != nil {
return base.ReplicationTarget{}, err
}
if lease == nil {
return base.ReplicationTarget{}, errors.New("no active lease")
}
// Find lease replica in order to examine the lease state.
store, err := tc.findMemberStore(lease.Replica.StoreID)
if err != nil {
return base.ReplicationTarget{}, err
}
replica, err := store.GetReplica(rangeDesc.RangeID)
if err != nil {
return base.ReplicationTarget{}, err
}
if !replica.IsLeaseValid(lease, now) {
return base.ReplicationTarget{}, errors.New("no valid lease")
}
replicaDesc := lease.Replica
return base.ReplicationTarget{NodeID: replicaDesc.NodeID, StoreID: replicaDesc.StoreID}, nil
}
开发者ID:BramGruneir,项目名称:cockroach,代码行数:26,代码来源:testcluster.go
示例17: newUserSelectedOTPLDeploySpecs
func newUserSelectedOTPLDeploySpecs(detected DetectedOTPLDeploySpecs, tmid TargetManifestID, flags *config.OTPLFlags, state *sous.State) (UserSelectedOTPLDeploySpecs, error) {
var nowt UserSelectedOTPLDeploySpecs
mid := sous.ManifestID(tmid)
// we don't care about these flags when a manifest already exists
if _, ok := state.Manifests.Get(mid); ok {
return nowt, nil
}
if !flags.UseOTPLDeploy && !flags.IgnoreOTPLDeploy && len(detected.DeploySpecs) != 0 {
return nowt, errors.New("otpl-deploy detected in config/, please specify either -use-otpl-deploy, or -ignore-otpl-deploy to proceed")
}
if !flags.UseOTPLDeploy {
return nowt, nil
}
if len(detected.DeploySpecs) == 0 {
return nowt, errors.New("use of otpl configuration was specified, but no valid deployments were found in config/")
}
deploySpecs := sous.DeploySpecs{}
for clusterName, spec := range detected.DeploySpecs {
if _, ok := state.Defs.Clusters[clusterName]; !ok {
sous.Log.Warn.Printf("otpl-deploy config for cluster %q ignored", clusterName)
continue
}
deploySpecs[clusterName] = spec
}
return UserSelectedOTPLDeploySpecs{deploySpecs}, nil
}
开发者ID:opentable,项目名称:sous,代码行数:26,代码来源:otpldeploy.go
示例18: getTLSConfig
// getTLSConfig creates a tls.Config object from the given certs, key, and CA files.
// you must give the full path to the files.
func getTLSConfig(
SSLCA, SSLCert, SSLKey string,
InsecureSkipVerify bool,
) (*tls.Config, error) {
t := &tls.Config{
InsecureSkipVerify: InsecureSkipVerify,
}
if SSLCert != "" && SSLKey != "" {
cert, err := tls.LoadX509KeyPair(SSLCert, SSLKey)
if err != nil {
return nil, fmt.Errorf(
"Could not load TLS client key/certificate: %s",
err)
}
t.Certificates = []tls.Certificate{cert}
} else if SSLCert != "" {
return nil, errors.New("Must provide both key and cert files: only cert file provided.")
} else if SSLKey != "" {
return nil, errors.New("Must provide both key and cert files: only key file provided.")
}
if SSLCA != "" {
caCert, err := ioutil.ReadFile(SSLCA)
if err != nil {
return nil, fmt.Errorf("Could not load TLS CA: %s",
err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
t.RootCAs = caCertPool
}
return t, nil
}
开发者ID:influxdata,项目名称:kapacitor,代码行数:35,代码来源:service.go
示例19: purgeCheck
// purgeCheck removes the root directory, if check is set then it
// refuses to do so if it has anything in
func (f *Fs) purgeCheck(check bool) error {
if f.root == "" {
return errors.New("can't purge root directory")
}
dc := f.dirCache
err := dc.FindRoot(false)
if err != nil {
return err
}
rootID := dc.RootID()
item, _, err := f.readMetaDataForPath(f.root)
if err != nil {
return err
}
if item.Folder == nil {
return errors.New("not a folder")
}
if check && item.Folder.ChildCount != 0 {
return errors.New("folder not empty")
}
err = f.deleteObject(rootID)
if err != nil {
return err
}
f.dirCache.ResetRoot()
if err != nil {
return err
}
return nil
}
开发者ID:pombredanne,项目名称:rclone,代码行数:32,代码来源:onedrive.go
示例20: Shell
func (p *FakeProvisioner) Shell(opts provision.ShellOptions) error {
var unit provision.Unit
units, err := p.Units(opts.App)
if err != nil {
return err
}
if len(units) == 0 {
return errors.New("app has no units")
} else if opts.Unit != "" {
for _, u := range units {
if u.ID == opts.Unit {
unit = u
break
}
}
} else {
unit = units[0]
}
if unit.ID == "" {
return errors.New("unit not found")
}
p.shellMut.Lock()
defer p.shellMut.Unlock()
p.shells[unit.ID] = append(p.shells[unit.ID], opts)
return nil
}
开发者ID:tsuru,项目名称:tsuru,代码行数:26,代码来源:fake_provisioner.go
注:本文中的github.com/pkg/errors.New函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论