feat(cmd): add sequence flag to execute multiple functions
All checks were successful
ci/woodpecker/push/lint Pipeline was successful
ci/woodpecker/push/test Pipeline was successful
ci/woodpecker/push/build Pipeline was successful

This commit is contained in:
Tom Neuber 2024-12-07 00:52:25 +01:00
parent 30ee41171a
commit 579f2a5df7
Signed by: tom
GPG key ID: F17EFE4272D89FF6
5 changed files with 37 additions and 4 deletions

View file

@ -20,6 +20,14 @@ FROM alpine
WORKDIR /app WORKDIR /app
# Copy built binary from build image # 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"]

8
entrypoint.sh Normal file
View file

@ -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

View file

@ -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().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 return backupCmd
} }

View file

@ -23,6 +23,21 @@ func NewRootCommand(c *config.Config) *cobra.Command {
Short: "Grafana Backuper CLI", Short: "Grafana Backuper CLI",
Long: "A command-line tool to back up and restore Grafana dashboards", Long: "A command-line tool to back up and restore Grafana dashboards",
Version: version.Version(), 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) { PersistentPreRun: func(cmd *cobra.Command, _ []string) {
initializeConfig(cmd) 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.GitRepo, "git-repo", "", "Complete Git repository URL")
rootCmd.PersistentFlags().StringVar(&c.GitUser, "git-user", "", "Git user name") rootCmd.PersistentFlags().StringVar(&c.GitUser, "git-user", "", "Git user name")
rootCmd.PersistentFlags().StringVar(&c.GitPass, "git-pass", "", "Git user password") 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 return rootCmd
} }

View file

@ -29,6 +29,7 @@ type Config struct {
GitPass string GitPass string
GPGKey string GPGKey string
Quiet bool Quiet bool
Sequence []string
Output io.Writer Output io.Writer
Logger zerolog.Logger Logger zerolog.Logger