Skip to content

Commit

Permalink
Linting fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Janos Bonic <[email protected]>
  • Loading branch information
Janos Bonic committed Jul 24, 2024
1 parent 367cc97 commit 2c31eeb
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 21 deletions.
35 changes: 21 additions & 14 deletions internal/tools/mockmirror-build-fake/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"bytes"
"errors"
"flag"
"fmt"
"log"
"os"
"os/exec"
Expand All @@ -19,22 +20,28 @@ import (
)

func main() {
if err := runMain(); err != nil {
log.Fatal(err)
}
}

func runMain() error {
file := ""
flag.StringVar(&file, "file", file, "File to write to.")
flag.Parse()

dir := path.Join(os.TempDir(), "fake")
if err := os.MkdirAll(dir, 0755); err != nil {
log.Fatal(err)
return err
}
defer func() {
_ = os.RemoveAll(dir)
}()
if err := os.WriteFile(path.Join(dir, "go.mod"), []byte(gomod), 0644); err != nil {
log.Fatal(err)
if err := os.WriteFile(path.Join(dir, "go.mod"), []byte(gomod), 0644); err != nil { //nolint:gosec //This is not sensitive.
return err
}
if err := os.WriteFile(path.Join(dir, "main.go"), []byte(code), 0644); err != nil {
log.Fatal()
if err := os.WriteFile(path.Join(dir, "main.go"), []byte(code), 0644); err != nil { //nolint:gosec //This is not sensitive.
return err
}

cmd := exec.Command("go", "build", "-ldflags", "-s -w", "-o", "fake")
Expand All @@ -44,20 +51,19 @@ func main() {
if err := cmd.Run(); err != nil {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
log.Println("Build failed.")
os.Exit(exitErr.ExitCode())
return fmt.Errorf("build failed (exit code %d)", exitErr.ExitCode())
}
log.Fatal(err)
return err
}

contents, err := os.ReadFile(path.Join(dir, "fake"))
if err != nil {
log.Fatal(err)
return err
}

tpl := template.New("")
if tpl, err = tpl.Parse(templateText); err != nil {
log.Fatal(err)
return err
}

parts := make([]string, len(contents))
Expand All @@ -82,15 +88,16 @@ func main() {
contentsCode,
file,
}); err != nil {
log.Fatal(err)
return err
}

if err := os.WriteFile(file+"~", buf.Bytes(), 0644); err != nil {
log.Fatal(err)
if err := os.WriteFile(file+"~", buf.Bytes(), 0644); err != nil { //nolint:gosec //This is not sensitive.
return err
}
if err := os.Rename(file+"~", file); err != nil {
log.Fatal(err)
return err
}
return nil
}

var templateText = `// Copyright (c) {{ .Authors }}
Expand Down
4 changes: 2 additions & 2 deletions mirror_create_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"fmt"
)

func (m *mirror) CreateVersion(ctx context.Context, version Version) error {
func (m *mirror) CreateVersion(_ context.Context, version Version) error {
if m.pullThroughDownloader != nil {
return fmt.Errorf("cannot use CreateVersionAsset when a pull-through mirror is configured")
}
Expand Down Expand Up @@ -43,7 +43,7 @@ func (m *mirror) CreateVersion(ctx context.Context, version Version) error {
ID: version,
Files: []string{},
},
})
}, responseData.Versions...)

marshalled, err := json.Marshal(responseData)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion mirror_create_version_asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"fmt"
)

func (m *mirror) CreateVersionAsset(ctx context.Context, version Version, assetName string, assetData []byte) error {
func (m *mirror) CreateVersionAsset(_ context.Context, version Version, assetName string, assetData []byte) error {
if m.pullThroughDownloader != nil {
return fmt.Errorf("cannot use CreateVersionAsset when a pull-through mirror is configured")
}
Expand Down
5 changes: 3 additions & 2 deletions mirror_serve_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (m *mirror) serveAPI(ctx context.Context, writer http.ResponseWriter) {
m.badGateway(writer)
return
}
writer.WriteHeader(200)
writer.WriteHeader(http.StatusOK)
writer.Header().Set("Content-Type", "application/json")
_, _ = writer.Write(encoded)
}
Expand All @@ -60,6 +60,7 @@ func (m *mirror) serveAsset(ctx context.Context, writer http.ResponseWriter, req
}
var foundVersion *VersionWithArtifacts
for _, ver := range versions {
ver := ver
if ver.ID == version {
foundVersion = &ver
break
Expand All @@ -75,7 +76,7 @@ func (m *mirror) serveAsset(ctx context.Context, writer http.ResponseWriter, req
m.badGateway(writer)
return
}
writer.WriteHeader(200)
writer.WriteHeader(http.StatusOK)
writer.Header().Set("Content-Type", "application/octet-stream")
_, _ = writer.Write(contents)
}
Expand Down
2 changes: 1 addition & 1 deletion mockmirror/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func buildFake(t *testing.T) []byte {
t.Fatalf("Failed to read compiled fake (%v)", err)
}

if err := os.WriteFile(binaryPath, contents, 0700); err != nil {
if err := os.WriteFile(binaryPath, contents, 0700); err != nil { //nolint:gosec //This needs to be executable.
t.Fatalf("Failed to create fake binary at %s (%v)", binaryPath, err)
}
return contents
Expand Down
9 changes: 8 additions & 1 deletion mockmirror/mockmirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"strconv"
"testing"
"time"

"github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/opentofu/tofudl"
Expand Down Expand Up @@ -62,10 +63,16 @@ func NewFromBinary(
if err != nil {
t.Fatalf("Failed to open listen socket for mock mirror (%v)", err)
}
srv := &http.Server{
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
Handler: tofudlMirror,
}
go func() {
_ = http.Serve(ln, tofudlMirror)
_ = srv.Serve(ln)
}()
t.Cleanup(func() {
_ = srv.Shutdown(context.Background())
_ = ln.Close()
})
return &mirror{
Expand Down

0 comments on commit 2c31eeb

Please sign in to comment.