Skip to content

Commit

Permalink
add context.appRevisionNum
Browse files Browse the repository at this point in the history
  • Loading branch information
yangsoon committed Apr 12, 2021
1 parent 2254777 commit 34030c9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/en/cue/component.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ output: {
| Context Variable | Description |
| :--: | :---------: |
| `context.appRevision` | The revision of the application |
| `context.appRevisionNum` | The revision number(`int` type) of the application, e.g., `context.appRevisionNum` will be `1` if `context.appRevision` is `app-v1`|
| `context.appName` | The name of the application |
| `context.name` | The name of the component of the application |
| `context.namespace` | The namespace of the application |
Expand Down
9 changes: 9 additions & 0 deletions pkg/dsl/process/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const (
ContextAppName = "appName"
// ContextAppRevision is the revision name of app of context
ContextAppRevision = "appRevision"
// ContextAppRevisionNum is the revision num of app of context
ContextAppRevisionNum = "appRevisionNum"
// ContextNamespace is the namespace of the app
ContextNamespace = "namespace"
// OutputSecretName is used to store all secret names which are generated by cloud resource components
Expand Down Expand Up @@ -128,6 +130,7 @@ func (ctx *templateContext) BaseContextFile() string {
buff += fmt.Sprintf(ContextName+": \"%s\"\n", ctx.name)
buff += fmt.Sprintf(ContextAppName+": \"%s\"\n", ctx.appName)
buff += fmt.Sprintf(ContextAppRevision+": \"%s\"\n", ctx.appRevision)
buff += fmt.Sprintf(ContextAppRevisionNum+": %s\n", extractRevisionNum(ctx.appRevision))
buff += fmt.Sprintf(ContextNamespace+": \"%s\"\n", ctx.namespace)

if ctx.base != nil {
Expand Down Expand Up @@ -223,3 +226,9 @@ func structMarshal(v string) string {
}
return fmt.Sprintf("{%s}", v)
}

func extractRevisionNum(appRevision string) string {
app := strings.Split(appRevision, "-")
vision := app[len(app)-1]
return strings.Replace(vision, "v", "", 1)
}
24 changes: 24 additions & 0 deletions pkg/dsl/process/handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ image: "myserver"
assert.Equal(t, nil, err)
assert.Equal(t, "myapp-v1", myAppRevision)

myAppRevisionNum, err := ctxInst.Lookup("context", ContextAppRevisionNum).Int64()
assert.Equal(t, nil, err)
assert.Equal(t, int64(1), myAppRevisionNum)

inputJs, err := ctxInst.Lookup("context", OutputFieldName).MarshalJSON()
assert.Equal(t, nil, err)
assert.Equal(t, `{"image":"myserver"}`, string(inputJs))
Expand All @@ -107,3 +111,23 @@ image: "myserver"
assert.Equal(t, nil, err)
assert.Equal(t, "{\"password\":\"123\"}", string(requiredSecrets))
}

func TestExtractRevisionNum(t *testing.T) {
testcases := []struct {
appRevision string
wantRevisionNum string
}{{
appRevision: "myapp-v1",
wantRevisionNum: "1",
}, {
appRevision: "new-app-v2",
wantRevisionNum: "2",
}, {
appRevision: "new-app-v10",
wantRevisionNum: "10",
}}

for _, tt := range testcases {
assert.Equal(t, tt.wantRevisionNum, extractRevisionNum(tt.appRevision))
}
}

0 comments on commit 34030c9

Please sign in to comment.