From 6cc0b09dea0a617596c474b7ce5ba374175593a4 Mon Sep 17 00:00:00 2001 From: "wolfgang.macher" Date: Fri, 6 Sep 2024 16:26:47 +0200 Subject: [PATCH] Added infrastructure cmd --- go.mod | 22 +++++++-- go.sum | 62 +++++++++++++++++++++-- internal/cmd/config.go | 73 +++++++++++++++++++++++++++- internal/cmd/config_test.go | 10 ++++ internal/cmd/git_cmd_runner.go | 14 ++---- internal/cmd/infrastructure_cmd.go | 53 ++++++++++++++++++++ internal/cmd/main.go | 10 +++- internal/cmd/mergehelper_cmd.go | 10 ++-- internal/cmd/mergehelper_cmd_test.go | 20 ++++---- internal/gitmate/abspath.go | 19 ++++++++ internal/gitmate/logger.go | 10 ++++ 11 files changed, 267 insertions(+), 36 deletions(-) create mode 100644 internal/cmd/config_test.go create mode 100644 internal/cmd/infrastructure_cmd.go create mode 100644 internal/gitmate/abspath.go diff --git a/go.mod b/go.mod index 56cfff9..33914fe 100644 --- a/go.mod +++ b/go.mod @@ -6,25 +6,41 @@ require ( github.com/fatih/color v1.17.0 github.com/junegunn/fzf v0.54.3 github.com/spf13/cobra v1.8.1 - github.com/stretchr/testify v1.7.0 + github.com/spf13/viper v1.19.0 + github.com/stretchr/testify v1.9.0 ) require ( github.com/charlievieth/fastwalk v1.0.8 // indirect - github.com/davecgh/go-spew v1.1.1 // indirect + github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect + github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/gdamore/encoding v1.0.0 // indirect github.com/gdamore/tcell/v2 v2.7.4 // indirect + github.com/hashicorp/hcl v1.0.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/lucasb-eyer/go-colorful v1.2.0 // indirect + github.com/magiconair/properties v1.8.7 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-runewidth v0.0.15 // indirect github.com/mattn/go-shellwords v1.0.12 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/mitchellh/mapstructure v1.5.0 // indirect + github.com/pelletier/go-toml/v2 v2.2.2 // indirect + github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/rivo/uniseg v0.4.7 // indirect + github.com/sagikazarmark/locafero v0.4.0 // indirect + github.com/sagikazarmark/slog-shim v0.1.0 // indirect + github.com/sourcegraph/conc v0.3.0 // indirect + github.com/spf13/afero v1.11.0 // indirect + github.com/spf13/cast v1.6.0 // indirect github.com/spf13/pflag v1.0.5 // indirect + github.com/subosito/gotenv v1.6.0 // indirect + go.uber.org/atomic v1.9.0 // indirect + go.uber.org/multierr v1.9.0 // indirect + golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect golang.org/x/sys v0.22.0 // indirect golang.org/x/term v0.22.0 // indirect golang.org/x/text v0.14.0 // indirect + gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index afc0571..c4328a6 100644 --- a/go.sum +++ b/go.sum @@ -2,20 +2,35 @@ github.com/charlievieth/fastwalk v1.0.8 h1:uaoH6cAKSk73aK7aKXqs0+bL+J3Txzd3NGH8t github.com/charlievieth/fastwalk v1.0.8/go.mod h1:yGy1zbxog41ZVMcKA/i8ojXLFsuayX5VvwhQVoj9PBI= github.com/cpuguy83/go-md2man/v2 v2.0.4/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 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 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/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4= github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI= +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/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko= github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg= github.com/gdamore/tcell/v2 v2.7.4 h1:sg6/UnTM9jGpZU+oFYAsDahfchWAFW8Xx2yFinNSAYU= github.com/gdamore/tcell/v2 v2.7.4/go.mod h1:dSXtXTSK0VsW1biw65DZLZ2NKr7j0qP/0J7ONmsraWg= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +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/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/junegunn/fzf v0.54.3 h1:iETXikQNR0bE42zHfQzY4ut1TRKdSyJQ1WOR8KFPUxA= github.com/junegunn/fzf v0.54.3/go.mod h1:trNoavBtk7Fj/J9Og8rJIVpL66XlVhAP5Qz1WolKqTA= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= +github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= +github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= @@ -25,23 +40,57 @@ github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZ github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/mattn/go-shellwords v1.0.12 h1:M2zGm7EW6UQJvDeQxo4T51eKPurbeFbe8WtebGE2xrk= github.com/mattn/go-shellwords v1.0.12/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +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/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= +github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= 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= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +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/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.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0= +github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= 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/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI= +github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+Ntkg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +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/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +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/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +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= +github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= +github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= +go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI= +go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g= +golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= @@ -79,8 +128,11 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -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/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +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.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 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/cmd/config.go b/internal/cmd/config.go index 22ab02a..3dd6ae2 100644 --- a/internal/cmd/config.go +++ b/internal/cmd/config.go @@ -1,18 +1,55 @@ package cmd import ( + "os" + "os/exec" + "strings" + "github.com/Terrorknubbel/gitmate/internal/gitmate" "github.com/spf13/cobra" + "github.com/spf13/viper" ) type Config struct { logger *gitmate.Logger + + infrastructureAbsPath gitmate.AbsPath + commandDirAbsPath gitmate.AbsPath } -func NewConfig() *Config { - return &Config{ +func NewConfig() (*Config, error) { + c := &Config{ logger: gitmate.DefaultLogger(), } + + wd, err := os.Getwd() + if err != nil { + return nil, err + } + + viper.SetConfigName("config") + viper.SetConfigType("json") + viper.AddConfigPath("$HOME/.config/gitmate") + viper.SetEnvPrefix("gitmate") + viper.BindEnv("infrastructure_path") + + if err := viper.ReadInConfig(); err != nil { + if _, ok := err.(viper.ConfigFileNotFoundError); ok { + c.infrastructureAbsPath = gitmate.AbsPath(viper.GetString("INFRASTRUCTURE_PATH")) + } else { + return c, err + } + } else { + c.infrastructureAbsPath = gitmate.AbsPath(viper.GetString("infrastructure_path")) + } + + if c.infrastructureAbsPath.Empty() { + c.infrastructureAbsPath = gitmate.AbsPath(viper.GetString("INFRASTRUCTURE_PATH")) + } + + c.commandDirAbsPath = gitmate.AbsPath(wd) + + return c, nil } // newRootCmd returns a new root github.com/spf13/cobra.Command. @@ -27,6 +64,7 @@ func (c *Config) newRootCmd() (*cobra.Command, error) { for _, cmd := range []*cobra.Command{ c.newMenuViewPreviewCmd(), c.newMergehelperCmd(), + c.newInfrastructureCmd(), } { if cmd != nil { rootCmd.AddCommand(cmd) @@ -46,3 +84,34 @@ func (c *Config) execute(args []string) error { return rootCmd.Execute() } + +func (c *Config) run(name string, args ...string) (string, error) { + cmd := exec.Command(name, args...) + + cmd.Dir = c.commandDirAbsPath.String() + + output, err := cmd.Output() + if err != nil { + return "", err + } + + outputString := strings.TrimSpace(string(output[:])) + + return outputString, nil +} + +// withinDir changes the current working directory to the given directory and +// executes the given function. It restores the current working directory after +// the function has finished. +func (c *Config) withinDir(dir gitmate.AbsPath, f func(c *Config) error) error { + oldDir := c.commandDirAbsPath + defer func() { + c.commandDirAbsPath = oldDir + c.logger.SystemInfo("Wechsle ins Verzeichnis " + oldDir.String()) + }() + + c.logger.SystemInfo("Wechsle ins Verzeichnis " + dir.String()) + c.commandDirAbsPath = dir + + return f(c) +} diff --git a/internal/cmd/config_test.go b/internal/cmd/config_test.go new file mode 100644 index 0000000..0eb3ba5 --- /dev/null +++ b/internal/cmd/config_test.go @@ -0,0 +1,10 @@ +package cmd + +import "github.com/stretchr/testify/suite" + +func newTestConfig(s *suite.Suite) *Config { + config, err := NewConfig() + s.NoError(err) + + return config +} diff --git a/internal/cmd/git_cmd_runner.go b/internal/cmd/git_cmd_runner.go index 2ae9d29..8d4e479 100644 --- a/internal/cmd/git_cmd_runner.go +++ b/internal/cmd/git_cmd_runner.go @@ -2,9 +2,6 @@ package cmd import ( "errors" - "fmt" - "os/exec" - "strings" ) type GitCommand struct { @@ -17,7 +14,7 @@ type GitCommand struct { type GitCommands []GitCommand -func (c *Config) RunCommands(commands GitCommands) error { +func (c *Config) RunGitCommands(commands GitCommands) error { for _, command := range commands { err := c.runCommand(command) if err != nil { @@ -31,20 +28,17 @@ func (c *Config) RunCommands(commands GitCommands) error { func (c *Config) runCommand(command GitCommand) error { c.logger.Info(command.Output) - output, err := exec.Command("git", command.Args...).Output() - outputString := strings.TrimSpace(string(output[:])) - + output, err := c.run("git", command.Args...) if err != nil { - fmt.Println(err.Error()) return errors.New(command.ErrorMessage) } - if command.Expectation != "*" && command.Expectation != outputString { + if command.Expectation != "*" && command.Expectation != output { return errors.New(command.ErrorMessage) } for _, v := range command.Forbidden { - if outputString == v { + if output == v { return errors.New(command.ErrorMessage) } } diff --git a/internal/cmd/infrastructure_cmd.go b/internal/cmd/infrastructure_cmd.go new file mode 100644 index 0000000..3045c8b --- /dev/null +++ b/internal/cmd/infrastructure_cmd.go @@ -0,0 +1,53 @@ +package cmd + +import ( + "errors" + + "github.com/spf13/cobra" +) + +func (c *Config) newInfrastructureCmd() *cobra.Command { + gitCmd := &cobra.Command{ + Use: "infrastructure", + Aliases: []string{"i"}, + Short: "Bringt das Infrastructure Repo auf den aktuellsten Stand und stellt den korrekten Zustand sicher", + RunE: c.infrastructure, + } + + return gitCmd +} + +func (c *Config) infrastructure(cmd *cobra.Command, args []string) error { + if c.infrastructureAbsPath.Empty() { + //lint:ignore ST1005 Capitalization is fine… + return errors.New("Kein Infrastructure Verzeichnis angegeben. Setze die Umgebungsvariable GITMATE_INFRASTRUCTURE_PATH oder setze den Wert für 'infrastructure_path' in '~/.config/gitmate/config.json'.") + } + + if !c.infrastructureAbsPath.IsAbs() { + return errors.New("Das Infrastructure Verzeichnis muss absolut sein. Aktuell gesetztes Verzeichnis '" + c.infrastructureAbsPath.String() + "'.") + } + + err := c.withinDir(c.infrastructureAbsPath, func(c *Config) error { + err := c.RunGitCommands(infrastructureCommands()) + if err == nil { + c.logger.Success("Infrastructure ist nun auf dem aktuellsten Stand.") + } + return err + }) + + return err +} + +func infrastructureCommands() GitCommands { + return GitCommands{ + {Args: []string{"--version"}, Output: "", Expectation: "*", Forbidden: []string{}, ErrorMessage: "Git ist nicht installiert."}, + {Args: []string{"rev-parse", "--is-inside-work-tree"}, Output: "", Expectation: "*", Forbidden: []string{}, ErrorMessage: "Du befindest dich in keinem Git Verzeichnis."}, + {Args: []string{"branch", "--show-current"}, Output: "Überprüfe Branch…", Expectation: "master", Forbidden: []string{}, ErrorMessage: "Du befindest dich nicht im master Branch."}, + {Args: []string{"status", "--porcelain"}, Output: "Prüfe auf Änderungen, die nicht zum Commit vorgesehen sind…", Expectation: "", Forbidden: []string{}, ErrorMessage: "Es gibt Änderungen, die nicht zum Commit vorgesehen sind. Bitte Committe oder Stashe diese vor einem Merge."}, + {Args: []string{"ls-remote", "origin", "master"}, Output: "Prüfe auf Remote Branch…", Expectation: "*", Forbidden: []string{}, ErrorMessage: "Der master Branch existiert nicht auf remote."}, + {Args: []string{"fetch"}, Output: "Aktualisiere Branch Informationen…", Expectation: "*", Forbidden: []string{}, ErrorMessage: ""}, + {Args: []string{"log", "origin/master..master"}, Output: "Prüfe auf nicht gepushte Commits…", Expectation: "", Forbidden: []string{}, ErrorMessage: "Es gibt nicht gepushte Änderungen. Bitte pushe diese vor einem Merge."}, + {Args: []string{"log", "master.." + "origin/master"}, Output: "Prüfe auf nicht gemergte Commits…", Expectation: "", Forbidden: []string{}, ErrorMessage: "Es gibt nicht gemergte Änderungen. Bitte führe ein 'git pull --rebase' vor einem Merge aus."}, + {Args: []string{"pull", "--rebase"}, Output: "Bringe den master Branch auf den aktuellsten Stand", Expectation: "*", Forbidden: []string{}, ErrorMessage: "Master pull fehlgeschlagen."}, + } +} diff --git a/internal/cmd/main.go b/internal/cmd/main.go index 155c657..8619f95 100644 --- a/internal/cmd/main.go +++ b/internal/cmd/main.go @@ -1,13 +1,19 @@ package cmd func Main(args []string) { - c := NewConfig() + c, err := NewConfig() + + if err != nil { + c.logger.Error(err) + return + } + if len(args) == 0 { c.RunMenuView() return } - err := c.execute(args) + err = c.execute(args) if err != nil { c.logger.Error(err) } diff --git a/internal/cmd/mergehelper_cmd.go b/internal/cmd/mergehelper_cmd.go index 382b48f..13e63b4 100644 --- a/internal/cmd/mergehelper_cmd.go +++ b/internal/cmd/mergehelper_cmd.go @@ -37,7 +37,7 @@ func (c *Config) newMergehelperCmd() *cobra.Command { func (c *Config) RunMergehelper(cmd *cobra.Command, args []string) error { c.logger.Success("Überprüfe Vorbedingungen") - err := c.RunCommands(prerequisiteCommands()) + err := c.RunGitCommands(prerequisiteCommands()) if err != nil { return err } @@ -47,7 +47,7 @@ func (c *Config) RunMergehelper(cmd *cobra.Command, args []string) error { return err } - err = c.RunCommands(branchConditionCommands(currentBranch)) + err = c.RunGitCommands(branchConditionCommands(currentBranch)) if err != nil { return err } @@ -61,12 +61,12 @@ func (c *Config) RunMergehelper(cmd *cobra.Command, args []string) error { c.logger.Success("Bringe den Master Branch auf den aktuellsten Stand") } - err = c.RunCommands(branchRebaseCommands(action)) + err = c.RunGitCommands(branchRebaseCommands(action)) if err != nil { return err } - err = c.RunCommands(branchMergeCommands(currentBranch, action)) + err = c.RunGitCommands(branchMergeCommands(currentBranch, action)) if err != nil { return err } @@ -154,7 +154,7 @@ func handleFinalPush(c *Config) { input = strings.TrimSpace(strings.ToLower(input)) if input == "y" || input == "yes" { - err = c.RunCommands(finalPushCommands()) + err = c.RunGitCommands(finalPushCommands()) if err != nil { c.logger.Error(err) diff --git a/internal/cmd/mergehelper_cmd_test.go b/internal/cmd/mergehelper_cmd_test.go index e9adb85..0185d45 100644 --- a/internal/cmd/mergehelper_cmd_test.go +++ b/internal/cmd/mergehelper_cmd_test.go @@ -20,8 +20,6 @@ var stagingArgs = []string{ "staging", } -var c = NewConfig() - func CaptureStderr(f func(args []string) error, args []string) string { // Suppress stdout oldStdout := os.Stdout @@ -66,7 +64,7 @@ func (suite *LocalRepoSuite) TearDownTest() { } func (suite *LocalRepoSuite) Test_PrerequisiteCommands_NotInAGitDir() { - output := CaptureStderr(c.execute, stagingArgs) + output := CaptureStderr(suite.testConfig().execute, stagingArgs) suite.Contains(output, "Du befindest dich in keinem Git Verzeichnis.") } @@ -74,7 +72,7 @@ func (suite *LocalRepoSuite) Test_PrerequisiteCommands_NotInAGitDir() { func (suite *LocalRepoSuite) Test_BranchConditionCommands_NotInAFeatureBranch() { exec.Command("git", "init").Run() - output := CaptureStderr(c.execute, stagingArgs) + output := CaptureStderr(suite.testConfig().execute, stagingArgs) suite.Contains(output, "Du befindest dich in keinem Feature Branch.") } @@ -82,7 +80,7 @@ func (suite *LocalRepoSuite) Test_BranchConditionCommands_CurrentBranchIsStaging exec.Command("git", "init").Run() exec.Command("git", "checkout", "-b", "staging").Run() - output := CaptureStderr(c.execute, stagingArgs) + output := CaptureStderr(suite.testConfig().execute, stagingArgs) suite.Contains(output, "Du befindest dich in keinem Feature Branch.") } @@ -95,7 +93,7 @@ func (suite *LocalRepoSuite) Test_BranchConditionCommands_UnstashedChanges() { outfile.WriteString("Commit 1") - output := CaptureStderr(c.execute, stagingArgs) + output := CaptureStderr(suite.testConfig().execute, stagingArgs) suite.Contains(output, "Es gibt Änderungen, die nicht zum Commit vorgesehen sind. Bitte Committe oder Stashe diese vor einem Merge.") } @@ -112,7 +110,7 @@ func (suite *LocalRepoSuite) Test_BranchConditionCommands_NoRemoteBranch() { exec.Command("git", "add", ".").Run() exec.Command("git", "commit", "-m", "commit1").Run() - output := CaptureStderr(c.execute, stagingArgs) + output := CaptureStderr(suite.testConfig().execute, stagingArgs) suite.Contains(output, "Der Branch feature_branch existiert nicht auf remote.") } @@ -128,7 +126,7 @@ func (suite *LocalRepoSuite) Test_BranchConditionCommands_UnstagedChanges() { exec.Command("git", "add", ".").Run() exec.Command("git", "commit", "-m", "commit1").Run() - output := CaptureStderr(c.execute, stagingArgs) + output := CaptureStderr(suite.testConfig().execute, stagingArgs) suite.Contains(output, "Es gibt nicht gepushte Änderungen. Bitte pushe diese vor einem Merge.") } @@ -154,7 +152,7 @@ func (suite *LocalRepoSuite) Test_BranchConditionCommands_UnmergedChanges() { os.Chdir(suite.localRepoDir) - output := CaptureStderr(c.execute, stagingArgs) + output := CaptureStderr(suite.testConfig().execute, stagingArgs) suite.Contains(output, "Es gibt nicht gemergte Änderungen. Bitte führe ein 'git pull --rebase' vor einem Merge aus.") } @@ -164,3 +162,7 @@ func (suite *LocalRepoSuite) Test_BranchConditionCommands_UnmergedChanges() { func TestExampleTestSuite(t *testing.T) { suite.Run(t, new(LocalRepoSuite)) } + +func (suite *LocalRepoSuite) testConfig() *Config { + return newTestConfig(&suite.Suite) +} diff --git a/internal/gitmate/abspath.go b/internal/gitmate/abspath.go new file mode 100644 index 0000000..6b0a883 --- /dev/null +++ b/internal/gitmate/abspath.go @@ -0,0 +1,19 @@ +package gitmate + +import ( + "path/filepath" +) + +type AbsPath string + +func (a AbsPath) Empty() bool { + return a == "" +} + +func (a AbsPath) IsAbs() bool { + return filepath.IsAbs(a.String()) +} + +func (a AbsPath) String() string { + return string(a) +} diff --git a/internal/gitmate/logger.go b/internal/gitmate/logger.go index 62110b2..c811278 100644 --- a/internal/gitmate/logger.go +++ b/internal/gitmate/logger.go @@ -24,6 +24,16 @@ func (l *Logger) Info(output string) { fmt.Println(output) } +func (l *Logger) SystemInfo(output string) { + if output == "" { + return + } + + color.Set(color.FgMagenta) + defer color.Unset() + fmt.Println(output) +} + func (l *Logger) Warning(output string) { if output == "" { return