diff --git a/internal/build/cmd/generate/commands/genexamples/command.go b/internal/build/cmd/generate/commands/genexamples/command.go index b4e72c9465..72bc4e4aad 100644 --- a/internal/build/cmd/generate/commands/genexamples/command.go +++ b/internal/build/cmd/generate/commands/genexamples/command.go @@ -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" @@ -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) } diff --git a/internal/build/cmd/generate/commands/genexamples/translator.go b/internal/build/cmd/generate/commands/genexamples/translator.go index 56ba0433ce..9090270ad3 100644 --- a/internal/build/cmd/generate/commands/genexamples/translator.go +++ b/internal/build/cmd/generate/commands/genexamples/translator.go @@ -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(")") @@ -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(")") @@ -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(")") @@ -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(")") @@ -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(")") @@ -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)") @@ -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") @@ -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") @@ -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(")") @@ -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) } } @@ -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(")") @@ -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]) @@ -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]) } @@ -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(")") @@ -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(")") @@ -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") } @@ -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") } @@ -1059,7 +1059,7 @@ var ConsoleToGo = []TranslateRule{ } var hasPretty bool - for k, _ := range params { + for k := range params { if k == "pretty" { hasPretty = true } @@ -1112,7 +1112,7 @@ var ConsoleToGo = []TranslateRule{ } var hasPretty bool - for k, _ := range params { + for k := range params { if k == "pretty" { hasPretty = true } @@ -1165,7 +1165,7 @@ var ConsoleToGo = []TranslateRule{ } var hasPretty bool - for k, _ := range params { + for k := range params { if k == "pretty" { hasPretty = true } @@ -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)") @@ -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)") @@ -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)") @@ -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)") @@ -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)") @@ -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)") @@ -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]) } @@ -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": diff --git a/internal/build/cmd/generate/commands/gensource/command.go b/internal/build/cmd/generate/commands/gensource/command.go index 53ddb47b50..ac9498801a 100644 --- a/internal/build/cmd/generate/commands/gensource/command.go +++ b/internal/build/cmd/generate/commands/gensource/command.go @@ -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" @@ -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])) @@ -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) @@ -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") } diff --git a/internal/build/cmd/generate/commands/gensource/generator.go b/internal/build/cmd/generate/commands/gensource/generator.go index 19a366e6b3..de2d04ee4a 100644 --- a/internal/build/cmd/generate/commands/gensource/generator.go +++ b/internal/build/cmd/generate/commands/gensource/generator.go @@ -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) diff --git a/internal/build/cmd/generate/commands/gensource/generator_test.go b/internal/build/cmd/generate/commands/gensource/generator_test.go index 13975af498..700bc869fe 100644 --- a/internal/build/cmd/generate/commands/gensource/generator_test.go +++ b/internal/build/cmd/generate/commands/gensource/generator_test.go @@ -18,7 +18,7 @@ package gensource_test import ( - "io/ioutil" + "io" "os" "strings" "testing" @@ -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) } diff --git a/internal/build/cmd/generate/commands/genstruct/command.go b/internal/build/cmd/generate/commands/genstruct/command.go index 15aeba43c1..18656a4715 100644 --- a/internal/build/cmd/generate/commands/genstruct/command.go +++ b/internal/build/cmd/generate/commands/genstruct/command.go @@ -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" diff --git a/internal/build/cmd/generate/commands/gentests/command.go b/internal/build/cmd/generate/commands/gentests/command.go index 80988a7616..5565d2a4f3 100644 --- a/internal/build/cmd/generate/commands/gentests/command.go +++ b/internal/build/cmd/generate/commands/gentests/command.go @@ -107,11 +107,11 @@ func (cmd *Command) Execute() error { 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])) @@ -119,7 +119,7 @@ func (cmd *Command) Execute() error { return err } if EsVersion == "" { - return errors.New("Elasticsearch version is empty") + return errors.New("elasticsearch version is empty") } if utils.IsTTY() { @@ -132,7 +132,7 @@ func (cmd *Command) Execute() error { GitCommit, err = utils.GitCommit(filepath.Dir(inputFiles[0])) if err != nil { - return fmt.Errorf("Failed to get Git commit: %s", err) + return fmt.Errorf("failed to get Git commit: %s", err) } GitTag, _ = utils.GitTag(filepath.Dir(inputFiles[0])) @@ -172,7 +172,7 @@ func (cmd *Command) Execute() error { } if err := cmd.processFile(fpath); err != nil { - return fmt.Errorf("Processing file %q: %s", fpath, err) + return fmt.Errorf("processing file %q: %s", fpath, err) } stats.n++ } diff --git a/internal/build/cmd/generate/commands/gentests/debug.go b/internal/build/cmd/generate/commands/gentests/debug.go index 69adb4a0b7..53eadbc7cc 100644 --- a/internal/build/cmd/generate/commands/gentests/debug.go +++ b/internal/build/cmd/generate/commands/gentests/debug.go @@ -83,9 +83,8 @@ func (tg TestSuite) DebugInfo() string { fmt.Fprint(&out, a.Method()+"()\n") } for _, a := range t.Steps { - switch a.(type) { + switch aa := a.(type) { case Action: - aa := a.(Action) fmt.Fprintf(&out, " ==> ") fmt.Fprint(&out, aa.Method()+"(") var i int @@ -98,7 +97,6 @@ func (tg TestSuite) DebugInfo() string { } fmt.Fprint(&out, ")\n") case Assertion: - aa := a.(Assertion) fmt.Fprintf(&out, " ~~> ") fmt.Fprintf(&out, "%q: %s", aa.operation, aa.payload) fmt.Fprint(&out, "\n") diff --git a/internal/build/cmd/generate/commands/gentests/generator.go b/internal/build/cmd/generate/commands/gentests/generator.go index 2f70c9a2a4..b840c83482 100644 --- a/internal/build/cmd/generate/commands/gentests/generator.go +++ b/internal/build/cmd/generate/commands/gentests/generator.go @@ -905,16 +905,16 @@ func (g *Generator) genSteps(t Test) { g.genVarSection(t, skipBody) for _, step := range t.Steps { - switch step.(type) { + switch step := step.(type) { case Action: // Generate debug info var dbg strings.Builder - dbg.WriteString("\t\t// => " + step.(Action).Method() + "(") + dbg.WriteString("\t\t// => " + step.Method() + "(") var j int - for k, v := range step.(Action).Params() { + for k, v := range step.Params() { j++ dbg.WriteString(k + ": " + strings.Replace(fmt.Sprintf("%v", v), "\n", "|", -1)) - if j < len(step.(Action).Params()) { + if j < len(step.Params()) { dbg.WriteString(", ") } } @@ -926,20 +926,20 @@ func (g *Generator) genSteps(t Test) { g.w(dbg.String() + strings.Repeat("-", pad) + "\n\t\t//\n") // Generate the action - g.genAction(step.(Action), skipBody) + g.genAction(step, skipBody) g.w("\t\t// " + strings.Repeat("-", 96) + "\n\n") case Assertion: // Generate debug info g.w("\t\t// ~> ") - g.w(fmt.Sprintf("%q: ", step.(Assertion).operation)) - g.w(strings.Replace(fmt.Sprintf("%s", step.(Assertion).payload), "\n", "|", -1)) + g.w(fmt.Sprintf("%q: ", step.operation)) + g.w(strings.Replace(fmt.Sprintf("%s", step.payload), "\n", "|", -1)) g.w("\n") // Generate the assertion - g.genAssertion(step.(Assertion)) + g.genAssertion(step) g.w("\n") case Stash: // Generate setting the stash - g.genStashSet(step.(Stash)) + g.genStashSet(step) g.w("\n") default: panic(fmt.Sprintf("Unknown step %T", step)) @@ -955,7 +955,7 @@ func (g *Generator) genVarSection(t Test, skipBody ...bool) { g.w("\t\t\tstash = make(map[string]interface{}, 0)\n\n") - if (len(skipBody) < 1 || (len(skipBody) > 0 && skipBody[0] == false)) && + if (len(skipBody) < 1 || (len(skipBody) > 0 && !skipBody[0])) && (t.Steps.ContainsAssertion() || t.Steps.ContainsCatch() || true) { g.w("\t\t\tbody []byte\n") g.w("\t\t\tmapi map[string]interface{}\n") @@ -980,7 +980,7 @@ func (g *Generator) genVarSection(t Test, skipBody ...bool) { g.w("\t\t)\n\n") - if (len(skipBody) < 1 || (len(skipBody) > 0 && skipBody[0] == false)) && + if (len(skipBody) < 1 || (len(skipBody) > 0 && !skipBody[0])) && (t.Steps.ContainsAssertion() || t.Steps.ContainsCatch() || true) { g.w("\t\t_ = mapi\n") g.w("\t\t_ = slic\n") @@ -1073,7 +1073,7 @@ func (g *Generator) genAction(a Action, skipBody ...bool) { v = `stash[` + strconv.Quote(strings.ReplaceAll(strings.ReplaceAll(fmt.Sprintf("%s", v), "{", ""), "}", "")) + `]` } - switch v.(type) { + switch vv := v.(type) { case bool: g.w("\t\t\t" + k + ": ") @@ -1084,23 +1084,23 @@ func (g *Generator) genAction(a Action, skipBody ...bool) { switch typ { case "bool": - g.w(strconv.FormatBool(v.(bool))) + g.w(strconv.FormatBool(vv)) case "*bool": - g.w(`esapi.BoolPtr(` + strconv.FormatBool(v.(bool)) + `)`) + g.w(`esapi.BoolPtr(` + strconv.FormatBool(vv) + `)`) case "string": - g.w(`"` + strconv.FormatBool(v.(bool)) + `"`) + g.w(`"` + strconv.FormatBool(vv) + `"`) case "[]string": // TODO: Listify - g.w(`[]string{"` + strconv.FormatBool(v.(bool)) + `"}`) + g.w(`[]string{"` + strconv.FormatBool(vv) + `"}`) default: - g.w(strconv.FormatBool(v.(bool))) + g.w(strconv.FormatBool(vv)) } g.w(",\n") case string: if k == "Body" { g.w("\t\t\t" + k + ": ") - body := v.(string) + body := vv if varDetection.MatchString(body) { g.w("strings.NewReader(strings.NewReplacer(") @@ -1230,13 +1230,11 @@ func (g *Generator) genAction(a Action, skipBody ...bool) { re := regexp.MustCompile("^(\\d+).*") value = re.ReplaceAllString(fmt.Sprintf("%d", v), "$1") case "*int": - switch v.(type) { + switch vv := v.(type) { case int: g.w(`esapi.IntPtr(` + fmt.Sprintf("%d", v) + `)`) case float64: - if vv, ok := v.(float64); ok { g.w(`esapi.IntPtr(` + fmt.Sprintf("%d", int(vv)) + `)`) - } default: panic(fmt.Sprintf("Unexpected type [%T] for [%s]", v, k)) } @@ -1256,12 +1254,12 @@ func (g *Generator) genAction(a Action, skipBody ...bool) { switch typ { case "string": - switch v.(type) { + switch v := v.(type) { case string: - g.w("`" + v.(string) + "`") + g.w("`" + v + "`") case []interface{}: vvv := make([]string, 0) - for _, vv := range v.([]interface{}) { + for _, vv := range v { vvv = append(vvv, fmt.Sprintf("%s", vv)) } g.w("`" + strings.Join(vvv, ",") + "`") @@ -1280,9 +1278,9 @@ func (g *Generator) genAction(a Action, skipBody ...bool) { if k == "Body" { var b strings.Builder for _, vv := range v.([]interface{}) { - switch vv.(type) { + switch vv := vv.(type) { case string: - b.WriteString(vv.(string)) + b.WriteString(vv) default: j, err := json.Marshal(convert(vv)) if err != nil { @@ -1300,13 +1298,13 @@ func (g *Generator) genAction(a Action, skipBody ...bool) { if err != nil { panic(fmt.Sprintf("%s{}.%s: %s (%s)", a.Request(), k, err, v)) } - g.w("\t\tstrings.NewReader(`" + fmt.Sprintf("%s", j) + "`)") + g.w("\t\tstrings.NewReader(`" + string(j) + "`)") } case "*bool": - switch v.(type) { + switch v := v.(type) { case []interface{}: var vvv string - for _, vv := range v.([]interface{}) { + for _, vv := range v { vvv = fmt.Sprintf("%v", vv) } g.w(fmt.Sprintf("&[]bool{%s}[0]", vvv)) @@ -1328,7 +1326,7 @@ func (g *Generator) genAction(a Action, skipBody ...bool) { reStash := regexp.MustCompile(`("\$[^"]+")`) j = reStash.ReplaceAll(j, []byte("` + strconv.Quote(fmt.Sprintf(\"%v\", stash[$1])) + `")) - g.w("\t\tstrings.NewReader(`" + fmt.Sprintf("%s", j) + "`)") + g.w("\t\tstrings.NewReader(`" + string(j) + "`)") g.w(",\n") } @@ -1388,7 +1386,7 @@ func (g *Generator) genAction(a Action, skipBody ...bool) { // TODO: Test catch } - if len(skipBody) < 1 || (len(skipBody) > 0 && skipBody[0] == false) { + if len(skipBody) < 1 || (len(skipBody) > 0 && !skipBody[0]) { // Read and parse the body g.w(` handleResponseBody(t, res)` + "\n") } @@ -1446,9 +1444,9 @@ func convert(i interface{}) interface{} { m2 := map[string]interface{}{} for k, v := range x { var ks string - switch k.(type) { + switch k := k.(type) { case string: - ks = k.(string) + ks = k case int: ks = fmt.Sprintf("%d", k) default: diff --git a/internal/build/cmd/generate/commands/gentests/model.go b/internal/build/cmd/generate/commands/gentests/model.go index a306d7614f..ae241cd18e 100644 --- a/internal/build/cmd/generate/commands/gentests/model.go +++ b/internal/build/cmd/generate/commands/gentests/model.go @@ -396,15 +396,15 @@ func (a Action) Params() map[string]interface{} { default: kk = utils.NameToGo(k.(string), utils.APIToGo(a.method)) } - switch v.(type) { + switch v := v.(type) { case bool: - out[kk] = v.(bool) + out[kk] = v case string: - out[kk] = v.(string) + out[kk] = v case int: - out[kk] = v.(int) + out[kk] = v case float64: - out[kk] = v.(float64) + out[kk] = v default: out[kk] = v } @@ -739,7 +739,7 @@ func (a Assertion) Error() string { expected string ) - output = `Unexpected value; <` + a.operation + `> : ` + fmt.Sprintf("%s", escape(a.payload)) + output = `Unexpected value; <` + a.operation + `> : ` + escape(a.payload) switch a.operation { case "is_true": diff --git a/internal/build/cmd/generate/commands/gentests/patches.go b/internal/build/cmd/generate/commands/gentests/patches.go index 6840547dc0..521226cd5f 100644 --- a/internal/build/cmd/generate/commands/gentests/patches.go +++ b/internal/build/cmd/generate/commands/gentests/patches.go @@ -20,7 +20,6 @@ package gentests import ( "bytes" "io" - "io/ioutil" "regexp" "strings" ) @@ -34,7 +33,7 @@ var ( // PatchTestSource performs a regex based patching of the input. // func PatchTestSource(fpath string, r io.Reader) (io.Reader, error) { - c, err := ioutil.ReadAll(r) + c, err := io.ReadAll(r) if err != nil { return nil, err } diff --git a/internal/build/cmd/tools/commands/spec/command.go b/internal/build/cmd/tools/commands/spec/command.go index 38934a81d0..6d5b4ccf7f 100644 --- a/internal/build/cmd/tools/commands/spec/command.go +++ b/internal/build/cmd/tools/commands/spec/command.go @@ -22,11 +22,7 @@ import ( "bytes" "encoding/json" "fmt" - "github.com/elastic/go-elasticsearch/v8/internal/build/cmd" - "github.com/elastic/go-elasticsearch/v8/internal/build/utils" - "github.com/elastic/go-elasticsearch/v8/internal/version" - "github.com/spf13/cobra" - "io/ioutil" + "io" "log" "net/http" "net/url" @@ -34,6 +30,11 @@ import ( "path/filepath" "strings" "time" + + "github.com/elastic/go-elasticsearch/v8/internal/build/cmd" + "github.com/elastic/go-elasticsearch/v8/internal/build/utils" + "github.com/elastic/go-elasticsearch/v8/internal/version" + "github.com/spf13/cobra" ) var ( @@ -139,7 +140,7 @@ func (c Command) Execute() (err error) { func (c Command) writeFileToDest(filename string, data []byte) error { path := filepath.Join(c.Output, filename) - if err := ioutil.WriteFile(path, data, 0644); err != nil { + if err := os.WriteFile(path, data, 0644); err != nil { return fmt.Errorf("cannot write file: %s", err) } if c.Debug { @@ -238,7 +239,7 @@ func (c Command) extractZipToDest(data []byte) error { path := filepath.Join(c.Output, file.Name) _ = os.MkdirAll(path, 0744) } else { - data, err := ioutil.ReadAll(f) + data, err := io.ReadAll(f) if err != nil { return err } @@ -277,7 +278,7 @@ func (c Command) downloadZip(b Build) ([]byte, error) { } defer res.Body.Close() - data, _ := ioutil.ReadAll(res.Body) + data, _ := io.ReadAll(res.Body) return data, err } diff --git a/internal/build/go.mod b/internal/build/go.mod index 067e5ec37b..a3254d9c05 100644 --- a/internal/build/go.mod +++ b/internal/build/go.mod @@ -1,6 +1,7 @@ module github.com/elastic/go-elasticsearch/v8/internal/build go 1.21 + toolchain go1.21.0 replace github.com/elastic/go-elasticsearch/v8 => ../../ @@ -9,7 +10,8 @@ require ( github.com/alecthomas/chroma v0.10.0 github.com/elastic/go-elasticsearch/v8 v8.0.0-20210817150010-57d659deaca7 github.com/spf13/cobra v1.8.0 - golang.org/x/crypto v0.19.0 + golang.org/x/term v0.17.0 + golang.org/x/text v0.16.0 golang.org/x/tools v0.22.0 gopkg.in/yaml.v2 v2.4.0 ) @@ -21,5 +23,4 @@ require ( golang.org/x/mod v0.18.0 // indirect golang.org/x/sync v0.7.0 // indirect golang.org/x/sys v0.21.0 // indirect - golang.org/x/term v0.17.0 // indirect ) diff --git a/internal/build/go.sum b/internal/build/go.sum index f352adc484..3e6324fbea 100644 --- a/internal/build/go.sum +++ b/internal/build/go.sum @@ -16,11 +16,8 @@ github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyh github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= -github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= -golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo= -golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0= golang.org/x/mod v0.18.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= @@ -29,6 +26,8 @@ golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U= golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk= +golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= +golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= golang.org/x/tools v0.22.0 h1:gqSGLZqv+AI9lIQzniJ0nZDRG5GBPsSi+DRNHWNz6yA= golang.org/x/tools v0.22.0/go.mod h1:aCwcsjqvq7Yqt6TNyX7QMU2enbQ/Gt0bo6krSeEri+c= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= diff --git a/internal/build/utils/chromatize.go b/internal/build/utils/chromatize.go index 389fcc3625..0966e0283d 100644 --- a/internal/build/utils/chromatize.go +++ b/internal/build/utils/chromatize.go @@ -20,7 +20,6 @@ package utils import ( "bytes" "io" - "io/ioutil" "github.com/alecthomas/chroma/formatters" "github.com/alecthomas/chroma/lexers" @@ -33,7 +32,7 @@ func Chromatize(r io.Reader) (io.Reader, error) { var b bytes.Buffer lexer := lexers.Get("go") - contents, err := ioutil.ReadAll(r) + contents, err := io.ReadAll(r) if err != nil { return nil, err } diff --git a/internal/build/utils/debug.go b/internal/build/utils/debug.go index 1c2ec1d9ac..538c1c24f6 100644 --- a/internal/build/utils/debug.go +++ b/internal/build/utils/debug.go @@ -78,7 +78,7 @@ func PrintSourceWithErr(out io.Reader, err error) { if IsTTY() { fmt.Fprint(os.Stderr, "\x1b[31m") } - fmt.Fprintf(os.Stderr, strings.Repeat(" ", 4)) + fmt.Fprint(os.Stderr, strings.Repeat(" ", 4)) for i := 0; i < e.Pos.Column; i++ { fmt.Fprintf(os.Stderr, "-") } diff --git a/internal/build/utils/naming.go b/internal/build/utils/naming.go index 03bfd6e94a..7779560791 100644 --- a/internal/build/utils/naming.go +++ b/internal/build/utils/naming.go @@ -17,7 +17,12 @@ package utils -import "strings" +import ( + "strings" + + "golang.org/x/text/cases" + "golang.org/x/text/language" +) var ( // Options: boolean, enum, list, number, string, time @@ -115,13 +120,14 @@ func NameToGo(s string, api ...string) string { ep := strings.Split(s, "_") ns := make([]string, len(ep)) + caser := cases.Title(language.Und) for _, v := range ep { if value, ok := acronyms[v]; ok { v = value } else if v == "uuid" { v = "UUID" } - ns = append(ns, strings.Title(v)) + ns = append(ns, caser.String(v)) } return strings.Join(ns, "") } diff --git a/internal/build/utils/terminal.go b/internal/build/utils/terminal.go index 641bed76c6..54493f0ba1 100644 --- a/internal/build/utils/terminal.go +++ b/internal/build/utils/terminal.go @@ -21,7 +21,7 @@ import ( "fmt" "os" - "golang.org/x/crypto/ssh/terminal" + "golang.org/x/term" ) var ( @@ -30,8 +30,8 @@ var ( ) func init() { - isTTY = terminal.IsTerminal(int(os.Stderr.Fd())) - tWidth, _, _ = terminal.GetSize(int(os.Stdout.Fd())) + isTTY = term.IsTerminal(int(os.Stderr.Fd())) + tWidth, _, _ = term.GetSize(int(os.Stdout.Fd())) } // PrintErr prints an error to STDERR.