From cd2306ac46253f8000b0f947c2e0894cfd7562f7 Mon Sep 17 00:00:00 2001 From: "roi.kramer" Date: Tue, 8 Jun 2021 11:38:09 +0300 Subject: [PATCH] fix a lot of stuff --- cmd/commands/app.go | 7 ++++--- cmd/commands/project.go | 6 +++--- cmd/commands/repo.go | 7 +++---- pkg/application/application.go | 25 +++++++++++++++++-------- pkg/git/repository.go | 8 +++++++- 5 files changed, 34 insertions(+), 19 deletions(-) diff --git a/cmd/commands/app.go b/cmd/commands/app.go index 074dc0d3..68ab9ad7 100644 --- a/cmd/commands/app.go +++ b/cmd/commands/app.go @@ -49,9 +49,6 @@ func NewAppCommand() *cobra.Command { Use: "application", Aliases: []string{"app"}, Short: "Manage applications", - PersistentPreRun: func(_ *cobra.Command, _ []string) { - cloneOpts.Parse() - }, Run: func(cmd *cobra.Command, args []string) { cmd.HelpFunc()(cmd, args) exit(1) @@ -93,6 +90,7 @@ func NewAppCreateCommand(cloneOpts *git.CloneOptions) *cobra.Command { app create --app github.com/some_org/some_repo/manifests?ref=v1.2.3 --project project_name `), + PreRun: func(cmd *cobra.Command, args []string) { cloneOpts.Parse() }, RunE: func(cmd *cobra.Command, args []string) error { if len(args) < 1 { log.G().Fatal("must enter application name") @@ -180,6 +178,7 @@ func setAppOptsDefaults(ctx context.Context, repofs fs.FS, opts *AppCreateOption Auth: opts.CloneOpts.Auth, FS: memfs.New(), } + cloneOpts.Parse() _, fsys, err = clone(ctx, cloneOpts) if err != nil { return err @@ -230,6 +229,7 @@ func NewAppListCommand(cloneOpts *git.CloneOptions) *cobra.Command { app list `), + PreRun: func(cmd *cobra.Command, args []string) { cloneOpts.Parse() }, RunE: func(cmd *cobra.Command, args []string) error { if len(args) < 1 { log.G().Fatal("must enter a project name") @@ -313,6 +313,7 @@ func NewAppDeleteCommand(cloneOpts *git.CloneOptions) *cobra.Command { app delete --project `), + PreRun: func(cmd *cobra.Command, args []string) { cloneOpts.Parse() }, RunE: func(cmd *cobra.Command, args []string) error { if len(args) < 1 { log.G().Fatal("must enter application name") diff --git a/cmd/commands/project.go b/cmd/commands/project.go index 2aa28b54..2151aa4b 100644 --- a/cmd/commands/project.go +++ b/cmd/commands/project.go @@ -65,9 +65,6 @@ func NewProjectCommand() *cobra.Command { Use: "project", Aliases: []string{"proj"}, Short: "Manage projects", - PersistentPreRun: func(_ *cobra.Command, _ []string) { - cloneOpts.Parse() - }, Run: func(cmd *cobra.Command, args []string) { cmd.HelpFunc()(cmd, args) exit(1) @@ -107,6 +104,7 @@ func NewProjectCreateCommand(cloneOpts *git.CloneOptions) *cobra.Command { project create `), + PreRun: func(cmd *cobra.Command, args []string) { cloneOpts.Parse() }, RunE: func(cmd *cobra.Command, args []string) error { if len(args) < 1 { log.G().Fatal("must enter project name") @@ -341,6 +339,7 @@ func NewProjectListCommand(cloneOpts *git.CloneOptions) *cobra.Command { project list `), + PreRun: func(cmd *cobra.Command, args []string) { cloneOpts.Parse() }, RunE: func(cmd *cobra.Command, args []string) error { return RunProjectList(cmd.Context(), &ProjectListOptions{ CloneOpts: cloneOpts, @@ -408,6 +407,7 @@ func NewProjectDeleteCommand(cloneOpts *git.CloneOptions) *cobra.Command { project delete `), + PreRun: func(cmd *cobra.Command, args []string) { cloneOpts.Parse() }, RunE: func(cmd *cobra.Command, args []string) error { if len(args) < 1 { log.G().Fatal("must enter project name") diff --git a/cmd/commands/repo.go b/cmd/commands/repo.go index 168dbb20..977b83cb 100644 --- a/cmd/commands/repo.go +++ b/cmd/commands/repo.go @@ -165,9 +165,8 @@ func NewRepoBootstrapCommand() *cobra.Command { repo bootstrap --repo https://github.com/example/repo/path/to/installation_root `), - PersistentPreRun: func(_ *cobra.Command, _ []string) { - cloneOpts.Parse() - }, RunE: func(cmd *cobra.Command, args []string) error { + PreRun: func(cmd *cobra.Command, args []string) { cloneOpts.Parse() }, + RunE: func(cmd *cobra.Command, args []string) error { return RunRepoBootstrap(cmd.Context(), &RepoBootstrapOptions{ AppSpecifier: appSpecifier, InstallationMode: installationMode, @@ -306,7 +305,7 @@ func RunRepoBootstrap(ctx context.Context, opts *RepoBootstrapOptions) error { return nil } - log.G().Infof("cloning repo: %s", opts.CloneOptions.URL) + log.G().Infof("cloning repo: %s", opts.CloneOptions.URL()) // clone GitOps repo r, repofs, err := clone(ctx, opts.CloneOptions) diff --git a/pkg/application/application.go b/pkg/application/application.go index c73f1317..55de787c 100644 --- a/pkg/application/application.go +++ b/pkg/application/application.go @@ -454,20 +454,24 @@ func checkBaseCollision(repofs fs.FS, orgBasePath string, newBase *kusttypes.Kus } // fixResourcesPaths adjusts all relative paths in the kustomization file to the specified -// `kustomizationPath`. -func fixResourcesPaths(k *kusttypes.Kustomization, kustomizationPath string) error { +// newKustDir. +func fixResourcesPaths(k *kusttypes.Kustomization, newKustDir string) error { for i, path := range k.Resources { - // if path is a local file + // if path is a remote resource ignore it if _, err := os.Stat(path); err != nil && os.IsNotExist(err) { continue } - ap, err := filepath.Abs(path) + absRes, err := filepath.Abs(path) if err != nil { return err } - k.Resources[i], err = filepath.Rel(kustomizationPath, ap) + k.Resources[i], err = filepath.Rel(newKustDir, absRes) + log.G().WithFields(log.Fields{ + "from": absRes, + "to": k.Resources[i], + }).Debug("adjusting kustomization paths to local filesystem") if err != nil { return err } @@ -477,14 +481,18 @@ func fixResourcesPaths(k *kusttypes.Kustomization, kustomizationPath string) err } var generateManifests = func(k *kusttypes.Kustomization) ([]byte, error) { - td, err := ioutil.TempDir("", "auto-pilot") + td, err := ioutil.TempDir(".", "auto-pilot") if err != nil { return nil, err } defer os.RemoveAll(td) - kustomizationPath := filepath.Join(td, "kustomization.yaml") - if err = fixResourcesPaths(k, kustomizationPath); err != nil { + absTd, err := filepath.Abs(td) + if err != nil { + return nil, err + } + + if err = fixResourcesPaths(k, absTd); err != nil { return nil, err } @@ -493,6 +501,7 @@ var generateManifests = func(k *kusttypes.Kustomization) ([]byte, error) { return nil, err } + kustomizationPath := filepath.Join(td, "kustomization.yaml") if err = ioutil.WriteFile(kustomizationPath, kyaml, 0400); err != nil { return nil, err } diff --git a/pkg/git/repository.go b/pkg/git/repository.go index 702a6c27..5e6bbaf5 100644 --- a/pkg/git/repository.go +++ b/pkg/git/repository.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "io" "os" "strings" @@ -39,6 +40,7 @@ type ( Repo string Auth Auth FS billy.Filesystem + Progress io.Writer url string revision string path string @@ -126,6 +128,10 @@ func (o *CloneOptions) Clone(ctx context.Context) (Repository, fs.FS, error) { return nil, nil, ErrNoParse } + if o.Progress == nil { + o.Progress = os.Stderr + } + r, err := clone(ctx, o) if err != nil { if err == transport.ErrEmptyRemoteRepository { @@ -197,7 +203,7 @@ var clone = func(ctx context.Context, opts *CloneOptions) (*repo, error) { URL: opts.url, Auth: getAuth(opts.Auth), Depth: 1, - Progress: os.Stderr, + Progress: opts.Progress, Tags: gg.NoTags, }