github.com/iasthc/atlas/cmd/atlas@v0.0.0-20230523071841-73246df3f88d/x/x.go (about)

     1  // Copyright 2021-present The Atlas Authors. All rights reserved.
     2  // This source code is licensed under the Apache 2.0 license found
     3  // in the LICENSE file in the root directory of this source tree.
     4  
     5  // Package x contains a small set of functions to explore how a public API
     6  // for the CLI might look in the future. Note that this package is intended
     7  // for experimental purposes only and should not be used externally, apart
     8  // from within the project's codebase.
     9  package x
    10  
    11  import (
    12  	"context"
    13  	"errors"
    14  
    15  	"github.com/iasthc/atlas/cmd/atlas/internal/lint"
    16  	"github.com/iasthc/atlas/sql/migrate"
    17  	"github.com/iasthc/atlas/sql/sqlcheck"
    18  	"github.com/iasthc/atlas/sql/sqlclient"
    19  )
    20  
    21  // Exposes the lint reporting types.
    22  type (
    23  	FileReport    = lint.FileReport
    24  	SummaryReport = lint.SummaryReport
    25  )
    26  
    27  // ErrEmptyReport is returned when the report is empty.
    28  var ErrEmptyReport = errors.New("empty report")
    29  
    30  // lintLatest runs the lint command on the latest changes (files) in the given directory.
    31  func lintLatest(ctx context.Context, dev *sqlclient.Client, dir migrate.Dir, latest int, az []sqlcheck.Analyzer) (report *SummaryReport, err error) {
    32  	r := lint.Runner{
    33  		Dev:            dev,
    34  		Dir:            dir,
    35  		Analyzers:      az,
    36  		ChangeDetector: lint.LatestChanges(dir, latest),
    37  		ReportWriter: reporterFunc(func(r *SummaryReport) error {
    38  			report = r
    39  			return nil
    40  		}),
    41  	}
    42  	if err = r.Run(ctx); err != nil && !errors.As(err, &lint.SilentError{}) {
    43  		return nil, err
    44  	}
    45  	if report == nil {
    46  		return nil, ErrEmptyReport
    47  	}
    48  	return report, nil
    49  }
    50  
    51  // reporterFunc implements the lint.ReportWriter interface.
    52  type reporterFunc func(*SummaryReport) error
    53  
    54  // WriteReport implements the WriteReport method.
    55  func (f reporterFunc) WriteReport(r *SummaryReport) error {
    56  	return f(r)
    57  }