Add a warning if spec is applied on a dirty worktree (#1966)
* Add a warning if spec is applied on a dirty worktree * Update go dependencies after rebase * Use the console package for showing the warning * Fix merge conflicts * Handle errors and use better messages Co-authored-by: Sanket Sudake <sanketsudake@gmail.com>
This commit is contained in:
co-authored by
Sanket Sudake
parent
4a1fc11b2d
commit
a9c56e5bb2
@@ -27,6 +27,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/go-git/go-git/v5"
|
||||
"github.com/mholt/archiver"
|
||||
"github.com/pkg/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
@@ -123,6 +124,11 @@ func (opts *ApplySubCommand) run(input cli.Input) error {
|
||||
}
|
||||
}
|
||||
|
||||
err = warnIfDirtyWorkTree(filepath.Clean(specDir + "/.."))
|
||||
if err != nil {
|
||||
console.Warn(err.Error())
|
||||
}
|
||||
|
||||
// make changes to the cluster based on the specs
|
||||
pkgMetas, as, err := applyResources(opts.Client(), specDir, fr, deleteResources)
|
||||
if err != nil {
|
||||
@@ -185,6 +191,30 @@ func (opts *ApplySubCommand) run(input cli.Input) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func warnIfDirtyWorkTree(path string) error {
|
||||
repo, err := git.PlainOpen(path)
|
||||
if err != nil {
|
||||
console.Info("Spec doesn't belong to Git Tree.")
|
||||
return nil
|
||||
}
|
||||
|
||||
workTree, err := repo.Worktree()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
status, err := workTree.Status()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !status.IsClean() {
|
||||
console.Warn("Worktree is not clean, please ensure you have committed the changes to git.")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func ignoreFile(path string) bool {
|
||||
return (strings.Contains(path, "/.#") || // editor autosave files
|
||||
strings.HasSuffix(path, "~")) // editor backups, usually
|
||||
|
||||
Reference in New Issue
Block a user