Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update imports to elastic-agent-libs/file where needed #31419

Merged
merged 18 commits into from
Apr 29, 2022
Prev Previous commit
Next Next commit
fix more issues by linter
  • Loading branch information
kvch committed Apr 26, 2022
commit 37c37cdc42012ceaf23cd294167b2be53310e4d8
6 changes: 3 additions & 3 deletions dev-tools/mage/crossbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func CrossBuild(options ...CrossBuildOption) error {
}
}
// If we're here, something isn't set.
return errors.New("Cannot crossbuild on AIX. Either run `mage build` or set PLATFORMS='aix/ppc64'")
return errors.New("cannot crossbuild on AIX, either run `mage build` or set PLATFORMS='aix/ppc64'")
}

// Docker is required for this target.
Expand Down Expand Up @@ -270,7 +270,7 @@ type GolangCrossBuilder struct {

// Build executes the build inside of Docker.
func (b GolangCrossBuilder) Build() error {
fmt.Printf(">> %v: Building for %v\n", b.Target, b.Platform)
fmt.Printf(">> %v: Building for %v\n", b.Target, b.Platform) //nolint:forbidigo // it's ok to fmt.println in mage

repoInfo, err := GetProjectRepoInfo()
if err != nil {
Expand Down Expand Up @@ -355,7 +355,7 @@ func chownPaths(uid, gid int, path string) error {
start := time.Now()
numFixed := 0
defer func() {
log.Printf("chown took: %v, changed %d files", time.Now().Sub(start), numFixed)
log.Printf("chown took: %v, changed %d files", time.Since(start), numFixed)
}()

return filepath.Walk(path, func(name string, info os.FileInfo, err error) error {
Expand Down
4 changes: 2 additions & 2 deletions filebeat/inputsource/unix/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ func TestReceiveNewEventsConcurrently(t *testing.T) {
}

for socketType := range socketTypes {
if runtime.GOOS == "darwin" && socketType == "datagram" {
if runtime.GOOS == "darwin" && socketType == "datagram" { //nolint:goconst // platform checking in test
t.Skip("test is only supported on linux. See https://github.com/elastic/beats/issues/22775")
return
}
Expand Down Expand Up @@ -397,7 +397,7 @@ func randomString(l int) string {
charsets := []byte("abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWZYZ0123456789")
message := make([]byte, l)
for i := range message {
message[i] = charsets[rand.Intn(len(charsets))]
message[i] = charsets[rand.Intn(len(charsets))] //nolint:gosec // it generates test string, allowed to be weak
}
return string(message)
}
Expand Down
24 changes: 11 additions & 13 deletions filebeat/registrar/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import (
"os"
"path/filepath"

"github.com/pkg/errors"

"github.com/elastic/beats/v7/filebeat/config"
"github.com/elastic/beats/v7/filebeat/input/file"
"github.com/elastic/beats/v7/libbeat/logp"
Expand Down Expand Up @@ -162,7 +160,7 @@ func initVersion0Registry(regHome string, perm os.FileMode) error {
if !isDir(regHome) {
logp.Info("No registry home found. Create: %v", regHome)
if err := os.MkdirAll(regHome, 0o750); err != nil {
return errors.Wrapf(err, "failed to create registry dir '%v'", regHome)
return fmt.Errorf("failed to create registry dir '%v': %w", regHome, err)
}
}

Expand All @@ -171,7 +169,7 @@ func initVersion0Registry(regHome string, perm os.FileMode) error {
logp.Info("Initialize registry meta file")
err := safeWriteFile(metaFile, []byte(`{"version": "0"}`), perm)
if err != nil {
return errors.Wrap(err, "failed writing registry meta.json")
return fmt.Errorf("failed writing registry meta.json: %w", err)
}
}

Expand All @@ -186,21 +184,21 @@ func (m *Migrator) updateToVersion1(regHome string) error {

origDataFile := filepath.Join(regHome, "data.json")
if !isFile(origDataFile) {
return fmt.Errorf("missing original data file at: %v", origDataFile)
return fmt.Errorf("missing original data file at: %v: %w", origDataFile, err)
}

// read states from file and ensure file is closed immediately.
states, err := func() ([]file.State, error) {
origIn, err := os.Open(origDataFile)
if err != nil {
return nil, errors.Wrap(err, "failed to open original data file")
return nil, fmt.Errorf("failed to open original data file: %w", err)
}
defer origIn.Close()

var states []file.State
decoder := json.NewDecoder(origIn)
if err := decoder.Decode(&states); err != nil {
return nil, errors.Wrapf(err, "Error decoding original data file '%v'", origDataFile)
return nil, fmt.Errorf("error decoding original data file '%v': %w", origDataFile, err)
}
return states, nil
}()
Expand All @@ -217,18 +215,18 @@ func (m *Migrator) updateToVersion1(regHome string) error {
IgnoreVersionCheck: true,
})
if err != nil {
return errors.Wrap(err, "failed to create new registry backend")
return fmt.Errorf("failed to create new registry backend: %w", err)
}
defer registryBackend.Close()

store, err := registryBackend.Access("filebeat")
if err != nil {
return errors.Wrap(err, "failed to open filebeat registry store")
return fmt.Errorf("failed to open filebeat registry store: %w", err)
}
defer store.Close()

if err := writeStates(store, states); err != nil {
return errors.Wrap(err, "failed to migrate registry states")
return fmt.Errorf("failed to migrate registry states: %w", err)
}

if checkpointer, ok := store.(interface{ Checkpoint() error }); ok {
Expand All @@ -239,7 +237,7 @@ func (m *Migrator) updateToVersion1(regHome string) error {
}

if err := os.Remove(origDataFile); err != nil {
return errors.Wrapf(err, "migration complete but failed to remove original data file: %v", origDataFile)
return fmt.Errorf("migration complete but failed to remove original data file: %v: %w", origDataFile, err)
}

if err := ioutil.WriteFile(filepath.Join(regHome, "meta.json"), []byte(`{"version": "1"}`), m.permissions); err != nil {
Expand All @@ -265,12 +263,12 @@ func readVersion(regHome, migrateFile string) (registryVersion, error) {

tmp, err := ioutil.ReadFile(metaFile)
if err != nil {
return noRegistry, errors.Wrap(err, "failed to open meta file")
return noRegistry, fmt.Errorf("failed to open meta file: %w", err)
}

meta := struct{ Version string }{}
if err := json.Unmarshal(tmp, &meta); err != nil {
return noRegistry, errors.Wrap(err, "failed reading meta file")
return noRegistry, fmt.Errorf("failed reading meta file: %w", err)
}

return registryVersion(meta.Version), nil
Expand Down
6 changes: 3 additions & 3 deletions libbeat/cmd/instance/beat.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func Run(settings Settings, bt beat.Creator) error {
monitoring.NewBool(registry, "elastic_licensed").Set(b.Info.ElasticLicensed)

if u, err := user.Current(); err != nil {
if _, ok := err.(user.UnknownUserIdError); ok {
if _, ok := err.(user.UnknownUserIdError); ok { //nolint:errorlint // keep legacy behaviour
// This usually happens if the user UID does not exist in /etc/passwd. It might be the case on K8S
// if the user set securityContext.runAsUser to an arbitrary value.
monitoring.NewString(registry, "uid").Set(strconv.Itoa(os.Getuid()))
Expand Down Expand Up @@ -356,7 +356,7 @@ func (b *Beat) createBeater(bt beat.Creator) (beat.Beater, error) {
if b.Manager.Enabled() {
logp.Info("Output is configured through Central Management")
} else {
msg := "No outputs are defined. Please define one under the output section."
msg := "no outputs are defined, please define one under the output section"
logp.Info(msg)
return nil, errors.New(msg)
}
Expand All @@ -382,7 +382,7 @@ func (b *Beat) createBeater(bt beat.Creator) (beat.Beater, error) {
publisher, err = pipeline.Load(b.Info, monitors, b.Config.Pipeline, b.processing, outputFactory)
}
if err != nil {
return nil, fmt.Errorf("error initializing publisher: %+v", err)
return nil, fmt.Errorf("error initializing publisher: %w", err)
}

reload.Register.MustRegister("output", b.makeOutputReloader(publisher.OutputReloader()))
Expand Down