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

Commit

Permalink
move options to cmd package
Browse files Browse the repository at this point in the history
  • Loading branch information
yp05327 committed Feb 9, 2024
1 parent ed38686 commit 808fa9c
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 90 deletions.
18 changes: 11 additions & 7 deletions cmd/teachart/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,30 @@ import (
)

type repoOptions struct {
repoName string
*options.GlobalOptions

manager *repo.Manager
}

func NewRepoCmd(ctx context.Context, globalOptions *options.GlobalOptions) *cobra.Command {
repoOptions := options.NewRepoOptions(globalOptions)
opts := &repoOptions{
GlobalOptions: globalOptions,
}

cmd := &cobra.Command{
Use: "repo add|remove|list",
Short: "add, remove, list chart repos.",
Args: NoArgs,
PersistentPreRunE: func(c *cobra.Command, args []string) error {
repoOptions.Manager = repo.NewManager(filepath.Join(globalOptions.GetInstallDir(), app.DefaultRepoDir), app.DefaultRemoteName)
return repoOptions.Manager.Init()
opts.manager = repo.NewManager(filepath.Join(globalOptions.GetInstallDir(), app.DefaultRepoDir), app.DefaultRemoteName)
return opts.manager.Init()
},
}

cmd.AddCommand(
NewRepoAddCmd(ctx, repoOptions),
NewRepoRemoveCmd(ctx, repoOptions),
NewRepoListCmd(ctx, repoOptions),
NewRepoAddCmd(ctx, opts),
NewRepoRemoveCmd(ctx, opts),
NewRepoListCmd(ctx, opts),
)

return cmd
Expand Down
26 changes: 18 additions & 8 deletions cmd/teachart/repo_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,39 @@ import (
"github.com/yp05327/teachart/pkg/repo"
)

func NewRepoAddCmd(ctx context.Context, repoOptions *options.RepoOptions) *cobra.Command {
repoAddOptions := options.NewRepoAddOptions(repoOptions)
type repoAddOptions struct {
*repoOptions
*options.ChartOptions

force bool
}

func NewRepoAddCmd(ctx context.Context, repoOpts *repoOptions) *cobra.Command {
opts := &repoAddOptions{
repoOptions: repoOpts,
ChartOptions: options.NewChartOptions(repoOpts.GetRepoRootDir()),
}

cmd := &cobra.Command{
Use: "add NAME URL",
Short: "Add a chart repo.",
Args: ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
repoAddOptions.Name = args[0]
repoAddOptions.URL = args[1]
opts.Name = args[0]
opts.URL = args[1]

return runRepoAdd(ctx, repoOptions.Manager, repoAddOptions)
return runRepoAdd(ctx, opts.manager, opts)
},
}

flags := cmd.Flags()
flags.BoolVarP(&repoAddOptions.Force, "force", "f", false, "overwrite the repo if it already exists.")
flags.BoolVarP(&opts.force, "force", "f", false, "overwrite the repo if it already exists.")

return cmd
}

func runRepoAdd(ctx context.Context, manager *repo.Manager, opts *options.RepoAddOptions) error {
err := manager.Add(ctx, opts.Name, opts.URL, opts.Force)
func runRepoAdd(ctx context.Context, manager *repo.Manager, opts *repoAddOptions) error {
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)
return nil
Expand Down
18 changes: 11 additions & 7 deletions cmd/teachart/repo_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,30 @@ import (
"github.com/gosuri/uitable"
"github.com/spf13/cobra"
"github.com/yp05327/teachart/pkg/app"
"github.com/yp05327/teachart/pkg/options"
"github.com/yp05327/teachart/pkg/repo"
)

func NewRepoListCmd(ctx context.Context, repoOptions *options.RepoOptions) *cobra.Command {
repoListOptions := options.NewRepoListOptions(repoOptions)
type repoListOptions struct {
*repoOptions
}

func NewRepoListCmd(ctx context.Context, repoOpts *repoOptions) *cobra.Command {
opts := &repoListOptions{
repoOptions: repoOpts,
}

cmd := &cobra.Command{
Use: "list",
Short: "List all chart repos.",
RunE: func(cmd *cobra.Command, args []string) error {
return runRepoList(ctx, repoOptions.Manager, repoListOptions)
return runRepoList(ctx, opts)
},
}

return cmd
}

func runRepoList(ctx context.Context, manager *repo.Manager, opts *options.RepoListOptions) error {
reposMap, err := manager.List()
func runRepoList(ctx context.Context, opts *repoListOptions) error {
reposMap, err := opts.manager.List()
if err != nil {
return err
}
Expand Down
21 changes: 14 additions & 7 deletions cmd/teachart/repo_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,34 @@ import (
"context"

"github.com/spf13/cobra"
"github.com/yp05327/teachart/pkg/options"
"github.com/yp05327/teachart/pkg/repo"
)

func NewRepoRemoveCmd(ctx context.Context, repoOptions *options.RepoOptions) *cobra.Command {
repoRemoveOptions := options.NewRepoRemoveOptions(repoOptions)
type repoRemoveOptions struct {
*repoOptions

name string
}

func NewRepoRemoveCmd(ctx context.Context, repoOpts *repoOptions) *cobra.Command {
opts := &repoRemoveOptions{
repoOptions: repoOpts,
}

cmd := &cobra.Command{
Use: "remove NAME",
Short: "Remove a chart repo.",
Args: ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
repoRemoveOptions.Name = args[0]
opts.name = args[0]

return runRepoRemove(ctx, repoOptions.Manager, repoRemoveOptions)
return runRepoRemove(ctx, opts.manager, opts)
},
}

return cmd
}

func runRepoRemove(ctx context.Context, manager *repo.Manager, opts *options.RepoRemoveOptions) error {
return manager.Remove(opts.Name)
func runRepoRemove(ctx context.Context, manager *repo.Manager, opts *repoRemoveOptions) error {
return manager.Remove(opts.name)
}
16 changes: 0 additions & 16 deletions pkg/options/repo.go

This file was deleted.

18 changes: 0 additions & 18 deletions pkg/options/repo_add.go

This file was deleted.

13 changes: 0 additions & 13 deletions pkg/options/repo_list.go

This file was deleted.

14 changes: 0 additions & 14 deletions pkg/options/repo_remove.go

This file was deleted.

0 comments on commit 808fa9c

Please sign in to comment.