Beispiel #1
0
// PromptPasswordMaxTries tries to read a password maxTries times.
//
// The typed password can be validated by the caller via the passfn function.
// If the user failed to pass the correct password, ErrTooManyTries is returned.
// For visual guidance the prompt color will gradually change from green to red
// with each failed try.
func PromptPasswordMaxTries(maxTries int, passfn func(string) bool) (string, error) {
	for i := 0; i < maxTries; i++ {
		color := triesToColor[util.Min(i, len(triesToColor))]
		pwd, err := promptPasswordColored(color)
		if err != nil {
			return "", err
		}

		if !passfn(pwd) {
			continue
		}

		return pwd, err
	}

	return "", ErrTooManyTries{maxTries}
}
Beispiel #2
0
func (w *writer) Write(p []byte) (n int, err error) {
	if err := w.writeHeaderIfNeeded(); err != nil {
		return 0, err
	}

	written := len(p)
	// Compress only maxChunkSize equal chunks.
	for {
		n, _ := w.chunkBuf.Write(p[:util.Min(len(p), maxChunkSize)])

		if w.chunkBuf.Len() < maxChunkSize {
			break
		}

		if err := w.flushBuffer(w.chunkBuf.Next(maxChunkSize)); err != nil {
			return 0, err
		}
		p = p[n:]
	}
	return written, nil
}