From 62b316b5efac630298ad48dce62ae2e0d96b9f70 Mon Sep 17 00:00:00 2001 From: Tom Neuber Date: Sat, 27 Jul 2024 18:56:09 +0200 Subject: [PATCH] refactor(cmd): add backup command to cli --- cfg/cfg.go | 99 ------------------------ go.mod | 22 +++++- go.sum | 53 +++++++++++++ internal/cmd/backup.go | 148 ++++++++++++++++++++++++++++++++++++ internal/cmd/root.go | 76 ++++++++++++++++++ internal/config/config.go | 52 ++++++++----- internal/version/version.go | 17 +++++ main.go | 83 ++------------------ 8 files changed, 357 insertions(+), 193 deletions(-) delete mode 100644 cfg/cfg.go create mode 100644 internal/cmd/backup.go create mode 100644 internal/cmd/root.go create mode 100644 internal/version/version.go diff --git a/cfg/cfg.go b/cfg/cfg.go deleted file mode 100644 index dac8eb7..0000000 --- a/cfg/cfg.go +++ /dev/null @@ -1,99 +0,0 @@ -package cfg - -import ( - "fmt" - "os" - - "github.com/alecthomas/kong" -) - -type AppSettings struct { - Force bool - GrafanaURL string - GrafanaToken string - GitBranch string - GitRepoURL string - GitUser string - GitEmail string - GitPass string - GPGKey string -} - -var cliStruct struct { - Force bool `name:"force" short:"f" help:"Force git commits / ignore existence check."` - GrafanaURL string `name:"grafana-url" env:"GRAFANA_URL" help:"Grafana URL to access the API"` - GrafanaToken string `name:"grafana-auth-token" env:"GRAFANA_AUTH_TOKEN" help:"Grafana auth token to access the API"` - GitBranch string `name:"git-branch" env:"GIT_BRANCH" help:"Git branch name" default:"${default_git_branch}"` - GitRepoURL string `name:"git-repo-url" env:"GIT_REPO_URL" help:"Complete Git repository URL"` - GitUser string `name:"git-user" env:"GIT_USER" help:"Git username"` - GitEmail string `name:"git-email" env:"GIT_EMAIL" help:"Git email address"` - GitPass string `name:"git-pass" env:"GIT_PASS" help:"Git user password"` - GPGKey string `name:"signing-key" env:"GIT_SIGNING_KEY" help:"GPG signing key"` -} - -func Parse() *AppSettings { - ctx := kong.Parse( - &cliStruct, - kong.Vars{ - "default_git_branch": "main", - }, - kong.Name("grafana-backuper"), - kong.Description("🚀 CLI tool to convert grafana dashboards to git"), - kong.UsageOnError(), - ) - - validateFlags(ctx) - return &AppSettings{ - Force: cliStruct.Force, - GrafanaURL: cliStruct.GrafanaURL, - GrafanaToken: cliStruct.GrafanaToken, - GitBranch: cliStruct.GitBranch, - GitRepoURL: cliStruct.GitRepoURL, - GitUser: cliStruct.GitUser, - GitEmail: cliStruct.GitEmail, - GitPass: cliStruct.GitPass, - GPGKey: cliStruct.GPGKey, - } -} - -func validateFlags(cliCtx *kong.Context) { - flagsValid := true - messages := []string{} - - if cliStruct.GrafanaURL == "" { - messages = append(messages, "error: invalid grafana URL, must not be blank") - flagsValid = false - } - - if cliStruct.GrafanaToken == "" { - messages = append(messages, "error: invalid auth token for grafana, must not be blank") - flagsValid = false - } - - if cliStruct.GitRepoURL == "" { - messages = append(messages, "error: invalid repo url for git, must not be blank") - flagsValid = false - } - - if cliStruct.GitUser == "" { - messages = append(messages, "error: invalid username for git, must not be blank") - flagsValid = false - } - - if cliStruct.GitPass == "" { - messages = append(messages, "error: invalid password for git, must not be blank") - flagsValid = false - } - - if !flagsValid { - if err := cliCtx.PrintUsage(false); err != nil { - fmt.Println(err.Error()) - os.Exit(1) - } - fmt.Println() - for i := 0; i < len(messages); i++ { - fmt.Println(messages[i]) - } - os.Exit(1) - } -} diff --git a/go.mod b/go.mod index 9ae5da0..6521940 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,10 @@ require ( github.com/go-git/go-billy/v5 v5.5.0 github.com/go-git/go-git/v5 v5.12.0 github.com/peterbourgon/ff v1.7.1 - golang.org/x/net v0.22.0 + github.com/spf13/cobra v1.8.1 + github.com/spf13/pflag v1.0.5 + github.com/spf13/viper v1.19.0 + golang.org/x/net v0.23.0 ) require ( @@ -17,18 +20,35 @@ require ( github.com/cloudflare/circl v1.3.7 // indirect github.com/cyphar/filepath-securejoin v0.2.4 // indirect github.com/emirpasic/gods v1.18.1 // indirect + github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect + github.com/hashicorp/hcl v1.0.0 // indirect + github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect github.com/kevinburke/ssh_config v1.2.0 // indirect + github.com/magiconair/properties v1.8.7 // indirect + github.com/mitchellh/mapstructure v1.5.0 // indirect + github.com/pelletier/go-toml/v2 v2.2.2 // indirect github.com/pjbgf/sha1cd v0.3.0 // indirect + github.com/sagikazarmark/locafero v0.4.0 // indirect + github.com/sagikazarmark/slog-shim v0.1.0 // indirect github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect github.com/skeema/knownhosts v1.2.2 // 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/subosito/gotenv v1.6.0 // indirect github.com/xanzy/ssh-agent v0.3.3 // indirect + go.uber.org/atomic v1.9.0 // indirect + go.uber.org/multierr v1.9.0 // indirect golang.org/x/crypto v0.21.0 // indirect + golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect golang.org/x/mod v0.15.0 // indirect golang.org/x/sys v0.18.0 // indirect golang.org/x/text v0.14.0 // indirect golang.org/x/tools v0.18.0 // indirect + gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/warnings.v0 v0.1.2 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 6714b3a..fdef2d5 100644 --- a/go.sum +++ b/go.sum @@ -20,15 +20,19 @@ github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7N github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA= github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU= github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA= +github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/cyphar/filepath-securejoin v0.2.4 h1:Ugdm7cg7i6ZK6x3xDF1oEu1nfkyfH53EtKeQYTC3kyg= github.com/cyphar/filepath-securejoin v0.2.4/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4= 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/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a h1:mATvB/9r/3gvcejNsXKSkQ6lcIaNec2nyfOdlTBR2lU= github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a/go.mod h1:Ro8st/ElPeALwNFlcTpWmkr6IoMFfkjXAvTHpevnDsM= github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ= +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/gliderlabs/ssh v0.3.7 h1:iV3Bqi942d9huXnzEF2Mt+CY9gLu8DNM4Obd+8bODRE= github.com/gliderlabs/ssh v0.3.7/go.mod h1:zpHEXBstFnQYtGnB8k8kQLol82umzn/2/snG7alWVD8= github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI= @@ -43,8 +47,12 @@ github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= 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/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/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM= github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4= @@ -56,10 +64,17 @@ 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/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo= +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/onsi/gomega v1.27.10 h1:naR28SdDFlqrG6kScpT8VWpu1xWY5nJRCF3XaYyBjhI= github.com/onsi/gomega v1.27.10/go.mod h1:RsS8tutOdbdgzbPtzzATp12yT7kM5I5aElG3evPbQ0M= +github.com/pelletier/go-toml v1.6.0 h1:aetoXYr0Tv7xRU/V4B4IZJ2QcbtMUFoNb3ORp7TzIK4= github.com/pelletier/go-toml v1.6.0/go.mod h1:5N711Q9dKgbdkxHL+MEfF31hpT7l0S0s/t2kKREewys= +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/peterbourgon/ff v1.7.1 h1:xt1lxTG+Nr2+tFtysY7abFgPoH3Lug8CwYJMOmJRXhk= github.com/peterbourgon/ff v1.7.1/go.mod h1:fYI5YA+3RDqQRExmFbHnBjEeWzh9TrS8rnRpEq7XIg0= github.com/pjbgf/sha1cd v0.3.0 h1:4D5XXmUUBUl/xQ6IjCkEAbqXskkq/4O7LmGn0AqMDs4= @@ -68,21 +83,52 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 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/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= +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/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8= github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4= github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/skeema/knownhosts v1.2.2 h1:Iug2P4fLmDw9f41PB6thxUkNUkJzB5i+1/exaj40L3A= github.com/skeema/knownhosts v1.2.2/go.mod h1:xYbVRSPxqBZFrdmDyMmsOs+uX1UZC3nTN3ThzgDxUwo= +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/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.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +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/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM= github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw= 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/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= @@ -90,6 +136,8 @@ golang.org/x/crypto v0.3.1-0.20221117191849-2c476679df9a/go.mod h1:hebNnKkNXi2Uz golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU= golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA= golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= +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/mod v0.15.0 h1:SernR4v+D55NyBH2QiEQrlBAnj1ECL6AGrA5+dPaMY8= @@ -103,6 +151,8 @@ golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc= golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= +golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs= +golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -151,10 +201,13 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/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/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME= gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 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= 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/backup.go b/internal/cmd/backup.go new file mode 100644 index 0000000..9b18f82 --- /dev/null +++ b/internal/cmd/backup.go @@ -0,0 +1,148 @@ +package cmd + +import ( + "context" + "encoding/json" + "fmt" + "slices" + + "git.ar21.de/yolokube/grafana-backuper/internal/config" + "git.ar21.de/yolokube/grafana-backuper/pkg/git" + "git.ar21.de/yolokube/grafana-backuper/pkg/grafana" + "github.com/spf13/cobra" +) + +func NewBackupCommand(c *config.Config) *cobra.Command { + backupCmd := &cobra.Command{ + Use: "backup", + Short: "Back up the dashboards from grafana to a git repository.", + Long: "Back up the dashboards from grafana to a git repository.", + RunE: func(cmd *cobra.Command, _ []string) error { + return backup(cmd.Context(), c) + }, + PreRunE: func(_ *cobra.Command, _ []string) error { + return c.Validate() + }, + } + + backupCmd.PersistentFlags().BoolVar(&c.ForceCommits, "force", false, "Force git commits / ignore existence check") + backupCmd.PersistentFlags().StringVar(&c.GitEmail, "git-email", "", "Git email address") + backupCmd.PersistentFlags().StringVar(&c.GPGKey, "signing-key", "", "Path to the GPG signing key") + + return backupCmd +} + +func backup(ctx context.Context, c *config.Config) error { + client := grafana.NewClient( + c.GrafanaURL, + grafana.WithToken(c.GrafanaToken), + ) + + project := git.NewProject( + c.GitRepo, + git.WithBasicAuth(c.GitUser, c.GitPass), + git.WithBranch(c.GitBranch), + ) + + if err := project.Clone(ctx); err != nil { + return err + } + + if err := project.Checkout(); err != nil { + return err + } + + if err := project.Pull(ctx); err != nil { + return err + } + + var signer git.SignKey + if c.GPGKey != "" { + signer = git.SignKey{KeyFile: c.GPGKey} + if err := signer.ReadKeyFile(); err != nil { + return err + } + } + + dashboards, _, err := client.File.Search(ctx, grafana.WithType(grafana.SearchTypeDashboard)) + if err != nil { + return err + } + + for _, dashboardInfo := range dashboards { + var dashboard *grafana.Dashboard + dashboard, _, err = client.Dashboard.Get(ctx, dashboardInfo.UID) + if err != nil { + return err + } + + var versions []*grafana.DashboardVersion + versions, _, err = client.DashboardVersion.List(ctx, dashboardInfo.UID) + if err != nil { + return err + } + + slices.Reverse(versions) + + var uncommitedVersion bool + for _, version := range versions { + var dashboardVersion *grafana.DashboardVersion + dashboardVersion, _, err = client.DashboardVersion.Get(ctx, dashboardInfo.UID, version.Version) + if err != nil { + return err + } + + dashboard.Dashboard = dashboardVersion.Data + dashboard.UpdatedBy = dashboardVersion.CreatedBy + dashboard.Updated = dashboardVersion.Created + dashboard.Version = dashboardVersion.Version + + var data []byte + data, err = json.MarshalIndent(grafana.SchemaFromDashboardMeta(dashboard), "", " ") + if err != nil { + return err + } + + commitMsg := fmt.Sprintf( + "%s: Update %s to version %d", + dashboardInfo.Title, + dashboardInfo.UID, + dashboardVersion.ID, + ) + if dashboardVersion.Message != "" { + commitMsg += fmt.Sprintf(" => %s", dashboardVersion.Message) + } + + commitOpts := []git.CommitOption{ + git.WithAuthor(dashboardVersion.CreatedBy, ""), + git.WithFileContent(data, dashboardInfo.Title, dashboard.FolderTitle), + git.WithSigner(signer), + } + + if c.GitUser != "" { + commitOpts = append(commitOpts, git.WithCommitter(c.GitUser, c.GitEmail)) + } + + commit := project.NewCommit(commitOpts...) + + if commit.Exists(dashboardVersion.DashboardUID, dashboardVersion.ID) && !c.ForceCommits && !uncommitedVersion { + fmt.Fprintf(c.Output, "%s -> already committed\n", commitMsg) + continue + } + + uncommitedVersion = true + if err = commit.Create(commitMsg); err != nil { + return err + } + fmt.Fprintln(c.Output, commitMsg) + } + } + + if project.HasChanges() { + if err = project.Push(ctx); err != nil { + return err + } + } + + return nil +} diff --git a/internal/cmd/root.go b/internal/cmd/root.go new file mode 100644 index 0000000..43bf4d8 --- /dev/null +++ b/internal/cmd/root.go @@ -0,0 +1,76 @@ +package cmd + +import ( + "fmt" + "os" + "strings" + + "git.ar21.de/yolokube/grafana-backuper/internal/config" + "git.ar21.de/yolokube/grafana-backuper/internal/version" + "github.com/spf13/cobra" + "github.com/spf13/pflag" + "github.com/spf13/viper" +) + +const envVarPrefix = "GB" + +func NewRootCommand(c *config.Config) *cobra.Command { + rootCmd := &cobra.Command{ + Use: "grafana-backuper", + Short: "Grafana Backuper CLI", + Long: "A command-line tool to back up and restore Grafana dashboards", + Version: version.Version(), + PersistentPreRunE: func(cmd *cobra.Command, _ []string) error { + initializeConfig(cmd) + + c.Output = os.Stdout + if c.Quiet { + var err error + c.Output, err = os.Open(os.DevNull) + if err != nil { + return err + } + } + + cmd.SetOut(c.Output) + return nil + }, + SilenceUsage: true, + } + + rootCmd.SetErrPrefix("Error:") + + rootCmd.PersistentFlags().BoolVarP(&c.Debug, "debug", "d", false, "Debug output") + rootCmd.PersistentFlags().StringVar(&c.GrafanaURL, "grafana-url", "", "Grafana URL to access the API") + rootCmd.PersistentFlags().StringVar(&c.GrafanaToken, "grafana-token", "", "Grafana auth token to access the API") + rootCmd.PersistentFlags().StringVar(&c.GitBranch, "git-branch", "main", "Git branch name") + rootCmd.PersistentFlags().StringVar(&c.GitRepo, "git-repo", "", "Complete Git repository URL") + rootCmd.PersistentFlags().StringVar(&c.GitUser, "git-user", "", "Git user name") + rootCmd.PersistentFlags().StringVar(&c.GitPass, "git-pass", "", "Git user password") + rootCmd.PersistentFlags().BoolVarP(&c.Quiet, "quiet", "q", false, "Mute console output") + + return rootCmd +} + +func initializeConfig(cmd *cobra.Command) { + v := viper.New() + v.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) + v.SetEnvPrefix(envVarPrefix) + v.AutomaticEnv() + bindFlags(cmd, v) +} + +func bindFlags(cmd *cobra.Command, v *viper.Viper) { + cmd.Flags().VisitAll(func(flag *pflag.Flag) { + // Apply the viper config value to the flag when the flag is not set and viper has a value + if !flag.Changed && v.IsSet(flag.Name) { + err := cmd.Flags().Set( + flag.Name, + fmt.Sprintf("%v", v.Get(flag.Name)), + ) + if err != nil { + fmt.Fprintf(os.Stderr, "Error: %v\n", err) + } + } + }) +} diff --git a/internal/config/config.go b/internal/config/config.go index 9da470a..c86a1ac 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -1,36 +1,52 @@ package config import ( - "flag" - "os" - - "github.com/peterbourgon/ff" + "errors" + "io" ) -const envVarPrefix = "GB" - type Config struct { + Debug bool ForceCommits bool GrafanaURL string GrafanaToken string GitBranch string + GitEmail string GitRepo string GitUser string - GitEmail string GitPass string GPGKey string + Quiet bool + + Output io.Writer } -func (c *Config) RegisterFlags(fs *flag.FlagSet) error { - fs.BoolVar(&c.ForceCommits, "force-commits", false, "Force git commits / ignore existence check") - fs.StringVar(&c.GrafanaURL, "grafana-url", "", "Grafana URL to access the API") - fs.StringVar(&c.GrafanaToken, "grafana-token", "", "Grafana auth token to access the API") - fs.StringVar(&c.GitBranch, "git-branch", "main", "Git branch name") - fs.StringVar(&c.GitRepo, "git-repo", "", "Complete Git repository URL") - fs.StringVar(&c.GitUser, "git-user", "", "Git username") - fs.StringVar(&c.GitEmail, "git-email", "", "Git email address") - fs.StringVar(&c.GitPass, "git-pass", "", "Git user password") - fs.StringVar(&c.GPGKey, "signing-key", "", "Path to the GPG signing key") +func (c *Config) Validate() error { + var err error - return ff.Parse(fs, os.Args[1:], ff.WithEnvVarPrefix(envVarPrefix)) + if c.GrafanaURL == "" { + err = errors.Join(err, errors.New("invalid grafana URL, must not be blank")) + } + + if c.GrafanaToken == "" { + err = errors.Join(err, errors.New("invalid auth token for grafana, must not be blank")) + } + + if c.GitRepo == "" { + err = errors.Join(err, errors.New("invalid repo url for git, must not be blank")) + } + + if c.GitUser == "" { + err = errors.Join(err, errors.New("invalid username for git, must not be blank")) + } + + if c.GitPass == "" { + err = errors.Join(err, errors.New("invalid password for git, must not be blank")) + } + + if c.GitBranch == "" { + err = errors.Join(err, errors.New("invalid branch name for git, must not be blank")) + } + + return err } diff --git a/internal/version/version.go b/internal/version/version.go new file mode 100644 index 0000000..bd65be1 --- /dev/null +++ b/internal/version/version.go @@ -0,0 +1,17 @@ +package version + +import ( + "fmt" +) + +var ( + version = "undefined" + versionPrerelease = "dev" //nolint:gochecknoglobals // this has to be a variable to set the version during release. +) + +func Version() string { + if versionPrerelease != "" { + return fmt.Sprintf("%s-%s", version, versionPrerelease) + } + return version +} diff --git a/main.go b/main.go index fd8c1bf..bd7579b 100644 --- a/main.go +++ b/main.go @@ -1,86 +1,19 @@ package main import ( - "context" - "fmt" - "log" - "slices" + "os" - "git.ar21.de/yolokube/grafana-backuper/cfg" - "git.ar21.de/yolokube/grafana-backuper/pkg/git" - "git.ar21.de/yolokube/grafana-backuper/pkg/grafana" + "git.ar21.de/yolokube/grafana-backuper/internal/cmd" + "git.ar21.de/yolokube/grafana-backuper/internal/config" ) func main() { - appSettings := cfg.Parse() + cfg := &config.Config{} - ctx := context.Background() - client, err := grafana.NewClient(appSettings.GrafanaURL, appSettings.GrafanaToken, grafana.DefaultHTTPClient) - if err != nil { - log.Fatalf("Error creating the grafana client: %v", err) - } + rootCmd := cmd.NewRootCommand(cfg) + rootCmd.AddCommand(cmd.NewBackupCommand(cfg)) - gitdata := git.NewPayload(appSettings.GPGKey) - if err = gitdata.GetRepo(appSettings.GitRepoURL, appSettings.GitUser, appSettings.GitPass); err != nil { - log.Fatalf("Error cloning git repo: %v", err) - } - - gitdata.AddCommitter(appSettings.GitUser, appSettings.GitEmail) - - dashboards, err := client.SearchDashboards(ctx, "", false) - if err != nil { - log.Fatalf("Error fetching dashboards: %v", err) - } - - for _, dashboard := range dashboards { - gitdata.UpdateDashboard(dashboard) - - _, dashboardInfo, err := client.GetRawDashboardByUID(ctx, dashboard.UID) - if err != nil { - log.Fatalf("Error fetching information of dashboard %s: %v", dashboard.Title, err) - } - - gitdata.UpdateDashboardInfo(dashboardInfo) - - versions, err := client.GetDashboardVersionsByDashboardUID(ctx, dashboard.UID) - if err != nil { - log.Fatalf("Error fetching versions for dashboard %s: %v", dashboard.Title, err) - } - - slices.Reverse(versions) - - for _, version := range versions { - gitdata.UpdateVersion(version) - - if !appSettings.Force && gitdata.IsVersionCommitted(appSettings.GitBranch) { - fmt.Printf("%s/%s - %s: %s -> already committed\n", dashboardInfo.FolderTitle, dashboard.Title, version.CreatedBy, version.Message) - continue - } - - fmt.Printf("%s/%s - %s: %s\n", dashboardInfo.FolderTitle, dashboard.Title, version.CreatedBy, version.Message) - - raw, info, err := client.GetRawDashboardByUIDAndVersion(ctx, dashboard.UID, version.Version) - if err != nil { - log.Fatalf("Error fetching dashboard %s version %d: %v", dashboard.Title, version.Version, err) - } - - gitdata.AddAuthor(info.CreatedBy, "") - - output, err := grafana.ConvertRawToIndent(raw) - if err != nil { - log.Fatalf("Error pritty-printing dashboard %s version %d: %v", dashboard.Title, info.Version, err) - } - - gitdata.UpdateContent([]byte(output)) - gitdata.UpdateCommitter() - - if err = gitdata.CreateCommit(); err != nil { - log.Fatalf("Error creating commit for dashboard %s version %d: %v", dashboard.Title, info.Version, err) - } - - if err = gitdata.PushToRemote(appSettings.GitUser, appSettings.GitPass); err != nil { - log.Fatalf("Error pushing to remote repo %s: %v", appSettings.GitRepoURL, err) - } - } + if err := rootCmd.Execute(); err != nil { + os.Exit(1) } }