Skip to content

Commit

Permalink
Update atmos describe affected command (#348)
Browse files Browse the repository at this point in the history
* Updates

* Updates

* Updates
  • Loading branch information
aknysh authored Mar 21, 2023
1 parent 5c5f0f4 commit ad314ac
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ require (
github.com/json-iterator/go v1.1.12
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/mapstructure v1.5.0
github.com/open-policy-agent/opa v0.50.1
github.com/open-policy-agent/opa v0.50.2
github.com/otiai10/copy v1.9.0
github.com/pkg/errors v0.9.1
github.com/samber/lo v1.37.0
github.com/samber/lo v1.38.1
github.com/santhosh-tekuri/jsonschema/v5 v5.2.0
github.com/spf13/cobra v1.6.1
github.com/spf13/viper v1.15.0
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -522,8 +522,8 @@ github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjY
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/open-policy-agent/opa v0.50.1 h1:ZQOqmzTUjcdX7Bu6gnmWZ6ghFTAQI0rI1fR7AqaOW70=
github.com/open-policy-agent/opa v0.50.1/go.mod h1:9jKfDk0L5b9rnhH4M0nq10cGHbYOxqygxzTT3dsvhec=
github.com/open-policy-agent/opa v0.50.2 h1:iD2kKLFkflgSCTMtrC/3jLmOQ7IWyDXMg6+VQA0tSC0=
github.com/open-policy-agent/opa v0.50.2/go.mod h1:9jKfDk0L5b9rnhH4M0nq10cGHbYOxqygxzTT3dsvhec=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/image-spec v1.1.0-rc2 h1:2zx/Stx4Wc5pIPDvIxHXvXtQFW/7XWJGmnM7r3wg034=
Expand Down Expand Up @@ -578,8 +578,8 @@ github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6L
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/samber/lo v1.37.0 h1:XjVcB8g6tgUp8rsPsJ2CvhClfImrpL04YpQHXeHPhRw=
github.com/samber/lo v1.37.0/go.mod h1:9vaz2O4o8oOnK23pd2TrXufcbdbJIa3b6cstBWKpopA=
github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM=
github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
github.com/santhosh-tekuri/jsonschema/v5 v5.2.0 h1:WCcC4vZDS1tYNxjWlwRJZQy28r8CMoggKnxNzxsVDMQ=
github.com/santhosh-tekuri/jsonschema/v5 v5.2.0/go.mod h1:FKdcjfQW6rpZSnxxUvEA5H/cDPdvJ/SZJQLWWXWGrZ0=
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
Expand Down
48 changes: 41 additions & 7 deletions internal/exec/describe_affected_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path"
"path/filepath"
"reflect"
"strconv"
"strings"
Expand Down Expand Up @@ -48,6 +49,13 @@ func ExecuteDescribeAffectedWithTargetRepoClone(
return nil, errors.Wrapf(err, "%v", localRepoIsNotGitRepoError)
}

localRepoWorktree, err := localRepo.Worktree()
if err != nil {
return nil, errors.Wrapf(err, "%v", localRepoIsNotGitRepoError)
}

localRepoPath := localRepoWorktree.Filesystem.Root()

// Get the remotes of the local repo
keys := []string{}
for k := range localRepoConfig.Remotes {
Expand Down Expand Up @@ -168,7 +176,7 @@ func ExecuteDescribeAffectedWithTargetRepoClone(
u.PrintInfoVerbose(verbose, fmt.Sprintf("\nChecked out commit SHA '%s'\n", sha))
}

affected, err := executeDescribeAffected(cliConfig, tempDir, localRepo, remoteRepo, verbose)
affected, err := executeDescribeAffected(cliConfig, localRepoPath, tempDir, localRepo, remoteRepo, verbose)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -198,6 +206,13 @@ func ExecuteDescribeAffectedWithTargetRepoPath(
return nil, errors.Wrapf(err, "%v", localRepoIsNotGitRepoError)
}

localRepoWorktree, err := localRepo.Worktree()
if err != nil {
return nil, errors.Wrapf(err, "%v", localRepoIsNotGitRepoError)
}

localRepoPath := localRepoWorktree.Filesystem.Root()

remoteRepo, err := git.PlainOpenWithOptions(repoPath, &git.PlainOpenOptions{
DetectDotGit: false,
EnableDotGitCommonDir: false,
Expand All @@ -212,7 +227,7 @@ func ExecuteDescribeAffectedWithTargetRepoPath(
return nil, errors.Wrapf(err, "%v", remoteRepoIsNotGitRepoError)
}

affected, err := executeDescribeAffected(cliConfig, repoPath, localRepo, remoteRepo, verbose)
affected, err := executeDescribeAffected(cliConfig, localRepoPath, repoPath, localRepo, remoteRepo, verbose)
if err != nil {
return nil, err
}
Expand All @@ -222,6 +237,7 @@ func ExecuteDescribeAffectedWithTargetRepoPath(

func executeDescribeAffected(
cliConfig cfg.CliConfiguration,
localRepoFileSystemPath string,
remoteRepoFileSystemPath string,
localRepo *git.Repository,
remoteRepo *git.Repository,
Expand All @@ -248,13 +264,31 @@ func executeDescribeAffected(
return nil, err
}

// Update paths to point to the temp dir
cliConfig.StacksBaseAbsolutePath = path.Join(remoteRepoFileSystemPath, cliConfig.BasePath, cliConfig.Stacks.BasePath)
cliConfig.TerraformDirAbsolutePath = path.Join(remoteRepoFileSystemPath, cliConfig.BasePath, cliConfig.Components.Terraform.BasePath)
cliConfig.HelmfileDirAbsolutePath = path.Join(remoteRepoFileSystemPath, cliConfig.BasePath, cliConfig.Components.Helmfile.BasePath)
localRepoFileSystemPathAbs, err := filepath.Abs(localRepoFileSystemPath)
if err != nil {
return nil, err
}

basePath := cliConfig.BasePath

// Handle `atmos` absolute base path.
// Absolute base path can be set in the `base_path` attribute in `atmos.yaml`, or using the ENV var `ATMOS_BASE_PATH` (as it's done in `geodesic`)
// If the `atmos` base path is absolute, find the relative path between the local repo path and the `atmos` base path.
// This relative path (the difference) is then used below to join with the remote (cloned) repo path.
if path.IsAbs(basePath) {
basePath, err = filepath.Rel(localRepoFileSystemPathAbs, basePath)
if err != nil {
return nil, err
}
}

// Update paths to point to the cloned remote repo dir
cliConfig.StacksBaseAbsolutePath = path.Join(remoteRepoFileSystemPath, basePath, cliConfig.Stacks.BasePath)
cliConfig.TerraformDirAbsolutePath = path.Join(remoteRepoFileSystemPath, basePath, cliConfig.Components.Terraform.BasePath)
cliConfig.HelmfileDirAbsolutePath = path.Join(remoteRepoFileSystemPath, basePath, cliConfig.Components.Helmfile.BasePath)

cliConfig.StackConfigFilesAbsolutePaths, err = u.JoinAbsolutePathWithPaths(
path.Join(remoteRepoFileSystemPath, cliConfig.BasePath, cliConfig.Stacks.BasePath),
path.Join(remoteRepoFileSystemPath, basePath, cliConfig.Stacks.BasePath),
cliConfig.StackConfigFilesRelativePaths,
)
if err != nil {
Expand Down

0 comments on commit ad314ac

Please sign in to comment.