Skip to content

Commit

Permalink
add example in readme file, and improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
rajatjindal committed Jan 30, 2020
1 parent b6af04e commit 9828fe4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ jobs:

- [bash](https://github.com/ahmetb/kubectx/blob/master/.github/workflows/release.yml)

# Testing the template file

You can test the template file rendering before check-in to the repo by running following command
```bash
$ docker run -v /path/to/your/template-file.yaml:/tmp/template-file.yaml rajatjindal/krew-release-bot:v0.0.32 \
krew-release-bot template --tag <tag-name> --template-file /tmp/template-file.yaml
```

# Limitations of krew-release-bot
- only works for repos hosted on github right now
- only supports one plugin per git repo right now
Expand Down
17 changes: 12 additions & 5 deletions cmd/action/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"os"

"github.com/rajatjindal/krew-release-bot/pkg/source"
"github.com/sirupsen/logrus"
Expand All @@ -17,8 +18,8 @@ var (
func init() {
rootCmd.AddCommand(templateCmd)

templateCmd.Flags().StringVar(&tagName, "tag-name", "", "tag name to use for templating")
templateCmd.MarkFlagRequired("tag-name")
templateCmd.Flags().StringVar(&tagName, "tag", "", "tag name to use for templating")
templateCmd.MarkFlagRequired("tag")

templateCmd.Flags().StringVar(&templateFile, "template-file", ".krew.yaml", "template file to use for templating")
templateCmd.MarkFlagRequired("template-file")
Expand All @@ -39,10 +40,16 @@ var templateCmd = &cobra.Command{
}

_, spec, err := source.ProcessTemplate(templateFile, releaseRequest)
if err != nil {
logrus.Fatal(err)
if err == nil {
fmt.Println(string(spec))
os.Exit(0)
}

fmt.Println(string(spec))
if invalidSpecError, ok := err.(source.InvalidPluginSpecError); ok {
fmt.Println(invalidSpecError.Spec)
logrus.Fatal(invalidSpecError.Error())
}

logrus.Fatal(err)
},
}
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
k8s.io/apimachinery v0.0.0-20190717022731-0bb8574e0887/go.mod h1:sBJWIJZfxLhp7mRsRyuAE/NfKTr3kXGR1iaqg8O0gJo=
k8s.io/apimachinery v0.17.0 h1:xRBnuie9rXcPxUkDizUsGvPf1cnlZCFu210op7J7LJo=
k8s.io/apimachinery v0.17.0/go.mod h1:b9qmWdKlLuU9EBh+06BtLcSf/Mu89rWL33naRxs1uZg=
k8s.io/apimachinery v0.17.2 h1:hwDQQFbdRlpnnsR64Asdi55GyCaIP/3WQpMmbNBeWr4=
k8s.io/client-go v7.0.0+incompatible/go.mod h1:7vJpHMYJwNQCWgzmNV+VYUl1zCObLyodBc8nIyt8L5s=
k8s.io/gengo v0.0.0-20190128074634-0689ccc1d7d6/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=
k8s.io/klog v0.0.0-20181102134211-b9b56d5dfc92/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk=
Expand Down
15 changes: 14 additions & 1 deletion pkg/source/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ import (
"github.com/sirupsen/logrus"
)

//InvalidPluginSpecError is invalid plugin spec error
type InvalidPluginSpecError struct {
Spec string
err string
}

func (i InvalidPluginSpecError) Error() string {
return i.err
}

//ProcessTemplate process the .krew.yaml template for the release request
func ProcessTemplate(templateFile string, values interface{}) (string, []byte, error) {
spec, err := RenderTemplate(templateFile, values)
Expand All @@ -19,7 +29,10 @@ func ProcessTemplate(templateFile string, values interface{}) (string, []byte, e

pluginName, err := krew.GetPluginName(spec)
if err != nil {
return "", nil, fmt.Errorf("failed to get plugin name from processed template.\nerr: %s\n\n\n%s", err.Error(), string(spec))
return "", nil, InvalidPluginSpecError{
err: fmt.Sprintf("failed to get plugin name from processed template.\nerr: %s", err.Error()),
Spec: string(spec),
}
}

return pluginName, spec, nil
Expand Down

0 comments on commit 9828fe4

Please sign in to comment.