2020-10-12 23:12:46 +00:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
2022-05-19 07:49:32 +00:00
|
|
|
"context"
|
2020-10-12 23:12:46 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/stashapp/stash/pkg/models"
|
2022-03-17 00:33:59 +00:00
|
|
|
"github.com/stashapp/stash/pkg/models/jsonschema"
|
2022-05-19 07:49:32 +00:00
|
|
|
"github.com/stashapp/stash/pkg/performer"
|
2022-03-17 00:33:59 +00:00
|
|
|
"github.com/stashapp/stash/pkg/sliceutil/stringslice"
|
2022-05-19 07:49:32 +00:00
|
|
|
"github.com/stashapp/stash/pkg/studio"
|
|
|
|
"github.com/stashapp/stash/pkg/tag"
|
2020-10-12 23:12:46 +00:00
|
|
|
)
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
type GalleryChecksumsFinder interface {
|
|
|
|
FindByChecksums(ctx context.Context, checksums []string) ([]*models.Gallery, error)
|
2022-05-19 07:49:32 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 23:12:46 +00:00
|
|
|
type Importer struct {
|
2022-07-13 06:30:54 +00:00
|
|
|
ReaderWriter FinderCreatorUpdater
|
2022-05-19 07:49:32 +00:00
|
|
|
StudioWriter studio.NameFinderCreator
|
2022-07-13 06:30:54 +00:00
|
|
|
GalleryWriter GalleryChecksumsFinder
|
2022-05-19 07:49:32 +00:00
|
|
|
PerformerWriter performer.NameFinderCreator
|
|
|
|
TagWriter tag.NameFinderCreator
|
2020-10-12 23:12:46 +00:00
|
|
|
Input jsonschema.Image
|
|
|
|
Path string
|
|
|
|
MissingRefBehaviour models.ImportMissingRefEnum
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
ID int
|
|
|
|
image models.Image
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (i *Importer) PreImport(ctx context.Context) error {
|
2020-10-12 23:12:46 +00:00
|
|
|
i.image = i.imageJSONToImage(i.Input)
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
if err := i.populateStudio(ctx); err != nil {
|
2020-10-12 23:12:46 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
if err := i.populateGalleries(ctx); err != nil {
|
2020-10-12 23:12:46 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
if err := i.populatePerformers(ctx); err != nil {
|
2020-10-12 23:12:46 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
if err := i.populateTags(ctx); err != nil {
|
2020-10-12 23:12:46 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Importer) imageJSONToImage(imageJSON jsonschema.Image) models.Image {
|
|
|
|
newImage := models.Image{
|
2022-07-13 06:30:54 +00:00
|
|
|
// Checksum: imageJSON.Checksum,
|
|
|
|
// Path: i.Path,
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if imageJSON.Title != "" {
|
2022-07-13 06:30:54 +00:00
|
|
|
newImage.Title = imageJSON.Title
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
if imageJSON.Rating != 0 {
|
2022-07-13 06:30:54 +00:00
|
|
|
newImage.Rating = &imageJSON.Rating
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 21:06:49 +00:00
|
|
|
newImage.Organized = imageJSON.Organized
|
2020-10-12 23:12:46 +00:00
|
|
|
newImage.OCounter = imageJSON.OCounter
|
2022-07-13 06:30:54 +00:00
|
|
|
newImage.CreatedAt = imageJSON.CreatedAt.GetTime()
|
|
|
|
newImage.UpdatedAt = imageJSON.UpdatedAt.GetTime()
|
|
|
|
|
|
|
|
// if imageJSON.File != nil {
|
|
|
|
// if imageJSON.File.Size != 0 {
|
|
|
|
// newImage.Size = &imageJSON.File.Size
|
|
|
|
// }
|
|
|
|
// if imageJSON.File.Width != 0 {
|
|
|
|
// newImage.Width = &imageJSON.File.Width
|
|
|
|
// }
|
|
|
|
// if imageJSON.File.Height != 0 {
|
|
|
|
// newImage.Height = &imageJSON.File.Height
|
|
|
|
// }
|
|
|
|
// }
|
2020-10-12 23:12:46 +00:00
|
|
|
|
|
|
|
return newImage
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (i *Importer) populateStudio(ctx context.Context) error {
|
2020-10-12 23:12:46 +00:00
|
|
|
if i.Input.Studio != "" {
|
2022-05-19 07:49:32 +00:00
|
|
|
studio, err := i.StudioWriter.FindByName(ctx, i.Input.Studio, false)
|
2020-10-12 23:12:46 +00:00
|
|
|
if err != nil {
|
Errorlint sweep + minor linter tweaks (#1796)
* Replace error assertions with Go 1.13 style
Use `errors.As(..)` over type assertions. This enables better use of
wrapped errors in the future, and lets us pass some errorlint checks
in the process.
The rewrite is entirely mechanical, and uses a standard idiom for
doing so.
* Use Go 1.13's errors.Is(..)
Rather than directly checking for error equality, use errors.Is(..).
This protects against error wrapping issues in the future.
Even though something like sql.ErrNoRows doesn't need the wrapping, do
so anyway, for the sake of consistency throughout the code base.
The change almost lets us pass the `errorlint` Go checker except for
a missing case in `js.go` which is to be handled separately; it isn't
mechanical, like these changes are.
* Remove goconst
goconst isn't a useful linter in many cases, because it's false positive
rate is high. It's 100% for the current code base.
* Avoid direct comparison of errors in recover()
Assert that we are catching an error from recover(). If we are,
check that the error caught matches errStop.
* Enable the "errorlint" checker
Configure the checker to avoid checking for errorf wraps. These are
often false positives since the suggestion is to blanket wrap errors
with %w, and that exposes the underlying API which you might not want
to do.
The other warnings are good however, and with the current patch stack,
the code base passes all these checks as well.
* Configure rowserrcheck
The project uses sqlx. Configure rowserrcheck to include said package.
* Mechanically rewrite a large set of errors
Mechanically search for errors that look like
fmt.Errorf("...%s", err.Error())
and rewrite those into
fmt.Errorf("...%v", err)
The `fmt` package is error-aware and knows how to call err.Error()
itself.
The rationale is that this is more idiomatic Go; it paves the
way for using error wrapping later with %w in some sites.
This patch only addresses the entirely mechanical rewriting caught by
a project-side search/replace. There are more individual sites not
addressed by this patch.
2021-10-12 03:03:08 +00:00
|
|
|
return fmt.Errorf("error finding studio by name: %v", err)
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if studio == nil {
|
|
|
|
if i.MissingRefBehaviour == models.ImportMissingRefEnumFail {
|
|
|
|
return fmt.Errorf("image studio '%s' not found", i.Input.Studio)
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.MissingRefBehaviour == models.ImportMissingRefEnumIgnore {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.MissingRefBehaviour == models.ImportMissingRefEnumCreate {
|
2022-05-19 07:49:32 +00:00
|
|
|
studioID, err := i.createStudio(ctx, i.Input.Studio)
|
2020-10-12 23:12:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-13 06:30:54 +00:00
|
|
|
i.image.StudioID = &studioID
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-07-13 06:30:54 +00:00
|
|
|
i.image.StudioID = &studio.ID
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (i *Importer) createStudio(ctx context.Context, name string) (int, error) {
|
2020-10-12 23:12:46 +00:00
|
|
|
newStudio := *models.NewStudio(name)
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
created, err := i.StudioWriter.Create(ctx, newStudio)
|
2020-10-12 23:12:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return created.ID, nil
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (i *Importer) populateGalleries(ctx context.Context) error {
|
2020-10-12 23:12:46 +00:00
|
|
|
for _, checksum := range i.Input.Galleries {
|
2022-05-19 07:49:32 +00:00
|
|
|
gallery, err := i.GalleryWriter.FindByChecksums(ctx, []string{checksum})
|
2020-10-12 23:12:46 +00:00
|
|
|
if err != nil {
|
Errorlint sweep + minor linter tweaks (#1796)
* Replace error assertions with Go 1.13 style
Use `errors.As(..)` over type assertions. This enables better use of
wrapped errors in the future, and lets us pass some errorlint checks
in the process.
The rewrite is entirely mechanical, and uses a standard idiom for
doing so.
* Use Go 1.13's errors.Is(..)
Rather than directly checking for error equality, use errors.Is(..).
This protects against error wrapping issues in the future.
Even though something like sql.ErrNoRows doesn't need the wrapping, do
so anyway, for the sake of consistency throughout the code base.
The change almost lets us pass the `errorlint` Go checker except for
a missing case in `js.go` which is to be handled separately; it isn't
mechanical, like these changes are.
* Remove goconst
goconst isn't a useful linter in many cases, because it's false positive
rate is high. It's 100% for the current code base.
* Avoid direct comparison of errors in recover()
Assert that we are catching an error from recover(). If we are,
check that the error caught matches errStop.
* Enable the "errorlint" checker
Configure the checker to avoid checking for errorf wraps. These are
often false positives since the suggestion is to blanket wrap errors
with %w, and that exposes the underlying API which you might not want
to do.
The other warnings are good however, and with the current patch stack,
the code base passes all these checks as well.
* Configure rowserrcheck
The project uses sqlx. Configure rowserrcheck to include said package.
* Mechanically rewrite a large set of errors
Mechanically search for errors that look like
fmt.Errorf("...%s", err.Error())
and rewrite those into
fmt.Errorf("...%v", err)
The `fmt` package is error-aware and knows how to call err.Error()
itself.
The rationale is that this is more idiomatic Go; it paves the
way for using error wrapping later with %w in some sites.
This patch only addresses the entirely mechanical rewriting caught by
a project-side search/replace. There are more individual sites not
addressed by this patch.
2021-10-12 03:03:08 +00:00
|
|
|
return fmt.Errorf("error finding gallery: %v", err)
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
if len(gallery) == 0 {
|
2020-10-12 23:12:46 +00:00
|
|
|
if i.MissingRefBehaviour == models.ImportMissingRefEnumFail {
|
|
|
|
return fmt.Errorf("image gallery '%s' not found", i.Input.Studio)
|
|
|
|
}
|
|
|
|
|
|
|
|
// we don't create galleries - just ignore
|
|
|
|
if i.MissingRefBehaviour == models.ImportMissingRefEnumIgnore || i.MissingRefBehaviour == models.ImportMissingRefEnumCreate {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else {
|
2022-07-13 06:30:54 +00:00
|
|
|
i.image.GalleryIDs = append(i.image.GalleryIDs, gallery[0].ID)
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (i *Importer) populatePerformers(ctx context.Context) error {
|
2020-10-12 23:12:46 +00:00
|
|
|
if len(i.Input.Performers) > 0 {
|
|
|
|
names := i.Input.Performers
|
2022-05-19 07:49:32 +00:00
|
|
|
performers, err := i.PerformerWriter.FindByNames(ctx, names, false)
|
2020-10-12 23:12:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var pluckedNames []string
|
|
|
|
for _, performer := range performers {
|
|
|
|
if !performer.Name.Valid {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
pluckedNames = append(pluckedNames, performer.Name.String)
|
|
|
|
}
|
|
|
|
|
2022-03-17 00:33:59 +00:00
|
|
|
missingPerformers := stringslice.StrFilter(names, func(name string) bool {
|
|
|
|
return !stringslice.StrInclude(pluckedNames, name)
|
2020-10-12 23:12:46 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if len(missingPerformers) > 0 {
|
|
|
|
if i.MissingRefBehaviour == models.ImportMissingRefEnumFail {
|
|
|
|
return fmt.Errorf("image performers [%s] not found", strings.Join(missingPerformers, ", "))
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.MissingRefBehaviour == models.ImportMissingRefEnumCreate {
|
2022-05-19 07:49:32 +00:00
|
|
|
createdPerformers, err := i.createPerformers(ctx, missingPerformers)
|
2020-10-12 23:12:46 +00:00
|
|
|
if err != nil {
|
Errorlint sweep + minor linter tweaks (#1796)
* Replace error assertions with Go 1.13 style
Use `errors.As(..)` over type assertions. This enables better use of
wrapped errors in the future, and lets us pass some errorlint checks
in the process.
The rewrite is entirely mechanical, and uses a standard idiom for
doing so.
* Use Go 1.13's errors.Is(..)
Rather than directly checking for error equality, use errors.Is(..).
This protects against error wrapping issues in the future.
Even though something like sql.ErrNoRows doesn't need the wrapping, do
so anyway, for the sake of consistency throughout the code base.
The change almost lets us pass the `errorlint` Go checker except for
a missing case in `js.go` which is to be handled separately; it isn't
mechanical, like these changes are.
* Remove goconst
goconst isn't a useful linter in many cases, because it's false positive
rate is high. It's 100% for the current code base.
* Avoid direct comparison of errors in recover()
Assert that we are catching an error from recover(). If we are,
check that the error caught matches errStop.
* Enable the "errorlint" checker
Configure the checker to avoid checking for errorf wraps. These are
often false positives since the suggestion is to blanket wrap errors
with %w, and that exposes the underlying API which you might not want
to do.
The other warnings are good however, and with the current patch stack,
the code base passes all these checks as well.
* Configure rowserrcheck
The project uses sqlx. Configure rowserrcheck to include said package.
* Mechanically rewrite a large set of errors
Mechanically search for errors that look like
fmt.Errorf("...%s", err.Error())
and rewrite those into
fmt.Errorf("...%v", err)
The `fmt` package is error-aware and knows how to call err.Error()
itself.
The rationale is that this is more idiomatic Go; it paves the
way for using error wrapping later with %w in some sites.
This patch only addresses the entirely mechanical rewriting caught by
a project-side search/replace. There are more individual sites not
addressed by this patch.
2021-10-12 03:03:08 +00:00
|
|
|
return fmt.Errorf("error creating image performers: %v", err)
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
performers = append(performers, createdPerformers...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore if MissingRefBehaviour set to Ignore
|
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
for _, p := range performers {
|
|
|
|
i.image.PerformerIDs = append(i.image.PerformerIDs, p.ID)
|
|
|
|
}
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (i *Importer) createPerformers(ctx context.Context, names []string) ([]*models.Performer, error) {
|
2020-10-12 23:12:46 +00:00
|
|
|
var ret []*models.Performer
|
|
|
|
for _, name := range names {
|
|
|
|
newPerformer := *models.NewPerformer(name)
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
created, err := i.PerformerWriter.Create(ctx, newPerformer)
|
2020-10-12 23:12:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = append(ret, created)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (i *Importer) populateTags(ctx context.Context) error {
|
2020-10-12 23:12:46 +00:00
|
|
|
if len(i.Input.Tags) > 0 {
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
tags, err := importTags(ctx, i.TagWriter, i.Input.Tags, i.MissingRefBehaviour)
|
2020-10-12 23:12:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
for _, t := range tags {
|
|
|
|
i.image.TagIDs = append(i.image.TagIDs, t.ID)
|
|
|
|
}
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (i *Importer) PostImport(ctx context.Context, id int) error {
|
2020-10-12 23:12:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Importer) Name() string {
|
|
|
|
return i.Path
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (i *Importer) FindExistingID(ctx context.Context) (*int, error) {
|
2022-07-13 06:30:54 +00:00
|
|
|
// var existing []*models.Image
|
|
|
|
// var err error
|
|
|
|
// existing, err = i.ReaderWriter.FindByChecksum(ctx, i.Input.Checksum)
|
2020-10-12 23:12:46 +00:00
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
// if err != nil {
|
|
|
|
// return nil, err
|
|
|
|
// }
|
2020-10-12 23:12:46 +00:00
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
// if len(existing) > 0 {
|
|
|
|
// id := existing[0].ID
|
|
|
|
// return &id, nil
|
|
|
|
// }
|
2020-10-12 23:12:46 +00:00
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (i *Importer) Create(ctx context.Context) (*int, error) {
|
2022-07-13 06:30:54 +00:00
|
|
|
err := i.ReaderWriter.Create(ctx, &models.ImageCreateInput{Image: &i.image})
|
2020-10-12 23:12:46 +00:00
|
|
|
if err != nil {
|
Errorlint sweep + minor linter tweaks (#1796)
* Replace error assertions with Go 1.13 style
Use `errors.As(..)` over type assertions. This enables better use of
wrapped errors in the future, and lets us pass some errorlint checks
in the process.
The rewrite is entirely mechanical, and uses a standard idiom for
doing so.
* Use Go 1.13's errors.Is(..)
Rather than directly checking for error equality, use errors.Is(..).
This protects against error wrapping issues in the future.
Even though something like sql.ErrNoRows doesn't need the wrapping, do
so anyway, for the sake of consistency throughout the code base.
The change almost lets us pass the `errorlint` Go checker except for
a missing case in `js.go` which is to be handled separately; it isn't
mechanical, like these changes are.
* Remove goconst
goconst isn't a useful linter in many cases, because it's false positive
rate is high. It's 100% for the current code base.
* Avoid direct comparison of errors in recover()
Assert that we are catching an error from recover(). If we are,
check that the error caught matches errStop.
* Enable the "errorlint" checker
Configure the checker to avoid checking for errorf wraps. These are
often false positives since the suggestion is to blanket wrap errors
with %w, and that exposes the underlying API which you might not want
to do.
The other warnings are good however, and with the current patch stack,
the code base passes all these checks as well.
* Configure rowserrcheck
The project uses sqlx. Configure rowserrcheck to include said package.
* Mechanically rewrite a large set of errors
Mechanically search for errors that look like
fmt.Errorf("...%s", err.Error())
and rewrite those into
fmt.Errorf("...%v", err)
The `fmt` package is error-aware and knows how to call err.Error()
itself.
The rationale is that this is more idiomatic Go; it paves the
way for using error wrapping later with %w in some sites.
This patch only addresses the entirely mechanical rewriting caught by
a project-side search/replace. There are more individual sites not
addressed by this patch.
2021-10-12 03:03:08 +00:00
|
|
|
return nil, fmt.Errorf("error creating image: %v", err)
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
id := i.image.ID
|
2020-10-12 23:12:46 +00:00
|
|
|
i.ID = id
|
|
|
|
return &id, nil
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (i *Importer) Update(ctx context.Context, id int) error {
|
2020-10-12 23:12:46 +00:00
|
|
|
image := i.image
|
|
|
|
image.ID = id
|
|
|
|
i.ID = id
|
2022-07-13 06:30:54 +00:00
|
|
|
err := i.ReaderWriter.Update(ctx, &image)
|
2020-10-12 23:12:46 +00:00
|
|
|
if err != nil {
|
Errorlint sweep + minor linter tweaks (#1796)
* Replace error assertions with Go 1.13 style
Use `errors.As(..)` over type assertions. This enables better use of
wrapped errors in the future, and lets us pass some errorlint checks
in the process.
The rewrite is entirely mechanical, and uses a standard idiom for
doing so.
* Use Go 1.13's errors.Is(..)
Rather than directly checking for error equality, use errors.Is(..).
This protects against error wrapping issues in the future.
Even though something like sql.ErrNoRows doesn't need the wrapping, do
so anyway, for the sake of consistency throughout the code base.
The change almost lets us pass the `errorlint` Go checker except for
a missing case in `js.go` which is to be handled separately; it isn't
mechanical, like these changes are.
* Remove goconst
goconst isn't a useful linter in many cases, because it's false positive
rate is high. It's 100% for the current code base.
* Avoid direct comparison of errors in recover()
Assert that we are catching an error from recover(). If we are,
check that the error caught matches errStop.
* Enable the "errorlint" checker
Configure the checker to avoid checking for errorf wraps. These are
often false positives since the suggestion is to blanket wrap errors
with %w, and that exposes the underlying API which you might not want
to do.
The other warnings are good however, and with the current patch stack,
the code base passes all these checks as well.
* Configure rowserrcheck
The project uses sqlx. Configure rowserrcheck to include said package.
* Mechanically rewrite a large set of errors
Mechanically search for errors that look like
fmt.Errorf("...%s", err.Error())
and rewrite those into
fmt.Errorf("...%v", err)
The `fmt` package is error-aware and knows how to call err.Error()
itself.
The rationale is that this is more idiomatic Go; it paves the
way for using error wrapping later with %w in some sites.
This patch only addresses the entirely mechanical rewriting caught by
a project-side search/replace. There are more individual sites not
addressed by this patch.
2021-10-12 03:03:08 +00:00
|
|
|
return fmt.Errorf("error updating existing image: %v", err)
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func importTags(ctx context.Context, tagWriter tag.NameFinderCreator, names []string, missingRefBehaviour models.ImportMissingRefEnum) ([]*models.Tag, error) {
|
|
|
|
tags, err := tagWriter.FindByNames(ctx, names, false)
|
2020-10-12 23:12:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var pluckedNames []string
|
|
|
|
for _, tag := range tags {
|
|
|
|
pluckedNames = append(pluckedNames, tag.Name)
|
|
|
|
}
|
|
|
|
|
2022-03-17 00:33:59 +00:00
|
|
|
missingTags := stringslice.StrFilter(names, func(name string) bool {
|
|
|
|
return !stringslice.StrInclude(pluckedNames, name)
|
2020-10-12 23:12:46 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if len(missingTags) > 0 {
|
|
|
|
if missingRefBehaviour == models.ImportMissingRefEnumFail {
|
|
|
|
return nil, fmt.Errorf("tags [%s] not found", strings.Join(missingTags, ", "))
|
|
|
|
}
|
|
|
|
|
|
|
|
if missingRefBehaviour == models.ImportMissingRefEnumCreate {
|
2022-05-19 07:49:32 +00:00
|
|
|
createdTags, err := createTags(ctx, tagWriter, missingTags)
|
2020-10-12 23:12:46 +00:00
|
|
|
if err != nil {
|
Errorlint sweep + minor linter tweaks (#1796)
* Replace error assertions with Go 1.13 style
Use `errors.As(..)` over type assertions. This enables better use of
wrapped errors in the future, and lets us pass some errorlint checks
in the process.
The rewrite is entirely mechanical, and uses a standard idiom for
doing so.
* Use Go 1.13's errors.Is(..)
Rather than directly checking for error equality, use errors.Is(..).
This protects against error wrapping issues in the future.
Even though something like sql.ErrNoRows doesn't need the wrapping, do
so anyway, for the sake of consistency throughout the code base.
The change almost lets us pass the `errorlint` Go checker except for
a missing case in `js.go` which is to be handled separately; it isn't
mechanical, like these changes are.
* Remove goconst
goconst isn't a useful linter in many cases, because it's false positive
rate is high. It's 100% for the current code base.
* Avoid direct comparison of errors in recover()
Assert that we are catching an error from recover(). If we are,
check that the error caught matches errStop.
* Enable the "errorlint" checker
Configure the checker to avoid checking for errorf wraps. These are
often false positives since the suggestion is to blanket wrap errors
with %w, and that exposes the underlying API which you might not want
to do.
The other warnings are good however, and with the current patch stack,
the code base passes all these checks as well.
* Configure rowserrcheck
The project uses sqlx. Configure rowserrcheck to include said package.
* Mechanically rewrite a large set of errors
Mechanically search for errors that look like
fmt.Errorf("...%s", err.Error())
and rewrite those into
fmt.Errorf("...%v", err)
The `fmt` package is error-aware and knows how to call err.Error()
itself.
The rationale is that this is more idiomatic Go; it paves the
way for using error wrapping later with %w in some sites.
This patch only addresses the entirely mechanical rewriting caught by
a project-side search/replace. There are more individual sites not
addressed by this patch.
2021-10-12 03:03:08 +00:00
|
|
|
return nil, fmt.Errorf("error creating tags: %v", err)
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tags = append(tags, createdTags...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore if MissingRefBehaviour set to Ignore
|
|
|
|
}
|
|
|
|
|
|
|
|
return tags, nil
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func createTags(ctx context.Context, tagWriter tag.NameFinderCreator, names []string) ([]*models.Tag, error) {
|
2020-10-12 23:12:46 +00:00
|
|
|
var ret []*models.Tag
|
|
|
|
for _, name := range names {
|
|
|
|
newTag := *models.NewTag(name)
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
created, err := tagWriter.Create(ctx, newTag)
|
2020-10-12 23:12:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = append(ret, created)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|