From 256e590e4d5ccacdbae3a0633db1b59722d4bd07 Mon Sep 17 00:00:00 2001 From: Alexandr Kolesov Date: Thu, 24 Oct 2024 19:21:51 +0400 Subject: [PATCH] Rename import to add, add version cmd --- .gitignore | 1 + Makefile | 45 ++++++++++ cmd/{image-vault-2 => imv}/main.go | 3 +- go.mod | 17 +--- go.sum | 37 --------- pkg/buildinfo/buildinfo.go | 49 +++++++++++ pkg/command/{import.go => add.go} | 22 ++--- pkg/command/info.go | 2 +- pkg/command/init.go | 2 +- pkg/command/root.go | 3 +- pkg/command/verify.go | 2 +- pkg/command/version.go | 18 ++++ pkg/util/sidecar.go | 77 ------------------ pkg/{util => vault}/compare.go | 0 pkg/{util => vault}/compare_test.go | 0 pkg/{util => vault}/config.go | 0 pkg/{util => vault}/config_test.go | 0 pkg/{util => vault}/copy.go | 0 pkg/{util => vault}/copy_test.go | 0 pkg/{util => vault}/filter.go | 0 pkg/{util => vault}/filter_test.go | 0 pkg/{util => vault}/list.go | 0 pkg/{util => vault}/list_test.go | 0 pkg/{util => vault}/metadata.go | 0 pkg/{util => vault}/metadata_test.go | 0 pkg/vault/sidecar.go | 74 +++++++++++++++++ pkg/{util => vault}/sidecar_test.go | 3 +- pkg/{util => vault}/template.go | 0 pkg/{util => vault}/template_test.go | 0 pkg/{util => vault}/testdata/.hidden.txt | 0 pkg/{util => vault}/testdata/capybara.png | Bin .../testdata/ignoredDir/ignored.txt | 0 pkg/{util => vault}/testdata/testDir/test.jpg | Bin pkg/{util => vault}/testdata/testDir/test.txt | 0 pkg/{util => vault}/testdata/testDir/test.xmp | 0 35 files changed, 208 insertions(+), 147 deletions(-) create mode 100644 Makefile rename cmd/{image-vault-2 => imv}/main.go (99%) create mode 100644 pkg/buildinfo/buildinfo.go rename pkg/command/{import.go => add.go} (86%) create mode 100644 pkg/command/version.go delete mode 100644 pkg/util/sidecar.go rename pkg/{util => vault}/compare.go (100%) rename pkg/{util => vault}/compare_test.go (100%) rename pkg/{util => vault}/config.go (100%) rename pkg/{util => vault}/config_test.go (100%) rename pkg/{util => vault}/copy.go (100%) rename pkg/{util => vault}/copy_test.go (100%) rename pkg/{util => vault}/filter.go (100%) rename pkg/{util => vault}/filter_test.go (100%) rename pkg/{util => vault}/list.go (100%) rename pkg/{util => vault}/list_test.go (100%) rename pkg/{util => vault}/metadata.go (100%) rename pkg/{util => vault}/metadata_test.go (100%) create mode 100644 pkg/vault/sidecar.go rename pkg/{util => vault}/sidecar_test.go (99%) rename pkg/{util => vault}/template.go (100%) rename pkg/{util => vault}/template_test.go (100%) rename pkg/{util => vault}/testdata/.hidden.txt (100%) rename pkg/{util => vault}/testdata/capybara.png (100%) rename pkg/{util => vault}/testdata/ignoredDir/ignored.txt (100%) rename pkg/{util => vault}/testdata/testDir/test.jpg (100%) rename pkg/{util => vault}/testdata/testDir/test.txt (100%) rename pkg/{util => vault}/testdata/testDir/test.xmp (100%) diff --git a/.gitignore b/.gitignore index 090a1f0..cd4a86d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .idea .DS_Store +build \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..75ddd42 --- /dev/null +++ b/Makefile @@ -0,0 +1,45 @@ +MODULE ?= github.com/askolesov/image-vault +PACKAGE ?= ./... +OUTPUT ?= build/ + +VERSION ?= $(shell git describe --tags --abbrev=0 2>/dev/null) +BRANCH ?= $(shell git symbolic-ref -q --short HEAD 2>/dev/null) +COMMIT_HASH ?= $(shell git rev-parse --short HEAD 2>/dev/null) +BUILD_DATE ?= $(shell date +%FT%T%z) + +GOARGS += -v +LDFLAGS += -s -w -X ${MODULE}/pkg/buildinfo.version=${VERSION} \ + -X ${MODULE}/pkg/buildinfo.commitHash=${COMMIT_HASH} \ + -X ${MODULE}/pkg/buildinfo.buildDate=${BUILD_DATE} \ + -X ${MODULE}/pkg/buildinfo.branch=${BRANCH} + +.PHONY: run +run: + go run ${GOARGS} -tags "${GOTAGS}" -ldflags "${LDFLAGS}" ${PACKAGE} + +.PHONY: build +build: + go build ${GOARGS} -tags "${GOTAGS}" -ldflags "${LDFLAGS}" -o ${OUTPUT} ${PACKAGE} + +.PHONY: install +install: + go install ${GOARGS} -tags "${GOTAGS}" -ldflags "${LDFLAGS}" ${PACKAGE} + +.PHONY: test +test: + go test -count=1 -v ./... + +.PHONY: build-clean +build-clean: + rm -rf ${OUTPUT} + +.PHONY: lint +lint: + golangci-lint run -v + +.PHONY: pre-push +pre-push: + go mod tidy + make lint + make test + make build \ No newline at end of file diff --git a/cmd/image-vault-2/main.go b/cmd/imv/main.go similarity index 99% rename from cmd/image-vault-2/main.go rename to cmd/imv/main.go index e46dd36..6e5d9a7 100644 --- a/cmd/image-vault-2/main.go +++ b/cmd/imv/main.go @@ -2,8 +2,9 @@ package main import ( "fmt" - "github.com/askolesov/image-vault/pkg/command" "os" + + "github.com/askolesov/image-vault/pkg/command" ) func main() { diff --git a/go.mod b/go.mod index 9bfc971..176c08a 100644 --- a/go.mod +++ b/go.mod @@ -6,13 +6,11 @@ require ( github.com/Masterminds/sprig/v3 v3.3.0 github.com/barasher/go-exiftool v1.10.0 github.com/jedib0t/go-pretty/v6 v6.5.4 - github.com/mitchellh/mapstructure v1.5.0 github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 github.com/samber/lo v1.39.0 github.com/spf13/cobra v1.8.0 - github.com/spf13/viper v1.18.2 github.com/stretchr/testify v1.8.4 - go.uber.org/zap v1.26.0 + gopkg.in/yaml.v2 v2.4.0 gopkg.in/yaml.v3 v3.0.1 ) @@ -21,34 +19,21 @@ require ( github.com/Masterminds/goutils v1.1.1 // indirect github.com/Masterminds/semver/v3 v3.3.0 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect - github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/google/uuid v1.6.0 // indirect - github.com/hashicorp/hcl v1.0.0 // indirect github.com/huandu/xstrings v1.5.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect - github.com/magiconair/properties v1.8.7 // indirect github.com/mattn/go-runewidth v0.0.15 // indirect github.com/mitchellh/copystructure v1.2.0 // indirect github.com/mitchellh/reflectwalk v1.0.2 // indirect - github.com/pelletier/go-toml/v2 v2.1.1 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/rivo/uniseg v0.2.0 // indirect github.com/rogpeppe/go-internal v1.10.0 // indirect - github.com/sagikazarmark/locafero v0.4.0 // indirect - github.com/sagikazarmark/slog-shim v0.1.0 // indirect github.com/shopspring/decimal v1.4.0 // indirect - github.com/sourcegraph/conc v0.3.0 // indirect - github.com/spf13/afero v1.11.0 // indirect github.com/spf13/cast v1.7.0 // indirect github.com/spf13/pflag v1.0.5 // indirect - github.com/subosito/gotenv v1.6.0 // indirect - go.uber.org/multierr v1.11.0 // indirect golang.org/x/crypto v0.26.0 // indirect golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc // indirect golang.org/x/sys v0.23.0 // indirect golang.org/x/term v0.23.0 // indirect - golang.org/x/text v0.17.0 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect - gopkg.in/ini.v1 v1.67.0 // indirect - gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/go.sum b/go.sum index 695ff82..c969f6a 100644 --- a/go.sum +++ b/go.sum @@ -10,19 +10,14 @@ github.com/barasher/go-exiftool v1.10.0 h1:f5JY5jc42M7tzR6tbL9508S2IXdIcG9QyieEX github.com/barasher/go-exiftool v1.10.0/go.mod h1:F9s/a3uHSM8YniVfwF+sbQUtP8Gmh9nyzigNF+8vsWo= github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= -github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= -github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= -github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/huandu/xstrings v1.5.0 h1:2ag3IFq9ZDANvthTwTiqSSZLjDc+BedvHPAp5tJy2TI= github.com/huandu/xstrings v1.5.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= @@ -36,18 +31,12 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= -github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw= github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s= -github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= -github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ= github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= -github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI= -github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -58,43 +47,21 @@ github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncj github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 h1:OkMGxebDjyw0ULyrTYWeN0UNCCkmCWfjPnIA2W6oviI= github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06/go.mod h1:+ePHsJ1keEjQtpvf9HHw0f4ZeJ0TLRsxhunSI2hYJSs= -github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ= -github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4= -github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE= -github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= github.com/samber/lo v1.39.0 h1:4gTz1wUhNYLhFSKl6O+8peW0v2F4BCY034GRpU9WnuA= github.com/samber/lo v1.39.0/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA= github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k= github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME= -github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= -github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= -github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8= -github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY= github.com/spf13/cast v1.7.0 h1:ntdiHjuueXFgm5nzDRdOS4yfT43P5Fnud6DH50rz/7w= github.com/spf13/cast v1.7.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0= github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho= 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/spf13/viper v1.18.2 h1:LUXCnvUvSM6FXAsj6nnfc8Q2tp1dIgUfY9Kc8GsSOiQ= -github.com/spf13/viper v1.18.2/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMVB+yk= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= -github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= -go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk= -go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo= -go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= -go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= -go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo= -go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so= golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw= golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54= golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc h1:ao2WRsKSzW6KuUY9IWPwWahcHCgR0s52IfwutMfEbdM= @@ -103,13 +70,9 @@ golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM= golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.23.0 h1:F6D4vR+EHoL9/sWAWgAR1H2DcHr4PareCbAaCo1RpuU= golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk= -golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= -golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= -gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= -gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pkg/buildinfo/buildinfo.go b/pkg/buildinfo/buildinfo.go new file mode 100644 index 0000000..d6660d0 --- /dev/null +++ b/pkg/buildinfo/buildinfo.go @@ -0,0 +1,49 @@ +package buildinfo + +import ( + "runtime" + + "gopkg.in/yaml.v3" +) + +// Provisioned by ldflags +var ( + version string + branch string + commitHash string + buildDate string +) + +type BuildInfo struct { + Version string `json:"version"` + Branch string `json:"branch"` + CommitHash string `json:"commit_hash"` + BuildDate string `json:"build_date"` + GoVersion string `json:"go_version"` + GoOS string `json:"go_os"` + GoArch string `json:"go_arch"` + Compiler string `json:"compiler"` +} + +func Get() *BuildInfo { + return &BuildInfo{ + Version: version, + Branch: branch, + CommitHash: commitHash, + BuildDate: buildDate, + GoVersion: runtime.Version(), + GoOS: runtime.GOOS, + GoArch: runtime.GOARCH, + Compiler: runtime.Compiler, + } +} + +// YAML returns build info in compressed yaml format +func (b *BuildInfo) YAML() []byte { + res, err := yaml.Marshal(b) //nolint: musttag + if err != nil { + panic(err) // should never happen + } + + return res +} diff --git a/pkg/command/import.go b/pkg/command/add.go similarity index 86% rename from pkg/command/import.go rename to pkg/command/add.go index 106d4f3..b6bdd74 100644 --- a/pkg/command/import.go +++ b/pkg/command/add.go @@ -9,18 +9,18 @@ import ( "strings" "time" - "github.com/askolesov/image-vault/pkg/util" + "github.com/askolesov/image-vault/pkg/vault" "github.com/barasher/go-exiftool" "github.com/jedib0t/go-pretty/v6/progress" "github.com/spf13/cobra" ) -func GetImportCmd() *cobra.Command { +func GetAddCmd() *cobra.Command { var dryRun bool res := &cobra.Command{ - Use: "import", - Short: "import files into the library", + Use: "add", + Short: "add files into the library", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { // Ensure library is initialized @@ -29,8 +29,8 @@ func GetImportCmd() *cobra.Command { return err } - // Import files - return importFiles(cmd, args[0], dryRun, false) + // Add files + return addFiles(cmd, args[0], dryRun, false) }, } @@ -39,7 +39,7 @@ func GetImportCmd() *cobra.Command { return res } -func importFiles(cmd *cobra.Command, importPath string, dryRun, errorOnAction bool) error { +func addFiles(cmd *cobra.Command, addPath string, dryRun, errorOnAction bool) error { // Get library path libPath, err := os.Getwd() if err != nil { @@ -78,7 +78,7 @@ func importFiles(cmd *cobra.Command, importPath string, dryRun, errorOnAction bo pw.AppendTracker(tracker) - inFilesRel, err := util.ListFilesRel(pw.Log, importPath, tracker.Increment, cfg.SkipPermissionDenied) + inFilesRel, err := util.ListFilesRel(pw.Log, addPath, tracker.Increment, cfg.SkipPermissionDenied) if err != nil { return err } @@ -130,7 +130,7 @@ func importFiles(cmd *cobra.Command, importPath string, dryRun, errorOnAction bo for _, f := range inFilesRelLinked { // Copy main file - info, err := util.ExtractMetadata(et, importPath, f.Path) + info, err := util.ExtractMetadata(et, addPath, f.Path) if err != nil { return fmt.Errorf("failed to extract metadata for %s: %w", f.Path, err) } @@ -142,7 +142,7 @@ func importFiles(cmd *cobra.Command, importPath string, dryRun, errorOnAction bo err = util.SmartCopyFile( pw.Log, - path.Join(importPath, f.Path), + path.Join(addPath, f.Path), path.Join(libPath, targetPath), dryRun, errorOnAction, @@ -157,7 +157,7 @@ func importFiles(cmd *cobra.Command, importPath string, dryRun, errorOnAction bo sidecarPath := replaceExtension(targetPath, filepath.Ext(sidecar)) err = util.SmartCopyFile( pw.Log, - path.Join(importPath, sidecarPath), + path.Join(addPath, sidecarPath), path.Join(libPath, sidecarPath), dryRun, errorOnAction, diff --git a/pkg/command/info.go b/pkg/command/info.go index cd6a17b..30d3fd2 100644 --- a/pkg/command/info.go +++ b/pkg/command/info.go @@ -3,7 +3,7 @@ package command import ( "os" - "github.com/askolesov/image-vault/pkg/util" + "github.com/askolesov/image-vault/pkg/vault" "github.com/barasher/go-exiftool" "github.com/spf13/cobra" "gopkg.in/yaml.v2" diff --git a/pkg/command/init.go b/pkg/command/init.go index fbcf28a..9e779f2 100644 --- a/pkg/command/init.go +++ b/pkg/command/init.go @@ -5,7 +5,7 @@ import ( "os" "strings" - "github.com/askolesov/image-vault/pkg/util" + "github.com/askolesov/image-vault/pkg/vault" "github.com/spf13/cobra" ) diff --git a/pkg/command/root.go b/pkg/command/root.go index 8a4058b..73d47eb 100644 --- a/pkg/command/root.go +++ b/pkg/command/root.go @@ -17,9 +17,10 @@ func GetRootCommand() *cobra.Command { } res.AddCommand(GetInitCmd()) - res.AddCommand(GetImportCmd()) + res.AddCommand(GetAddCmd()) res.AddCommand(GetInfoCmd()) res.AddCommand(GetVerifyCmd()) + res.AddCommand(GetVersionCmd()) return res } diff --git a/pkg/command/verify.go b/pkg/command/verify.go index 497c838..d6eda3f 100644 --- a/pkg/command/verify.go +++ b/pkg/command/verify.go @@ -24,7 +24,7 @@ func GetVerifyCmd() *cobra.Command { } // Verify library - return importFiles(cmd, libPath, false, true) + return addFiles(cmd, libPath, false, true) }, } diff --git a/pkg/command/version.go b/pkg/command/version.go new file mode 100644 index 0000000..2d7de60 --- /dev/null +++ b/pkg/command/version.go @@ -0,0 +1,18 @@ +package command + +import ( + "github.com/askolesov/image-vault/pkg/buildinfo" + "github.com/spf13/cobra" +) + +func GetVersionCmd() *cobra.Command { + return &cobra.Command{ + Use: "version", + Short: "Display version information", + RunE: func(cmd *cobra.Command, args []string) error { + info := buildinfo.Get() + cmd.Println(string(info.YAML())) + return nil + }, + } +} diff --git a/pkg/util/sidecar.go b/pkg/util/sidecar.go deleted file mode 100644 index 99c090f..0000000 --- a/pkg/util/sidecar.go +++ /dev/null @@ -1,77 +0,0 @@ -package util - -import ( - "path/filepath" - "strings" - - "github.com/samber/lo" -) - -type FileWithSidecars struct { - Path string - Sidecars []string -} - -func LinkSidecars( - sidecarExtensions []string, - files []string, -) []FileWithSidecars { - // 1. split files into primaries and sidecars - - sidecarExts := lo.Associate(sidecarExtensions, func(item string) (string, any) { - return strings.ToLower(item), true - }) - - isSidecar := func(f string) bool { - _, ok := sidecarExts[strings.ToLower(filepath.Ext(f))] - return ok - } - - var primaries, sidecars []string - - for _, f := range files { - if isSidecar(f) { - sidecars = append(sidecars, f) - } else { - primaries = append(primaries, f) - } - } - - // 2. add primaries with their sidecars to the result - - var result []FileWithSidecars - - sidecarsByPathWithoutExt := lo.GroupBy(sidecars, PathWithoutExtension) - - for _, f := range primaries { - fs := FileWithSidecars{ - Path: f, - } - - pathWithoutExt := PathWithoutExtension(f) - if fSidecars, ok := sidecarsByPathWithoutExt[pathWithoutExt]; ok { - fs.Sidecars = fSidecars - } - - result = append(result, fs) - } - - // 3. add sidecars without primaries to the result as primaries - - primariesByPathWithoutExt := lo.GroupBy(primaries, PathWithoutExtension) - - for _, f := range sidecars { - pathWithoutExt := PathWithoutExtension(f) - if _, ok := primariesByPathWithoutExt[pathWithoutExt]; !ok { - result = append(result, FileWithSidecars{ - Path: f, - }) - } - } - - return result -} - -func PathWithoutExtension(path string) string { - return strings.TrimSuffix(path, filepath.Ext(path)) -} diff --git a/pkg/util/compare.go b/pkg/vault/compare.go similarity index 100% rename from pkg/util/compare.go rename to pkg/vault/compare.go diff --git a/pkg/util/compare_test.go b/pkg/vault/compare_test.go similarity index 100% rename from pkg/util/compare_test.go rename to pkg/vault/compare_test.go diff --git a/pkg/util/config.go b/pkg/vault/config.go similarity index 100% rename from pkg/util/config.go rename to pkg/vault/config.go diff --git a/pkg/util/config_test.go b/pkg/vault/config_test.go similarity index 100% rename from pkg/util/config_test.go rename to pkg/vault/config_test.go diff --git a/pkg/util/copy.go b/pkg/vault/copy.go similarity index 100% rename from pkg/util/copy.go rename to pkg/vault/copy.go diff --git a/pkg/util/copy_test.go b/pkg/vault/copy_test.go similarity index 100% rename from pkg/util/copy_test.go rename to pkg/vault/copy_test.go diff --git a/pkg/util/filter.go b/pkg/vault/filter.go similarity index 100% rename from pkg/util/filter.go rename to pkg/vault/filter.go diff --git a/pkg/util/filter_test.go b/pkg/vault/filter_test.go similarity index 100% rename from pkg/util/filter_test.go rename to pkg/vault/filter_test.go diff --git a/pkg/util/list.go b/pkg/vault/list.go similarity index 100% rename from pkg/util/list.go rename to pkg/vault/list.go diff --git a/pkg/util/list_test.go b/pkg/vault/list_test.go similarity index 100% rename from pkg/util/list_test.go rename to pkg/vault/list_test.go diff --git a/pkg/util/metadata.go b/pkg/vault/metadata.go similarity index 100% rename from pkg/util/metadata.go rename to pkg/vault/metadata.go diff --git a/pkg/util/metadata_test.go b/pkg/vault/metadata_test.go similarity index 100% rename from pkg/util/metadata_test.go rename to pkg/vault/metadata_test.go diff --git a/pkg/vault/sidecar.go b/pkg/vault/sidecar.go new file mode 100644 index 0000000..966c7ac --- /dev/null +++ b/pkg/vault/sidecar.go @@ -0,0 +1,74 @@ +package util + +import ( + "path/filepath" + "strings" + + "github.com/samber/lo" +) + +type FileWithSidecars struct { + Path string + Sidecars []string +} + +func LinkSidecars( + sidecarExtensions []string, + files []string, +) []FileWithSidecars { + // helper functions + + sidecarExts := lo.Associate(sidecarExtensions, func(item string) (string, any) { + return strings.ToLower(item), true + }) + + isSidecar := func(f string) bool { + _, ok := sidecarExts[strings.ToLower(filepath.Ext(f))] + return ok + } + + // result + + var result []FileWithSidecars + + // group all files by their path without extension and process each group + filesByPathWithoutExt := lo.GroupBy(files, PathWithoutExtension) + + for _, group := range filesByPathWithoutExt { + primaries := lo.Filter(group, func(f string, _ int) bool { + return !isSidecar(f) + }) + + sidecars := lo.Filter(group, func(f string, _ int) bool { + return isSidecar(f) + }) + + if len(sidecars) == 0 { // reset empty arrays to nil + sidecars = nil + } + + hasPrimaries := len(primaries) > 0 + + if hasPrimaries { + for _, p := range primaries { + result = append(result, FileWithSidecars{ + Path: p, + Sidecars: sidecars, + }) + } + } else { // no primaries, so all sidecars are added as primaries + for _, f := range sidecars { + result = append(result, FileWithSidecars{ + Path: f, + }) + } + } + } + + // return result + return result +} + +func PathWithoutExtension(path string) string { + return strings.TrimSuffix(path, filepath.Ext(path)) +} diff --git a/pkg/util/sidecar_test.go b/pkg/vault/sidecar_test.go similarity index 99% rename from pkg/util/sidecar_test.go rename to pkg/vault/sidecar_test.go index defc815..f8ce46c 100644 --- a/pkg/util/sidecar_test.go +++ b/pkg/vault/sidecar_test.go @@ -1,8 +1,9 @@ package util import ( - "github.com/stretchr/testify/require" "testing" + + "github.com/stretchr/testify/require" ) func TestLinkSidecars(t *testing.T) { diff --git a/pkg/util/template.go b/pkg/vault/template.go similarity index 100% rename from pkg/util/template.go rename to pkg/vault/template.go diff --git a/pkg/util/template_test.go b/pkg/vault/template_test.go similarity index 100% rename from pkg/util/template_test.go rename to pkg/vault/template_test.go diff --git a/pkg/util/testdata/.hidden.txt b/pkg/vault/testdata/.hidden.txt similarity index 100% rename from pkg/util/testdata/.hidden.txt rename to pkg/vault/testdata/.hidden.txt diff --git a/pkg/util/testdata/capybara.png b/pkg/vault/testdata/capybara.png similarity index 100% rename from pkg/util/testdata/capybara.png rename to pkg/vault/testdata/capybara.png diff --git a/pkg/util/testdata/ignoredDir/ignored.txt b/pkg/vault/testdata/ignoredDir/ignored.txt similarity index 100% rename from pkg/util/testdata/ignoredDir/ignored.txt rename to pkg/vault/testdata/ignoredDir/ignored.txt diff --git a/pkg/util/testdata/testDir/test.jpg b/pkg/vault/testdata/testDir/test.jpg similarity index 100% rename from pkg/util/testdata/testDir/test.jpg rename to pkg/vault/testdata/testDir/test.jpg diff --git a/pkg/util/testdata/testDir/test.txt b/pkg/vault/testdata/testDir/test.txt similarity index 100% rename from pkg/util/testdata/testDir/test.txt rename to pkg/vault/testdata/testDir/test.txt diff --git a/pkg/util/testdata/testDir/test.xmp b/pkg/vault/testdata/testDir/test.xmp similarity index 100% rename from pkg/util/testdata/testDir/test.xmp rename to pkg/vault/testdata/testDir/test.xmp