Skip to content

Commit

Permalink
Merge pull request #33 from hathora/post-release-bugfixes
Browse files Browse the repository at this point in the history
Post release bugfixes
  • Loading branch information
drewfead committed Jun 25, 2024
2 parents a54e617 + 36f04c6 commit c73e33a
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 26 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,13 @@ jobs:
TARGETOS: ${{ matrix.os }}
TARGETARCH: ${{ matrix.arch }}
BUILD_VERSION: ${{ needs.prepare.outputs.version }}
BINARY_SUFFIX: ${{ matrix.os == 'windows' && '.exe' || '' }}
run: make build

- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: hathora-${{ matrix.os }}-${{ matrix.arch }}
name: hathora-${{ matrix.os }}-${{ matrix.arch }}${{ matrix.os == 'windows' && '.exe' || '' }}
path: bin/hathora-*

release:
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ RUN go mod download

# copy source
COPY Makefile Makefile
COPY cmd cmd
COPY hathora.go hathora.go
COPY internal internal

ARG TARGETOS
Expand Down
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ SHELL = /usr/bin/env bash -o pipefail
TARGETOS ?= darwin
TARGETARCH ?= arm64
BUILD_VERSION ?= $(shell git describe --always --dirty)
BINARY_SUFFIX ?= ""

.PHONY: all
all: build
Expand All @@ -19,9 +20,9 @@ clean: ## Delete all built binaries.
build: ## Build the command binaries.
CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} \
go build \
-o bin/hathora-${TARGETOS}-${TARGETARCH} \
-o bin/hathora-${TARGETOS}-${TARGETARCH}${BINARY_SUFFIX} \
-ldflags "-X 'github.com/hathora/ci/internal/commands.BuildVersion=${BUILD_VERSION}'" \
cmd/run.go
hathora.go

.PHONY: sdk-clean
sdk-clean: ## Re-generate the SDK using speakeasyapi.dev
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ For documentation on how to use this CLI, check out our [docs.](https://hathora.
To run the CLI locally, execute the following:

```sh
go run cmd/main.go --help
go run hathora.go --help
```

### Running tests
Expand All @@ -38,6 +38,14 @@ The target OS and architecture can be specified by setting the `OS` and `ARCH` e
TARGETOS=linux TARGETARCH=amd64 make build
```

> [!NOTE]
> When building on windows you'll want the built binaries to include the `.exe` suffix. To achieve this, you can use the `BINARY_SUFFIX`
> variable, e.g.:
>
> ```sh
> TARGETOS=windows TARGETARCH=amd64 BINARY_SUFFIX=.exe make build
> ```
The binary will be available in the `bin` directory.

```sh
Expand Down
File renamed without changes.
40 changes: 29 additions & 11 deletions internal/archive/targz.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"slices"
"strings"

"github.com/h2non/filetype"
"github.com/monochromegane/go-gitignore"
Expand Down Expand Up @@ -47,9 +48,9 @@ func shouldIgnoreFilepath(filepath string, isDir bool, matchers []gitignore.Igno
return anyMatches
}

func CreateTGZ(srcFolder string) (string, error) {
func CreateTGZ(srcFolder string, ext string) (string, error) {
fileName := filepath.Base(filepath.Clean(srcFolder))
destinationFile := fmt.Sprintf("%s.*.tgz", fileName)
destinationFile := fmt.Sprintf("%s.*.%s", fileName, ext)
tarGzFile, err := os.CreateTemp("", destinationFile)
if err != nil {
return "", err
Expand All @@ -76,6 +77,10 @@ func CreateTGZ(srcFolder string) (string, error) {
return err
}

if !info.Mode().IsRegular() {
return nil
}

relPath, err := filepath.Rel(srcFolder, filePath)
if err != nil {
return err
Expand Down Expand Up @@ -115,7 +120,7 @@ func CreateTGZ(srcFolder string) (string, error) {
return fmt.Errorf("expected to write %d bytes but wrote %d bytes for file %s", info.Size(), written, filePath)
}

zap.L().Debug("Inlcluding file: " + relPath)
zap.L().Debug("Including file: " + relPath)
return nil
})

Expand Down Expand Up @@ -151,17 +156,30 @@ func isTGZ(filePath string) (bool, error) {
return false, fmt.Errorf("could not read file: %s", filePath)
}

if filetype.IsArchive(buff) {
fileExt := filepath.Ext(filePath)
if !slices.Contains(supportedFileExtensions, fileExt) {
err := fmt.Errorf("unsupported archive file extension: %s", fileExt)
return false, err
if !filetype.IsArchive(buff) {
return false, nil
}

var fileExtParts []string
for {
ext := filepath.Ext(filePath)
if ext == "" {
break
}
fileExtParts = append(fileExtParts, ext)
filePath = filePath[:len(filePath)-len(ext)]
}
slices.Reverse(fileExtParts)

return true, nil
var fileExt string
for i := 0; i < len(fileExtParts); i++ {
fileExt = strings.Join(fileExtParts[i:], "")
if slices.Contains(supportedFileExtensions, fileExt) {
return true, nil
}
}

return false, nil
return false, fmt.Errorf("unsupported archive file extension: %s", fileExt)
}

func RequireTGZ(srcFolder string) (*TGZFile, error) {
Expand All @@ -187,7 +205,7 @@ func RequireTGZ(srcFolder string) (*TGZFile, error) {

zap.L().Debug(srcFolder + " is not a gzipped tar archive. Archiving and compressing now.")

destFile, err := CreateTGZ(srcFolder)
destFile, err := CreateTGZ(srcFolder, "tgz")
if err != nil {
return nil, err
}
Expand Down
76 changes: 75 additions & 1 deletion internal/archive/targz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func Test_CreateTGZ(t *testing.T) {
}
}

archivePath, err := archive.CreateTGZ(srcFolder)
archivePath, err := archive.CreateTGZ(srcFolder, "tgz")
if tt.shouldFail {
assert.Error(err)
return
Expand Down Expand Up @@ -137,3 +137,77 @@ func Test_CreateTGZ(t *testing.T) {
})
}
}

func Test_RequireTGZ(t *testing.T) {
zapLogger, _ := zap.NewDevelopment()
zap.ReplaceGlobals(zapLogger)
tests := []struct {
name string
files map[string]string
archiveExt string
precreateArchive bool
}{
{
name: "existing .tgz",
files: map[string]string{
"file1.txt": "This is file 1",
},
archiveExt: "tgz",
precreateArchive: true,
},
{
name: "existing .tar.gz",
files: map[string]string{
"file1.txt": "This is file 1",
},
archiveExt: "tar.gz",
precreateArchive: true,
},
{
name: "existing .tar.tgz",
files: map[string]string{
"file1.txt": "This is file 1",
},
archiveExt: "tar.tgz",
precreateArchive: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert := assert.New(t)

srcFolder, err := os.MkdirTemp("", "testsrc")
require.NoError(t, err)
t.Cleanup(func() {
os.RemoveAll(srcFolder)
})

for path, content := range tt.files {
fullPath := filepath.Join(srcFolder, path)
if strings.HasSuffix(path, "/") {
require.NoError(t, os.MkdirAll(fullPath, os.ModePerm))
} else {
err := os.MkdirAll(filepath.Dir(fullPath), os.ModePerm)
require.NoError(t, err)
err = os.WriteFile(fullPath, []byte(content), 0644)
require.NoError(t, err)
}
}

var archivePath string
if tt.precreateArchive {
archivePath, err = archive.CreateTGZ(srcFolder, tt.archiveExt)
require.NoError(t, err)
t.Cleanup(func() {
os.Remove(archivePath)
})
}

tgzFile, err := archive.RequireTGZ(archivePath)
require.NoError(t, err)
require.NotNil(t, tgzFile)
assert.Equal(archivePath, tgzFile.Path)
})
}
}
3 changes: 1 addition & 2 deletions internal/commands/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,12 @@ func BuildTextFormatter() output.FormatWriter {
"RequestedMemoryMB",
"DefaultContainerPort",
"AdditionalContainerPorts",
"Env",
),
output.RenameField(deployment, "RequestedMemoryMB", "RequestedMemory"),
output.WithPropertyFormatter(deployment, "RequestedMemoryMB", func(f float64) string {
return humanize.IBytes((uint64)(f * 1024 * 1024))
}),
output.WithoutFields(deployment, "AppID", "CreatedBy"),
output.WithoutFields(deployment, "AppID", "CreatedBy", "Env"),
output.WithFormatter(envVar,
func(e shared.DeploymentV2Env) string {
return fmt.Sprintf("%s=%s", e.Name, e.Value)
Expand Down
14 changes: 7 additions & 7 deletions internal/output/text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ func Test_DeploymentTextOutput(t *testing.T) {
RequestedCPU: 0.5,
},
expect: [][]string{
{"DeploymentID", "BuildID", "CreatedAt", "IdleTimeoutEnabled", "RoomsPerProcess", "RequestedCPU", "RequestedMemory", "DefaultContainerPort", "AdditionalContainerPorts", "Env"},
{"2", "1", "2021-01-01T00:00:00Z", "true", "3", "0.5", "1.0", "GiB", "default:3000/tcp", "debug:4000/tcp", "EULA=TRUE"},
{"DeploymentID", "BuildID", "CreatedAt", "IdleTimeoutEnabled", "RoomsPerProcess", "RequestedCPU", "RequestedMemory", "DefaultContainerPort", "AdditionalContainerPorts"},
{"2", "1", "2021-01-01T00:00:00Z", "true", "3", "0.5", "1.0", "GiB", "default:3000/tcp", "debug:4000/tcp"},
},
},
{
Expand Down Expand Up @@ -88,8 +88,8 @@ func Test_DeploymentTextOutput(t *testing.T) {
RequestedCPU: 0.5,
},
expect: [][]string{
{"DeploymentID", "BuildID", "CreatedAt", "IdleTimeoutEnabled", "RoomsPerProcess", "RequestedCPU", "RequestedMemory", "DefaultContainerPort", "AdditionalContainerPorts", "Env"},
{"2", "1", "2021-01-01T00:00:00Z", "true", "3", "0.5", "1.0", "GiB", "default:3000/tcp", "debug:4000/tcp", "EULA=TRUE"},
{"DeploymentID", "BuildID", "CreatedAt", "IdleTimeoutEnabled", "RoomsPerProcess", "RequestedCPU", "RequestedMemory", "DefaultContainerPort", "AdditionalContainerPorts"},
{"2", "1", "2021-01-01T00:00:00Z", "true", "3", "0.5", "1.0", "GiB", "default:3000/tcp", "debug:4000/tcp"},
},
},
{
Expand Down Expand Up @@ -155,9 +155,9 @@ func Test_DeploymentTextOutput(t *testing.T) {
},
},
expect: [][]string{
{"DeploymentID", "BuildID", "CreatedAt", "IdleTimeoutEnabled", "RoomsPerProcess", "RequestedCPU", "RequestedMemory", "DefaultContainerPort", "AdditionalContainerPorts", "Env"},
{"2", "1", "2021-01-01T00:00:00Z", "true", "3", "0.5", "1.0", "GiB", "default:3000/tcp", "debug:4000/tcp", "EULA=TRUE"},
{"2", "1", "2021-01-01T00:00:00Z", "true", "3", "0.5", "1.0", "GiB", "default:3000/tcp", "debug:4000/tcp", "EULA=TRUE"},
{"DeploymentID", "BuildID", "CreatedAt", "IdleTimeoutEnabled", "RoomsPerProcess", "RequestedCPU", "RequestedMemory", "DefaultContainerPort", "AdditionalContainerPorts"},
{"2", "1", "2021-01-01T00:00:00Z", "true", "3", "0.5", "1.0", "GiB", "default:3000/tcp", "debug:4000/tcp"},
{"2", "1", "2021-01-01T00:00:00Z", "true", "3", "0.5", "1.0", "GiB", "default:3000/tcp", "debug:4000/tcp"},
},
},
}
Expand Down

0 comments on commit c73e33a

Please sign in to comment.