diff --git a/.gitignore b/.gitignore index 23bf8fb..b49c209 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,6 @@ -parser +railcar config.json + +# Parser build output for binaries +/assets/dist/ +dist/ diff --git a/.goreleaser.yaml b/.goreleaser.yaml new file mode 100644 index 0000000..b54ce20 --- /dev/null +++ b/.goreleaser.yaml @@ -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:" diff --git a/Makefile b/Makefile index 70a1df3..1ddfccb 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/assets/.goreleaser.yaml b/assets/.goreleaser.yaml new file mode 100644 index 0000000..56312a4 --- /dev/null +++ b/assets/.goreleaser.yaml @@ -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 diff --git a/assets/embed.go b/assets/embed.go new file mode 100644 index 0000000..8d5cb94 --- /dev/null +++ b/assets/embed.go @@ -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 diff --git a/cmd/parser/main.go b/cmd/railcar/main.go similarity index 88% rename from cmd/parser/main.go rename to cmd/railcar/main.go index b3bf769..dfbb788 100644 --- a/cmd/parser/main.go +++ b/cmd/railcar/main.go @@ -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() { @@ -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, "") } } diff --git a/config.json b/config.json deleted file mode 100755 index 4659513..0000000 --- a/config.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "skiff": { - "pre-commit": { - "echo": "echo skiff is running!" - }, - "prepare-commit-msg": { - }, - "commit-msg": { - }, - "post-commit": { - } - } -} \ No newline at end of file diff --git a/embed.go b/embed.go deleted file mode 100644 index 4596842..0000000 --- a/embed.go +++ /dev/null @@ -1,8 +0,0 @@ -package hookinator - -import _ "embed" - -// This file is meant to embed the parser into skiff. This allows skiff to install the parser onto your machine - -//go:embed assets/parser -var Parser []byte diff --git a/go.mod b/go.mod index d553fe1..1d7f121 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 912390a..4353b61 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/blueprint/templates/config.go b/internal/blueprint/templates/config.go index c3e97fc..b93f7f5 100644 --- a/internal/blueprint/templates/config.go +++ b/internal/blueprint/templates/config.go @@ -1,7 +1,7 @@ package templates const Config = `{ - "skiff": { + "carriage": { "pre-commit": { "echo": "echo skiff is running!" }, diff --git a/internal/blueprint/templates/hooks.go b/internal/blueprint/templates/hooks.go index b79412d..714c694 100644 --- a/internal/blueprint/templates/hooks.go +++ b/internal/blueprint/templates/hooks.go @@ -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` diff --git a/internal/commands/root.go b/internal/commands/root.go index 5805c13..ff8c187 100644 --- a/internal/commands/root.go +++ b/internal/commands/root.go @@ -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" ) diff --git a/internal/embed/embeder.go b/internal/embed/embeder.go index ec7b17e..c9302d0 100644 --- a/internal/embed/embeder.go +++ b/internal/embed/embeder.go @@ -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 } diff --git a/internal/embed/embeder_test.go b/internal/embed/embeder_test.go new file mode 100644 index 0000000..eb65da7 --- /dev/null +++ b/internal/embed/embeder_test.go @@ -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()) +}