Skip to content
This repository has been archived by the owner on Dec 13, 2024. It is now read-only.

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
yp05327 committed Feb 13, 2024
1 parent 9de061b commit 70ef13e
Show file tree
Hide file tree
Showing 19 changed files with 143 additions and 36 deletions.
22 changes: 11 additions & 11 deletions .github/workflows/build-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
check-latest: true
- run: make deps
- run: make checks
- run: make lint
- run: make build
- run: make test
timeout-minutes: 30
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
check-latest: true
- run: make deps
- run: make checks
- run: make lint
- run: make build
- run: make test
timeout-minutes: 30
2 changes: 1 addition & 1 deletion cmd/teachart/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func NewInstallCmd(ctx context.Context, globalOptions *options.GlobalOptions) *c
if err != nil {
return errors.Wrap(err, "Create compose client error")
}
renderEngine, err := engine.NewRenderEngine(opts.GetChartDir(), opts.GetTeaChart())
renderEngine, err := engine.NewRenderEngine(opts.GetChartDir(), opts.GetTeaChart(), nil)
if err != nil {
return errors.Wrap(err, "Create helm engine error")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/teachart/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func runLint(ctx context.Context, opts lintOptions) error {
logrus.Debugf("Temp directory created:%s", tempDir)

// render templates
renderEngine, err := engine.NewRenderEngine(chartDir, opts.GetTeaChart())
renderEngine, err := engine.NewRenderEngine(chartDir, opts.GetTeaChart(), &engine.NewEngineOptions{Strict: true})
if err != nil {
return nil, errors.Wrap(err, "Create helm engine error")
}
Expand Down
6 changes: 4 additions & 2 deletions cmd/teachart/repo_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package cmd
import (
"context"
"errors"
"fmt"

"github.com/go-git/go-git/v5"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -34,7 +35,7 @@ func NewRepoAddCmd(ctx context.Context, repoOpts *repoOptions) *cobra.Command {
opts.Name = args[0]
opts.URL = args[1]

return runRepoAdd(ctx, opts.manager, opts)
return runRepoAdd(ctx, cmd, opts.manager, opts)
},
}

Expand All @@ -44,7 +45,8 @@ func NewRepoAddCmd(ctx context.Context, repoOpts *repoOptions) *cobra.Command {
return cmd
}

func runRepoAdd(ctx context.Context, manager *repo.Manager, opts *repoAddOptions) error {
func runRepoAdd(ctx context.Context, cmd *cobra.Command, manager *repo.Manager, opts *repoAddOptions) error {
fmt.Fprintf(cmd.OutOrStdout(), "adding repo `%s` from %s\n", opts.Name, opts.URL)
err := manager.Add(ctx, opts.Name, opts.URL, opts.force)
if errors.Is(err, git.ErrRepositoryAlreadyExists) {
logrus.Errorf("repo `%s` already exists. use --force/-f to overwrite the repo.", opts.Name)
Expand Down
8 changes: 4 additions & 4 deletions cmd/teachart/repo_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ func NewRepoListCmd(ctx context.Context, repoOpts *repoOptions) *cobra.Command {
Use: "list",
Short: "List all chart repos.",
RunE: func(cmd *cobra.Command, args []string) error {
return runRepoList(ctx, opts)
return runRepoList(ctx, cmd, opts)
},
}

return cmd
}

func runRepoList(ctx context.Context, opts *repoListOptions) error {
func runRepoList(ctx context.Context, cmd *cobra.Command, opts *repoListOptions) error {
reposMap, err := opts.manager.List()
if err != nil {
return err
}

if len(reposMap) == 0 {
fmt.Println("no repositories. use `repo add` to add repos")
fmt.Fprintln(cmd.OutOrStdout(), "no repositories. use `repo add` to add repos")
return nil
}

Expand All @@ -51,6 +51,6 @@ func runRepoList(ctx context.Context, opts *repoListOptions) error {
}
table.AddRow(name, remote.Config().URLs[0])
}
fmt.Println(table.String())
fmt.Fprintln(cmd.OutOrStdout(), table.String())
return nil
}
11 changes: 8 additions & 3 deletions cmd/teachart/repo_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package cmd

import (
"context"
"fmt"

"github.com/spf13/cobra"
"github.com/yp05327/teachart/pkg/repo"
Expand All @@ -27,13 +28,17 @@ func NewRepoRemoveCmd(ctx context.Context, repoOpts *repoOptions) *cobra.Command
RunE: func(cmd *cobra.Command, args []string) error {
opts.name = args[0]

return runRepoRemove(ctx, opts.manager, opts)
return runRepoRemove(ctx, cmd, opts.manager, opts)
},
}

return cmd
}

func runRepoRemove(ctx context.Context, manager *repo.Manager, opts *repoRemoveOptions) error {
return manager.Remove(opts.name)
func runRepoRemove(ctx context.Context, cmd *cobra.Command, manager *repo.Manager, opts *repoRemoveOptions) error {
err := manager.Remove(opts.name)
if err == nil {
fmt.Fprintf(cmd.OutOrStdout(), "repo %s removed\n", opts.name)
}
return err
}
2 changes: 1 addition & 1 deletion cmd/teachart/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func NewTemplateCmd(ctx context.Context, globalOptions *options.GlobalOptions) *
}

func runTemplate(ctx context.Context, opts templateOptions) error {
renderEngine, err := engine.NewRenderEngine(opts.GetChartDir(), opts.GetTeaChart())
renderEngine, err := engine.NewRenderEngine(opts.GetChartDir(), opts.GetTeaChart(), nil)
if err != nil {
return errors.Wrap(err, "Create helm engine error")
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import (
"helm.sh/helm/v3/pkg/cli/values"
)

type NewEngineOptions struct {
Strict bool
}
type RenderEngine interface {
Render(valueOpts values.Options, save bool) (map[string]string, error)
GetConfigPaths(files map[string]string) []string
}

func NewRenderEngine(chartDir string, teachart *TeaChart) (RenderEngine, error) {
return newHelm(chartDir, teachart)
func NewRenderEngine(chartDir string, teachart *TeaChart, opts *NewEngineOptions) (RenderEngine, error) {
return newHelm(chartDir, teachart, opts)
}
9 changes: 6 additions & 3 deletions pkg/engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
"helm.sh/helm/v3/pkg/cli/values"
)

const testChartDir = "../../tests/chart"
const testChartDir = "../../tests/data/chart"
const testValuesFile = "../../tests/data/values.yaml"

var testTeaChart = TeaChart{
ProjectName: "test",
Expand All @@ -17,9 +18,11 @@ var testTeaChart = TeaChart{
}

func TestHelmRender(t *testing.T) {
e, err := NewRenderEngine(testChartDir, &testTeaChart)
e, err := NewRenderEngine(testChartDir, &testTeaChart, &NewEngineOptions{Strict: true})
assert.NoError(t, err)

_, err = e.Render(values.Options{}, false)
_, err = e.Render(values.Options{
ValueFiles: []string{testValuesFile},
}, false)
assert.NoError(t, err)
}
9 changes: 7 additions & 2 deletions pkg/engine/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,20 @@ type Helm struct {
teachart *TeaChart
}

func newHelm(chartDir string, teachart *TeaChart) (*Helm, error) {
func newHelm(chartDir string, teachart *TeaChart, opts *NewEngineOptions) (*Helm, error) {
chart, err := loader.Load(chartDir)
if err != nil {
return nil, err
}
// TODO support Dependencies

if opts == nil {
opts = &NewEngineOptions{}
}
return &Helm{
engine: &helm_engine.Engine{},
engine: &helm_engine.Engine{
Strict: opts.Strict,
},
chart: chart,
teachart: teachart,
}, nil
Expand Down
4 changes: 1 addition & 3 deletions pkg/repo/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ func (m *Manager) getRepoPath(name string) string {
func (m *Manager) Remove(name string) error {
err := os.RemoveAll(m.getRepoPath(name))
if os.IsNotExist(err) {
logrus.Warnf("repo `%s` does not exist in %s", name, m.rootDir)
return nil
return fmt.Errorf("repo `%s` does not exist in %s", name, m.rootDir)
}
return err
}
Expand Down Expand Up @@ -91,7 +90,6 @@ func (m *Manager) Add(ctx context.Context, name, url string, force bool) error {
}

// load git repo
fmt.Printf("adding repo `%s` from %s\n", name, url)
if err := repoClient.Init(ctx, url); err != nil {
if !errors.Is(err, git.ErrRepositoryAlreadyExists) {
// need rollback when init failed
Expand Down
12 changes: 9 additions & 3 deletions pkg/repo/repo_checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package repo

import (
"context"
"errors"
"fmt"

"github.com/go-git/go-git/v5"
Expand Down Expand Up @@ -31,11 +32,16 @@ func (c *Client) Checkout(ctx context.Context, authMethod transport.AuthMethod,
gitOpts.Hash = plumbing.NewHash(opts.Commit)
} else {
tag, err := c.GetTagByVersion(opts.Version)
if err != nil {
if errors.Is(err, git.ErrTagNotFound) {
// try to get the main branch
fetchOpts.Ref = config.RefSpec("refs/heads/main:refs/heads/main")
gitOpts.Branch = "refs/heads/main"
} else if err != nil {
return err
} else {
fetchOpts.Ref = config.RefSpec(fmt.Sprintf("refs/tags/%s:refs/tags/%s", tag, tag))
gitOpts.Branch = plumbing.ReferenceName(tag)
}
fetchOpts.Ref = config.RefSpec(fmt.Sprintf("refs/tags/%s:refs/tags/%s", tag, tag))
gitOpts.Branch = plumbing.ReferenceName(tag)
}

if err := c.Fetch(ctx, authMethod, fetchOpts); err != nil {
Expand Down
Empty file removed tests/chart/values.yaml
Empty file.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@
{{ .Chart.Description }}
{{ .Chart.Version }}
{{ .Chart.AppVersion }}

# default values
{{ .Values.default.string }}

# user values from file
{{ .Values.file.string }}
2 changes: 2 additions & 0 deletions tests/data/chart/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
default:
string: test
2 changes: 2 additions & 0 deletions tests/data/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
file:
string: test
75 changes: 75 additions & 0 deletions tests/integration/integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package integration

import (
"bytes"
"context"
"strings"
"testing"

"github.com/stretchr/testify/assert"
cmd "github.com/yp05327/teachart/cmd/teachart"
"github.com/yp05327/teachart/pkg/app"
"github.com/yp05327/teachart/pkg/options"
)

func TestRepo(t *testing.T) {
ctx := context.Background()

var out bytes.Buffer
c := cmd.NewRepoCmd(ctx, options.NewGlobalOptions(app.DefaultRepoDir))
c.SetOut(&out)

tests := []struct {
args string
expected string
}{
{
// list without repos exist
args: "list",
expected: "no repositories. use `repo add` to add repos",
},
{
// add a repo
args: "add test https://github.com/TeaChart/gitea",
expected: "adding repo `test` from https://github.com/TeaChart/gitea",
},
{
// list repo
args: "list",
expected: "NAME\tURL \ntest\thttps://github.com/TeaChart/gitea",
}, {
// remove the repo
args: "remove test",
expected: "repo test removed",
},
}

for _, tt := range tests {
c.SetArgs(strings.Split(tt.args, " "))
err := c.Execute()
assert.NoError(t, err)
length := len(out.String())
assert.Less(t, 0, length)
assert.Equal(t, tt.expected, out.String()[:length-1])
out.Reset()
}
}

func TestInstallAndUninstall(t *testing.T) {
ctx := context.Background()

c, err := cmd.NewRootCmd(ctx, options.NewGlobalOptions(app.DefaultRepoDir))
assert.NoError(t, err)

tests := []string{
"repo add test https://github.com/TeaChart/gitea",
"install test",
"uninstall",
}

for _, tt := range tests {
c.SetArgs(strings.Split(tt, " "))
err := c.Execute()
assert.NoError(t, err)
}
}
Empty file removed tests/values.yaml
Empty file.

0 comments on commit 70ef13e

Please sign in to comment.