From 32b55eda5e6956e7ee2f913ae79e1e2a3414c9ed Mon Sep 17 00:00:00 2001 From: Than McIntosh Date: Mon, 22 Jul 2024 15:50:33 +0000 Subject: cmd: relocate cmd/go/internal/script to cmd/internal/script Relocate cmd/go's internal/script package up a level into cmd/internal/script, so as to enable the use of script tests in other cmd packages. No change in functionality. Updates #68606. Change-Id: I3974b0bf59c76e0f459184c9f3090d6077dd5d91 Reviewed-on: https://go-review.googlesource.com/c/go/+/601358 Reviewed-by: Michael Matloob LUCI-TryBot-Result: Go LUCI --- src/cmd/internal/script/errors.go | 64 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/cmd/internal/script/errors.go (limited to 'src/cmd/internal/script/errors.go') diff --git a/src/cmd/internal/script/errors.go b/src/cmd/internal/script/errors.go new file mode 100644 index 0000000000..7f43e72888 --- /dev/null +++ b/src/cmd/internal/script/errors.go @@ -0,0 +1,64 @@ +// Copyright 2022 The Go 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 script + +import ( + "errors" + "fmt" +) + +// ErrUnexpectedSuccess indicates that a script command that was expected to +// fail (as indicated by a "!" prefix) instead completed successfully. +var ErrUnexpectedSuccess = errors.New("unexpected success") + +// A CommandError describes an error resulting from attempting to execute a +// specific command. +type CommandError struct { + File string + Line int + Op string + Args []string + Err error +} + +func cmdError(cmd *command, err error) *CommandError { + return &CommandError{ + File: cmd.file, + Line: cmd.line, + Op: cmd.name, + Args: cmd.args, + Err: err, + } +} + +func (e *CommandError) Error() string { + if len(e.Args) == 0 { + return fmt.Sprintf("%s:%d: %s: %v", e.File, e.Line, e.Op, e.Err) + } + return fmt.Sprintf("%s:%d: %s %s: %v", e.File, e.Line, e.Op, quoteArgs(e.Args), e.Err) +} + +func (e *CommandError) Unwrap() error { return e.Err } + +// A UsageError reports the valid arguments for a command. +// +// It may be returned in response to invalid arguments. +type UsageError struct { + Name string + Command Cmd +} + +func (e *UsageError) Error() string { + usage := e.Command.Usage() + suffix := "" + if usage.Async { + suffix = " [&]" + } + return fmt.Sprintf("usage: %s %s%s", e.Name, usage.Args, suffix) +} + +// ErrUsage may be returned by a Command to indicate that it was called with +// invalid arguments; its Usage method may be called to obtain details. +var ErrUsage = errors.New("invalid usage") -- cgit v1.3