Skip to content

Commit

Permalink
Fix diff command
Browse files Browse the repository at this point in the history
  • Loading branch information
kajogo777 committed Jun 8, 2023
1 parent 9419e8d commit d3fe9f5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
4 changes: 2 additions & 2 deletions cmd/devx/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
)

var diffCmd = &cobra.Command{
Use: "diff [target revision] [environment]",
Use: "diff [environment] [target git revision]",
Short: "Diff the current stack with that @ target (e.g. HEAD, commit, tag).",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
if err := client.Diff(args[0], args[1], configDir, stackPath, buildersPath, server, noStrict); err != nil {
if err := client.Diff(args[1], args[0], configDir, stackPath, buildersPath, server, noStrict); err != nil {
return fmt.Errorf(errors.Details(err, nil))
}
return nil
Expand Down
14 changes: 14 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ func Diff(target string, environment string, configDir string, stackPath string,
}

log.Info("\n📍 Processing current stack")
err = project.Update(configDir, server)
if err != nil {
return err
}
currentCtx := context.Background()
currentCtx = context.WithValue(currentCtx, utils.ConfigDirKey, configDir)
currentCtx = context.WithValue(currentCtx, utils.DryRunKey, true)
Expand All @@ -148,17 +152,20 @@ func Diff(target string, environment string, configDir string, stackPath string,
addColor := color.New(color.FgGreen)
updColor := color.New(color.FgYellow)
log.Info("\n🔬 Diff")
foundDiff := false
ci, ti := 0, 0
for ci < len(currentValues) || ti < len(targetValues) {
if ci == len(currentValues) {
tv := targetValues[ti]
log.Infof("\t%s %s: %s", remColor.Sprintf("-"), tv.Path, tv.Value)
foundDiff = true
ti++
continue
}
if ti == len(targetValues) {
cv := currentValues[ci]
log.Infof("\t%s %s: %s", addColor.Sprintf("+"), cv.Path, cv.Value)
foundDiff = true
ci++
continue
}
Expand All @@ -169,18 +176,25 @@ func Diff(target string, environment string, configDir string, stackPath string,
case 0:
if strings.Compare(cv.Value, tv.Value) != 0 {
log.Infof("\t%s %s: %s -> %s", updColor.Sprintf("~"), cv.Path, tv.Value, cv.Value)
foundDiff = true
}
ci++
ti++
case -1:
log.Infof("\t%s %s: %s", addColor.Sprintf("+"), cv.Path, cv.Value)
foundDiff = true
ci++
case 1:
log.Infof("\t%s %s: %s", remColor.Sprintf("-"), tv.Path, tv.Value)
foundDiff = true
ti++
}
}

if !foundDiff {
log.Infof("No changes found")
}

return nil
}

Expand Down
12 changes: 7 additions & 5 deletions pkg/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,18 +410,20 @@ func Update(configDir string, server auth.ServerConfig) error {
}

for _, info := range packageInfo {
pkgDir := path.Join(configDir, moduleDepPkgPath, info.Name())
log.Debug("Updating dependency ", pkgDir)
modPkgDir := path.Join(moduleDepPkgPath, info.Name())
pkgDir := path.Join(configDir, modPkgDir)
log.Debug("Updating dependency ", modPkgDir)
err = os.RemoveAll(pkgDir)
if err != nil {
return err
}

err = utils.FsWalk(*mfs, pkgDir, func(file string, content []byte) error {
if err := os.MkdirAll(filepath.Dir(file), 0755); err != nil {
err = utils.FsWalk(*mfs, modPkgDir, func(file string, content []byte) error {
writePath := path.Join(configDir, file)
if err := os.MkdirAll(filepath.Dir(writePath), 0755); err != nil {
return err
}
return os.WriteFile(file, content, 0700)
return os.WriteFile(writePath, content, 0700)
})
if err != nil {
return err
Expand Down

0 comments on commit d3fe9f5

Please sign in to comment.