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

fix: decrease amount of git-related errors and warnings #208

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func newInitCmd() *cobra.Command {
log.Fatal().Err(err).Msg("Failed to read flag")
}

if err := myks.New(".").Bootstrap(force, onlyPrint, components); errors.As(err, &myks.ErrBootstrapTargetExists{}) {
if err := myks.NewWithDefaults().Bootstrap(force, onlyPrint, components); errors.As(err, &myks.ErrBootstrapTargetExists{}) {
log.Error().Err(err).Msg("The target already exists. Use --force to overwrite data.")
} else if err != nil {
log.Fatal().Err(err).Msg("Failed to initialize project")
Expand Down
8 changes: 0 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,6 @@ golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20231226003508-02704c960a9b h1:kLiC65FbiHWFAOu+lxwNPujcsl8VYyTYYEZnsOO1WK4=
golang.org/x/exp v0.0.0-20231226003508-02704c960a9b/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc h1:ao2WRsKSzW6KuUY9IWPwWahcHCgR0s52IfwutMfEbdM=
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand Down Expand Up @@ -376,8 +374,6 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down Expand Up @@ -405,15 +401,11 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220906165534-d0df966e6959/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.0.0-20220411215600-e5f449aeb171/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4=
golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
golang.org/x/term v0.16.0 h1:m+B6fahuftsE9qjo0VWp2FW0mB3MTJvR0BaMQrq0pmE=
golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
47 changes: 47 additions & 0 deletions internal/myks/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,50 @@ func convertToChangedFiles(changes string) ChangedFiles {
}
return cfs
}

func runGitCmd(args []string, root string, silent bool) (string, error) {
logFn := func(name string, err error, stderr string, args []string) {
cmd := msgRunCmd("run git command", name, args)
if err == nil {
log.Debug().Msg(cmd)
return
}
if silent {
log.Debug().Msg(cmd)
log.Debug().Msg(stderr)
return
}
log.Error().Msg(cmd)
log.Error().Msg(stderr)
}

gitArgs := []string{}
if root != "" {
gitArgs = append(gitArgs, "-C", root)
}
gitArgs = append(gitArgs, args...)
result, err := runCmd("git", nil, gitArgs, logFn)
return strings.Trim(result.Stdout, "\n"), err
}

// isGitRepo returns true if the given directory is a git repository
// It does it by running `git rev-parse --git-dir` and checking if it returns an error.
func isGitRepo(root string) bool {
_, err := runGitCmd([]string{"rev-parse", "--git-dir"}, root, true)
return err == nil
}

func getGitPathPrefix(root string) (string, error) {
args := []string{"rev-parse", "--show-prefix"}
return runGitCmd(args, root, false)
}

func getGitRepoUrl(root string) (string, error) {
args := []string{"remote", "get-url", "origin"}
return runGitCmd(args, root, false)
}

func getGitRepoBranch(root string) (string, error) {
args := []string{"rev-parse", "--abbrev-ref", "HEAD"}
return runGitCmd(args, root, false)
}
106 changes: 31 additions & 75 deletions internal/myks/globe.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ type Globe struct {

/// Runtime data

// Running in a git repository
WithGit bool
// Git repository path prefix (non-empty if running in a subdirectory of a git repository)
GitPathPrefix string
// Git repository branch
Expand Down Expand Up @@ -114,25 +116,41 @@ type VendirCredentials struct {

type EnvAppMap map[string][]string

func New(rootDir string) *Globe {
g := &Globe{
RootDir: rootDir,
environments: make(map[string]*Environment),
}
func NewWithDefaults() *Globe {
g := &Globe{}
if err := defaults.Set(g); err != nil {
log.Fatal().Err(err).Msg("Unable to set defaults")
}
return g
}

if err := g.setGitPathPrefix(); err != nil {
log.Warn().Err(err).Msg("Unable to set git path prefix")
}
func New(rootDir string) *Globe {
g := NewWithDefaults()
g.RootDir = rootDir
g.environments = make(map[string]*Environment)

if err := g.setGitRepoUrl(); err != nil {
log.Warn().Err(err).Msg("Unable to set git repo url")
}
if isGitRepo(g.RootDir) {
g.WithGit = true

if gitPathPrefix, err := getGitPathPrefix(g.RootDir); err != nil {
log.Warn().Err(err).Msg("Unable to set git path prefix")
} else {
g.GitPathPrefix = gitPathPrefix
}

if gitRepoBranch, err := getGitRepoBranch(g.RootDir); err != nil {
log.Warn().Err(err).Msg("Unable to set git repo url")
} else {
g.GitRepoBranch = gitRepoBranch
}

if err := g.setGitRepoBranch(); err != nil {
log.Warn().Err(err).Msg("Unable to set git repo branch")
if gitRepoUrl, err := getGitRepoUrl(g.RootDir); err != nil {
log.Warn().Err(err).Msg("Unable to set git repo branch")
} else {
g.GitRepoUrl = gitRepoUrl
}
} else {
log.Warn().Msg("Not in a git repository, Smart Mode and git-related data will not be available")
}

yttLibraryDir := filepath.Join(g.RootDir, g.YttLibraryDirName)
Expand Down Expand Up @@ -363,68 +381,6 @@ func (g Globe) isEnvPath(path string) bool {
return false
}

func (g *Globe) setGitPathPrefix() error {
if g.GitPathPrefix == "" {
gitArgs := []string{}
if g.RootDir != "" {
gitArgs = append(gitArgs, "-C", g.RootDir)
}
gitArgs = append(gitArgs, "rev-parse", "--show-prefix")
result, err := runCmd("git", nil, gitArgs, func(name string, err error, stderr string, args []string) {
cmd := msgRunCmd("set git path prefix", name, args)
if err != nil {
log.Error().Msg(cmd)
log.Error().Msg(stderr)
} else {
log.Debug().Msg(cmd)
}
})
if err != nil {
return err
}
g.GitPathPrefix = strings.Trim(result.Stdout, "\n")
}
return nil
}

func (g *Globe) setGitRepoUrl() error {
if g.GitRepoUrl == "" {
result, err := runCmd("git", nil, []string{"remote", "get-url", "origin"}, func(name string, err error, stderr string, args []string) {
cmd := msgRunCmd("set git repository url", name, args)
if err != nil {
log.Error().Msg(cmd)
log.Error().Msg(stderr)
} else {
log.Debug().Msg(cmd)
}
})
if err != nil {
return err
}
g.GitRepoUrl = strings.Trim(result.Stdout, "\n")
}
return nil
}

func (g *Globe) setGitRepoBranch() error {
if g.GitRepoBranch == "" {
result, err := runCmd("git", nil, []string{"rev-parse", "--abbrev-ref", "HEAD"}, func(name string, err error, stderr string, args []string) {
cmd := msgRunCmd("set git repository branch", name, args)
if err != nil {
log.Error().Msg(cmd)
log.Error().Msg(stderr)
} else {
log.Debug().Msg(cmd)
}
})
if err != nil {
return err
}
g.GitRepoBranch = strings.Trim(result.Stdout, "\n")
}
return nil
}

func (g *Globe) Msg(msg string) string {
formattedMessage := fmt.Sprintf(GlobalLogFormat, msg)
return formattedMessage
Expand Down
5 changes: 5 additions & 0 deletions internal/myks/smart_mode.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package myks

import (
"errors"
"fmt"
"regexp"
"strings"
Expand All @@ -10,6 +11,10 @@ import (
)

func (g *Globe) DetectChangedEnvsAndApps(baseRevision string) (EnvAppMap, error) {
if !g.WithGit {
return nil, errors.New("Git is unavailable")
}

// envAppMap is built later by calling g.runSmartMode
_ = g.collectEnvironments(nil)

Expand Down
Loading