Skip to content

Commit

Permalink
feat: support getting package versions from external files
Browse files Browse the repository at this point in the history
  • Loading branch information
suzuki-shunsuke committed Dec 15, 2024
1 parent 54a5885 commit 04fdb5d
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/wc-integration-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ jobs:
run: diff aqua.yaml expect.yaml
working-directory: tests/insert

- name: Test version_expr readFile
run: aqua which -v terraform
working-directory: tests/version_expr_file
- name: Test version_expr readJSON
run: aqua which -v terraform
working-directory: tests/version_expr_json
- name: Test version_expr readYAML
run: aqua which -v terraform
working-directory: tests/version_expr_yaml

- run: aqua g -i suzuki-shunsuke/tfcmt
working-directory: tests/main
- name: add duplicated package
Expand Down
3 changes: 3 additions & 0 deletions json-schema/aqua-yaml.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@
"go_version_file": {
"type": "string"
},
"version_expr": {
"type": "string"
},
"vars": {
"type": "object"
},
Expand Down
13 changes: 13 additions & 0 deletions pkg/config-reader/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/aquaproj/aqua/v2/pkg/config"
"github.com/aquaproj/aqua/v2/pkg/config/aqua"
"github.com/aquaproj/aqua/v2/pkg/expr"
"github.com/aquaproj/aqua/v2/pkg/osfile"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
Expand Down Expand Up @@ -97,6 +98,18 @@ func (r *ConfigReader) readPackage(logE *logrus.Entry, configFilePath string, pk
}
return nil, nil
}
if pkg.VersionExpr != "" {
// version_expr
dir := filepath.Dir(configFilePath)
s, err := expr.EvalVersionExpr(r.fs, dir, pkg.VersionExpr)
if err != nil {
return nil, fmt.Errorf("evaluate a version_expr: %w", logerr.WithFields(err, logrus.Fields{
"version_expr": pkg.VersionExpr,
}))
}
pkg.Version = s
return nil, nil //nolint:nilnil
}
if pkg.Import == "" {
// version
return nil, nil
Expand Down
1 change: 1 addition & 0 deletions pkg/config/aqua/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Package struct {
Update *Update `yaml:",omitempty" json:"update,omitempty"`
FilePath string `json:"-" yaml:"-"`
GoVersionFile string `json:"go_version_file,omitempty" yaml:"go_version_file,omitempty"`
VersionExpr string `json:"version_expr,omitempty" yaml:"version_expr,omitempty"`
Vars map[string]any `json:"vars,omitempty" yaml:",omitempty"`
CommandAliases []*CommandAlias `json:"command_aliases,omitempty" yaml:"command_aliases,omitempty"`
}
Expand Down
75 changes: 75 additions & 0 deletions pkg/expr/version_expr.go
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
}
1 change: 1 addition & 0 deletions tests/version_expr_file/.terraform-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.10.2
18 changes: 18 additions & 0 deletions tests/version_expr_file/aqua.yaml
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}}
16 changes: 16 additions & 0 deletions tests/version_expr_json/aqua.yaml
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
3 changes: 3 additions & 0 deletions tests/version_expr_json/version.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"version": "1.10.1"
}
16 changes: 16 additions & 0 deletions tests/version_expr_yaml/aqua.yaml
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
1 change: 1 addition & 0 deletions tests/version_expr_yaml/version.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version: 1.10.1

0 comments on commit 04fdb5d

Please sign in to comment.