Skip to content

Commit

Permalink
fix a lot of stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
roi-codefresh committed Jun 8, 2021
1 parent 076d214 commit cd2306a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 19 deletions.
7 changes: 4 additions & 3 deletions cmd/commands/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -93,6 +90,7 @@ func NewAppCreateCommand(cloneOpts *git.CloneOptions) *cobra.Command {
<BIN> app create <new_app_name> --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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -230,6 +229,7 @@ func NewAppListCommand(cloneOpts *git.CloneOptions) *cobra.Command {
<BIN> app list <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 a project name")
Expand Down Expand Up @@ -313,6 +313,7 @@ func NewAppDeleteCommand(cloneOpts *git.CloneOptions) *cobra.Command {
<BIN> app delete <app_name> --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")
Expand Down
6 changes: 3 additions & 3 deletions cmd/commands/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -107,6 +104,7 @@ func NewProjectCreateCommand(cloneOpts *git.CloneOptions) *cobra.Command {
<BIN> project create <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 project name")
Expand Down Expand Up @@ -341,6 +339,7 @@ func NewProjectListCommand(cloneOpts *git.CloneOptions) *cobra.Command {
<BIN> 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,
Expand Down Expand Up @@ -408,6 +407,7 @@ func NewProjectDeleteCommand(cloneOpts *git.CloneOptions) *cobra.Command {
<BIN> project delete <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 project name")
Expand Down
7 changes: 3 additions & 4 deletions cmd/commands/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,8 @@ func NewRepoBootstrapCommand() *cobra.Command {
<BIN> 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,
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 17 additions & 8 deletions pkg/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}

Expand All @@ -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
}
Expand Down
8 changes: 7 additions & 1 deletion pkg/git/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"io"
"os"
"strings"

Expand Down Expand Up @@ -39,6 +40,7 @@ type (
Repo string
Auth Auth
FS billy.Filesystem
Progress io.Writer
url string
revision string
path string
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
}

Expand Down

0 comments on commit cd2306a

Please sign in to comment.