Skip to content

chore: update deprecated features and improve code quality for /internal/build #890

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions internal/build/cmd/generate/commands/genexamples/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/elastic/go-elasticsearch/v8/internal/build/cmd"
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"time"

"github.com/elastic/go-elasticsearch/v8/internal/build/cmd"

"github.com/spf13/cobra"
"golang.org/x/tools/imports"

Expand Down Expand Up @@ -330,7 +330,7 @@ func (cmd *DocCommand) Execute() error {
}
}

files, err := ioutil.ReadDir(cmd.Input)
files, err := os.ReadDir(cmd.Input)
if err != nil {
return fmt.Errorf("failed to read input: %s", err)
}
Expand Down
56 changes: 28 additions & 28 deletions internal/build/cmd/generate/commands/genexamples/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ var ConsoleToGo = []TranslateRule{
if err != nil {
return "", fmt.Errorf("error converting params to arguments: %s", err)
}
fmt.Fprintf(&src, args)
fmt.Fprint(&src, args)
}

src.WriteString(")")
Expand Down Expand Up @@ -116,7 +116,7 @@ var ConsoleToGo = []TranslateRule{
if err != nil {
return "", fmt.Errorf("error converting params to arguments: %s", err)
}
fmt.Fprintf(&src, args)
fmt.Fprint(&src, args)
}

src.WriteString(")")
Expand Down Expand Up @@ -149,7 +149,7 @@ var ConsoleToGo = []TranslateRule{
if err != nil {
return "", fmt.Errorf("error converting params to arguments: %s", err)
}
fmt.Fprintf(&src, args)
fmt.Fprint(&src, args)
}

src.WriteString(")")
Expand Down Expand Up @@ -183,7 +183,7 @@ var ConsoleToGo = []TranslateRule{
if err != nil {
return "", fmt.Errorf("error converting params to arguments: %s", err)
}
fmt.Fprintf(&src, args)
fmt.Fprint(&src, args)
}

src.WriteString(")")
Expand Down Expand Up @@ -228,7 +228,7 @@ var ConsoleToGo = []TranslateRule{
if err != nil {
return "", fmt.Errorf("error converting params to arguments: %s", err)
}
fmt.Fprintf(&src, args)
fmt.Fprint(&src, args)
}

src.WriteString(")")
Expand Down Expand Up @@ -268,7 +268,7 @@ var ConsoleToGo = []TranslateRule{
if err != nil {
return "", fmt.Errorf("error converting params to arguments: %s", err)
}
fmt.Fprintf(&src, args)
fmt.Fprint(&src, args)
}

src.WriteString("\t)")
Expand Down Expand Up @@ -380,7 +380,7 @@ var ConsoleToGo = []TranslateRule{
case "_update":
apiName = "Update"
default:
return "", fmt.Errorf("Unknown API [%s]", matches[3])
return "", fmt.Errorf("unknown API [%s]", matches[3])
}

src.WriteString("\tres, err := es." + apiName + "(\n")
Expand Down Expand Up @@ -412,7 +412,7 @@ var ConsoleToGo = []TranslateRule{
if err != nil {
return "", fmt.Errorf("error converting params to arguments: %s", err)
}
fmt.Fprintf(&src, args)
fmt.Fprint(&src, args)
}

src.WriteString("\tes." + apiName + ".WithPretty(),\n")
Expand Down Expand Up @@ -446,7 +446,7 @@ var ConsoleToGo = []TranslateRule{
if err != nil {
return "", fmt.Errorf("error converting params to arguments: %s", err)
}
fmt.Fprintf(&src, args)
fmt.Fprint(&src, args)
}

src.WriteString(")")
Expand Down Expand Up @@ -602,7 +602,7 @@ var ConsoleToGo = []TranslateRule{
if err != nil {
return "", fmt.Errorf("error converting params to arguments: %s", err)
}
fmt.Fprintf(&src, args)
fmt.Fprint(&src, args)
}
}

Expand Down Expand Up @@ -635,7 +635,7 @@ var ConsoleToGo = []TranslateRule{
if err != nil {
return "", fmt.Errorf("error converting params to arguments: %s", err)
}
fmt.Fprintf(&src, args)
fmt.Fprint(&src, args)
}

src.WriteString(")")
Expand Down Expand Up @@ -674,7 +674,7 @@ var ConsoleToGo = []TranslateRule{
if err != nil {
return "", fmt.Errorf("error converting params to arguments: %s", err)
}
fmt.Fprintf(&src, args)
fmt.Fprint(&src, args)
}
} else {
fmt.Fprintf(&src, "%q", matches[1])
Expand Down Expand Up @@ -793,7 +793,7 @@ var ConsoleToGo = []TranslateRule{
if err != nil {
return "", fmt.Errorf("error converting params to arguments: %s", err)
}
fmt.Fprintf(&src, args)
fmt.Fprint(&src, args)
} else {
fmt.Fprintf(&src, "[]string{%q}", matches[1])
}
Expand Down Expand Up @@ -896,7 +896,7 @@ var ConsoleToGo = []TranslateRule{
if err != nil {
return "", fmt.Errorf("error converting params to arguments: %s", err)
}
fmt.Fprintf(&src, args)
fmt.Fprint(&src, args)
}

src.WriteString(")")
Expand Down Expand Up @@ -929,7 +929,7 @@ var ConsoleToGo = []TranslateRule{
if err != nil {
return "", fmt.Errorf("error converting params to arguments: %s", err)
}
fmt.Fprintf(&src, args)
fmt.Fprint(&src, args)
}

src.WriteString(")")
Expand Down Expand Up @@ -970,7 +970,7 @@ var ConsoleToGo = []TranslateRule{
if err != nil {
return "", fmt.Errorf("error converting params to arguments: %s", err)
}
fmt.Fprintf(&src, args)
fmt.Fprint(&src, args)

src.WriteString("\tes." + apiName + ".WithPretty(),\n")
}
Expand Down Expand Up @@ -1013,7 +1013,7 @@ var ConsoleToGo = []TranslateRule{
if err != nil {
return "", fmt.Errorf("error converting params to arguments: %s", err)
}
fmt.Fprintf(&src, args)
fmt.Fprint(&src, args)

src.WriteString("\tes." + apiName + ".WithPretty(),\n")
}
Expand Down Expand Up @@ -1059,7 +1059,7 @@ var ConsoleToGo = []TranslateRule{
}

var hasPretty bool
for k, _ := range params {
for k := range params {
if k == "pretty" {
hasPretty = true
}
Expand Down Expand Up @@ -1112,7 +1112,7 @@ var ConsoleToGo = []TranslateRule{
}

var hasPretty bool
for k, _ := range params {
for k := range params {
if k == "pretty" {
hasPretty = true
}
Expand Down Expand Up @@ -1165,7 +1165,7 @@ var ConsoleToGo = []TranslateRule{
}

var hasPretty bool
for k, _ := range params {
for k := range params {
if k == "pretty" {
hasPretty = true
}
Expand Down Expand Up @@ -1213,7 +1213,7 @@ var ConsoleToGo = []TranslateRule{
if err != nil {
return "", fmt.Errorf("error converting params to arguments: %s", err)
}
fmt.Fprintf(&src, args)
fmt.Fprint(&src, args)
}

src.WriteString("\t)")
Expand Down Expand Up @@ -1296,7 +1296,7 @@ var ConsoleToGo = []TranslateRule{
if err != nil {
return "", fmt.Errorf("error converting params to arguments: %s", err)
}
fmt.Fprintf(&src, args)
fmt.Fprint(&src, args)
}

src.WriteString("\t)")
Expand Down Expand Up @@ -1335,7 +1335,7 @@ var ConsoleToGo = []TranslateRule{
if err != nil {
return "", fmt.Errorf("error converting params to arguments: %s", err)
}
fmt.Fprintf(&src, args)
fmt.Fprint(&src, args)
}

src.WriteString("\t)")
Expand Down Expand Up @@ -1407,7 +1407,7 @@ var ConsoleToGo = []TranslateRule{
if err != nil {
return "", fmt.Errorf("error converting params to arguments: %s", err)
}
fmt.Fprintf(&src, args)
fmt.Fprint(&src, args)
}

src.WriteString("\t)")
Expand Down Expand Up @@ -1438,7 +1438,7 @@ var ConsoleToGo = []TranslateRule{
if err != nil {
return "", fmt.Errorf("error converting params to arguments: %s", err)
}
fmt.Fprintf(&src, args)
fmt.Fprint(&src, args)
}

src.WriteString("\t)")
Expand Down Expand Up @@ -1472,7 +1472,7 @@ var ConsoleToGo = []TranslateRule{
if err != nil {
return "", fmt.Errorf("error converting params to arguments: %s", err)
}
fmt.Fprintf(&src, args)
fmt.Fprint(&src, args)
}

src.WriteString("\t)")
Expand Down Expand Up @@ -1503,7 +1503,7 @@ var ConsoleToGo = []TranslateRule{
if err != nil {
return "", fmt.Errorf("error converting params to arguments: %s", err)
}
fmt.Fprintf(&src, args)
fmt.Fprint(&src, args)
} else {
fmt.Fprintf(&src, "[]string{%q}", matches[1])
}
Expand Down Expand Up @@ -1648,7 +1648,7 @@ func paramsToArguments(api string, params url.Values) (string, error) {
}
value = fmt.Sprintf("time.Duration(%d)", time.Duration(dur))
case "from", "size", "terminate_after", "version", "requests_per_second", "scroll_size", "max_num_segments": // numeric
value = fmt.Sprintf("%s", value)
value = fmt.Sprint(value)
case "pretty", "error_trace":
value = "" // Helper methods don't take any value
case "refresh":
Expand Down
11 changes: 6 additions & 5 deletions internal/build/cmd/generate/commands/gensource/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ package gensource
import (
"bytes"
"fmt"
"github.com/elastic/go-elasticsearch/v8/internal/build/cmd"
"io"
"os"
"path/filepath"
"strings"
"time"

"github.com/elastic/go-elasticsearch/v8/internal/build/cmd"

"github.com/spf13/cobra"

"github.com/elastic/go-elasticsearch/v8/internal/build/utils"
Expand Down Expand Up @@ -103,12 +104,12 @@ func (cmd *Command) Execute() (err error) {
} else {
inputFiles, err = filepath.Glob(cmd.Input)
if err != nil {
return fmt.Errorf("Failed to glob input %q: %s", cmd.Input, err)
return fmt.Errorf("failed to glob input %q: %s", cmd.Input, err)
}
}

if len(inputFiles) < 1 {
return fmt.Errorf("No files matching input %q", cmd.Input)
return fmt.Errorf("no files matching input %q", cmd.Input)
}

EsVersion, err = utils.EsVersion(filepath.Dir(inputFiles[0]))
Expand Down Expand Up @@ -157,7 +158,7 @@ func (cmd *Command) Execute() (err error) {
}

if err := cmd.processFile(f); err != nil {
return fmt.Errorf("Processing file %q: %s", fname, err)
return fmt.Errorf("processing file %q: %s", fname, err)
}

f.Seek(0, 0)
Expand Down Expand Up @@ -220,7 +221,7 @@ func (cmd *Command) processFile(f *os.File) (err error) {
if utils.IsTTY() {
fmt.Fprint(os.Stderr, "\x1b[2m")
}
fmt.Fprintf(os.Stderr, gen.Endpoint.DebugInfo())
fmt.Fprint(os.Stderr, gen.Endpoint.DebugInfo())
if utils.IsTTY() {
fmt.Fprint(os.Stderr, "\x1b[0m")
}
Expand Down
8 changes: 2 additions & 6 deletions internal/build/cmd/generate/commands/gensource/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,12 +334,8 @@ func (f ` + g.Endpoint.MethodWithNamespace() + `) WithContext(v context.Context)
)

// Adjust descriptions
if strings.Contains(pDesc, "a comma-separated list") {
pDesc = strings.Replace(pDesc, "a comma-separated list", "a list", -1)
}
if strings.Contains(pDesc, "use `_all` or empty string") {
pDesc = strings.Replace(pDesc, "use `_all` or empty string", "use _all", -1)
}
pDesc = strings.Replace(pDesc, "a comma-separated list", "a list", -1)
pDesc = strings.Replace(pDesc, "use `_all` or empty string", "use _all", -1)

// Generate annotation
b.WriteString("\n// With" + pFieldName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package gensource_test

import (
"io/ioutil"
"io"
"os"
"strings"
"testing"
Expand All @@ -45,7 +45,7 @@ func TestGenerator(t *testing.T) {
t.Fatalf("Error generating output for %q: %s", f.Name(), err)
}

s, err := ioutil.ReadAll(out)
s, err := io.ReadAll(out)
if err != nil {
t.Fatalf("Error reading output for %q: %s", f.Name(), err)
}
Expand Down
3 changes: 2 additions & 1 deletion internal/build/cmd/generate/commands/genstruct/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ package genstruct
import (
"bytes"
"fmt"
"github.com/elastic/go-elasticsearch/v8/internal/build/cmd"
"go/types"
"io"
"os"
"path/filepath"
"strings"

"github.com/elastic/go-elasticsearch/v8/internal/build/cmd"

"golang.org/x/tools/go/packages"
"golang.org/x/tools/imports"

Expand Down
Loading