Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
parser
railcar
config.json

# Parser build output for binaries
/assets/dist/
dist/
44 changes: 44 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# This is an example .goreleaser.yml file with some sensible defaults.
# Make sure to check the documentation at https://goreleaser.com

# The lines below are called `modelines`. See `:help modeline`
# Feel free to remove those if you don't want/need to use them.
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
# vim: set ts=2 sw=2 tw=0 fo=cnqoj

version: 2

before:
hooks:
- make railcar-build-binaries


#builds:
# - env:
# - CGO_ENABLED=0
# goos:
# - linux
# - windows
# - darwin
#
#archives:
# - format: tar.gz
# # this name template makes the OS and Arch compatible with the results of `uname`.
# name_template: >-
# {{ .ProjectName }}_
# {{- title .Os }}_
# {{- if eq .Arch "amd64" }}x86_64
# {{- else if eq .Arch "386" }}i386
# {{- else }}{{ .Arch }}{{ end }}
# {{- if .Arm }}v{{ .Arm }}{{ end }}
# # use zip for windows archives
# format_overrides:
# - goos: windows
# format: zip
#
#changelog:
# sort: asc
# filters:
# exclude:
# - "^docs:"
# - "^test:"
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,11 @@ build-skiff:

.Phony: build-all
build-all: build-parser build-skiff

.Phony: railcar-build-binaries
railcar-build-binaries:
goreleaser release --snapshot --clean --config=./assets/.goreleaser.yaml

.Phony: go-test
go-test:
go test ./... --race
22 changes: 22 additions & 0 deletions assets/.goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# This .goreleaser.yaml configuration is used to generate all railcar binaries.
# These binaries are then embedded into the main application, allowing the main
# application to install the railcar on the user's machine. The embedding process
# is facilitated through the use of `//go:embed` directives in `assets/embed.go`.
version: 2

dist: ./assets/dist

builds:
- id: railcar
binary: railcar
main: ./cmd/railcar/main.go
env:
- CGO_ENABLED=0
goos:
- linux
- windows
- darwin
goarch:
- amd64
- arm
- arm64
29 changes: 29 additions & 0 deletions assets/embed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package assets

import _ "embed"

// This file is meant to embed the railcar into freight. This allows freight to install the railcar onto your machine

//go:embed dist/railcar_darwin_amd64_v1/railcar
var MacOSIntel []byte

//go:embed dist/railcar_darwin_arm64/railcar
var MacOSSilicon []byte

//go:embed dist/railcar_linux_amd64_v1/railcar
var LinuxAMD64 []byte

//go:embed dist/railcar_linux_arm64/railcar
var LinuxARM64 []byte

//go:embed dist/railcar_linux_arm_6/railcar
var LinuxARM32 []byte

//go:embed dist/railcar_windows_amd64_v1/railcar.exe
var WindowsAMD64 []byte

//go:embed dist/railcar_windows_arm64/railcar.exe
var WindowsARM64 []byte

//go:embed dist/railcar_windows_arm_6/railcar.exe
var WindowsARM32 []byte
12 changes: 6 additions & 6 deletions cmd/parser/main.go → cmd/railcar/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import (
"github.com/devbytes-cloud/hookinator/internal/blueprint"
)

type Hookinator struct {
type RailCar struct {
PreCommit map[string]string `json:"pre-commit"`
PostCommit map[string]string `json:"post-commit"`
CommitMsg map[string]string `json:"commit-msg"`
}

type Config struct {
Hookinator Hookinator `json:"skiff"`
RailCar RailCar `json:"carriage"`
}

func main() {
Expand Down Expand Up @@ -49,13 +49,13 @@ func main() {

// Process the commit message
fmt.Println("Commit message is:", os.Args[2])
if len(config.Hookinator.CommitMsg) != 0 {
run(config.Hookinator.CommitMsg, os.Args[2])
if len(config.RailCar.CommitMsg) != 0 {
run(config.RailCar.CommitMsg, os.Args[2])
}

case blueprint.PreCommit:
if len(config.Hookinator.PreCommit) != 0 {
run(config.Hookinator.PreCommit, "")
if len(config.RailCar.PreCommit) != 0 {
run(config.RailCar.PreCommit, "")
}

}
Expand Down
13 changes: 0 additions & 13 deletions config.json

This file was deleted.

8 changes: 0 additions & 8 deletions embed.go

This file was deleted.

8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ module github.com/devbytes-cloud/hookinator

go 1.22.4

require github.com/spf13/cobra v1.8.1
require (
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.9.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
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/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2 changes: 1 addition & 1 deletion internal/blueprint/templates/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package templates

const Config = `{
"skiff": {
"carriage": {
"pre-commit": {
"echo": "echo skiff is running!"
},
Expand Down
4 changes: 2 additions & 2 deletions internal/blueprint/templates/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package templates

const PreHookTmpl = `#!/bin/bash
echo "================================"
echo "Skiff {{ .Type }} Hook"
echo "Carriage {{ .Type }} Hook"
echo "================================"
{{ .Path }}/parser {{ .Type }} $1`
{{ .Path }}/railcar {{ .Type }} $1`
3 changes: 2 additions & 1 deletion internal/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"fmt"
"os"

"github.com/devbytes-cloud/hookinator/internal/validate"

"github.com/devbytes-cloud/hookinator/internal/blueprint"
"github.com/devbytes-cloud/hookinator/internal/embed"
"github.com/devbytes-cloud/hookinator/internal/validate"
"github.com/spf13/cobra"
)

Expand Down
63 changes: 57 additions & 6 deletions internal/embed/embeder.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,69 @@
package embed

import (
"fmt"
"os"
"path/filepath"
"runtime"

"github.com/devbytes-cloud/hookinator"
"github.com/devbytes-cloud/hookinator/assets"
)

// WriteBinary ...
// List of supported OS + Arch railcar binaries
const (
macOSSilicon string = "darwin-arm64"
macOSIntel string = "darwin-amd64"
linuxAMD64 string = "linux-amd64"
linuxARM64 string = "linux-arm64"
linuxARM32 string = "linux-arm"
windowsAMD64 string = "windows-amd64"
windowsARM64 string = "windows-arm64"
windowsARM32 string = "windows-arm"
)

// WriteBinary will install railcar into your working directory
func WriteBinary() error {
op := filepath.Join(".", "parser")
if err := os.WriteFile(op, hookinator.Parser, 0o755); err != nil {
return err
systemInfo := fmt.Sprintf("%s-%s", fetchOS(), fetchArch())
binary := fetchBinary(systemInfo)

if binary == nil {
return fmt.Errorf("no matching railcar binary for %s", systemInfo)
}

op := filepath.Join(".", "railcar")
return os.WriteFile(op, binary, 0o755)
}

// fetchBinary will return the proper railcar binary for your system
func fetchBinary(systemInfo string) []byte {
switch systemInfo {
case macOSSilicon:
return assets.MacOSSilicon
case macOSIntel:
return assets.MacOSIntel
case linuxAMD64:
return assets.LinuxAMD64
case linuxARM64:
return assets.LinuxARM64
case linuxARM32:
return assets.LinuxARM32
case windowsAMD64:
return assets.WindowsAMD64
case windowsARM64:
return assets.WindowsARM64
case windowsARM32:
return assets.WindowsARM32
default:
return nil
}
}

// fetchOS returns the current os that is running
func fetchOS() string {
return runtime.GOOS
}

return nil
// fetchArch returns the current architecture that is running
func fetchArch() string {
return runtime.GOARCH
}
39 changes: 39 additions & 0 deletions internal/embed/embeder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package embed

import (
"fmt"
"os"
"runtime"
"testing"

"github.com/stretchr/testify/assert"
)

func TestWriteBinary(t *testing.T) {
err := WriteBinary()
assert.NoError(t, err)
assert.FileExists(t, "./railcar")
defer func() {
err := os.Remove("./railcar")
assert.NoError(t, err)
}()
}

func TestFetchBinary(t *testing.T) {
t.Run("binary does not exist", func(t *testing.T) {
assert.Nil(t, fetchBinary("junk"))
})

t.Run("binary exists", func(t *testing.T) {
systemInfo := fmt.Sprintf("%s-%s", fetchOS(), fetchArch())
assert.NotNil(t, fetchBinary(systemInfo))
})
}

func TestFetchOS(t *testing.T) {
assert.Equal(t, runtime.GOOS, fetchOS())
}

func TestFetchArch(t *testing.T) {
assert.Equal(t, runtime.GOARCH, fetchArch())
}