justfile 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. commonenv := "CGO_ENABLED=0"
  2. version := `./tools/image-tag`
  3. commit := `git rev-parse --short HEAD`
  4. default:
  5. just --list
  6. # format all Go code
  7. fmt:
  8. go fmt ./...
  9. cd ./core && go fmt ./...
  10. cd ./modules/collector-source && go fmt ./...
  11. cd ./modules/prometheus-source && go fmt ./...
  12. # check if code is formatted
  13. fmt-check:
  14. #!/bin/sh
  15. echo "Checking code formatting..."
  16. unformatted="$(gofmt -l .)"
  17. if [ -n "$unformatted" ]; then \
  18. echo "The following files are not formatted:"; \
  19. echo "$unformatted"; \
  20. echo ""; \
  21. echo "Run 'just fmt' to format your code"; \
  22. exit 1; \
  23. fi
  24. echo "All files are properly formatted"
  25. # run core unit tests
  26. test-core:
  27. {{commonenv}} cd ./core && go test ./... -coverprofile=coverage.out
  28. {{commonenv}} cd ./core && go vet ./...
  29. # run prometheus-source unit tests
  30. test-prometheus-source:
  31. {{commonenv}} cd ./modules/prometheus-source && go test ./... -coverprofile=coverage.out
  32. {{commonenv}} cd ./modules/prometheus-source && go vet ./...
  33. # run collector-source unit tests
  34. test-collector-source:
  35. {{commonenv}} cd ./modules/collector-source && go test ./... -coverprofile=coverage.out
  36. {{commonenv}} cd ./modules/collector-source && go vet ./...
  37. # run the opencost unit tests
  38. test-opencost:
  39. {{commonenv}} go test ./... -coverprofile=coverage.out
  40. {{commonenv}} go tool cover -html=coverage.out -o coverage.html
  41. {{commonenv}} go vet ./...
  42. # Run unit tests, merge coverage reports, remove old reports
  43. test: test-core test-prometheus-source test-collector-source test-opencost
  44. find . -name "coverage.out" -print0 | xargs -0 cat > coverage.new
  45. find . -name "coverage.out" -delete
  46. mv coverage.new coverage.out
  47. # Run unit tests and integration tests
  48. test-integration:
  49. {{commonenv}} INTEGRATION=true go test ./... -coverprofile=coverage.out
  50. # Compile a local binary
  51. build-local:
  52. cd ./cmd/costmodel && \
  53. {{commonenv}} go build \
  54. -ldflags \
  55. "-X github.com/opencost/opencost/core/pkg/version.Version={{version}} \
  56. -X github.com/opencost/opencost/core/pkg/version.GitCommit={{commit}}" \
  57. -o ./costmodel
  58. # Build multiarch binaries
  59. build-binary VERSION=version:
  60. cd ./cmd/costmodel && \
  61. {{commonenv}} GOOS=linux GOARCH=amd64 go build \
  62. -ldflags \
  63. "-X github.com/opencost/opencost/core/pkg/version.Version={{VERSION}} \
  64. -X github.com/opencost/opencost/core/pkg/version.GitCommit={{commit}}" \
  65. -o ./costmodel-amd64
  66. cd ./cmd/costmodel && \
  67. {{commonenv}} GOOS=linux GOARCH=arm64 go build \
  68. -ldflags \
  69. "-X github.com/opencost/opencost/core/pkg/version.Version={{VERSION}} \
  70. -X github.com/opencost/opencost/core/pkg/version.GitCommit={{commit}}" \
  71. -o ./costmodel-arm64
  72. # Build and push a multi-arch Docker image
  73. build IMAGE_TAG RELEASE_VERSION: (build-binary RELEASE_VERSION)
  74. docker buildx build \
  75. --rm \
  76. --platform "linux/amd64" \
  77. -f 'Dockerfile.cross' \
  78. --build-arg binarypath=./cmd/costmodel/costmodel-amd64 \
  79. --build-arg version={{RELEASE_VERSION}} \
  80. --build-arg commit={{commit}} \
  81. --provenance=false \
  82. -t {{IMAGE_TAG}}-amd64 \
  83. --push \
  84. .
  85. docker buildx build \
  86. --rm \
  87. --platform "linux/arm64" \
  88. -f 'Dockerfile.cross' \
  89. --build-arg binarypath=./cmd/costmodel/costmodel-arm64 \
  90. --build-arg version={{RELEASE_VERSION}} \
  91. --build-arg commit={{commit}} \
  92. --provenance=false \
  93. -t {{IMAGE_TAG}}-arm64 \
  94. --push \
  95. .
  96. manifest-tool push from-args \
  97. --platforms "linux/amd64,linux/arm64" \
  98. --template {{IMAGE_TAG}}-ARCH \
  99. --target {{IMAGE_TAG}}
  100. validate-protobuf:
  101. ./generate.sh
  102. git diff --exit-code