Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(DEV-2781): Notify Not Running in a Git Repo #990

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions cmd/cmd_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/cloudposse/atmos/pkg/ui/theme"
u "github.com/cloudposse/atmos/pkg/utils"
"github.com/cloudposse/atmos/pkg/version"
"github.com/go-git/go-git/v5"
)

// ValidateConfig holds configuration options for Atmos validation.
Expand Down Expand Up @@ -458,6 +459,9 @@ func printMessageForMissingAtmosConfig(atmosConfig schema.AtmosConfiguration) {
u.LogErrorAndExit(err)
}

// Check if we're in a git repo. Warn if not.
verifyInsideGitRepo()

if atmosConfig.Default {
// If Atmos did not find an `atmos.yaml` config file and is using the default config
u.PrintMessageInColor("atmos.yaml", c1)
Expand Down Expand Up @@ -605,3 +609,33 @@ func getConfigAndStacksInfo(commandName string, cmd *cobra.Command, args []strin
}
return info
}

// isGitRepository checks if the current directory is within a git repository
func isGitRepository() bool {
_, err := git.PlainOpenWithOptions(".", &git.PlainOpenOptions{
DetectDotGit: true,
})
if err != nil {
if err != git.ErrRepositoryNotExists {
u.LogTrace(fmt.Sprintf("git check failed: %v", err))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Semantic logging

}
return false
}

return true
}

// verifyInsideGitRepo checks if we're in a git repo
func verifyInsideGitRepo() bool {
// Skip check if either env var is set
if os.Getenv("ATMOS_BASE_PATH") != "" || os.Getenv("ATMOS_CLI_CONFIG_PATH") != "" {
return true
}

// Check if we're in a git repo
if !isGitRepository() {
u.LogWarning("You're not inside a git repository. Atmos feels lonely outside - bring it home!\n")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move to the semantic logging.

return false
}
return true
}
18 changes: 18 additions & 0 deletions tests/test-cases/empty-dir.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,21 @@ tests:
stderr:
- "^$"
exit_code: 0

- name: atmos warns if not in git repo
enabled: true
snapshot: true
description: "Test that atmos warns if not run inside of a git repo"
workdir: "fixtures/scenarios/empty-dir"
command: "atmos"
args:
- version
env:
ATMOS_LOGS_LEVEL: Warning
expect:
diff: []
stdout:
- "You're not inside a git repository. Atmos feels lonely outside - bring it home!"
stderr:
- "^$"
exit_code: 0
2 changes: 1 addition & 1 deletion tests/test-cases/log-level-validation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,4 @@ tests:
- '^\n👽 Atmos (\d+\.\d+\.\d+|test) on [a-z]+/[a-z0-9_]+\n\n'
stderr:
- "^$"
exit_code: 0
exit_code: 0
Loading