blob: d3c999784ab6f29b826ea48251eb780f6be1839c [file] [edit]
// Copyright 2026 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package androidtools
import (
"context"
"unicode"
"go.chromium.org/infra/cros/recovery/internal/log"
)
// The Response base interface describes responses.
type Response interface {
GetStdout() []byte
GetStderr() []byte
GetExitCode() int32
}
// Local implementation of Response interface.
type responseImp struct {
Stdout []byte
Stderr []byte
ExitCode int32
}
func (r *responseImp) GetStderr() []byte {
return r.Stderr
}
func (r *responseImp) GetStdout() []byte {
return r.Stdout
}
func (r *responseImp) GetExitCode() int32 {
return r.ExitCode
}
func emptyResponse() *responseImp {
return &responseImp{}
}
const (
serialParam = "-s"
connectCommand = "connect"
disconnectCommand = "disconnect"
devicesCommand = "devices"
shellCommand = "shell"
rootCommand = "root"
)
// truncateString truncates a string to a max length.
func truncateString(str string, max int) string {
if str == "" || len(str) <= max {
return str
}
lastSpaceIx := -1
for i, r := range str {
if unicode.IsSpace(r) {
lastSpaceIx = i
}
// We stop when max is reached.
if i+1 >= max {
break
}
}
// If a space is found, then we cut at the last one.
if lastSpaceIx != -1 {
return str[:lastSpaceIx] + "..."
}
// If there are no spaces, then we just cut at max.
return str[:max]
}
func printResponse(ctx context.Context, res Response) {
log.Debugf(ctx, "STDOUT: %s", truncateString(string(res.GetStdout()), 50))
log.Debugf(ctx, "STDERR: %s", truncateString(string(res.GetStderr()), 50))
log.Debugf(ctx, "EXITCODE: %v", res.GetExitCode())
}