Skip to content

Commit

Permalink
Fix many golint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
0405ysj committed Dec 16, 2024
1 parent 2fb78e8 commit 75f9960
Show file tree
Hide file tree
Showing 24 changed files with 132 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ func TestInstance(t *testing.T) {
if err := common.UploadAndExtract(srv, uploadDir, "../artifacts/cvd-host_package.tar.gz"); err != nil {
t.Fatal(err)
}
const group_name = "foo"
const groupName = "foo"
config := `
{
"common": {
"group_name": "` + group_name + `",
"group_name": "` + groupName + `",
"host_package": "@user_artifacts/` + uploadDir + `"
},
Expand Down Expand Up @@ -79,7 +79,7 @@ func TestInstance(t *testing.T) {

got, createErr := srv.CreateCVD(createReq, &hoclient.AccessTokenBuildAPICreds{})

if err := common.DownloadHostBugReport(srv, group_name); err != nil {
if err := common.DownloadHostBugReport(srv, groupName); err != nil {
t.Errorf("failed creating bugreport: %s\n", err)
}
if createErr != nil {
Expand Down
10 changes: 5 additions & 5 deletions e2etests/orchestration/create_local_image_test/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ func TestInstance(t *testing.T) {
if err := common.UploadAndExtract(srv, uploadDir, "../artifacts/cvd-host_package.tar.gz"); err != nil {
t.Fatal(err)
}
const group_name = "foo"
const groupName = "foo"
config := `
{
"common": {
"group_name": "` + group_name + `",
"group_name": "` + groupName + `",
"host_package": "@user_artifacts/` + uploadDir + `"
},
"instances": [
Expand Down Expand Up @@ -84,19 +84,19 @@ func TestInstance(t *testing.T) {

got, createErr := srv.CreateCVD(createReq, &hoclient.AccessTokenBuildAPICreds{})

if err := common.DownloadHostBugReport(srv, group_name); err != nil {
if err := common.DownloadHostBugReport(srv, groupName); err != nil {
t.Errorf("failed creating bugreport: %s\n", err)
}
if createErr != nil {
t.Fatal(createErr)
}
if err := common.VerifyLogsEndpoint(ctx.ServiceURL, group_name, "1"); err != nil {
if err := common.VerifyLogsEndpoint(ctx.ServiceURL, groupName, "1"); err != nil {
t.Fatalf("failed verifying /logs endpoint: %s", err)
}
want := &hoapi.CreateCVDResponse{
CVDs: []*hoapi.CVD{
&hoapi.CVD{
Group: group_name,
Group: groupName,
Name: "1",
BuildSource: &hoapi.BuildSource{},
Status: "Running",
Expand Down
6 changes: 3 additions & 3 deletions e2etests/orchestration/snapshot_test/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ func uploadArtifacts(srv hoclient.HostOrchestratorService) (string, error) {
return uploadDir, nil
}

func createDevice(srv hoclient.HostOrchestratorService, group_name, artifactsDir string) (*hoapi.CVD, error) {
func createDevice(srv hoclient.HostOrchestratorService, groupName, artifactsDir string) (*hoapi.CVD, error) {
config := `
{
"common": {
"group_name": "` + group_name + `",
"group_name": "` + groupName + `",
"host_package": "@user_artifacts/` + artifactsDir + `"
},
"instances": [
Expand All @@ -155,7 +155,7 @@ func createDevice(srv hoclient.HostOrchestratorService, group_name, artifactsDir
createReq := &hoapi.CreateCVDRequest{EnvConfig: envConfig}
res, createErr := srv.CreateCVD(createReq, &hoclient.AccessTokenBuildAPICreds{})
if createErr != nil {
if err := common.DownloadHostBugReport(srv, group_name); err != nil {
if err := common.DownloadHostBugReport(srv, groupName); err != nil {
log.Printf("error downloading cvd bugreport: %v", err)
}
return nil, createErr
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/host_orchestrator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ func defaultCVDArtifactsDir() string {
return fmt.Sprintf("/tmp/cvd/%s/artifacts", u.Uid)
}

func startHttpServer(addr string, port int) error {
func startHTTPServer(addr string, port int) error {
log.Printf("Host Orchestrator is listening at http://%s:%d", addr, port)

// handler is nil, so DefaultServeMux is used.
return http.ListenAndServe(fmt.Sprintf("%s:%d", addr, port), nil)
}

func startHttpsServer(addr string, port int, certPath string, keyPath string) error {
func startHTTPSServer(addr string, port int, certPath string, keyPath string) error {
log.Printf("Host Orchestrator is listening at https://%s:%d", addr, port)
return http.ListenAndServeTLS(fmt.Sprintf("%s:%d", addr, port),
certPath,
Expand Down Expand Up @@ -174,7 +174,7 @@ func main() {
http.Handle("/", r)

starters := []func() error{
func() error { return startHttpServer(*address, *httpPort) },
func() error { return startHTTPServer(*address, *httpPort) },
}
start(starters)
}
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,8 @@ func createNewDir(dir string) error {
}

func createDir(dir string) error {
if err := createNewDir(dir); os.IsExist(err) {
return nil
} else {
if err := createNewDir(dir); !os.IsExist(err) {
return err
}
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ func (s *AndroidCIBuildAPI) GetLatestGreenBuildID(branch, target string) (string
url := fmt.Sprintf(format, s.BaseURL, url.PathEscape(branch), url.PathEscape(target))
res := listBuildResponse{}
if err := s.doGETToJSON(url, &res); err != nil {
return "", fmt.Errorf("Failed to get the latest green build id for `%s/%s`: %w", branch, target, err)
return "", fmt.Errorf("failed to get the latest green build id for `%s/%s`: %w", branch, target, err)
}
if len(res.Builds) != 1 {
return "", fmt.Errorf("Unexpected number of build: expected 1 and got %d", len(res.Builds))
return "", fmt.Errorf("unexpected number of build: expected 1 and got %d", len(res.Builds))
}
return res.Builds[0].BuildID, nil
}
Expand All @@ -99,7 +99,7 @@ func (s *AndroidCIBuildAPI) getSignedURL(name, buildID, target string) (string,
SignedURL string `json:"signedUrl"`
}{}
if err := s.doGETToJSON(url, &res); err != nil {
return "", fmt.Errorf("Failed to get the download artifact signed url for %q (%s/%s): %w", name, buildID, target, err)
return "", fmt.Errorf("failed to get the download artifact signed url for %q (%s/%s): %w", name, buildID, target, err)
}
return res.SignedURL, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type CreateCVDAction struct {
hostValidator Validator
paths IMPaths
om OperationManager
execContext cvd.CVDExecContext
execContext cvd.ExecContext
buildAPI artifacts.BuildAPI
artifactsFetcher artifacts.Fetcher
cvdBundleFetcher artifacts.CVDBundleFetcher
Expand Down Expand Up @@ -337,7 +337,7 @@ func createTempFile(pattern string, data []byte, mode os.FileMode) (*os.File, er
}

// Create the credential file so it's owned by the configured `cvd user`, e.g: `_cvd-executor`.
func createCredsFile(ctx cvd.CVDExecContext) (string, error) {
func createCredsFile(ctx cvd.ExecContext) (string, error) {
name, err := tempFilename("cvdload*.creds")
if err != nil {
return "", err
Expand All @@ -352,7 +352,7 @@ func createCredsFile(ctx cvd.CVDExecContext) (string, error) {
}

// Write into credential files by granting temporary write permission to `cvdnetwork` group.
func writeCredsFile(ctx cvd.CVDExecContext, name string, data []byte) error {
func writeCredsFile(ctx cvd.ExecContext, name string, data []byte) error {
info, err := os.Stat(name)
if err != nil {
return err
Expand Down Expand Up @@ -394,7 +394,7 @@ func writeCredsFile(ctx cvd.CVDExecContext, name string, data []byte) error {
return nil
}

func removeCredsFile(ctx cvd.CVDExecContext, name string) error {
func removeCredsFile(ctx cvd.ExecContext, name string) error {
_, err := cvd.Exec(ctx, "rm", name)
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ type CreateCVDBugReportActionOpts struct {
Group string
Paths IMPaths
OperationManager OperationManager
ExecContext cvd.CVDExecContext
ExecContext cvd.ExecContext
UUIDGen func() string
}

type CreateCVDBugReportAction struct {
group string
paths IMPaths
om OperationManager
execContext cvd.CVDExecContext
execContext cvd.ExecContext
uuidgen func() string
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type CreateSnapshotAction struct {
selector CVDSelector
paths IMPaths
om OperationManager
execContext cvd.CVDExecContext
execContext cvd.ExecContext
}

func NewCreateSnapshotAction(opts CreateSnapshotActionOpts) *CreateSnapshotAction {
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/host_orchestrator/orchestrator/cvd/cvd.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"strings"
)

type CVDExecContext = func(ctx context.Context, env []string, name string, arg ...string) *exec.Cmd
type ExecContext = func(ctx context.Context, env []string, name string, arg ...string) *exec.Cmd

const (
CVDBin = "/usr/bin/cvd"
Expand All @@ -43,13 +43,13 @@ type CommandOpts struct {
}

type Command struct {
execContext CVDExecContext
execContext ExecContext
cvdBin string
args []string
opts CommandOpts
}

func NewCommand(execContext CVDExecContext, args []string, opts CommandOpts) *Command {
func NewCommand(execContext ExecContext, args []string, opts CommandOpts) *Command {
return &Command{
execContext: execContext,
cvdBin: CVDBin,
Expand Down Expand Up @@ -117,7 +117,7 @@ func LogCombinedStdoutStderr(cmd *exec.Cmd, val string) {
log.Printf(msg, strings.Join(cmd.Args, " "), OutputLogMessage(val))
}

func Exec(ctx CVDExecContext, name string, args ...string) (string, error) {
func Exec(ctx ExecContext, name string, args ...string) (string, error) {
cmd := ctx(context.TODO(), nil, name, args...)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type ExecCVDCommandAction struct {
selector CVDSelector
paths IMPaths
om OperationManager
execContext cvd.CVDExecContext
execContext cvd.ExecContext
}

func NewExecCVDCommandAction(opts ExecCVDCommandActionOpts) *ExecCVDCommandAction {
Expand Down
25 changes: 12 additions & 13 deletions frontend/src/host_orchestrator/orchestrator/instancemanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (s *CVDSelector) ToCVDCLI() []string {

// Creates a CVD execution context from a regular execution context.
// If a non-nil user is provided the returned execution context executes commands as that user.
func newCVDExecContext(execContext ExecContext, usr *user.User) cvd.CVDExecContext {
func newCVDExecContext(execContext ExecContext, usr *user.User) cvd.ExecContext {
if usr != nil {
return func(ctx context.Context, env []string, name string, arg ...string) *exec.Cmd {
newArgs := []string{"-u", usr.Username}
Expand Down Expand Up @@ -156,7 +156,7 @@ func (i *cvdInstance) toAPIObject(group string) *apiv1.CVD {
}
}

func cvdFleet(ctx cvd.CVDExecContext) (*cvdFleetOutput, error) {
func cvdFleet(ctx cvd.ExecContext) (*cvdFleetOutput, error) {
stdout := &bytes.Buffer{}
cvdCmd := cvd.NewCommand(ctx, []string{"fleet"}, cvd.CommandOpts{Stdout: stdout})
err := cvdCmd.Run()
Expand All @@ -173,7 +173,7 @@ func cvdFleet(ctx cvd.CVDExecContext) (*cvdFleetOutput, error) {

// Helper for listing first group instances only. Legacy flows didn't have a multi-group environment hence using
// the first group only.
func cvdFleetFirstGroup(ctx cvd.CVDExecContext) (*cvdGroup, error) {
func cvdFleetFirstGroup(ctx cvd.ExecContext) (*cvdGroup, error) {
fleet, err := cvdFleet(ctx)
if err != nil {
return nil, err
Expand All @@ -184,7 +184,7 @@ func cvdFleetFirstGroup(ctx cvd.CVDExecContext) (*cvdGroup, error) {
return fleet.Groups[0], nil
}

func CVDLogsDir(ctx cvd.CVDExecContext, groupName, name string) (string, error) {
func CVDLogsDir(ctx cvd.ExecContext, groupName, name string) (string, error) {
fleet, err := cvdFleet(ctx)
if err != nil {
return "", err
Expand All @@ -200,7 +200,7 @@ func CVDLogsDir(ctx cvd.CVDExecContext, groupName, name string) (string, error)
return ins.InstanceDir + "/logs", nil
}

func HostBugReport(ctx cvd.CVDExecContext, paths IMPaths, out string) error {
func HostBugReport(ctx cvd.ExecContext, paths IMPaths, out string) error {
group, err := cvdFleetFirstGroup(ctx)
if err != nil {
return err
Expand All @@ -223,11 +223,11 @@ func defaultMainBuild() *apiv1.AndroidCIBuild {
}

type fetchCVDCommandArtifactsFetcher struct {
execContext cvd.CVDExecContext
execContext cvd.ExecContext
buildAPICredentials BuildAPICredentials
}

func newFetchCVDCommandArtifactsFetcher(execContext cvd.CVDExecContext, buildAPICredentials BuildAPICredentials) *fetchCVDCommandArtifactsFetcher {
func newFetchCVDCommandArtifactsFetcher(execContext cvd.ExecContext, buildAPICredentials BuildAPICredentials) *fetchCVDCommandArtifactsFetcher {
return &fetchCVDCommandArtifactsFetcher{
execContext: execContext,
buildAPICredentials: buildAPICredentials,
Expand Down Expand Up @@ -287,7 +287,7 @@ func (f *fetchCVDCommandArtifactsFetcher) Fetch(outDir, buildID, target string,
func createCredentialsFile(content string) (*os.File, error) {
p1, p2, err := os.Pipe()
if err != nil {
return nil, fmt.Errorf("Failed to create pipe for credentials: %w", err)
return nil, fmt.Errorf("failed to create pipe for credentials: %w", err)
}
go func(f *os.File) {
defer f.Close()
Expand Down Expand Up @@ -348,7 +348,7 @@ type startCVDParams struct {
BootloaderDir string
}

func CreateCVD(ctx cvd.CVDExecContext, p startCVDParams) error {
func CreateCVD(ctx cvd.ExecContext, p startCVDParams) error {
args := []string{groupNameArg, "create", daemonArg, reportAnonymousUsageStatsArg}
if len(p.InstanceNumbers) > 1 {
args = append(args, fmt.Sprintf("--instance_nums=%s", strings.Join(SliceItoa(p.InstanceNumbers), ",")))
Expand Down Expand Up @@ -390,11 +390,10 @@ func createNewDir(dir string) error {
}

func createDir(dir string) error {
if err := createNewDir(dir); os.IsExist(err) {
return nil
} else {
if err := createNewDir(dir); !os.IsExist(err) {
return err
}
return nil
}

func fileExist(name string) (bool, error) {
Expand Down Expand Up @@ -492,7 +491,7 @@ func (s cvdInstances) findByName(name string) (bool, *cvdInstance) {
return false, &cvdInstance{}
}

func runAcloudSetup(execContext cvd.CVDExecContext, artifactsRootDir, artifactsDir string) {
func runAcloudSetup(execContext cvd.ExecContext, artifactsRootDir, artifactsDir string) {
run := func(cmd *exec.Cmd) {
var b bytes.Buffer
cmd.Stdout = &b
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type ListCVDsActionOpts struct {
type ListCVDsAction struct {
group string
paths IMPaths
execContext cvd.CVDExecContext
execContext cvd.ExecContext
}

func NewListCVDsAction(opts ListCVDsActionOpts) *ListCVDsAction {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type StartCVDAction struct {
selector CVDSelector
paths IMPaths
om OperationManager
execContext cvd.CVDExecContext
execContext cvd.ExecContext
}

func NewStartCVDAction(opts StartCVDActionOpts) *StartCVDAction {
Expand Down
Loading

0 comments on commit 75f9960

Please sign in to comment.