Skip to content

Commit

Permalink
Show line by line diff of embedded template
Browse files Browse the repository at this point in the history
  • Loading branch information
amirabbas8 committed Aug 22, 2024
1 parent 8b093a6 commit 6fa3f03
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 2 deletions.
7 changes: 6 additions & 1 deletion command/job_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"
"time"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/helper/pointer"
"github.com/hashicorp/nomad/scheduler"
Expand Down Expand Up @@ -649,7 +650,11 @@ func formatFieldDiff(diff *api.FieldDiff, startPrefix, keyPrefix, valuePrefix in
case "Deleted":
out += fmt.Sprintf("%q", diff.Old)
case "Edited":
out += fmt.Sprintf("%q => %q", diff.Old, diff.New)
if diff.Name == "EmbeddedTmpl" {
out += cmp.Diff(diff.Old, diff.New)
} else {
out += fmt.Sprintf("%q => %q", diff.Old, diff.New)
}
default:
out += fmt.Sprintf("%q", diff.New)
}
Expand Down
17 changes: 17 additions & 0 deletions ui/app/components/job-diff-template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/

import Component from '@ember/component';
import { tagName } from '@ember-decorators/component';
import classic from 'ember-classic-decorator';
import LineDiff from 'line-diff';

@classic
@tagName('')
export default class JobDiffTemplate extends Component {
get diff() {
return new LineDiff(this.field.Old, this.field.New).toString();
}
}
4 changes: 4 additions & 0 deletions ui/app/styles/components/job-diff.scss
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,8 @@
text-indent: 1ch;
}
}

.diff-section-table-pre-wrap {
white-space: pre-wrap;
}
}
6 changes: 5 additions & 1 deletion ui/app/templates/components/job-diff-fields-and-objects.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@
{{else if (eq (lowercase field.Type) "deleted")}}
<span class="diff-section-table-cell diff-section-change">"{{field.Old}}"</span>
{{else if (eq (lowercase field.Type) "edited")}}
<span class="diff-section-table-cell diff-section-change">"{{field.Old}}" => "{{field.New}}"</span>
{{#if (eq (lowercase field.Name) "embeddedtmpl")}}
<JobDiffTemplate @field={{field}} />
{{else}}
<span class="diff-section-table-cell diff-section-change">"{{field.Old}}" => "{{field.New}}"</span>
{{/if}}
{{else}}
<span class="diff-section-table-cell">"{{field.New}}"</span>
{{/if}}
Expand Down
6 changes: 6 additions & 0 deletions ui/app/templates/components/job-diff-template.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{{!
Copyright (c) HashiCorp, Inc.
SPDX-License-Identifier: BUSL-1.1
~}}

<div class="diff-section-table-cell diff-section-change diff-section-table-pre-wrap">{{this.diff}}</div>
1 change: 1 addition & 0 deletions ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@
"curved-arrows": "^0.1.0",
"d3": "^7.3.0",
"dompurify": "^3.1.3",
"line-diff": "^2.1.1",
"lru_map": "^0.4.1",
"marked": "^12.0.2",
"no-case": "^3.0.4",
Expand Down
12 changes: 12 additions & 0 deletions ui/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9501,6 +9501,11 @@ [email protected]:
lodash.assign "^3.2.0"
rsvp "^3.0.21"

levdist@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/levdist/-/levdist-1.0.0.tgz#91d7a3044964f2ccc421a0477cac827fe75c5718"
integrity sha512-YguwC2spb0pqpJM3a5OsBhih/GG2ZHoaSHnmBqhEI7997a36buhqcRTegEjozHxyxByIwLpZHZTVYMThq+Zd3g==

levn@^0.4.1:
version "0.4.1"
resolved "https://registry.yarnpkg.com/levn/-/levn-0.4.1.tgz#ae4562c007473b932a6200d403268dd2fffc6ade"
Expand All @@ -9517,6 +9522,13 @@ line-column@^1.0.2:
isarray "^1.0.0"
isobject "^2.0.0"

line-diff@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/line-diff/-/line-diff-2.1.1.tgz#a389799b931375a3b1e764964ad0b0b3ce60d6f6"
integrity sha512-vswdynAI5AMPJacOo2o+JJ4caDJbnY2NEqms4MhMW0NJbjh3skP/brpVTAgBxrg55NRZ2Vtw88ef18hnagIpYQ==
dependencies:
levdist "^1.0.0"

lines-and-columns@^1.1.6:
version "1.2.4"
resolved "https://registry.yarnpkg.com/lines-and-columns/-/lines-and-columns-1.2.4.tgz#eca284f75d2965079309dc0ad9255abb2ebc1632"
Expand Down

0 comments on commit 6fa3f03

Please sign in to comment.