-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support getting package versions from external files
- Loading branch information
1 parent
54a5885
commit 04fdb5d
Showing
11 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package expr | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/expr-lang/expr" | ||
"github.com/spf13/afero" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
type Reader struct { | ||
pwd string | ||
fs afero.Fs | ||
} | ||
|
||
func EvalVersionExpr(fs afero.Fs, pwd string, expression string) (string, error) { | ||
r := Reader{fs: fs, pwd: pwd} | ||
compiled, err := expr.Compile(expression, expr.Env(map[string]any{ | ||
"readFile": r.readFile, | ||
"readJSON": r.readJSON, | ||
"readYAML": r.readYAML, | ||
})) | ||
if err != nil { | ||
return "", fmt.Errorf("parse the expression: %w", err) | ||
} | ||
a, err := expr.Run(compiled, map[string]any{ | ||
"readFile": r.readFile, | ||
"readJSON": r.readJSON, | ||
"readYAML": r.readYAML, | ||
}) | ||
if err != nil { | ||
return "", fmt.Errorf("evaluate the expression: %w", err) | ||
} | ||
s, ok := a.(string) | ||
if !ok { | ||
return "", errMustBeBoolean | ||
} | ||
return s, nil | ||
} | ||
|
||
func (r *Reader) read(s string) []byte { | ||
if !filepath.IsAbs(s) { | ||
s = filepath.Join(r.pwd, s) | ||
} | ||
b, err := afero.ReadFile(r.fs, s) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return b | ||
} | ||
|
||
func (r *Reader) readFile(s string) string { | ||
return strings.TrimSpace(string(r.read(s))) | ||
} | ||
|
||
func (r *Reader) readJSON(s string) any { | ||
b := r.read(s) | ||
var a any | ||
if err := json.Unmarshal(b, &a); err != nil { | ||
panic(err) | ||
} | ||
return a | ||
} | ||
|
||
func (r *Reader) readYAML(s string) any { | ||
b := r.read(s) | ||
var a any | ||
if err := yaml.Unmarshal(b, &a); err != nil { | ||
panic(err) | ||
} | ||
return a | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1.10.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--- | ||
# yaml-language-server: $schema=https://raw.githubusercontent.com/aquaproj/aqua/main/json-schema/aqua-yaml.json | ||
# aqua - Declarative CLI Version Manager | ||
# https://aquaproj.github.io/ | ||
# checksum: | ||
# enabled: true | ||
# require_checksum: true | ||
# supported_envs: | ||
# - all | ||
registries: | ||
- type: standard | ||
ref: v4.276.0 # renovate: depName=aquaproj/aqua-registry | ||
packages: | ||
- name: hashicorp/terraform | ||
version_expr: | | ||
"v" + readFile('.terraform-version') | ||
# version_template: v{{readFile '.terraform-version'}} | ||
# version_template: v{{(readYAML 'foo.yaml').version}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--- | ||
# yaml-language-server: $schema=https://raw.githubusercontent.com/aquaproj/aqua/main/json-schema/aqua-yaml.json | ||
# aqua - Declarative CLI Version Manager | ||
# https://aquaproj.github.io/ | ||
# checksum: | ||
# enabled: true | ||
# require_checksum: true | ||
# supported_envs: | ||
# - all | ||
registries: | ||
- type: standard | ||
ref: v4.276.0 # renovate: depName=aquaproj/aqua-registry | ||
packages: | ||
- name: hashicorp/terraform | ||
version_expr: | | ||
readJSON('version.json').version |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"version": "1.10.1" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--- | ||
# yaml-language-server: $schema=https://raw.githubusercontent.com/aquaproj/aqua/main/json-schema/aqua-yaml.json | ||
# aqua - Declarative CLI Version Manager | ||
# https://aquaproj.github.io/ | ||
# checksum: | ||
# enabled: true | ||
# require_checksum: true | ||
# supported_envs: | ||
# - all | ||
registries: | ||
- type: standard | ||
ref: v4.276.0 # renovate: depName=aquaproj/aqua-registry | ||
packages: | ||
- name: hashicorp/terraform | ||
version_expr: | | ||
readYAML('version.yaml').version |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
version: 1.10.1 |