Separate output checks from session creation
This commit is contained in:
@@ -68,10 +68,20 @@ type OutputSession struct {
|
||||
stdout io.Writer
|
||||
}
|
||||
|
||||
// SelectOutput checks active destinations before any output resources are opened.
|
||||
func SelectOutput(selection OutputSelection) (*OutputSession, error) {
|
||||
session := &OutputSession{enabled: selection.ReportingEnabled, stdout: selection.Stdout}
|
||||
session.replace = session.replaceReport
|
||||
// CheckedDestinations records destination paths and kinds without opening them.
|
||||
type CheckedDestinations struct {
|
||||
transcript string
|
||||
publishPath string
|
||||
reports [3]string
|
||||
reportDestinations [3]string
|
||||
enabled bool
|
||||
direct bool
|
||||
stdout io.Writer
|
||||
}
|
||||
|
||||
// CheckOutput checks active destinations before any output resources are opened.
|
||||
func CheckOutput(selection OutputSelection) (*CheckedDestinations, error) {
|
||||
checked := &CheckedDestinations{enabled: selection.ReportingEnabled, stdout: selection.Stdout}
|
||||
type destination struct {
|
||||
display string
|
||||
path string
|
||||
@@ -89,7 +99,7 @@ func SelectOutput(selection OutputSelection) (*OutputSession, error) {
|
||||
return destination{display: path, path: resolved, info: info}, nil
|
||||
}
|
||||
var resolvedDir string
|
||||
if session.enabled {
|
||||
if checked.enabled {
|
||||
if strings.TrimSpace(selection.ReportDir) == "" {
|
||||
return nil, fmt.Errorf("report directory is required")
|
||||
}
|
||||
@@ -115,11 +125,11 @@ func SelectOutput(selection OutputSelection) (*OutputSession, error) {
|
||||
if target.info != nil && target.info.IsDir() {
|
||||
return nil, fmt.Errorf("transcript output %q is a directory", target.display)
|
||||
}
|
||||
session.transcript, session.publishPath = target.display, target.path
|
||||
session.direct = target.info != nil && !target.info.Mode().IsRegular()
|
||||
checked.transcript, checked.publishPath = target.display, target.path
|
||||
checked.direct = target.info != nil && !target.info.Mode().IsRegular()
|
||||
active = append(active, target)
|
||||
}
|
||||
if session.enabled {
|
||||
if checked.enabled {
|
||||
label := selection.ReportDir
|
||||
if !os.IsPathSeparator(label[len(label)-1]) {
|
||||
label += string(filepath.Separator)
|
||||
@@ -133,7 +143,7 @@ func SelectOutput(selection OutputSelection) (*OutputSession, error) {
|
||||
if target.info != nil && !target.info.Mode().IsRegular() {
|
||||
return nil, fmt.Errorf("report output %q is not a regular file", target.display)
|
||||
}
|
||||
session.reports[i], session.reportDestinations[i] = target.display, target.path
|
||||
checked.reports[i], checked.reportDestinations[i] = target.display, target.path
|
||||
active = append(active, target)
|
||||
}
|
||||
}
|
||||
@@ -141,7 +151,7 @@ func SelectOutput(selection OutputSelection) (*OutputSession, error) {
|
||||
return a.path == b.path || (a.info != nil && b.info != nil && os.SameFile(a.info, b.info))
|
||||
}
|
||||
if len(active) == 0 {
|
||||
return session, nil
|
||||
return checked, nil
|
||||
}
|
||||
var protected []destination
|
||||
for _, path := range selection.ProtectedPaths {
|
||||
@@ -154,7 +164,7 @@ func SelectOutput(selection OutputSelection) (*OutputSession, error) {
|
||||
}
|
||||
}
|
||||
var input destination
|
||||
if session.enabled && selection.InputTranscriptPath != "" {
|
||||
if checked.enabled && selection.InputTranscriptPath != "" {
|
||||
var err error
|
||||
input, err = check(selection.InputTranscriptPath)
|
||||
if err != nil {
|
||||
@@ -167,7 +177,7 @@ func SelectOutput(selection OutputSelection) (*OutputSession, error) {
|
||||
return nil, fmt.Errorf("output destination %q conflicts with input %q", target.display, source.display)
|
||||
}
|
||||
}
|
||||
if (session.transcript == "" || i > 0) && input.path != "" && same(target, input) {
|
||||
if (checked.transcript == "" || i > 0) && input.path != "" && same(target, input) {
|
||||
return nil, fmt.Errorf("output destination %q conflicts with input %q", target.display, input.display)
|
||||
}
|
||||
for _, prior := range active[:i] {
|
||||
@@ -176,19 +186,18 @@ func SelectOutput(selection OutputSelection) (*OutputSession, error) {
|
||||
}
|
||||
}
|
||||
}
|
||||
return session, nil
|
||||
return checked, nil
|
||||
}
|
||||
|
||||
// PrepareOutput validates destinations and acquires the transcript resource.
|
||||
func PrepareOutput(selection OutputSelection) (*OutputSession, error) {
|
||||
session, err := SelectOutput(selection)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
// NewOutputSession constructs a session without acquiring output resources.
|
||||
func NewOutputSession(checked *CheckedDestinations) *OutputSession {
|
||||
session := &OutputSession{
|
||||
transcript: checked.transcript, publishPath: checked.publishPath,
|
||||
reports: checked.reports, reportDestinations: checked.reportDestinations,
|
||||
enabled: checked.enabled, direct: checked.direct, stdout: checked.stdout,
|
||||
}
|
||||
if err := session.Prepare(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return session, nil
|
||||
session.replace = session.replaceReport
|
||||
return session
|
||||
}
|
||||
|
||||
// Prepare opens a sibling temporary file for regular transcript output.
|
||||
|
||||
Reference in New Issue
Block a user