diff --git a/cmd/teachart/repo.go b/cmd/teachart/repo.go index 1134680..8c02a02 100644 --- a/cmd/teachart/repo.go +++ b/cmd/teachart/repo.go @@ -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 diff --git a/cmd/teachart/repo_add.go b/cmd/teachart/repo_add.go index 56802b0..6da8b04 100644 --- a/cmd/teachart/repo_add.go +++ b/cmd/teachart/repo_add.go @@ -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 diff --git a/cmd/teachart/repo_list.go b/cmd/teachart/repo_list.go index a9d6459..227b986 100644 --- a/cmd/teachart/repo_list.go +++ b/cmd/teachart/repo_list.go @@ -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 } diff --git a/cmd/teachart/repo_remove.go b/cmd/teachart/repo_remove.go index cbb8add..a50085f 100644 --- a/cmd/teachart/repo_remove.go +++ b/cmd/teachart/repo_remove.go @@ -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) } diff --git a/pkg/options/repo.go b/pkg/options/repo.go deleted file mode 100644 index 6c93514..0000000 --- a/pkg/options/repo.go +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright © 2024 TeaChart Authors - -package options - -import "github.com/yp05327/teachart/pkg/repo" - -type RepoOptions struct { - *GlobalOptions - Manager *repo.Manager -} - -func NewRepoOptions(globalOptions *GlobalOptions) *RepoOptions { - return &RepoOptions{ - GlobalOptions: globalOptions, - } -} diff --git a/pkg/options/repo_add.go b/pkg/options/repo_add.go deleted file mode 100644 index 77faedd..0000000 --- a/pkg/options/repo_add.go +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright © 2024 TeaChart Authors - -package options - -type RepoAddOptions struct { - *RepoOptions - *ChartOptions - - Name string - Force bool -} - -func NewRepoAddOptions(repoOptions *RepoOptions) *RepoAddOptions { - return &RepoAddOptions{ - RepoOptions: repoOptions, - ChartOptions: NewChartOptions(repoOptions.GetRepoRootDir()), - } -} diff --git a/pkg/options/repo_list.go b/pkg/options/repo_list.go deleted file mode 100644 index d43258d..0000000 --- a/pkg/options/repo_list.go +++ /dev/null @@ -1,13 +0,0 @@ -// Copyright © 2024 TeaChart Authors - -package options - -type RepoListOptions struct { - *RepoOptions -} - -func NewRepoListOptions(repoOptions *RepoOptions) *RepoListOptions { - return &RepoListOptions{ - RepoOptions: repoOptions, - } -} diff --git a/pkg/options/repo_remove.go b/pkg/options/repo_remove.go deleted file mode 100644 index 964ca7c..0000000 --- a/pkg/options/repo_remove.go +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright © 2024 TeaChart Authors - -package options - -type RepoRemoveOptions struct { - *RepoOptions - Name string -} - -func NewRepoRemoveOptions(repoOptions *RepoOptions) *RepoRemoveOptions { - return &RepoRemoveOptions{ - RepoOptions: repoOptions, - } -}