From 57ac7bdd80176fdd2d82c86f38946219c1049a58 Mon Sep 17 00:00:00 2001 From: German Lashevich Date: Mon, 8 Jan 2024 22:42:42 +0100 Subject: [PATCH 1/3] fix(init): do not discover git environment --- cmd/init.go | 2 +- internal/myks/globe.go | 14 +++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/cmd/init.go b/cmd/init.go index 422b96b8..0706e4aa 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -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") diff --git a/internal/myks/globe.go b/internal/myks/globe.go index 51c73309..9ceaf59b 100644 --- a/internal/myks/globe.go +++ b/internal/myks/globe.go @@ -114,14 +114,18 @@ 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 +} + +func New(rootDir string) *Globe { + g := NewWithDefaults() + g.RootDir = rootDir + g.environments = make(map[string]*Environment) if err := g.setGitPathPrefix(); err != nil { log.Warn().Err(err).Msg("Unable to set git path prefix") From 4a763b51cf30630953d363b1b359ddc0d215a82c Mon Sep 17 00:00:00 2001 From: German Lashevich Date: Mon, 8 Jan 2024 22:43:06 +0100 Subject: [PATCH 2/3] chore: go mod tidy --- go.sum | 8 -------- 1 file changed, 8 deletions(-) diff --git a/go.sum b/go.sum index 839a98e9..1559a1cc 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= @@ -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= From 6bfae8fcede6e119908ceb23fd43566d2f91291a Mon Sep 17 00:00:00 2001 From: German Lashevich Date: Mon, 8 Jan 2024 23:27:25 +0100 Subject: [PATCH 3/3] move git functions out of Globe, handle no-git situation --- internal/myks/git.go | 47 +++++++++++++++++++ internal/myks/globe.go | 92 +++++++++---------------------------- internal/myks/smart_mode.go | 5 ++ 3 files changed, 74 insertions(+), 70 deletions(-) diff --git a/internal/myks/git.go b/internal/myks/git.go index 2583a1c2..9222ec65 100644 --- a/internal/myks/git.go +++ b/internal/myks/git.go @@ -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) +} diff --git a/internal/myks/globe.go b/internal/myks/globe.go index 9ceaf59b..031c786f 100644 --- a/internal/myks/globe.go +++ b/internal/myks/globe.go @@ -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 @@ -127,16 +129,28 @@ func New(rootDir string) *Globe { g.RootDir = rootDir g.environments = make(map[string]*Environment) - if err := g.setGitPathPrefix(); err != nil { - log.Warn().Err(err).Msg("Unable to set git path prefix") - } + if isGitRepo(g.RootDir) { + g.WithGit = true - if err := g.setGitRepoUrl(); err != nil { - log.Warn().Err(err).Msg("Unable to set git repo url") - } + 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) @@ -367,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 diff --git a/internal/myks/smart_mode.go b/internal/myks/smart_mode.go index c2334914..777216db 100644 --- a/internal/myks/smart_mode.go +++ b/internal/myks/smart_mode.go @@ -1,6 +1,7 @@ package myks import ( + "errors" "fmt" "regexp" "strings" @@ -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)