Skip to content

Commit

Permalink
Make main and lib (#1)
Browse files Browse the repository at this point in the history
* Extract code drom main, add lambda
* Add cloudbuild
* Fix cmd/sim install in README
  • Loading branch information
tamadamas authored Jun 29, 2024
1 parent c42f540 commit 84e9f74
Show file tree
Hide file tree
Showing 13 changed files with 153 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
goos: ${{ matrix.goos }}
goarch: ${{ matrix.goarch }}
goversion: "1.21"
project_path: "./sim"
project_path: "./cmd/sim"
binary_name: "sim"
overwrite: true
upload: true
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ To install `go` check [instructions here](https://go.dev/doc/install)
and then run

```
go install github.com/rxx/od_tools/sim@latest
go install github.com/rxx/od_tools/cmd/sim@latest
```

If `@latest` does not work, try to include specific version as `@v1.0.2-sim`
Expand Down
137 changes: 137 additions & 0 deletions cloud/functions/generate-log/function.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package helloworld

import (
"context"
"fmt"
"log"
"time"
"io"
"os"
"path/filepath"

"github.com/GoogleCloudPlatform/functions-framework-go/functions"
"github.com/cloudevents/sdk-go/v2/event"
"cloud.google.com/go/storage"
"github.com/rxx/od_tools/pkg/sim"

)

func init() {
functions.CloudEvent("HelloStorage", helloStorage)
}

// StorageObjectData contains metadata of the Cloud Storage object.
type StorageObjectData struct {
Bucket string `json:"bucket,omitempty"`
Name string `json:"name,omitempty"`
Metageneration int64 `json:"metageneration,string,omitempty"`
TimeCreated time.Time `json:"timeCreated,omitempty"`
Updated time.Time `json:"updated,omitempty"`
}

// helloStorage consumes a CloudEvent message and logs details about the changed object.
func helloStorage(ctx context.Context, e event.Event) error {
log.Printf("Event ID: %s", e.ID())
log.Printf("Event Type: %s", e.Type())

var data StorageObjectData
if err := e.DataAs(&data); err != nil {
return fmt.Errorf("event.DataAs: %v", err)
}

log.Printf("Bucket: %s", data.Bucket)
log.Printf("File: %s", data.Name)
log.Printf("Metageneration: %d", data.Metageneration)
log.Printf("Created: %s", data.TimeCreated)
log.Printf("Updated: %s", data.Updated)

// Get storage client
client, err := storage.NewClient(ctx)
if err != nil {
return fmt.Errorf("storage.NewClient: %v", err)
}
defer client.Close()

rc, err := client.Bucket(data.Bucket).Object(data.Name).NewReader(ctx)
if err != nil {
return fmt.Errorf("Object(%q).NewReader: %v", data.Name, err)
}
defer rc.Close()

ext := filepath.Ext(data.Name)

inputFile, err := os.CreateTemp("", "sim-*"+ext)
if err != nil {
return fmt.Errorf("os.CreateTemp: %v", err)
}
defer inputFile.Close()

// Copy from storage reader to the local file.
_, err := io.Copy(inputFile, rc)

if err != nil {
return fmt.Errorf("io.Copy: %v", err)
}

if err = fileIsEmpty(inputFile); err != nil {
return err
}

outputFile, err := os.CreateTemp("", "log-*.txt")
if err != nil {
return fmt.Errorf("os.CreateTemp: %v", err)
}
defer outputFile.Close()

//process file
gameLogCmd := NewGameLog(inputFile.Name(), outputFile.Name())
gameLogCmd.Execute()

if err = fileIsEmpty(inputFile); err != nil {
return fmt.Errorf("Log generation failed: %v", err)
}

// Upload to output bucket
outputBucketName := "od_sim_output"
outputObjectName := "log-" + data.Name

wc := client.Bucket(outputBucketName).Object(outputObjectName).NewWriter(ctx)
wc.ObjectAttrs.ContentType = "text/plain"

if _, err = io.Copy(wc, outputFile); err != nil {
return fmt.Errorf("io.Copy: %v", err)
}
if err := wc.Close(); err != nil {
return fmt.Errorf("Writer.Close: %v", err)
}
log.Printf("Blob %v uploaded to bucket %v.\n", outputObjectName, outputBucketName)

// Clean up local files
if err := os.Remove(localFile.Name()); err != nil {
return fmt.Errorf("error removing local file: %v", err)
}

if err := os.Remove(outputFile.Name()); err != nil {
return fmt.Errorf("error removing output file: %v", err)
}

// Remove input file from Cloud Storage
if err := client.Bucket(data.Bucket).Object(data.Name).Delete(ctx); err != nil {
return fmt.Errorf("error deleting object: %v", err)
}

return nil
}

func fileIsEmpty(file *os.File) error {
fileInfo, err := file.Stat()
if err != nil {
return fmt.Errorf("error getting file info: %v", err)
}

if fileInfo.Size() == 0 {
return fmt.Errorf("file is empty")
}

return nil
}
6 changes: 6 additions & 0 deletions cloud/functions/generate-log/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module example.com/gcf

require (
github.com/GoogleCloudPlatform/functions-framework-go v1.5.2
github.com/cloudevents/sdk-go/v2 v2.5.0
)
3 changes: 3 additions & 0 deletions cloudbuild.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
steps:
- name: 'gcr.io/cloud-builders/gcloud'
args: ['functions', 'deploy', 'simgenfunc', '--source=./cloud/functions/generate-log']
File renamed without changes.
1 change: 1 addition & 0 deletions sim/main.go → cmd/sim/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"os"
"github.com/rxx/od_tools/pkg/sim"
)

var cmdVars *FlagSetVars
Expand Down
2 changes: 1 addition & 1 deletion sim/generate_log.go → pkg/sim/generate_log.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package sim

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion sim/generate_log_test.go → pkg/sim/generate_log_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package sim

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion sim/helpers.go → pkg/sim/helpers.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package sim

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion sim/parse_log.go → pkg/sim/parse_log.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package sim

import (
"bufio"
Expand Down
1 change: 0 additions & 1 deletion sim/go.mod

This file was deleted.

1 change: 0 additions & 1 deletion sim/go.sum

This file was deleted.

0 comments on commit 84e9f74

Please sign in to comment.