mattermost/server/cmd/mmctl/commands/importer/utils.go
Miguel de la Cruz 951456c780
Includes mmctl into the mono-repo (#23091)
* Includes mmctl into the mono-repo

* Update to use the new public module paths

* Adds docs check to the mmctl CI

* Fix public utils import path

* Tidy up modules

* Fix linter

* Update CI tasks to use the new file structure

* Update CI references
2023-06-05 12:42:55 +02:00

66 lines
1.5 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package importer
import (
"encoding/json"
"fmt"
"strings"
)
type ImportFileInfo struct {
Source string `json:"archive_name"`
FileName string `json:"file_name,omitempty"`
CurrentLine uint64 `json:"current_line,omitempty"`
TotalLines uint64 `json:"total_lines,omitempty"`
}
type ImportValidationError struct { //nolint:govet
ImportFileInfo
FieldName string
Err error
Suggestion string
SuggestedValues []any
ApplySuggestion func(any) error
}
func (e *ImportValidationError) MarshalJSON() ([]byte, error) {
t := struct { //nolint:govet
ImportFileInfo
FieldName string `json:"field_name,omitempty"`
Err string `json:"error,omitempty"`
Suggestion string `json:"suggestion,omitempty"`
SuggestedValues []any `json:"suggested_values,omitempty"`
}{
ImportFileInfo: e.ImportFileInfo,
FieldName: e.FieldName,
Suggestion: e.Suggestion,
SuggestedValues: e.SuggestedValues,
}
if e.Err != nil {
t.Err = e.Err.Error()
}
return json.Marshal(t)
}
func (e *ImportValidationError) Error() string {
msg := &strings.Builder{}
msg.WriteString("import validation error")
if e.FileName != "" || e.Source != "" {
fmt.Fprintf(msg, " in %s->%s:%d", e.Source, e.FileName, e.CurrentLine)
}
if e.FieldName != "" {
fmt.Fprintf(msg, " field %q", e.FieldName)
}
if e.Err != nil {
fmt.Fprintf(msg, ": %s", e.Err)
}
return msg.String()
}