summaryrefslogtreecommitdiff
path: root/cmd/template.go
blob: 8d8d8d3dbea6ad256da718d2d6dca3ea0477a141 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main

import (
	"fmt"
	"os"
	"text/template"
	"time"

	"github.com/argoproj-labs/argocd-image-updater/pkg/argocd"
	"github.com/argoproj-labs/argocd-image-updater/pkg/common"
	"github.com/argoproj-labs/argocd-image-updater/registry-scanner/pkg/image"
	"github.com/argoproj-labs/argocd-image-updater/registry-scanner/pkg/tag"

	"github.com/spf13/cobra"
)

func newTemplateCommand() *cobra.Command {
	var (
		commitMessageTemplatePath string
		tplStr                    string
	)
	var runCmd = &cobra.Command{
		Use:   "template [<PATH>]",
		Short: "Test & render a commit message template",
		Long: `
The template command lets you validate your commit message template. It will
parse the template at given PATH and execute it with a defined set of changes
so that you can see how it looks like when being templated by Image Updater.

If PATH is not given, will show you the default message that is used.
`,
		Run: func(cmd *cobra.Command, args []string) {
			var tpl *template.Template
			var err error
			if len(args) != 1 {
				tplStr = common.DefaultGitCommitMessage
			} else {
				commitMessageTemplatePath = args[0]
				tplData, err := os.ReadFile(commitMessageTemplatePath)
				if err != nil {
					fmt.Fprintf(cmd.ErrOrStderr(), "%v", err)
					return
				}
				tplStr = string(tplData)
			}
			if tpl, err = template.New("commitMessage").Parse(tplStr); err != nil {
				fmt.Fprintf(cmd.ErrOrStderr(), "could not parse commit message template: %v", err)
				return
			}
			chL := []argocd.ChangeEntry{
				{
					Image:  image.NewFromIdentifier("gcr.io/example/example:1.0.0"),
					OldTag: tag.NewImageTag("1.0.0", time.Now(), ""),
					NewTag: tag.NewImageTag("1.0.1", time.Now(), ""),
				},
				{
					Image:  image.NewFromIdentifier("gcr.io/example/updater@sha256:f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2"),
					OldTag: tag.NewImageTag("", time.Now(), "sha256:01d09d19c2139a46aebfb577780d123d7396e97201bc7ead210a2ebff8239dee"),
					NewTag: tag.NewImageTag("", time.Now(), "sha256:7aa7a5359173d05b63cfd682e3c38487f3cb4f7f1d60659fe59fab1505977d4c"),
				},
			}
			fmt.Fprintf(cmd.OutOrStdout(), "%s\n", argocd.TemplateCommitMessage(tpl, "example-app", chL))
		},
	}
	return runCmd
}