#If I did something like `WithStaticcheck

1 messages · Page 1 of 1 (latest)

serene mantle
#

Interesting question, lots of possibilities. Sketched out one immediate idea:

type Staticcheck struct {
    BaseImage *Container
}

// optional for user to call, otherwise default is used, as set in Check
func (s *Staticcheck) WithBaseImage(ctr *Container) *Staticcheck {
    s.BaseImage = ctr
    return s
}

// Run staticcheck on the given directory, error if it fails
// (error will show up in progress output for user)
func (s *Staticcheck) Check(ctx context.Context, srcDir *Directory) error {
    if s.BaseImage == nil {
        // set a default; if you want to be fancy, parse the go.mod from the given srcDir and figure out their go version
    }

    _, err := s.BaseImage.WithExec(...).Sync(ctx)
    return err
}

// Run staticcheck on the given directory, return the result whether or not it fails
func (s *Staticcheck) CheckResult(ctx context.Context, srcDir *Directory) (*Result, error) {
    if s.BaseImage == nil {
        // set a default; if you want to be fancy, parse the go.mod from the given srcDir and figure out their go version
    }

    output, err := s.BaseImage.WithExec(...).Stdout(ctx)
    // this should work, but pretty sure it doesn't right now; making issue!
    var execErr *ExecError
    if errors.As(err, &execErr) {
        return &Result{
            Output: execErr.Stderr,
            Passed: false,
        }
    }
    return &Result{Output: output, Passed: true}, nil
}

type Result struct {
    Output string
    Passed bool
}
#

The optional WithBaseImage method can be used to override the base image if the user wants.

The other interesting part is that there's two methods: Check just returns an error if the lint fails. CheckResult will not error if the check fails and instead returns and object indicating whether it failed or not + the output.

Realistically, users could want either behavior, so it feels reasonable for a module author to provide both if they want.

#

(to be clear, this is a very rough sketch and just some vague opinions, definitely no really well established patterns here or anything yet)