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

Miscellaneous code cleanup #2397

Merged
merged 12 commits into from
May 17, 2023
85 changes: 0 additions & 85 deletions provider/cmd/pulumi-gen-kubernetes/copy.go

This file was deleted.

6 changes: 4 additions & 2 deletions provider/cmd/pulumi-gen-kubernetes/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"io"
"net/http"
"os"

"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
)

func DownloadFile(filepath string, url string) (err error) {
Expand All @@ -32,15 +34,15 @@ func DownloadFile(filepath string, url string) (err error) {
if err != nil {
return err
}
defer out.Close()
defer contract.IgnoreClose(out)

// Get the data
// nolint: gosec
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
defer contract.IgnoreClose(resp.Body)

// Check server response
if resp.StatusCode != http.StatusOK {
Expand Down
15 changes: 7 additions & 8 deletions provider/cmd/pulumi-gen-kubernetes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"flag"
"fmt"
"go/format"
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -109,7 +108,7 @@ func main() {

func readSchema(schemaPath string, version string) *schema.Package {
// Read in, decode, and import the schema.
schemaBytes, err := ioutil.ReadFile(schemaPath)
schemaBytes, err := os.ReadFile(schemaPath)
if err != nil {
panic(err)
}
Expand All @@ -128,7 +127,7 @@ func readSchema(schemaPath string, version string) *schema.Package {
}

func generateSchema(swaggerPath string) schema.PackageSpec {
swagger, err := ioutil.ReadFile(swaggerPath)
swagger, err := os.ReadFile(swaggerPath)
if err != nil {
panic(err)
}
Expand All @@ -150,7 +149,7 @@ func generateSchema(swaggerPath string) schema.PackageSpec {
if err != nil {
panic(err)
}
legacySwagger, err := ioutil.ReadFile(legacySwaggerPath)
legacySwagger, err := os.ReadFile(legacySwaggerPath)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -331,7 +330,7 @@ func writeDotnetClient(pkg *schema.Package, outdir, templateDir string) {
if err = os.MkdirAll(filepath.Dir(path), 0755); err != nil {
panic(err)
}
err := ioutil.WriteFile(path, contents, 0644)
err := os.WriteFile(path, contents, 0644)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -409,7 +408,7 @@ func writeGoClient(pkg *schema.Package, outdir string, templateDir string) {
}

func mustLoadFile(path string) []byte {
b, err := ioutil.ReadFile(path)
b, err := os.ReadFile(path)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -453,7 +452,7 @@ func genK8sResourceTypes(pkg *schema.Package) {
continue
}
parts := strings.Split(resource.Token, ":")
contract.Assert(len(parts) == 3)
contract.Assertf(len(parts) == 3, "expected resource token to have three elements: %s", resource.Token)

groupVersion, kind := parts[1], parts[2]

Expand Down Expand Up @@ -491,7 +490,7 @@ func mustWriteFile(rootDir, filename string, contents []byte) {
if err := os.MkdirAll(filepath.Dir(outPath), 0755); err != nil {
panic(err)
}
err := ioutil.WriteFile(outPath, contents, 0644)
err := os.WriteFile(outPath, contents, 0644)
if err != nil {
panic(err)
}
Expand Down
5 changes: 2 additions & 3 deletions provider/cmd/pulumi-resource-kubernetes/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package main

import (
"encoding/json"
"io/ioutil"
"log"
"os"

Expand All @@ -32,7 +31,7 @@ func main() {
log.Fatal("version not found")
}

schemaContents, err := ioutil.ReadFile("./schema.json")
schemaContents, err := os.ReadFile("./schema.json")
if err != nil {
log.Fatal(err)
}
Expand All @@ -49,7 +48,7 @@ func main() {
log.Fatalf("cannot reserialize schema: %v", err)
}

err = ioutil.WriteFile("./schema-embed.json", versionedContents, 0600)
err = os.WriteFile("./schema-embed.json", versionedContents, 0600)
if err != nil {
log.Fatal(err)
}
Expand Down
7 changes: 4 additions & 3 deletions provider/pkg/gen/_go-templates/yaml/yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ package yaml

import (
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"os"
"path/filepath"

"github.com/pkg/errors"
Expand Down Expand Up @@ -52,7 +53,7 @@ func parseDecodeYamlFiles(ctx *pulumi.Context, args *ConfigGroupArgs, glob bool,
return nil, errors.Wrapf(err, "fetching YAML over network")
}
defer resp.Body.Close()
yaml, err = ioutil.ReadAll(resp.Body)
yaml, err = io.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrapf(err, "reading YAML over network")
}
Expand All @@ -70,7 +71,7 @@ func parseDecodeYamlFiles(ctx *pulumi.Context, args *ConfigGroupArgs, glob bool,
files = []string{file}
}
for _, f := range files {
yaml, err = ioutil.ReadFile(f)
yaml, err = os.ReadFile(f)
if err != nil {
return nil, errors.Wrapf(err, "reading YAML file from disk")
}
Expand Down
20 changes: 9 additions & 11 deletions provider/pkg/gen/examples/upstream/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand All @@ -25,7 +24,7 @@ func main() {

if !filepath.IsAbs(yamlPath) {
cwd, err := os.Getwd()
contract.AssertNoError(err)
contract.AssertNoErrorf(err, "unexpected error while fetching working directory")
yamlPath = filepath.Join(cwd, yamlPath)
}

Expand Down Expand Up @@ -100,7 +99,7 @@ func processYaml(path string, mdDir string) error {

description := example["description"].(string)
fmt.Fprintf(os.Stdout, "Processing %s\n", description)
dir, err := ioutil.TempDir("", "")
dir, err := os.MkdirTemp("", "")
if err != nil {
return err
}
Expand All @@ -119,7 +118,7 @@ func processYaml(path string, mdDir string) error {
if err = yaml.NewEncoder(src).Encode(example); err != nil {
return err
}
contract.AssertNoError(src.Close())
contract.AssertNoErrorf(src.Close(), "unexpected error while encoding YAML")

cmd := exec.Command("pulumi", "convert", "--language", "typescript", "--out",
filepath.Join(dir, "example-nodejs"))
Expand All @@ -129,7 +128,7 @@ func processYaml(path string, mdDir string) error {
if err = cmd.Run(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "convert nodejs failed, ignoring: %+v", err)
}
content, err := ioutil.ReadFile(filepath.Join(dir, "example-nodejs", "index.ts"))
content, err := os.ReadFile(filepath.Join(dir, "example-nodejs", "index.ts"))
if err != nil {
return err
}
Expand All @@ -143,7 +142,7 @@ func processYaml(path string, mdDir string) error {
if err := cmd.Run(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "convert python failed, ignoring: %+v", err)
}
content, err = ioutil.ReadFile(filepath.Join(dir, "example-py", "__main__.py"))
content, err = os.ReadFile(filepath.Join(dir, "example-py", "__main__.py"))
if err != nil {
return err
}
Expand All @@ -157,7 +156,7 @@ func processYaml(path string, mdDir string) error {
if err = cmd.Run(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "convert go failed, ignoring: %+v", err)
}
content, err = ioutil.ReadFile(filepath.Join(dir, "example-dotnet", "MyStack.cs"))
content, err = os.ReadFile(filepath.Join(dir, "example-dotnet", "MyStack.cs"))
if err != nil {
return err
}
Expand All @@ -171,30 +170,29 @@ func processYaml(path string, mdDir string) error {
if err = cmd.Run(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "convert go failed, ignoring: %+v", err)
}
content, err = ioutil.ReadFile(filepath.Join(dir, "example-go", "main.go"))
content, err = os.ReadFile(filepath.Join(dir, "example-go", "main.go"))
if err != nil {
return err
}
golang := string(content)

// TODO add java when convert supports it.

content, err = ioutil.ReadFile(filepath.Join(dir, "Pulumi.yaml"))
content, err = os.ReadFile(filepath.Join(dir, "Pulumi.yaml"))
if err != nil {
return err
}
yaml := string(content)

exampleStrings = append(exampleStrings, markdownExample(description, typescript, python, csharp, golang, yaml))
}
contract.AssertNoError(err)
fmt.Fprintf(os.Stdout, "Writing %s\n", filepath.Join(mdDir, md))
f, err := os.OpenFile(filepath.Join(mdDir, md), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777)
if err != nil {
return err
}
defer contract.IgnoreClose(f)
_, err = f.Write([]byte(markdownExamples(exampleStrings)))
contract.AssertNoError(err)
contract.AssertNoErrorf(err, "unexpected error while writing markdown examples")
return nil
}
Loading