blob: b1bc316746ce4e13da4637762259c0bd843349c5 [file] [log] [blame]
// Copyright 2020 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Package cmd contains the interface for running commands in packages such as iw and ping.
package cmd
import (
"context"
"chromiumos/tast/common/network/cmd"
"chromiumos/tast/ssh"
)
const logName = "cmdOutput.txt"
// RemoteCmdRunner is the object used for running remote commands.
type RemoteCmdRunner struct {
Host *ssh.Conn
NoLogOnError bool // Default false: dump log on error.
}
var _ cmd.Runner = (*RemoteCmdRunner)(nil)
// Run runs a command and waits for its completion.
func (r *RemoteCmdRunner) Run(ctx context.Context, cmd string, args ...string) error {
_, err := r.Output(ctx, cmd, args...)
return err
}
// Output runs a command, waits for its completion and returns stdout output of the command.
func (r *RemoteCmdRunner) Output(ctx context.Context, cmd string, args ...string) ([]byte, error) {
cc := r.Host.Command(cmd, args...)
if r.NoLogOnError {
return cc.Output(ctx)
}
return cc.Output(ctx, ssh.DumpLogOnError)
}