From 579f2a5df76c92fffbe7c664e269ff8b18a29c0c Mon Sep 17 00:00:00 2001 From: Tom Neuber Date: Sat, 7 Dec 2024 00:52:25 +0100 Subject: [PATCH] feat(cmd): add `sequence` flag to execute multiple functions --- Dockerfile | 12 ++++++++++-- entrypoint.sh | 8 ++++++++ internal/cmd/backup.go | 2 -- internal/cmd/root.go | 18 ++++++++++++++++++ internal/config/config.go | 1 + 5 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 entrypoint.sh diff --git a/Dockerfile b/Dockerfile index d25a8ac..ee958ea 100644 --- a/Dockerfile +++ b/Dockerfile @@ -20,6 +20,14 @@ FROM alpine WORKDIR /app # Copy built binary from build image -COPY --from=build /workspace/grafana-backuper /app +COPY --from=build /workspace/grafana-backuper . -ENTRYPOINT ["/app/grafana-backuper backup --json"] +RUN chmod +x grafana-backuper + +# Copy the wrapper script +COPY entrypoint.sh . + +# Ensure the script is executable +RUN chmod +x entrypoint.sh + +ENTRYPOINT ["entrypoint.sh"] diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..b5a87a3 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +# Check if the environment variable GRAFANA_MODE is set +if [ -z "$GB_SEQUENCE" ]; then + exec ./grafana-backuper backup --json +else + exec ./grafana-backuper --json +fi diff --git a/internal/cmd/backup.go b/internal/cmd/backup.go index fc7629c..dac2628 100644 --- a/internal/cmd/backup.go +++ b/internal/cmd/backup.go @@ -37,8 +37,6 @@ func NewBackupCommand(c *config.Config) *cobra.Command { } 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 } diff --git a/internal/cmd/root.go b/internal/cmd/root.go index 896dc8e..4d5fb05 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -23,6 +23,21 @@ func NewRootCommand(c *config.Config) *cobra.Command { Short: "Grafana Backuper CLI", Long: "A command-line tool to back up and restore Grafana dashboards", Version: version.Version(), + RunE: func(cmd *cobra.Command, _ []string) error { + if len(c.Sequence) == 0 { + return cmd.Help() + } + + for _, command := range c.Sequence { + log.Info().Str("function", command).Msg("Executing function") + cmd.SetArgs([]string{command}) + if err := cmd.Execute(); err != nil { + return err + } + } + + return nil + }, PersistentPreRun: func(cmd *cobra.Command, _ []string) { initializeConfig(cmd) @@ -56,6 +71,9 @@ func NewRootCommand(c *config.Config) *cobra.Command { 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().StringVar(&c.GitEmail, "git-email", "", "Git email address") + rootCmd.PersistentFlags().StringVar(&c.GPGKey, "signing-key", "", "Path to the GPG signing key") + rootCmd.Flags().StringSliceVar(&c.Sequence, "sequence", nil, "Command sequence to execute multiple functions") return rootCmd } diff --git a/internal/config/config.go b/internal/config/config.go index 766f749..f95cb6a 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -29,6 +29,7 @@ type Config struct { GitPass string GPGKey string Quiet bool + Sequence []string Output io.Writer Logger zerolog.Logger