├── .github ├── dependabot.yml └── workflows │ ├── build_release.yml │ └── ci.yml ├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── cmd ├── build.go ├── build_test.go ├── depend.go ├── eval.go ├── eval_test.go ├── init.go ├── list.go ├── root.go ├── test.go ├── test_test.go ├── testdata │ └── projects │ │ ├── empty │ │ └── opa.project │ │ ├── local-dependencies │ │ └── opa.project │ │ ├── no-dependencies │ │ ├── opa.project │ │ ├── src │ │ │ └── policy.rego │ │ └── tst │ │ │ └── tests.rego │ │ ├── source-list │ │ ├── data │ │ │ ├── do │ │ │ │ └── data.yml │ │ │ └── foo │ │ │ │ └── data.json │ │ ├── opa.project │ │ ├── src │ │ │ └── policy.rego │ │ └── test │ │ │ └── test.rego │ │ └── transitive-dependencies │ │ └── opa.project ├── update.go └── update_test.go ├── go.mod ├── go.sum ├── main.go ├── printer └── printer.go ├── proj ├── project.go └── project_test.go └── utils ├── opa.go └── utils.go /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: gomod 4 | directory: / 5 | schedule: 6 | interval: daily 7 | - package-ecosystem: github-actions 8 | directory: / 9 | schedule: 10 | interval: weekly 11 | -------------------------------------------------------------------------------- /.github/workflows/build_release.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a golang project 2 | # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go 3 | 4 | name: Build Release 5 | 6 | on: 7 | push: 8 | tags: 9 | - 'v[0-9]+.[0-9]+.[0-9]+' 10 | 11 | jobs: 12 | create_release: 13 | runs-on: ubuntu-latest 14 | outputs: 15 | upload_url: ${{ steps.create_release.outputs.upload_url }} 16 | 17 | steps: 18 | - name: Create Release 19 | id: create_release 20 | uses: actions/create-release@v1 21 | env: 22 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 23 | with: 24 | tag_name: ${{ github.ref }} 25 | release_name: Release ${{ github.ref }} 26 | body: CHANGELOG.md 27 | draft: true 28 | prerelease: false 29 | 30 | build_release: 31 | runs-on: ubuntu-latest 32 | needs: create_release 33 | strategy: 34 | matrix: 35 | go-os: [ 'darwin', 'linux', 'windows' ] 36 | go-arch: [ 'arm64', 'amd64' ] 37 | 38 | steps: 39 | - uses: actions/checkout@v4 40 | 41 | - name: Set up Go 42 | uses: actions/setup-go@v5 43 | with: 44 | go-version: 1.19 45 | 46 | - name: Install dependencies 47 | run: go get . 48 | 49 | - name: Build 50 | run: env GOOS=${{ matrix.go-os }} GOARCH=${{ matrix.go-arch }} go build -v -o ${{ matrix.go-os }}_${{ matrix.go-arch }}/ 51 | 52 | # - name: Test 53 | # run: go test -v ./... 54 | 55 | - name: Tar 56 | run: tar -zcvf ${{ matrix.go-os }}_${{ matrix.go-arch }}.tar.gz ${{ matrix.go-os }}_${{ matrix.go-arch }}/* 57 | 58 | # - name: Upload artifacts 59 | # uses: actions/upload-artifact@v3 60 | # with: 61 | # name: build-${{ matrix.go-os }}_${{ matrix.go-arch }} 62 | # path: ${{ matrix.go-os }}_${{ matrix.go-arch }}.tar.gz 63 | 64 | - name: Upload release artifact 65 | uses: actions/upload-release-asset@v1 66 | env: 67 | GITHUB_TOKEN: ${{ github.token }} 68 | with: 69 | upload_url: ${{ needs.create_release.outputs.upload_url }} 70 | asset_path: ./${{ matrix.go-os }}_${{ matrix.go-arch }}.tar.gz 71 | asset_name: odm-${{ matrix.go-os }}_${{ matrix.go-arch }}.tar.gz 72 | asset_content_type: application/gzip 73 | 74 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a golang project 2 | # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go 3 | 4 | name: PR 5 | 6 | on: 7 | push: 8 | branches: [ "main" ] 9 | pull_request: 10 | branches: [ "main" ] 11 | 12 | jobs: 13 | 14 | build: 15 | runs-on: ubuntu-latest 16 | steps: 17 | - uses: actions/checkout@v4 18 | 19 | - name: Setup OPA 20 | uses: open-policy-agent/setup-opa@v2 21 | 22 | - name: Set up Go 23 | uses: actions/setup-go@v5 24 | with: 25 | go-version: 1.19 26 | 27 | - name: Build 28 | run: go build -v ./... 29 | 30 | - name: Test 31 | run: go test -v ./... 32 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.exe 2 | *.exe~ 3 | *.dll 4 | *.so 5 | *.dylib 6 | 7 | odm 8 | 9 | # Go 10 | *.test 11 | *.out 12 | go.work 13 | 14 | # IntelliJ 15 | .idea 16 | out/ 17 | *.iml 18 | 19 | # macOS 20 | .DS_Store 21 | .AppleDouble 22 | .LSOverride 23 | Icon 24 | ._* 25 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | ## [0.3.0] 6 | 7 | - Flattened dependency directory structure ([#21](https://github.com/johanfylling/opa-dependency-manager/issues/21)) 8 | - Added `list source` command for enumerating source directories 9 | - Added support for listing multiple `source` and `test` directories in `opa.project` 10 | 11 | ## [0.2.0] 12 | 13 | - Configurable OPA executable path through optional `OPA_PATH` environment variable ([#1](https://github.com/johanfylling/opa-dependency-manager/issues/1)) 14 | - Refactored `opa.project` yaml structure ([#4](https://github.com/johanfylling/opa-dependency-manager/pull/14)) 15 | - Info/debug printing to `stderr` ([#6](https://github.com/johanfylling/opa-dependency-manager/issues/6)) 16 | - Added `build` command for building OPA bundles ([#8](https://github.com/johanfylling/opa-dependency-manager/issues/8)) 17 | - Respecting `src` and `tests` project parameters for `eval`, `test`, and `build` commands ([#15](https://github.com/johanfylling/opa-dependency-manager/issues/15)) 18 | - Override opa executable through `OPA_PATH` environment variable ([#1](https://github.com/johanfylling/opa-dependency-manager/issues/1)) 19 | 20 | ## [0.1.0] 21 | 22 | - Initial release 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OPA Dependency Manager (ODM) 2 | 3 | ODM is a tool for managing dependencies for [Open Policy Agent](https://www.openpolicyagent.org/) (OPA) projects. 4 | 5 | __NOTE__: This is an experimental project not officially supported by the OPA team or Styra. 6 | 7 | ```bash 8 | $ odm init my_project 9 | $ cd my_project 10 | $ odm depend --no-namespace rego-test-assertions \ 11 | git+https://github.com/anderseknert/rego-test-assertions 12 | $ mkdir src 13 | 14 | $ cat < src/policy.rego 15 | package main 16 | 17 | import data.test.assert 18 | 19 | foo := 42 20 | 21 | test_foo { 22 | assert.equals(42, foo) 23 | } 24 | EOF 25 | 26 | $ odm test 27 | ``` 28 | 29 | An example project can be found [here](https://github.com/johanfylling/odm-example-project). 30 | 31 | ## Running 32 | 33 | Where you have your `.rego` project/files. 34 | 35 | ### Setup new project 36 | 37 | ```bash 38 | $ odm init [project name] 39 | ``` 40 | 41 | ### Add a dependency 42 | 43 | ```bash 44 | $ odm depend 45 | ``` 46 | 47 | In `opa.project`: 48 | 49 | ```yaml 50 | dependencies: 51 | : 52 | ``` 53 | 54 | #### Local dependency 55 | 56 | Local dependencies can be specified with relative or absolute paths, or URLs.: 57 | 58 | * `file:/` 59 | 60 | Examples: 61 | 62 | * Absolute path: `file://tmp/my/dependency` 63 | * Relative path: `file:/../my/dependency` 64 | 65 | #### Git dependency 66 | 67 | Git dependencies are URLs prefixed with `git+`: 68 | 69 | * `git+http://[#tag|branch|commit]]` 70 | * `git+https://[#tag|branch|commit]]` 71 | * `git+ssh://[#tag|branch|commit]]` 72 | 73 | Examples: 74 | 75 | * GitHub dependency at `HEAD` of repo: `git+https://github.com/johanfylling/odm-example-dependency.git` 76 | * GitHub dependency at `v1.0` tag: `git+https://github.com/johanfylling/odm-example-dependency.git#v1.0` 77 | * GitHub dependency at `foo` branch: `git+https://github.com/johanfylling/odm-example-dependency.git#foo` 78 | * GitHub dependency at `88c5cde` commit: `git+https://github.com/johanfylling/odm-example-dependency.git#88c5cde` 79 | 80 | ### Update dependencies 81 | 82 | ```bash 83 | $ odm update 84 | ``` 85 | 86 | ### Evaluating policies 87 | 88 | Example: 89 | ```bash 90 | $ odm eval -- 'data.main.allow' 91 | ``` 92 | 93 | if a `source` folder is specified in `opa.project`, it will be automatically included in the evaluation. 94 | 95 | ### Testing policies 96 | 97 | Example: 98 | ```bash 99 | $ odm test -- -d policy.rego 100 | ``` 101 | 102 | if a `source` folder is specified in `opa.project`, it will be automatically included in the evaluation. 103 | 104 | ## Namespacing 105 | 106 | By default, dependencies are namespaced by their declared name. 107 | 108 | When a dependency is namespaced, all contained Rego packages will be prefixed with the namespace. 109 | E.g.: a dependency with the following package structure: 110 | 111 | ``` 112 | foo 113 | +-- bar 114 | | +-- baz 115 | +-- qux 116 | ``` 117 | 118 | when namespaced with `utils`, it will have the following structure: 119 | 120 | ``` 121 | utils 122 | +-- foo 123 | +-- bar 124 | | +-- baz 125 | +-- qux 126 | ``` 127 | 128 | Transitive dependencies will be namespaced as well. 129 | Any transitive dependency already namespaced by its enclosing dependency project will have its packages prefixed by the namespace assigned by the enclosing project, and then by the namespace defined in the main project, recursively. 130 | 131 | ### Custom namespace 132 | 133 | ```bash 134 | $ odm dep my_dep file:/path/to/dependency -n mynamespace 135 | ``` 136 | 137 | In `opa.project`: 138 | 139 | ```yaml 140 | dependencies: 141 | my_dep: 142 | path: file:/path/to/dependency 143 | namespace: mynamespace 144 | ``` 145 | 146 | ### Disabling namespacing 147 | 148 | ```bash 149 | $ odm dep my_dep file:/path/to/dependency --no-namespace 150 | ``` 151 | 152 | In `opa.project`: 153 | 154 | ```yaml 155 | dependencies: 156 | my_dep: 157 | path: file:/path/to/dependency 158 | namespace: false 159 | ``` 160 | 161 | ## The `opa.project` file 162 | 163 | The `opa.project` file is a YAML file that contains the project configuration. 164 | 165 | Example: 166 | 167 | ```yaml 168 | name: 169 | source: 170 | dependencies: 171 | : 172 | ``` 173 | 174 | ### Attributes 175 | 176 | | Attribute | Type | Default | Description | 177 | |---------------------------------|----------------------|-------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| 178 | | `name` | `string` | none | The name of the project. | 179 | | `source` | `string`, `[]string` | none | The path to the source folder. If specified, the source directory will be automatically included in the `eval` and `test` commands. Can either be the path of a single directory, or a list of directories. | 180 | | `tests` | `string`, `[]string` | none | The path to the test folder. If specified, the test directory will be automatically included in the `test` command. Can either be the path of a single directory, or a list of directories. | 181 | | `dependencies` | `map` | | A map of dependency declaration, keyed by their name. | 182 | | `dependencies.` | `map`, `string` | none | A dependency declaration. A short form is supported, where the dependency value is its location as a string. | 183 | | `dependencies..location` | `string` | none | The location of the dependency. | 184 | | `dependencies..namespace` | `string`, `bool` | `true` | If a `string`: the namespace to use for the dependency. If a `bool`: if `true`, use the dependency `name` as namespace; if `false`, don't namesapace the dependency. | 185 | | `build` | `map` | | Settings for building bundles. | 186 | | `build.output` | `string` | `./build/bundle.tar.gz` | The location of the target bundle. | 187 | | `build.target` | `string` | `rego` | The target bundle format. E.g. `rego`, `wasm`, or `plan` | 188 | | `build.entrypoints` | `[]string` | `[]` | List of entrypoints. | 189 | -------------------------------------------------------------------------------- /cmd/build.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "fmt" 5 | "github.com/johanfylling/odm/printer" 6 | "github.com/johanfylling/odm/proj" 7 | "github.com/johanfylling/odm/utils" 8 | "github.com/spf13/cobra" 9 | "os" 10 | "path/filepath" 11 | ) 12 | 13 | var ( 14 | defaultTargetDir = "build" 15 | defaultTargetFile = "bundle.tar.gz" 16 | ) 17 | 18 | func init() { 19 | var noUpdate bool 20 | 21 | var buildCmd = &cobra.Command{ 22 | Use: "build", 23 | Short: "Build OPA bundle", 24 | Run: func(cmd *cobra.Command, args []string) { 25 | projPath := "." 26 | 27 | if !noUpdate { 28 | if err := doUpdate(projPath); err != nil { 29 | _, _ = fmt.Fprintf(os.Stderr, "%s\n", err) 30 | os.Exit(1) 31 | } 32 | } 33 | 34 | if err := doBuild(projPath, args); err != nil { 35 | _, _ = fmt.Fprintf(os.Stderr, "%s\n", err) 36 | os.Exit(1) 37 | } 38 | }, 39 | } 40 | 41 | addNoUpdateFlag(buildCmd, &noUpdate) 42 | RootCommand.AddCommand(buildCmd) 43 | } 44 | 45 | func doBuild(projPath string, args []string) error { 46 | printer.Trace("--- Eval start ---") 47 | defer printer.Trace("--- Eval end ---") 48 | 49 | project, err := proj.ReadAndLoadProject(projPath, true) 50 | if err != nil { 51 | return err 52 | } 53 | 54 | outputDir, outputFile := filepath.Split(project.Build.Output) 55 | if outputFile == "" { 56 | outputFile = defaultTargetFile 57 | if outputDir == "" { 58 | outputDir = defaultTargetDir 59 | } 60 | } 61 | 62 | if outputDir != "" { 63 | outputDir = filepath.Join(project.Dir(), outputDir) 64 | if err := utils.MakeDir(outputDir); err != nil { 65 | return fmt.Errorf("error creating build directory: %s", err) 66 | } 67 | } else { 68 | outputDir = project.Dir() 69 | } 70 | 71 | outputPath := filepath.Join(filepath.Clean(outputDir), outputFile) 72 | 73 | dataLocations, err := project.DataLocations() 74 | if err != nil { 75 | return fmt.Errorf("error getting data locations: %s", err) 76 | } 77 | 78 | opa := utils.NewOpa(dataLocations...). 79 | WithEntrypoints(project.Build.Entrypoints). 80 | WithTarget(project.Build.Target) 81 | if output, err := opa.Build(outputPath, args...); err != nil { 82 | return fmt.Errorf("error running opa eval:\n %s", err) 83 | } else { 84 | printer.Info(output) 85 | } 86 | 87 | return nil 88 | } 89 | -------------------------------------------------------------------------------- /cmd/build_test.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "archive/tar" 5 | "bytes" 6 | "compress/gzip" 7 | "github.com/johanfylling/odm/printer" 8 | "github.com/johanfylling/odm/proj" 9 | "github.com/johanfylling/odm/utils" 10 | "io" 11 | "os" 12 | "path/filepath" 13 | "runtime" 14 | "strings" 15 | "testing" 16 | ) 17 | 18 | func TestBuildProjects(t *testing.T) { 19 | _, file, _, _ := runtime.Caller(0) 20 | rootDir := filepath.Dir(file) 21 | 22 | tests := []struct { 23 | name string 24 | projectDir string 25 | bundleLocation string 26 | bundleContent []string 27 | cleanup string 28 | }{ 29 | { 30 | name: "Empty project", 31 | projectDir: filepath.Join(rootDir, "testdata", "projects", "empty"), 32 | bundleLocation: filepath.Join(rootDir, "testdata", "projects", "empty", "build", "bundle.tar.gz"), 33 | cleanup: "build", 34 | bundleContent: []string{ 35 | "/data.json", 36 | }, 37 | }, 38 | { 39 | name: "Project with source, no dependencies", 40 | projectDir: filepath.Join(rootDir, "testdata", "projects", "no-dependencies"), 41 | bundleLocation: filepath.Join(rootDir, "testdata", "projects", "no-dependencies", "build", "bundle.tar.gz"), 42 | cleanup: "build", 43 | bundleContent: []string{ 44 | "/data.json", 45 | "/src/policy.rego", 46 | }, 47 | }, 48 | { 49 | name: "Project with multiple source dirs, no dependencies", 50 | projectDir: filepath.Join(rootDir, "testdata", "projects", "source-list"), 51 | bundleLocation: filepath.Join(rootDir, "testdata", "projects", "source-list", "build", "bundle.tar.gz"), 52 | cleanup: "build", 53 | bundleContent: []string{ 54 | "/data.json", 55 | "/src/policy.rego", 56 | }, 57 | }, 58 | { 59 | name: "Project with local dependencies", 60 | projectDir: filepath.Join(rootDir, "testdata", "projects", "local-dependencies"), 61 | bundleLocation: filepath.Join(rootDir, "testdata", "projects", "local-dependencies", "build", "bundle.tar.gz"), 62 | cleanup: "build", 63 | bundleContent: []string{ 64 | "/data.json", 65 | filepath.Join(proj.DepId("no_deps", "file:/../no-dependencies"), "src", "policy.rego"), 66 | }, 67 | }, 68 | { 69 | name: "Project with transitive dependencies", 70 | projectDir: filepath.Join(rootDir, "testdata", "projects", "transitive-dependencies"), 71 | bundleLocation: filepath.Join(rootDir, "testdata", "projects", "transitive-dependencies", "build", "bundle.tar.gz"), 72 | cleanup: "build", 73 | bundleContent: []string{ 74 | "/data.json", 75 | filepath.Join(proj.DepId("foo.no_deps", "file:/../no-dependencies"), "src", "policy.rego"), 76 | filepath.Join(proj.DepId("bar.no_deps", "file:/../no-dependencies"), "src", "policy.rego"), 77 | filepath.Join(proj.DepId("no_deps", "file:/../no-dependencies"), "src", "policy.rego"), 78 | }, 79 | }, 80 | } 81 | 82 | for _, tc := range tests { 83 | //goland:noinspection GoDeferInLoop 84 | defer cleanup(tc.projectDir, tc.cleanup) 85 | 86 | t.Run(tc.name, func(t *testing.T) { 87 | output := bytes.Buffer{} 88 | printer.PrintWriter = &output 89 | args := []string{} 90 | if err := doUpdate(tc.projectDir); err != nil { 91 | t.Fatal(err) 92 | } 93 | if err := doBuild(tc.projectDir, args); err != nil { 94 | t.Fatal(err) 95 | } 96 | if !utils.FileExists(tc.bundleLocation) { 97 | t.Fatalf("expected bundle file to exist at %s", tc.bundleLocation) 98 | } 99 | 100 | var bundleFiles []string 101 | if r, err := os.Open(tc.bundleLocation); err != nil { 102 | t.Fatal(err) 103 | } else { 104 | uncompressedStream, err := gzip.NewReader(r) 105 | if err != nil { 106 | t.Fatal(err) 107 | } 108 | reader := tar.NewReader(uncompressedStream) 109 | for { 110 | header, err := reader.Next() 111 | if err == io.EOF { 112 | break 113 | } 114 | if err != nil { 115 | t.Fatal(err) 116 | } 117 | bundleFiles = append(bundleFiles, header.Name) 118 | } 119 | } 120 | if len(bundleFiles) != len(tc.bundleContent) { 121 | t.Fatalf("expected files in bundle:\n\n%vgot:\n\n%v", tc.bundleContent, bundleFiles) 122 | } 123 | for _, expectedFile := range tc.bundleContent { 124 | found := false 125 | for _, actualFile := range bundleFiles { 126 | // tared file header name is full original source path, for some reason, so we can't do full match 127 | if strings.HasSuffix(actualFile, expectedFile) { 128 | found = true 129 | break 130 | } 131 | } 132 | if !found { 133 | t.Fatalf("expected file %s to be in bundle, but it wasn't", expectedFile) 134 | } 135 | } 136 | }) 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /cmd/depend.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "fmt" 5 | "github.com/johanfylling/odm/printer" 6 | "github.com/johanfylling/odm/proj" 7 | "github.com/spf13/cobra" 8 | "os" 9 | ) 10 | 11 | func init() { 12 | var namespace string 13 | var noNamespace bool 14 | 15 | var depCommand = &cobra.Command{ 16 | Use: "depend [flags]", 17 | Short: "Add a dependency to the project", 18 | Long: `Add a dependency to the project 19 | 20 | Supported location types: 21 | - Git repository: git+http://..., git+https://..., git+ssh://... 22 | - Local file/directory: file://path/to/dir, file:/../path/to/dir 23 | 24 | Example:`, 25 | PreRunE: func(cmd *cobra.Command, args []string) error { 26 | if len(args) < 2 { 27 | return fmt.Errorf("expected exactly one dependency name and one location") 28 | } 29 | return nil 30 | }, 31 | Run: func(cmd *cobra.Command, args []string) { 32 | name := args[0] 33 | location := args[1] 34 | 35 | projPath := "." 36 | 37 | if noNamespace { 38 | namespace = "" 39 | } else if namespace == "" { 40 | namespace = name 41 | } 42 | 43 | if err := doAddDependency(name, location, namespace, projPath); err != nil { 44 | _, _ = cmd.OutOrStderr().Write([]byte(err.Error())) 45 | os.Exit(1) 46 | } 47 | }, 48 | } 49 | 50 | depCommand.Flags().StringVarP(&namespace, "namespace", "n", "", "namespace of the dependency. Ignored if --no-namespace is set") 51 | depCommand.Flags().BoolVar(&noNamespace, "no-namespace", false, "") 52 | 53 | RootCommand.AddCommand(depCommand) 54 | } 55 | 56 | func doAddDependency(name string, location string, namespace string, projectPath string) error { 57 | printer.Trace("--- Dep start ---") 58 | defer printer.Trace("--- Dep end ---") 59 | 60 | var nsInfo string 61 | if namespace == "" { 62 | nsInfo = "no" 63 | } else { 64 | nsInfo = fmt.Sprintf("'%s'", namespace) 65 | } 66 | printer.Info("Setting dependency '%s' @ '%s', with %s namespace", name, location, nsInfo) 67 | 68 | project, err := proj.ReadProjectFromFile(projectPath, false) 69 | if err != nil { 70 | return err 71 | } 72 | 73 | dependency := proj.DependencyInfo{ 74 | Namespace: namespace, 75 | Location: location, 76 | } 77 | 78 | project.SetDependency(name, dependency) 79 | 80 | return project.WriteToFile(projectPath, true) 81 | } 82 | -------------------------------------------------------------------------------- /cmd/eval.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "fmt" 5 | "github.com/johanfylling/odm/printer" 6 | "github.com/johanfylling/odm/proj" 7 | "github.com/johanfylling/odm/utils" 8 | "github.com/spf13/cobra" 9 | "os" 10 | ) 11 | 12 | func init() { 13 | var noUpdate bool 14 | 15 | var evalCommand = &cobra.Command{ 16 | Use: "eval [flags] -- [opa eval flags]", 17 | Short: "Evaluate a Rego query using OPA", 18 | Long: `Evaluate a Rego query using OPA 19 | 20 | Convenience command for running 'opa eval' with project dependencies. 21 | 22 | Example: 23 | 'odm eval -- -d policy.rego "data.main.allow"' is equivalent to running: 24 | 'opa eval -d ./opa/dependencies -d policy.rego "data.main.allow"' 25 | `, 26 | Run: func(cmd *cobra.Command, args []string) { 27 | projPath := "." 28 | 29 | if !noUpdate { 30 | if err := doUpdate(projPath); err != nil { 31 | _, _ = fmt.Fprintf(os.Stderr, "%s\n", err) 32 | os.Exit(1) 33 | } 34 | } 35 | 36 | if err := doEval(projPath, args); err != nil { 37 | _, _ = fmt.Fprintf(os.Stderr, "%s\n", err) 38 | os.Exit(1) 39 | } 40 | }, 41 | } 42 | 43 | addNoUpdateFlag(evalCommand, &noUpdate) 44 | RootCommand.AddCommand(evalCommand) 45 | } 46 | 47 | func doEval(projPath string, args []string) error { 48 | printer.Trace("--- Eval start ---") 49 | defer printer.Trace("--- Eval end ---") 50 | 51 | if len(args) == 0 { 52 | // We're still calling OPA, so it can print its usage message 53 | printer.Info("no OPA flags provided") 54 | } 55 | 56 | project, err := proj.ReadAndLoadProject(projPath, true) 57 | if err != nil { 58 | return err 59 | } 60 | 61 | dataLocations, err := project.DataLocations() 62 | if err != nil { 63 | return fmt.Errorf("error getting data locations: %s", err) 64 | } 65 | 66 | opa := utils.NewOpa(dataLocations...) 67 | if output, err := opa.Eval(args...); err != nil { 68 | return fmt.Errorf("error running opa eval:\n %s", err) 69 | } else { 70 | printer.Output(output) 71 | } 72 | 73 | return nil 74 | } 75 | -------------------------------------------------------------------------------- /cmd/eval_test.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "bytes" 5 | "github.com/johanfylling/odm/printer" 6 | "path/filepath" 7 | "runtime" 8 | "strings" 9 | "testing" 10 | ) 11 | 12 | func TestEvalProjects(t *testing.T) { 13 | _, file, _, _ := runtime.Caller(0) 14 | rootDir := filepath.Dir(file) 15 | 16 | tests := []struct { 17 | name string 18 | projectDir string 19 | query string 20 | expectedOutput string 21 | }{ 22 | { 23 | name: "Empty project", 24 | projectDir: filepath.Join(rootDir, "testdata", "projects", "empty"), 25 | query: "data", 26 | expectedOutput: `{}`, 27 | }, 28 | { 29 | name: "Empty project, call in query", 30 | projectDir: filepath.Join(rootDir, "testdata", "projects", "empty"), 31 | query: "x := 1 + 2", 32 | expectedOutput: `{ 33 | "x": 3 34 | }`, 35 | }, 36 | { 37 | name: "Project with source, no dependencies", 38 | projectDir: filepath.Join(rootDir, "testdata", "projects", "no-dependencies"), 39 | query: "x := data", 40 | expectedOutput: `{ 41 | "x": { 42 | "test": { 43 | "allow": true 44 | } 45 | } 46 | }`, 47 | }, 48 | { 49 | name: "Project with multiple source dirs, no dependencies", 50 | projectDir: filepath.Join(rootDir, "testdata", "projects", "source-list"), 51 | query: "x := data", 52 | expectedOutput: `{ 53 | "x": { 54 | "do": { 55 | "re": { 56 | "mi": "fa" 57 | } 58 | }, 59 | "foo": { 60 | "bar": "baz" 61 | }, 62 | "test": { 63 | "allow": true 64 | } 65 | } 66 | }`, 67 | }, 68 | { 69 | name: "Project with local dependencies", 70 | projectDir: filepath.Join(rootDir, "testdata", "projects", "local-dependencies"), 71 | query: "x := data", 72 | expectedOutput: `{ 73 | "x": { 74 | "no_deps": { 75 | "test": { 76 | "allow": true 77 | } 78 | } 79 | } 80 | }`, 81 | }, 82 | { 83 | name: "Project with transitive dependencies", 84 | projectDir: filepath.Join(rootDir, "testdata", "projects", "transitive-dependencies"), 85 | query: "x := data", 86 | expectedOutput: `{ 87 | "x": { 88 | "bar": { 89 | "no_deps": { 90 | "test": { 91 | "allow": true 92 | } 93 | } 94 | }, 95 | "foo": { 96 | "no_deps": { 97 | "test": { 98 | "allow": true 99 | } 100 | } 101 | }, 102 | "no_deps": { 103 | "test": { 104 | "allow": true 105 | } 106 | } 107 | } 108 | }`, 109 | }, 110 | } 111 | 112 | for _, tc := range tests { 113 | //goland:noinspection GoDeferInLoop 114 | defer cleanup(tc.projectDir) 115 | 116 | t.Run(tc.name, func(t *testing.T) { 117 | output := bytes.Buffer{} 118 | printer.PrintWriter = &output 119 | args := []string{tc.query, "--format", "bindings"} 120 | if err := doUpdate(tc.projectDir); err != nil { 121 | t.Fatal(err) 122 | } 123 | if err := doEval(tc.projectDir, args); err != nil { 124 | t.Fatal(err) 125 | } 126 | if !strings.Contains(output.String(), tc.expectedOutput) { 127 | t.Fatalf("expected output:\n\n%s\n\ngot:\n\n%s", tc.expectedOutput, output.String()) 128 | } 129 | }) 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /cmd/init.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "fmt" 5 | "github.com/johanfylling/odm/printer" 6 | "github.com/johanfylling/odm/proj" 7 | "github.com/johanfylling/odm/utils" 8 | "github.com/spf13/cobra" 9 | "os" 10 | ) 11 | 12 | func init() { 13 | var sourceDir string 14 | var noSource bool 15 | 16 | var initCommand = &cobra.Command{ 17 | Use: "init [name]", 18 | Short: "Initialize a new OPA project", 19 | Run: func(cmd *cobra.Command, args []string) { 20 | path := "." 21 | var name string 22 | if len(args) == 1 { 23 | name = args[0] 24 | path = fmt.Sprintf("./%s", name) 25 | } 26 | if noSource { 27 | sourceDir = "" 28 | } 29 | if err := doInit(path, name, sourceDir); err != nil { 30 | _, _ = fmt.Fprintf(os.Stderr, "%s\n", err) 31 | os.Exit(1) 32 | } 33 | }, 34 | } 35 | 36 | initCommand.Flags().StringVarP(&sourceDir, "source", "s", "src", "source directory for the project. Mutually exclusive with --no-source") 37 | initCommand.Flags().BoolVarP(&noSource, "no-source", "", false, "don't assign a source directory for the project. Mutually exclusive with --source") 38 | 39 | RootCommand.AddCommand(initCommand) 40 | } 41 | 42 | func doInit(path string, name string, sourceDir string) error { 43 | printer.Trace("--- Init start ---") 44 | defer printer.Trace("--- Init end ---") 45 | 46 | project := proj.Project{ 47 | Name: name, 48 | } 49 | 50 | printer.Info("initializing OPA project: %s", name) 51 | 52 | if sourceDir != "" { 53 | project.SourceDirs = []string{sourceDir} 54 | } 55 | 56 | if !utils.FileExists(path) { 57 | if err := os.MkdirAll(path, 0755); err != nil { 58 | return fmt.Errorf("error creating project directory: %s", err) 59 | } 60 | } else { 61 | printer.Debug("directory %s already exists, not creating new\n", path) 62 | } 63 | 64 | err := project.WriteToFile(path, false) 65 | if err != nil { 66 | return err 67 | } 68 | 69 | err = createDotOpaDirectory(path) 70 | if err != nil { 71 | return err 72 | } 73 | 74 | return nil 75 | } 76 | 77 | // create a new .opa directory in working directory 78 | func createDotOpaDirectory(path string) error { 79 | path = path + "/.opa" 80 | 81 | // check if .opa directory already exists 82 | if utils.FileExists(path) { 83 | printer.Debug("directory %s already exists, not creating new\n", path) 84 | return nil 85 | } 86 | 87 | // create directory at path 88 | printer.Debug("creating directory %s\n", path) 89 | err := os.MkdirAll(path, 0755) 90 | if err != nil { 91 | return fmt.Errorf("error creating .opa directory: %s", err) 92 | } 93 | return nil 94 | } 95 | -------------------------------------------------------------------------------- /cmd/list.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "fmt" 5 | "github.com/johanfylling/odm/printer" 6 | "github.com/johanfylling/odm/proj" 7 | "github.com/spf13/cobra" 8 | "os" 9 | "strings" 10 | ) 11 | 12 | func init() { 13 | var noUpdate bool 14 | var includeTestDirs bool 15 | var includeDepTests bool 16 | 17 | var listCommand = &cobra.Command{ 18 | Use: "list", 19 | Short: "List project resources", 20 | } 21 | 22 | addNoUpdateFlag(listCommand, &noUpdate) 23 | RootCommand.AddCommand(listCommand) 24 | 25 | var listSourceCommand = &cobra.Command{ 26 | Use: "source", 27 | Short: "List OPA project source folders", 28 | Run: func(cmd *cobra.Command, args []string) { 29 | projPath := "." 30 | 31 | if !noUpdate { 32 | if err := doUpdate(projPath); err != nil { 33 | _, _ = fmt.Fprintf(os.Stderr, "%s\n", err) 34 | os.Exit(1) 35 | } 36 | } 37 | 38 | if err := doListSource(projPath, includeTestDirs, includeDepTests); err != nil { 39 | _, _ = fmt.Fprintf(os.Stderr, "%s\n", err) 40 | os.Exit(1) 41 | } 42 | }, 43 | } 44 | 45 | listSourceCommand.Flags().BoolVarP(&includeTestDirs, "include-test-dirs", "t", false, "Include test directories in the list") 46 | listSourceCommand.Flags().BoolVar(&includeDepTests, "include-dep-tests", false, "Include dependency tests") 47 | listCommand.AddCommand(listSourceCommand) 48 | } 49 | 50 | func doListSource(projPath string, includeTestDirs, includeDepTests bool) error { 51 | printer.Trace("--- List sources start ---") 52 | defer printer.Trace("--- List sources end ---") 53 | 54 | project, err := proj.ReadAndLoadProject(projPath, true) 55 | if err != nil { 56 | return err 57 | } 58 | 59 | dataLocations, err := project.DataLocations() 60 | if err != nil { 61 | return fmt.Errorf("error getting data locations: %s", err) 62 | } 63 | 64 | if includeTestDirs { 65 | testDataLocations, err := project.TestLocations(includeDepTests) 66 | if err != nil { 67 | return fmt.Errorf("error getting test data locations: %s", err) 68 | } 69 | 70 | dataLocations = append(dataLocations, testDataLocations...) 71 | } 72 | 73 | printer.Output(strings.Join(dataLocations, "\n")) 74 | 75 | return nil 76 | } 77 | -------------------------------------------------------------------------------- /cmd/root.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "github.com/johanfylling/odm/printer" 5 | "github.com/spf13/cobra" 6 | "os" 7 | "path" 8 | ) 9 | 10 | var RootCommand = &cobra.Command{ 11 | Use: path.Base(os.Args[0]), 12 | Short: "OPA Dependency Manager (ODM)", 13 | } 14 | 15 | func init() { 16 | // Add verbose flag to all commands 17 | RootCommand.PersistentFlags().CountVarP(&printer.LogLevel, "verbose", "v", "verbose output") 18 | } 19 | 20 | func addNoUpdateFlag(cmd *cobra.Command, v *bool) { 21 | cmd.Flags().BoolVar(v, "no-update", false, "do not sync dependencies before executing this command") 22 | } 23 | -------------------------------------------------------------------------------- /cmd/test.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "fmt" 5 | "github.com/johanfylling/odm/printer" 6 | "github.com/johanfylling/odm/proj" 7 | "github.com/johanfylling/odm/utils" 8 | "github.com/spf13/cobra" 9 | "os" 10 | ) 11 | 12 | func init() { 13 | var noUpdate bool 14 | var includeDeps bool 15 | 16 | var testCommand = &cobra.Command{ 17 | Use: "test [flags] -- [opa test flags]", 18 | Short: "Run OPA tests", 19 | Long: `Run OPA tests`, 20 | Run: func(cmd *cobra.Command, args []string) { 21 | projPath := "." 22 | 23 | if !noUpdate { 24 | if err := doUpdate(projPath); err != nil { 25 | _, _ = fmt.Fprintf(os.Stderr, "%s\n", err) 26 | os.Exit(1) 27 | } 28 | } 29 | 30 | if err := doTest(projPath, includeDeps, args); err != nil { 31 | _, _ = fmt.Fprintf(os.Stderr, "%s\n", err) 32 | os.Exit(1) 33 | } 34 | }, 35 | } 36 | 37 | testCommand.Flags().BoolVar(&includeDeps, "include-deps", false, "Include dependency tests") 38 | addNoUpdateFlag(testCommand, &noUpdate) 39 | RootCommand.AddCommand(testCommand) 40 | } 41 | 42 | func doTest(projPath string, includeDependencies bool, args []string) error { 43 | printer.Trace("--- Test start ---") 44 | defer printer.Trace("--- Test end ---") 45 | 46 | project, err := proj.ReadAndLoadProject(projPath, true) 47 | if err != nil { 48 | return err 49 | } 50 | 51 | dataLocations, err := project.DataLocations() 52 | if err != nil { 53 | return fmt.Errorf("error getting data locations: %s", err) 54 | } 55 | 56 | testLocations, err := project.TestLocations(includeDependencies) 57 | if err != nil { 58 | return fmt.Errorf("error getting test locations: %s", err) 59 | } 60 | 61 | dataLocations = append(dataLocations, testLocations...) 62 | 63 | opa := utils.NewOpa(dataLocations...) 64 | if output, err := opa.Test(args...); err != nil { 65 | return fmt.Errorf("error running opa test:\n %s", err) 66 | } else { 67 | printer.Output(output) 68 | } 69 | 70 | return nil 71 | } 72 | -------------------------------------------------------------------------------- /cmd/test_test.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "bytes" 5 | "github.com/johanfylling/odm/printer" 6 | "path/filepath" 7 | "regexp" 8 | "runtime" 9 | "strings" 10 | "testing" 11 | ) 12 | 13 | func TestTestProjects(t *testing.T) { 14 | _, file, _, _ := runtime.Caller(0) 15 | rootDir := filepath.Dir(file) 16 | 17 | tests := []struct { 18 | name string 19 | projectDir string 20 | expectedOutput string 21 | }{ 22 | { 23 | name: "Empty project", 24 | projectDir: filepath.Join(rootDir, "testdata", "projects", "empty"), 25 | expectedOutput: ``, 26 | }, 27 | { 28 | name: "Project with source, no dependencies", 29 | projectDir: filepath.Join(rootDir, "testdata", "projects", "no-dependencies"), 30 | expectedOutput: `%ROOT_DIR%/testdata/projects/no-dependencies/tst/tests.rego: 31 | data.test.test_allow: PASS (%TIME%) 32 | -------------------------------------------------------------------------------- 33 | PASS: 1/1 34 | `, 35 | }, 36 | { 37 | name: "Project with multiple source dirs, no dependencies", 38 | projectDir: filepath.Join(rootDir, "testdata", "projects", "source-list"), 39 | expectedOutput: `%ROOT_DIR%/testdata/projects/source-list/test/test.rego: 40 | data.test.test_allow: PASS (%TIME%) 41 | -------------------------------------------------------------------------------- 42 | PASS: 1/1 43 | `, 44 | }, 45 | { 46 | name: "Project with local dependencies", 47 | projectDir: filepath.Join(rootDir, "testdata", "projects", "local-dependencies"), 48 | expectedOutput: `%ROOT_DIR%/testdata/projects/local-dependencies/.opa/dependencies/4310d81b00f2b2cc64a7ecdccb1ec277c4e83c547c0369398aeb0f695a37e37e/tst/tests.rego: 49 | data.no_deps.test.test_allow: PASS (%TIME%) 50 | -------------------------------------------------------------------------------- 51 | PASS: 1/1 52 | `, 53 | }, 54 | { 55 | name: "Project with transitive dependencies", 56 | projectDir: filepath.Join(rootDir, "testdata", "projects", "transitive-dependencies"), 57 | expectedOutput: `%ROOT_DIR%/testdata/projects/transitive-dependencies/.opa/dependencies/4310d81b00f2b2cc64a7ecdccb1ec277c4e83c547c0369398aeb0f695a37e37e/tst/tests.rego: 58 | data.no_deps.test.test_allow: PASS (%TIME%) 59 | 60 | %ROOT_DIR%/testdata/projects/transitive-dependencies/.opa/dependencies/8f8992cd45d31e54855edaef07238cf7a5be7d8225250ea3dd5f821cec3efe2a/tst/tests.rego: 61 | data.foo.no_deps.test.test_allow: PASS (%TIME%) 62 | 63 | %ROOT_DIR%/testdata/projects/transitive-dependencies/.opa/dependencies/b5d22e423449cd9944eaccddc3b302e48d5f1c3634838160fe56f91a5c58407f/tst/tests.rego: 64 | data.bar.no_deps.test.test_allow: PASS (%TIME%) 65 | -------------------------------------------------------------------------------- 66 | PASS: 3/3`, 67 | }, 68 | } 69 | 70 | r := regexp.MustCompile(`(FAIL|PASS) \(.*s\)`) 71 | 72 | for _, tc := range tests { 73 | //goland:noinspection GoDeferInLoop 74 | defer cleanup(tc.projectDir) 75 | 76 | t.Run(tc.name, func(t *testing.T) { 77 | output := bytes.Buffer{} 78 | printer.PrintWriter = &output 79 | args := []string{"-v"} 80 | if err := doUpdate(tc.projectDir); err != nil { 81 | t.Fatal(err) 82 | } 83 | if err := doTest(tc.projectDir, true, args); err != nil { 84 | t.Fatal(err) 85 | } 86 | actual := r.ReplaceAllString(output.String(), "$1 (%TIME%)") 87 | expected := strings.ReplaceAll(tc.expectedOutput, "%ROOT_DIR%", rootDir) 88 | if !strings.Contains(actual, expected) { 89 | t.Fatalf("expected output:\n\n%s\n\ngot:\n\n%s", expected, actual) 90 | } 91 | }) 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /cmd/testdata/projects/empty/opa.project: -------------------------------------------------------------------------------- 1 | name: Empty -------------------------------------------------------------------------------- /cmd/testdata/projects/local-dependencies/opa.project: -------------------------------------------------------------------------------- 1 | name: Local Dependencies 2 | source: src 3 | dependencies: 4 | empty: file:/../empty 5 | no_deps: file:/../no-dependencies -------------------------------------------------------------------------------- /cmd/testdata/projects/no-dependencies/opa.project: -------------------------------------------------------------------------------- 1 | name: No Dependencies 2 | source: src 3 | tests: tst -------------------------------------------------------------------------------- /cmd/testdata/projects/no-dependencies/src/policy.rego: -------------------------------------------------------------------------------- 1 | package test 2 | 3 | allow { 4 | 1 + 1 == 2 5 | } -------------------------------------------------------------------------------- /cmd/testdata/projects/no-dependencies/tst/tests.rego: -------------------------------------------------------------------------------- 1 | package test 2 | 3 | test_allow { 4 | allow 5 | } -------------------------------------------------------------------------------- /cmd/testdata/projects/source-list/data/do/data.yml: -------------------------------------------------------------------------------- 1 | re: 2 | mi: fa -------------------------------------------------------------------------------- /cmd/testdata/projects/source-list/data/foo/data.json: -------------------------------------------------------------------------------- 1 | { 2 | "bar": "baz" 3 | } -------------------------------------------------------------------------------- /cmd/testdata/projects/source-list/opa.project: -------------------------------------------------------------------------------- 1 | name: Source List 2 | source: 3 | - src 4 | - data 5 | tests: test -------------------------------------------------------------------------------- /cmd/testdata/projects/source-list/src/policy.rego: -------------------------------------------------------------------------------- 1 | package test 2 | 3 | allow { 4 | data.foo.bar == "baz" 5 | data.do.re.mi == "fa" 6 | } -------------------------------------------------------------------------------- /cmd/testdata/projects/source-list/test/test.rego: -------------------------------------------------------------------------------- 1 | package test 2 | 3 | test_allow { 4 | allow 5 | } -------------------------------------------------------------------------------- /cmd/testdata/projects/transitive-dependencies/opa.project: -------------------------------------------------------------------------------- 1 | name: Local Dependencies 2 | source: src 3 | dependencies: 4 | foo: file:/../local-dependencies 5 | bar: file:/../local-dependencies 6 | baz: 7 | location: file:/../local-dependencies 8 | namespace: false -------------------------------------------------------------------------------- /cmd/update.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "fmt" 5 | "github.com/johanfylling/odm/printer" 6 | "github.com/johanfylling/odm/proj" 7 | "github.com/johanfylling/odm/utils" 8 | "github.com/spf13/cobra" 9 | "os" 10 | "path/filepath" 11 | ) 12 | 13 | func init() { 14 | var updateCommand = &cobra.Command{ 15 | Use: "update", 16 | Short: "Update OPA project dependencies", 17 | Run: func(cmd *cobra.Command, args []string) { 18 | projPath := "." 19 | 20 | if err := doUpdate(projPath); err != nil { 21 | _, _ = fmt.Fprintf(os.Stderr, "%s\n", err) 22 | os.Exit(1) 23 | } 24 | }, 25 | } 26 | 27 | RootCommand.AddCommand(updateCommand) 28 | } 29 | 30 | func doUpdate(projectPath string) error { 31 | printer.Trace("--- Project update start ---") 32 | defer printer.Trace("--- Project update end ---") 33 | 34 | project, err := proj.ReadProjectFromFile(projectPath, false) 35 | if err != nil { 36 | return err 37 | } 38 | 39 | printer.Info("Updating project '%s'", project.Name) 40 | 41 | dotOpaDir := filepath.Join(project.Dir(), ".opa") 42 | depRootDir := fmt.Sprintf("%s/dependencies", dotOpaDir) 43 | 44 | if !utils.FileExists(dotOpaDir) { 45 | if err := os.Mkdir(dotOpaDir, 0755); err != nil { 46 | return err 47 | } 48 | } 49 | 50 | if err := os.RemoveAll(depRootDir); err != nil { 51 | return err 52 | } 53 | 54 | if err := os.Mkdir(depRootDir, 0755); err != nil { 55 | return err 56 | } 57 | 58 | if err := project.Update(); err != nil { 59 | return err 60 | } 61 | 62 | return nil 63 | } 64 | -------------------------------------------------------------------------------- /cmd/update_test.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "github.com/johanfylling/odm/proj" 5 | "github.com/johanfylling/odm/utils" 6 | "os" 7 | "path/filepath" 8 | "runtime" 9 | "strings" 10 | "testing" 11 | ) 12 | 13 | func TestUpdateProjects(t *testing.T) { 14 | _, file, _, _ := runtime.Caller(0) 15 | rootDir := filepath.Dir(file) 16 | 17 | tests := []struct { 18 | name string 19 | projectDir string 20 | expectedFiles map[string]*string 21 | }{ 22 | { 23 | name: "Empty project", 24 | projectDir: filepath.Join(rootDir, "testdata", "projects", "empty"), 25 | expectedFiles: map[string]*string{ 26 | filepath.Join(rootDir, "testdata", "projects", "empty", ".opa", "dependencies"): nil, 27 | }, 28 | }, 29 | { 30 | name: "Project with source, no dependencies", 31 | projectDir: filepath.Join(rootDir, "testdata", "projects", "no-dependencies"), 32 | expectedFiles: map[string]*string{ 33 | filepath.Join(rootDir, "testdata", "projects", "no-dependencies", ".opa", "dependencies"): nil, 34 | }, 35 | }, 36 | { 37 | name: "Project with multiple source dirs, no dependencies", 38 | projectDir: filepath.Join(rootDir, "testdata", "projects", "source-list"), 39 | expectedFiles: map[string]*string{ 40 | filepath.Join(rootDir, "testdata", "projects", "source-list", ".opa", "dependencies"): nil, 41 | }, 42 | }, 43 | { 44 | name: "Project with local dependencies", 45 | projectDir: filepath.Join(rootDir, "testdata", "projects", "local-dependencies"), 46 | expectedFiles: map[string]*string{ 47 | filepath.Join(rootDir, "testdata", "projects", "local-dependencies", ".opa", "dependencies", proj.DepId("empty", "file:/../empty"), "opa.project"): mustReadFile(rootDir, "testdata", "projects", "empty", "opa.project"), 48 | filepath.Join(rootDir, "testdata", "projects", "local-dependencies", ".opa", "dependencies", proj.DepId("no_deps", "file:/../no-dependencies"), "opa.project"): mustReadFile(rootDir, "testdata", "projects", "no-dependencies", "opa.project"), 49 | filepath.Join(rootDir, "testdata", "projects", "local-dependencies", ".opa", "dependencies", proj.DepId("no_deps", "file:/../no-dependencies"), "src", "policy.rego"): ptr(`package no_deps.test 50 | 51 | allow { 52 | 1 + 1 == 2 53 | } 54 | `), 55 | filepath.Join(rootDir, "testdata", "projects", "local-dependencies", ".opa", "dependencies", proj.DepId("no_deps", "file:/../no-dependencies"), "tst", "tests.rego"): ptr(`package no_deps.test 56 | 57 | test_allow { 58 | allow 59 | } 60 | `), 61 | }, 62 | }, 63 | { 64 | name: "Project with local transitive dependencies", 65 | projectDir: filepath.Join(rootDir, "testdata", "projects", "transitive-dependencies"), 66 | expectedFiles: map[string]*string{ 67 | // foo 68 | filepath.Join(rootDir, "testdata", "projects", "transitive-dependencies", ".opa", "dependencies", proj.DepId("foo", "file:/../local-dependencies"), "opa.project"): mustReadFile(rootDir, "testdata", "projects", "local-dependencies", "opa.project"), 69 | filepath.Join(rootDir, "testdata", "projects", "transitive-dependencies", ".opa", "dependencies", proj.DepId("foo.empty", "file:/../empty"), "opa.project"): mustReadFile(rootDir, "testdata", "projects", "empty", "opa.project"), 70 | filepath.Join(rootDir, "testdata", "projects", "transitive-dependencies", ".opa", "dependencies", proj.DepId("foo.no_deps", "file:/../no-dependencies"), "opa.project"): mustReadFile(rootDir, "testdata", "projects", "no-dependencies", "opa.project"), 71 | filepath.Join(rootDir, "testdata", "projects", "transitive-dependencies", ".opa", "dependencies", proj.DepId("foo.no_deps", "file:/../no-dependencies"), "src", "policy.rego"): ptr(`package foo.no_deps.test 72 | 73 | allow { 74 | 1 + 1 == 2 75 | } 76 | `), 77 | filepath.Join(rootDir, "testdata", "projects", "transitive-dependencies", ".opa", "dependencies", proj.DepId("foo.no_deps", "file:/../no-dependencies"), "tst", "tests.rego"): ptr(`package foo.no_deps.test 78 | 79 | test_allow { 80 | allow 81 | } 82 | `), 83 | // bar 84 | filepath.Join(rootDir, "testdata", "projects", "transitive-dependencies", ".opa", "dependencies", proj.DepId("bar", "file:/../local-dependencies"), "opa.project"): mustReadFile(rootDir, "testdata", "projects", "local-dependencies", "opa.project"), 85 | filepath.Join(rootDir, "testdata", "projects", "transitive-dependencies", ".opa", "dependencies", proj.DepId("bar.empty", "file:/../empty"), "opa.project"): mustReadFile(rootDir, "testdata", "projects", "empty", "opa.project"), 86 | filepath.Join(rootDir, "testdata", "projects", "transitive-dependencies", ".opa", "dependencies", proj.DepId("bar.no_deps", "file:/../no-dependencies"), "opa.project"): mustReadFile(rootDir, "testdata", "projects", "no-dependencies", "opa.project"), 87 | filepath.Join(rootDir, "testdata", "projects", "transitive-dependencies", ".opa", "dependencies", proj.DepId("bar.no_deps", "file:/../no-dependencies"), "src", "policy.rego"): ptr(`package bar.no_deps.test 88 | 89 | allow { 90 | 1 + 1 == 2 91 | } 92 | `), 93 | filepath.Join(rootDir, "testdata", "projects", "transitive-dependencies", ".opa", "dependencies", proj.DepId("bar.no_deps", "file:/../no-dependencies"), "tst", "tests.rego"): ptr(`package bar.no_deps.test 94 | 95 | test_allow { 96 | allow 97 | } 98 | `), 99 | // baz 100 | filepath.Join(rootDir, "testdata", "projects", "transitive-dependencies", ".opa", "dependencies", proj.DepId("", "file:/../local-dependencies"), "opa.project"): mustReadFile(rootDir, "testdata", "projects", "local-dependencies", "opa.project"), 101 | filepath.Join(rootDir, "testdata", "projects", "transitive-dependencies", ".opa", "dependencies", proj.DepId("empty", "file:/../empty"), "opa.project"): mustReadFile(rootDir, "testdata", "projects", "empty", "opa.project"), 102 | filepath.Join(rootDir, "testdata", "projects", "transitive-dependencies", ".opa", "dependencies", proj.DepId("no_deps", "file:/../no-dependencies"), "opa.project"): mustReadFile(rootDir, "testdata", "projects", "no-dependencies", "opa.project"), 103 | filepath.Join(rootDir, "testdata", "projects", "transitive-dependencies", ".opa", "dependencies", proj.DepId("no_deps", "file:/../no-dependencies"), "src", "policy.rego"): ptr(`package no_deps.test 104 | 105 | allow { 106 | 1 + 1 == 2 107 | } 108 | `), 109 | filepath.Join(rootDir, "testdata", "projects", "transitive-dependencies", ".opa", "dependencies", proj.DepId("no_deps", "file:/../no-dependencies"), "tst", "tests.rego"): ptr(`package no_deps.test 110 | 111 | test_allow { 112 | allow 113 | } 114 | `), 115 | }, 116 | }, 117 | } 118 | 119 | for _, tc := range tests { 120 | //goland:noinspection GoDeferInLoop 121 | defer cleanup(tc.projectDir) 122 | 123 | t.Run(tc.name, func(t *testing.T) { 124 | if err := doUpdate(tc.projectDir); err != nil { 125 | t.Fatal(err) 126 | } 127 | 128 | for filePath, expected := range tc.expectedFiles { 129 | if !utils.FileExists(filePath) { 130 | t.Fatalf("expected file '%s' to exist", filePath) 131 | } 132 | 133 | if expected != nil { 134 | b, err := os.ReadFile(filePath) 135 | if err != nil { 136 | t.Fatal(err) 137 | } 138 | if strings.Compare(string(b), *expected) != 0 { 139 | t.Fatalf("expected file '%s' to contain:\n\n%s\n\ngot:\n\n%s", filePath, *expected, string(b)) 140 | } 141 | } else { 142 | children, err := os.ReadDir(filePath) 143 | if err != nil { 144 | t.Fatal(err) 145 | } 146 | if len(children) != 0 { 147 | t.Fatalf("expected dir '%s' to be empty, got %d children", filePath, len(children)) 148 | } 149 | } 150 | } 151 | }) 152 | } 153 | } 154 | 155 | func ptr(s string) *string { 156 | return &s 157 | } 158 | 159 | func readFiles(t *testing.T, paths ...string) map[string]*string { 160 | t.Helper() 161 | files := make(map[string]*string) 162 | for _, path := range paths { 163 | p, v := readFile(t, path) 164 | files[p] = v 165 | } 166 | return files 167 | } 168 | 169 | func readFile(t *testing.T, path string) (string, *string) { 170 | t.Helper() 171 | b, err := os.ReadFile(path) 172 | if err != nil { 173 | t.Fatal(err) 174 | } 175 | s := string(b) 176 | return path, &s 177 | } 178 | 179 | func mustReadFile(path ...string) *string { 180 | p := filepath.Join(path...) 181 | b, err := os.ReadFile(p) 182 | if err != nil { 183 | panic(err) 184 | } 185 | s := string(b) 186 | return &s 187 | } 188 | 189 | func cleanup(projectDir string, files ...string) { 190 | dotOpaDir := filepath.Join(projectDir, ".opa") 191 | _ = os.RemoveAll(dotOpaDir) 192 | 193 | for _, file := range files { 194 | _ = os.RemoveAll(filepath.Join(projectDir, file)) 195 | } 196 | } 197 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/johanfylling/odm 2 | 3 | go 1.20 4 | 5 | require ( 6 | github.com/go-git/go-git/v5 v5.11.0 7 | github.com/spf13/cobra v1.8.0 8 | gopkg.in/yaml.v3 v3.0.1 9 | ) 10 | 11 | require ( 12 | dario.cat/mergo v1.0.0 // indirect 13 | github.com/Microsoft/go-winio v0.6.1 // indirect 14 | github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 // indirect 15 | github.com/cloudflare/circl v1.3.3 // indirect 16 | github.com/cyphar/filepath-securejoin v0.2.4 // indirect 17 | github.com/emirpasic/gods v1.18.1 // indirect 18 | github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect 19 | github.com/go-git/go-billy/v5 v5.5.0 // indirect 20 | github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect 21 | github.com/inconshreveable/mousetrap v1.1.0 // indirect 22 | github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect 23 | github.com/kevinburke/ssh_config v1.2.0 // indirect 24 | github.com/pjbgf/sha1cd v0.3.0 // indirect 25 | github.com/sergi/go-diff v1.1.0 // indirect 26 | github.com/skeema/knownhosts v1.2.1 // indirect 27 | github.com/spf13/pflag v1.0.5 // indirect 28 | github.com/xanzy/ssh-agent v0.3.3 // indirect 29 | golang.org/x/crypto v0.16.0 // indirect 30 | golang.org/x/mod v0.12.0 // indirect 31 | golang.org/x/net v0.19.0 // indirect 32 | golang.org/x/sys v0.15.0 // indirect 33 | golang.org/x/tools v0.13.0 // indirect 34 | gopkg.in/warnings.v0 v0.1.2 // indirect 35 | ) 36 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk= 2 | dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk= 3 | github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY= 4 | github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow= 5 | github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM= 6 | github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 h1:kkhsdkhsCvIsutKu5zLMgWtgh9YxGCNAw8Ad8hjwfYg= 7 | github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0= 8 | github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8= 9 | github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= 10 | github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0= 11 | github.com/cloudflare/circl v1.3.3 h1:fE/Qz0QdIGqeWfnwq0RE0R7MI51s0M2E4Ga9kq5AEMs= 12 | github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA= 13 | github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= 14 | github.com/cyphar/filepath-securejoin v0.2.4 h1:Ugdm7cg7i6ZK6x3xDF1oEu1nfkyfH53EtKeQYTC3kyg= 15 | github.com/cyphar/filepath-securejoin v0.2.4/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4= 16 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 17 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 18 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 19 | github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a h1:mATvB/9r/3gvcejNsXKSkQ6lcIaNec2nyfOdlTBR2lU= 20 | github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= 21 | github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ= 22 | github.com/gliderlabs/ssh v0.3.5 h1:OcaySEmAQJgyYcArR+gGGTHCyE7nvhEMTlYY+Dp8CpY= 23 | github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI= 24 | github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376/go.mod h1:an3vInlBmSxCcxctByoQdvwPiA7DTK7jaaFDBTtu0ic= 25 | github.com/go-git/go-billy/v5 v5.5.0 h1:yEY4yhzCDuMGSv83oGxiBotRzhwhNr8VZyphhiu+mTU= 26 | github.com/go-git/go-billy/v5 v5.5.0/go.mod h1:hmexnoNsr2SJU1Ju67OaNz5ASJY3+sHgFRpCtpDCKow= 27 | github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399 h1:eMje31YglSBqCdIqdhKBW8lokaMrL3uTkpGYlE2OOT4= 28 | github.com/go-git/go-git/v5 v5.11.0 h1:XIZc1p+8YzypNr34itUfSvYJcv+eYdTnTvOZ2vD3cA4= 29 | github.com/go-git/go-git/v5 v5.11.0/go.mod h1:6GFcX2P3NM7FPBfpePbpLd21XxsgdAt+lKqXmCUiUCY= 30 | github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE= 31 | github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= 32 | github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= 33 | github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= 34 | github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= 35 | github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= 36 | github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= 37 | github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4= 38 | github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM= 39 | github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= 40 | github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= 41 | github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= 42 | github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= 43 | github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= 44 | github.com/onsi/gomega v1.27.10 h1:naR28SdDFlqrG6kScpT8VWpu1xWY5nJRCF3XaYyBjhI= 45 | github.com/pjbgf/sha1cd v0.3.0 h1:4D5XXmUUBUl/xQ6IjCkEAbqXskkq/4O7LmGn0AqMDs4= 46 | github.com/pjbgf/sha1cd v0.3.0/go.mod h1:nZ1rrWOcGJ5uZgEEVL1VUM9iRQiZvWdbZjkKyFzPPsI= 47 | github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= 48 | github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 49 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 50 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 51 | github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= 52 | github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= 53 | github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= 54 | github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= 55 | github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= 56 | github.com/skeema/knownhosts v1.2.1 h1:SHWdIUa82uGZz+F+47k8SY4QhhI291cXCpopT1lK2AQ= 57 | github.com/skeema/knownhosts v1.2.1/go.mod h1:xYbVRSPxqBZFrdmDyMmsOs+uX1UZC3nTN3ThzgDxUwo= 58 | github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0= 59 | github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho= 60 | github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= 61 | github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= 62 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 63 | github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= 64 | github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= 65 | github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= 66 | github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM= 67 | github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw= 68 | github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= 69 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 70 | golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= 71 | golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= 72 | golang.org/x/crypto v0.3.1-0.20221117191849-2c476679df9a/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4= 73 | golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU= 74 | golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY= 75 | golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= 76 | golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= 77 | golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= 78 | golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= 79 | golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= 80 | golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= 81 | golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= 82 | golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= 83 | golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= 84 | golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY= 85 | golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= 86 | golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= 87 | golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= 88 | golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= 89 | golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 90 | golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 91 | golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 92 | golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= 93 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 94 | golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 95 | golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 96 | golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 97 | golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 98 | golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 99 | golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 100 | golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 101 | golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 102 | golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 103 | golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 104 | golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 105 | golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 106 | golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= 107 | golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 108 | golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= 109 | golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= 110 | golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc= 111 | golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= 112 | golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U= 113 | golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4= 114 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 115 | golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= 116 | golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= 117 | golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= 118 | golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= 119 | golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= 120 | golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= 121 | golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= 122 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 123 | golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= 124 | golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= 125 | golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= 126 | golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ= 127 | golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= 128 | golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 129 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 130 | gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 131 | gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= 132 | gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME= 133 | gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI= 134 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 135 | gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 136 | gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= 137 | gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 138 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/johanfylling/odm/cmd" 5 | "os" 6 | ) 7 | 8 | func main() { 9 | if err := cmd.RootCommand.Execute(); err != nil { 10 | os.Exit(1) 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /printer/printer.go: -------------------------------------------------------------------------------- 1 | package printer 2 | 3 | import ( 4 | "fmt" 5 | "io" 6 | "os" 7 | ) 8 | 9 | const ( 10 | OutputLevel = iota 11 | InfoLevel 12 | DebugLevel 13 | TraceLevel 14 | ) 15 | 16 | var ( 17 | LogLevel = OutputLevel 18 | PrintWriter io.Writer = os.Stdout 19 | LogWriter io.Writer = os.Stderr 20 | noOpWriter io.Writer = nil 21 | ) 22 | 23 | func out(writer io.Writer, format string, args ...any) { 24 | _, _ = fmt.Fprintf(writer, format+"\n", args...) 25 | } 26 | 27 | func Output(format string, args ...any) { 28 | out(PrintWriter, format, args...) 29 | } 30 | 31 | func Info(format string, args ...any) { 32 | if LogLevel >= InfoLevel { 33 | out(LogWriter, format, args...) 34 | } 35 | } 36 | 37 | func InfoPrinter() io.Writer { 38 | if LogLevel >= InfoLevel { 39 | return LogWriter 40 | } else { 41 | return noOpWriter 42 | } 43 | } 44 | 45 | func Debug(format string, args ...any) { 46 | if LogLevel >= DebugLevel { 47 | out(LogWriter, format, args...) 48 | } 49 | } 50 | 51 | func DebugPrinter() io.Writer { 52 | if LogLevel >= DebugLevel { 53 | return LogWriter 54 | } else { 55 | return noOpWriter 56 | } 57 | } 58 | 59 | func Trace(format string, args ...any) { 60 | if LogLevel >= TraceLevel { 61 | out(LogWriter, format, args...) 62 | } 63 | } 64 | 65 | func TracePrinter() io.Writer { 66 | if LogLevel >= TraceLevel { 67 | return LogWriter 68 | } else { 69 | return noOpWriter 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /proj/project.go: -------------------------------------------------------------------------------- 1 | package proj 2 | 3 | import ( 4 | "crypto/sha256" 5 | "fmt" 6 | "github.com/go-git/go-git/v5" 7 | "github.com/go-git/go-git/v5/plumbing" 8 | "github.com/johanfylling/odm/printer" 9 | "github.com/johanfylling/odm/utils" 10 | "gopkg.in/yaml.v3" 11 | "io" 12 | "os" 13 | "path/filepath" 14 | "strings" 15 | ) 16 | 17 | const ( 18 | dotOpaDir = ".opa" 19 | depDir = "dependencies" 20 | ) 21 | 22 | type Project struct { 23 | Name string `yaml:"name,omitempty"` 24 | Version string `yaml:"version,omitempty"` 25 | SourceDirs []string `yaml:"source,omitempty"` 26 | TestDirs []string `yaml:"tests,omitempty"` 27 | Dependencies Dependencies `yaml:"dependencies,omitempty"` 28 | Build Build `yaml:"build,omitempty"` 29 | filePath string 30 | } 31 | 32 | type ProjectSerialization struct { 33 | Name string `yaml:"name,omitempty"` 34 | Version string `yaml:"version,omitempty"` 35 | Source interface{} `yaml:"source,omitempty"` 36 | Test interface{} `yaml:"tests,omitempty"` 37 | Dependencies Dependencies `yaml:"dependencies,omitempty"` 38 | Build Build `yaml:"build,omitempty"` 39 | } 40 | 41 | type Build struct { 42 | Output string `yaml:"output,omitempty"` 43 | Target string `yaml:"target,omitempty"` 44 | Entrypoints []string `yaml:"entrypoints,omitempty"` 45 | } 46 | 47 | type DependencyInfo struct { 48 | Location string `yaml:"location"` 49 | Namespace string `yaml:"namespace,omitempty"` 50 | } 51 | 52 | type Dependency struct { 53 | DependencyInfo `yaml:",inline"` 54 | Name string `yaml:"-"` 55 | Project *Project `yaml:"-"` 56 | ParentDependency *Dependency `yaml:"-"` 57 | dirPath string `yaml:"-"` 58 | } 59 | 60 | type Dependencies map[string]Dependency 61 | 62 | func NewProject(path string) *Project { 63 | return &Project{ 64 | Dependencies: make(map[string]Dependency), 65 | filePath: path, 66 | } 67 | } 68 | 69 | func (ds *Dependencies) UnmarshalYAML(unmarshal func(interface{}) error) error { 70 | raw := make(map[string]interface{}) 71 | if err := unmarshal(&raw); err != nil { 72 | return err 73 | } 74 | 75 | *ds = make(map[string]Dependency) 76 | for k, v := range raw { 77 | var info DependencyInfo 78 | switch v.(type) { 79 | case string: 80 | info = DependencyInfo{ 81 | Location: v.(string), 82 | Namespace: k, 83 | } 84 | case map[string]interface{}: 85 | var namespace = "" 86 | if ns := v.(map[string]interface{})["namespace"]; ns != nil { 87 | switch ns := ns.(type) { 88 | case bool: 89 | if ns { 90 | namespace = k 91 | } 92 | case string: 93 | namespace = ns 94 | default: 95 | return fmt.Errorf("invalid namespace type: %T", ns) 96 | } 97 | } else { 98 | // If no namespace is specified, default to the dependency name 99 | namespace = k 100 | } 101 | info = DependencyInfo{ 102 | Location: v.(map[string]interface{})["location"].(string), 103 | Namespace: namespace, 104 | } 105 | } 106 | (*ds)[k] = Dependency{ 107 | DependencyInfo: info, 108 | Name: k, 109 | } 110 | } 111 | 112 | return nil 113 | } 114 | 115 | func (ds *Dependencies) MarshalYAML() (interface{}, error) { 116 | depMap := make(map[string]Dependency) 117 | for _, dep := range *ds { 118 | depMap[dep.Location] = dep 119 | } 120 | 121 | return depMap, nil 122 | } 123 | 124 | func (d Dependency) MarshalYAML() (interface{}, error) { 125 | printer.Debug("Marshalling dependency %s", d.Name) 126 | 127 | if d.Namespace == d.Name { 128 | return d.Location, nil 129 | } 130 | 131 | if d.Namespace == "" { 132 | return map[string]interface{}{ 133 | "namespace": false, 134 | "location": d.Location, 135 | }, nil 136 | 137 | } 138 | 139 | return map[string]interface{}{ 140 | "namespace": d.Namespace, 141 | "location": d.Location, 142 | }, nil 143 | } 144 | 145 | func (d Dependency) id() string { 146 | return DepId(d.fullNamespace(), d.Location) 147 | } 148 | 149 | func DepId(namespace, location string) string { 150 | cleartext := fmt.Sprintf("%s:%s", namespace, location) 151 | h := sha256.New() 152 | h.Write([]byte(cleartext)) 153 | return fmt.Sprintf("%x", h.Sum(nil)) 154 | } 155 | 156 | func (d Dependency) dir(rootDir string) string { 157 | return filepath.Join(rootDir, d.id()) 158 | } 159 | 160 | func (d Dependency) Update(rootDir, depsRootDir string) error { 161 | targetDir := d.dir(depsRootDir) 162 | 163 | if err := os.RemoveAll(targetDir); err != nil { 164 | return err 165 | } 166 | 167 | if err := os.MkdirAll(targetDir, 0755); err != nil { 168 | return err 169 | } 170 | 171 | if err := os.MkdirAll(targetDir, 0755); err != nil { 172 | return fmt.Errorf("failed to create destination directory %s: %w", targetDir, err) 173 | } 174 | 175 | if strings.HasPrefix(d.Location, "git+") { 176 | printer.Debug("Updating git dependency %s", d.Namespace) 177 | if err := d.updateGit(targetDir); err != nil { 178 | return err 179 | } 180 | } else if strings.HasPrefix(d.Location, "file:") { 181 | printer.Debug("Updating git dependency %s", d.Namespace) 182 | printer.Debug("Updating transitive dependencies for %s", d.Namespace) 183 | if err := d.updateLocal(rootDir, targetDir); err != nil { 184 | return err 185 | } 186 | } else { 187 | return fmt.Errorf("unsupported dependency location: %s", d.Location) 188 | } 189 | 190 | depProjectFile := fmt.Sprintf("%s/opa.project", targetDir) 191 | if utils.FileExists(depProjectFile) { 192 | var err error 193 | d.Project, err = ReadProjectFromFile(depProjectFile, false) 194 | if err != nil { 195 | return err 196 | } 197 | } 198 | d.dirPath = targetDir 199 | 200 | if err := d.updateTransitive(rootDir, depsRootDir); err != nil { 201 | return fmt.Errorf("failed to update transitive dependencies for %s: %w", d.Namespace, err) 202 | } 203 | 204 | if namespace := d.fullNamespace(); namespace != "" { 205 | var dirs []string 206 | if srcDirs := d.SourceDirs(); len(srcDirs) > 0 { 207 | dirs = append(dirs, srcDirs...) 208 | } else { 209 | dirs = append(dirs, targetDir) 210 | } 211 | dirs = append(dirs, d.TestDirs()...) 212 | dirs = utils.FilterExistingFiles(dirs) 213 | 214 | if len(dirs) > 0 { 215 | opa := utils.NewOpa(dirs...) 216 | if err := opa.Refactor("data", fmt.Sprintf("data.%s", namespace)); err != nil { 217 | return fmt.Errorf("failed to refactor namespace %s: %w", d.Namespace, err) 218 | } 219 | } else { 220 | printer.Debug("Dependency %s has no source, skipping namespace refactoring", d.Name) 221 | } 222 | } 223 | 224 | return nil 225 | } 226 | 227 | func (d Dependency) Load(rootDir, targetDir string) (*Dependency, error) { 228 | targetDir = d.dir(targetDir) 229 | d.dirPath = targetDir 230 | depProjectFile := fmt.Sprintf("%s/opa.project", targetDir) 231 | if utils.FileExists(depProjectFile) { 232 | var err error 233 | d.Project, err = ReadProjectFromFile(depProjectFile, false) 234 | if err != nil { 235 | return nil, err 236 | } 237 | } 238 | if err := d.loadTransitive(rootDir, targetDir); err != nil { 239 | return nil, fmt.Errorf("failed to update transitive dependencies for %s: %w", d.Namespace, err) 240 | } 241 | return &d, nil 242 | } 243 | 244 | func (d Dependency) updateLocal(rootDir, targetDir string) error { 245 | sourceLocation, err := utils.NormalizeFilePath(d.Location) 246 | if err != nil { 247 | return err 248 | } 249 | 250 | if !filepath.IsAbs(sourceLocation) { 251 | sourceLocation = filepath.Join(rootDir, sourceLocation) 252 | } 253 | 254 | if !utils.FileExists(sourceLocation) { 255 | return fmt.Errorf("dependency %s does not exist", sourceLocation) 256 | } 257 | 258 | if !utils.IsDir(sourceLocation) && utils.GetFileName(sourceLocation) == "opa.project" { 259 | sourceLocation = utils.GetParentDir(sourceLocation) 260 | } 261 | 262 | // Ignore empty files, as an empty module will break the 'opa refactor' command 263 | if err := utils.CopyAll(sourceLocation, targetDir, []string{".opa"}, true); err != nil { 264 | return err 265 | } 266 | 267 | return nil 268 | } 269 | 270 | func (d Dependency) updateGit(targetDir string) error { 271 | url, tag, err := parseGitUrl(d.Location) 272 | if err != nil { 273 | return err 274 | } 275 | 276 | repo, err := git.PlainClone(targetDir, false, &git.CloneOptions{ 277 | URL: url, 278 | Progress: printer.DebugPrinter(), 279 | }) 280 | if err != nil { 281 | return fmt.Errorf("failed to clone git repository %s: %w", url, err) 282 | } 283 | 284 | if tag != "" { 285 | w, err := repo.Worktree() 286 | if err != nil { 287 | return fmt.Errorf("failed to get worktree for git repository %s: %w", url, err) 288 | } 289 | 290 | if err := w.Checkout(&git.CheckoutOptions{ 291 | Branch: plumbing.NewTagReferenceName(tag), 292 | }); err != nil { 293 | return fmt.Errorf("failed to checkout tag '%s' for git repository %s: %w", tag, url, err) 294 | } 295 | } else { 296 | printer.Debug("No tag specified, using HEAD") 297 | } 298 | 299 | return nil 300 | } 301 | 302 | func parseGitUrl(fullUrl string) (url string, tag string, err error) { 303 | trimmedUrl := strings.TrimPrefix(fullUrl, "git+") 304 | parts := strings.Split(trimmedUrl, "#") 305 | if len(parts) > 2 { 306 | return "", "", fmt.Errorf("invalid git url %s; only one tag separator '#' allowed", fullUrl) 307 | } 308 | 309 | url = parts[0] 310 | if len(parts) == 2 { 311 | tag = parts[1] 312 | } 313 | return 314 | } 315 | 316 | func (d Dependency) loadTransitive(rootDir, targetDir string) error { 317 | printer.Debug("Loading transitive dependencies for %s (%s)", d.Namespace, d.id()) 318 | 319 | if d.Project != nil { 320 | for i, dep := range d.Project.Dependencies { 321 | if dep, err := dep.Load(rootDir, targetDir); err != nil { 322 | return err 323 | } else { 324 | dep.ParentDependency = &d 325 | d.Project.Dependencies[i] = *dep 326 | } 327 | } 328 | } 329 | 330 | return nil 331 | } 332 | 333 | func (d Dependency) updateTransitive(rootDir, targetDir string) error { 334 | printer.Debug("Updating transitive dependencies for %s (%s)", d.Namespace, d.id()) 335 | 336 | if d.Project != nil { 337 | for name, dep := range d.Project.Dependencies { 338 | dep.ParentDependency = &d 339 | if err := dep.Update(rootDir, targetDir); err != nil { 340 | return err 341 | } 342 | d.Project.Dependencies[name] = dep 343 | } 344 | } 345 | 346 | return nil 347 | } 348 | 349 | func (d Dependency) fullNamespace() string { 350 | if d.ParentDependency != nil && d.ParentDependency.Namespace != "" { 351 | if parentNamespace := d.ParentDependency.fullNamespace(); parentNamespace != "" { 352 | if d.Namespace == "" { 353 | return parentNamespace 354 | } 355 | return fmt.Sprintf("%s.%s", parentNamespace, d.Namespace) 356 | } 357 | } 358 | return d.Namespace 359 | } 360 | 361 | func (d Dependency) SourceDirs() []string { 362 | if d.Project != nil && len(d.Project.SourceDirs) > 0 { 363 | dirs := make([]string, 0, len(d.Project.SourceDirs)) 364 | for _, dir := range d.Project.SourceDirs { 365 | dirs = append(dirs, filepath.Join(d.dirPath, dir)) 366 | } 367 | return dirs 368 | } 369 | return []string{d.dirPath} 370 | } 371 | 372 | func (d Dependency) TestDirs() []string { 373 | if d.Project != nil && len(d.Project.TestDirs) > 0 { 374 | dirs := make([]string, 0, len(d.Project.TestDirs)) 375 | for _, dir := range d.Project.TestDirs { 376 | dirs = append(dirs, filepath.Join(d.dirPath, dir)) 377 | } 378 | return dirs 379 | } 380 | return []string{} 381 | } 382 | 383 | func (p *Project) UnmarshalYAML(unmarshal func(interface{}) error) error { 384 | var raw ProjectSerialization 385 | if err := unmarshal(&raw); err != nil { 386 | return err 387 | } 388 | 389 | p.Name = raw.Name 390 | p.Version = raw.Version 391 | p.Dependencies = raw.Dependencies 392 | p.Build = raw.Build 393 | 394 | var err error 395 | p.SourceDirs, err = unmarshalDirs(raw.Source) 396 | if err != nil { 397 | return fmt.Errorf("invalid source: %w", err) 398 | } 399 | 400 | p.TestDirs, err = unmarshalDirs(raw.Test) 401 | if err != nil { 402 | return fmt.Errorf("invalid tests: %w", err) 403 | } 404 | 405 | return nil 406 | } 407 | 408 | func (p Project) MarshalYAML() (interface{}, error) { 409 | var raw ProjectSerialization 410 | raw.Name = p.Name 411 | raw.Version = p.Version 412 | raw.Dependencies = p.Dependencies 413 | raw.Build = p.Build 414 | if len(p.SourceDirs) == 1 { 415 | raw.Source = p.SourceDirs[0] 416 | } else if len(p.SourceDirs) > 1 { 417 | raw.Source = p.SourceDirs 418 | } 419 | if len(p.TestDirs) == 1 { 420 | raw.Test = p.TestDirs[0] 421 | } else if len(p.TestDirs) > 1 { 422 | raw.Test = p.TestDirs 423 | } 424 | return raw, nil 425 | } 426 | 427 | func unmarshalDirs(raw interface{}) ([]string, error) { 428 | switch t := raw.(type) { 429 | case nil: 430 | return nil, nil 431 | case string: 432 | return []string{t}, nil 433 | case []string: 434 | return t, nil 435 | case []interface{}: 436 | dirs := make([]string, len(t)) 437 | for i, v := range t { 438 | var ok bool 439 | if dirs[i], ok = v.(string); !ok { 440 | return nil, fmt.Errorf("invalid dir type %T", v) 441 | } 442 | } 443 | return dirs, nil 444 | default: 445 | return nil, fmt.Errorf("invalid dir type %T", t) 446 | } 447 | } 448 | 449 | func (p *Project) SetDependency(name string, info DependencyInfo) { 450 | if p.Dependencies == nil { 451 | p.Dependencies = make(map[string]Dependency) 452 | } 453 | p.Dependencies[name] = Dependency{ 454 | DependencyInfo: info, 455 | Name: name, 456 | } 457 | } 458 | 459 | func ReadProjectFromFile(path string, allowMissing bool) (*Project, error) { 460 | path = normalizeProjectPath(path) 461 | 462 | if !utils.FileExists(path) { 463 | if allowMissing { 464 | return NewProject(path), nil 465 | } else { 466 | return nil, fmt.Errorf("project file %s does not exist", path) 467 | } 468 | } 469 | 470 | data, err := os.ReadFile(path) 471 | if err != nil { 472 | return nil, fmt.Errorf("failed to read project file %s: %w", path, err) 473 | } 474 | 475 | var project Project 476 | err = yaml.Unmarshal(data, &project) 477 | if err != nil { 478 | return nil, fmt.Errorf("failed to unmarshal project file %s: %w", path, err) 479 | } 480 | 481 | project.filePath = path 482 | 483 | return &project, nil 484 | } 485 | 486 | func ReadAndLoadProject(path string, allowMissing bool) (*Project, error) { 487 | project, err := ReadProjectFromFile(path, allowMissing) 488 | if err != nil { 489 | return nil, err 490 | } 491 | 492 | if err := project.Load(); err != nil { 493 | return nil, err 494 | } 495 | 496 | return project, nil 497 | } 498 | 499 | func (p *Project) Update() error { 500 | rootDir := filepath.Dir(p.filePath) 501 | return p.update(rootDir) 502 | } 503 | 504 | func (p *Project) update(rootDir string) error { 505 | depRootDir := dependenciesDir(rootDir) 506 | 507 | for name, dep := range p.Dependencies { 508 | if err := dep.Update(rootDir, depRootDir); err != nil { 509 | return fmt.Errorf("failed to update dependency %s: %w", name, err) 510 | } 511 | p.Dependencies[name] = dep 512 | } 513 | 514 | return nil 515 | } 516 | 517 | func (p *Project) Load() error { 518 | rootDir := filepath.Dir(p.filePath) 519 | return p.load(rootDir) 520 | } 521 | 522 | func (p *Project) load(rootDir string) error { 523 | depRootDir := dependenciesDir(rootDir) 524 | 525 | for name, dep := range p.Dependencies { 526 | // Load, don't update dependencies, this is done separately 527 | if loadedDep, err := dep.Load(rootDir, depRootDir); err != nil { 528 | return fmt.Errorf("failed to load dependency %s: %w", name, err) 529 | } else { 530 | dep = *loadedDep 531 | } 532 | if dep.Project != nil { 533 | if err := dep.Project.load(rootDir); err != nil { 534 | return fmt.Errorf("failed loading dependency project: %w", err) 535 | } 536 | } 537 | p.Dependencies[name] = dep 538 | } 539 | 540 | return nil 541 | } 542 | 543 | func (p *Project) DataLocations() ([]string, error) { 544 | var dataLocations []string 545 | projDir := filepath.Dir(p.filePath) 546 | if len(p.SourceDirs) > 0 { 547 | for _, dir := range p.SourceDirs { 548 | if dir, err := utils.NormalizeFilePath(dir); err != nil { 549 | return nil, err 550 | } else { 551 | dataLocations = append(dataLocations, filepath.Join(projDir, dir)) 552 | } 553 | } 554 | } else { 555 | dataLocations = append(dataLocations, projDir) 556 | } 557 | 558 | err := WalkDependencies(p, func(dep Dependency) error { 559 | dataLocations = append(dataLocations, dep.SourceDirs()...) 560 | return nil 561 | }) 562 | if err != nil { 563 | return nil, err 564 | } 565 | 566 | dataLocations = utils.FilterExistingFiles(dataLocations) 567 | 568 | return dataLocations, nil 569 | } 570 | 571 | func (p *Project) TestLocations(includeDependencies bool) ([]string, error) { 572 | var testLocations []string 573 | projDir := filepath.Dir(p.filePath) 574 | if len(p.TestDirs) > 0 { 575 | for _, dir := range p.TestDirs { 576 | if dir, err := utils.NormalizeFilePath(dir); err != nil { 577 | return nil, err 578 | } else { 579 | testLocations = append(testLocations, filepath.Join(projDir, dir)) 580 | } 581 | } 582 | } 583 | 584 | if includeDependencies { 585 | err := WalkDependencies(p, func(dep Dependency) error { 586 | testLocations = append(testLocations, dep.TestDirs()...) 587 | return nil 588 | }) 589 | if err != nil { 590 | return nil, err 591 | } 592 | } 593 | 594 | return testLocations, nil 595 | } 596 | 597 | func (p *Project) WriteToFile(path string, override bool) error { 598 | path = normalizeProjectPath(path) 599 | printer.Debug("Writing project file to %s", path) 600 | 601 | if !override && utils.FileExists(path) { 602 | return fmt.Errorf("project file %s already exists", path) 603 | } 604 | 605 | data, err := yaml.Marshal(p) 606 | if err != nil { 607 | return fmt.Errorf("failed to marshal project file %s: %w", path, err) 608 | } 609 | 610 | err = os.WriteFile(path, data, 0644) 611 | if err != nil { 612 | return fmt.Errorf("failed to write project file %s: %w", path, err) 613 | } 614 | 615 | return nil 616 | } 617 | 618 | func (p *Project) PrintTree(w io.Writer) error { 619 | if err := p.printTree(w, "root", 0); err != nil { 620 | return err 621 | } 622 | return nil 623 | } 624 | 625 | func (p *Project) printTree(w io.Writer, name string, indent int) error { 626 | indentStr := strings.Repeat(" ", indent*2) 627 | if p == nil { 628 | _, err := fmt.Fprintf(w, "%s%s\n", indentStr, name) 629 | return err 630 | } 631 | 632 | if len(p.Name) > 0 { 633 | if _, err := fmt.Fprintf(w, "%s%s (%s)\n", indentStr, name, p.Name); err != nil { 634 | return err 635 | } 636 | } else { 637 | if _, err := fmt.Fprintf(w, "%s%s\n", indentStr, name); err != nil { 638 | return err 639 | } 640 | } 641 | for _, dep := range p.Dependencies { 642 | if err := dep.Project.printTree(w, dep.Name, indent+1); err != nil { 643 | return err 644 | } 645 | } 646 | return nil 647 | } 648 | 649 | func (p *Project) Dir() string { 650 | return filepath.Dir(p.filePath) 651 | } 652 | 653 | func normalizeProjectPath(path string) string { 654 | l := len(path) 655 | if l >= 11 && path[l-11:] == "opa.project" { 656 | return path 657 | } else if l >= 1 && path[l-1] == '/' { 658 | return path + "opa.project" 659 | } else { 660 | return path + "/opa.project" 661 | } 662 | } 663 | 664 | func dependenciesDir(root string) string { 665 | return filepath.Join(root, dotOpaDir, depDir) 666 | } 667 | 668 | func WalkDependencies(p *Project, f func(Dependency) error) error { 669 | if p == nil { 670 | return nil 671 | } 672 | 673 | for _, dep := range p.Dependencies { 674 | if err := f(dep); err != nil { 675 | return err 676 | } 677 | if dep.Project != nil { 678 | if err := WalkDependencies(dep.Project, f); err != nil { 679 | return err 680 | } 681 | } 682 | } 683 | 684 | return nil 685 | } 686 | -------------------------------------------------------------------------------- /proj/project_test.go: -------------------------------------------------------------------------------- 1 | package proj 2 | 3 | import ( 4 | "fmt" 5 | "gopkg.in/yaml.v3" 6 | "os" 7 | "path/filepath" 8 | "reflect" 9 | "testing" 10 | ) 11 | 12 | func TestMarshalProject(t *testing.T) { 13 | tests := []struct { 14 | note string 15 | project *Project 16 | expected string 17 | }{ 18 | { 19 | note: "no dependencies", 20 | project: &Project{ 21 | Name: "test_project", 22 | Version: "0.0.1", 23 | SourceDirs: []string{"src"}, 24 | }, 25 | expected: `name: test_project 26 | version: 0.0.1 27 | source: src 28 | `, 29 | }, 30 | { 31 | note: "file dependency with only name & location", 32 | project: &Project{ 33 | Name: "test_project", 34 | Version: "0.0.1", 35 | SourceDirs: []string{"src"}, 36 | Dependencies: Dependencies{ 37 | "foo": Dependency{ 38 | Name: "foo", 39 | DependencyInfo: DependencyInfo{ 40 | Location: "file://dev/null", 41 | Namespace: "foo", 42 | }, 43 | }, 44 | }, 45 | }, 46 | expected: `name: test_project 47 | version: 0.0.1 48 | source: src 49 | dependencies: 50 | foo: file://dev/null 51 | `, 52 | }, 53 | { 54 | note: "file dependency with no namespace", 55 | project: &Project{ 56 | Name: "test_project", 57 | Version: "0.0.1", 58 | SourceDirs: []string{"src"}, 59 | Dependencies: Dependencies{ 60 | "foo": Dependency{ 61 | Name: "foo", 62 | DependencyInfo: DependencyInfo{ 63 | Location: "file://dev/null", 64 | Namespace: "", 65 | }, 66 | }, 67 | }, 68 | }, 69 | expected: `name: test_project 70 | version: 0.0.1 71 | source: src 72 | dependencies: 73 | foo: 74 | location: file://dev/null 75 | namespace: false 76 | `, 77 | }, 78 | { 79 | note: "file dependency with named namespace", 80 | project: &Project{ 81 | Name: "test_project", 82 | Version: "0.0.1", 83 | SourceDirs: []string{"src"}, 84 | Dependencies: Dependencies{ 85 | "foo": Dependency{ 86 | Name: "foo", 87 | DependencyInfo: DependencyInfo{ 88 | Location: "file://dev/null", 89 | Namespace: "bar", 90 | }, 91 | }, 92 | }, 93 | }, 94 | expected: `name: test_project 95 | version: 0.0.1 96 | source: src 97 | dependencies: 98 | foo: 99 | location: file://dev/null 100 | namespace: bar 101 | `, 102 | }, 103 | { 104 | note: "git dependency with only name & location", 105 | project: &Project{ 106 | Name: "test_project", 107 | Version: "0.0.1", 108 | SourceDirs: []string{"src"}, 109 | Dependencies: Dependencies{ 110 | "foo": Dependency{ 111 | Name: "foo", 112 | DependencyInfo: DependencyInfo{ 113 | Location: "git+https://example.com/my/repo", 114 | Namespace: "foo", 115 | }, 116 | }, 117 | }, 118 | }, 119 | expected: `name: test_project 120 | version: 0.0.1 121 | source: src 122 | dependencies: 123 | foo: git+https://example.com/my/repo 124 | `, 125 | }, 126 | } 127 | 128 | for _, test := range tests { 129 | t.Run(test.note, func(t *testing.T) { 130 | bs, err := yaml.Marshal(test.project) 131 | if err != nil { 132 | t.Fatal(err) 133 | } 134 | 135 | if string(bs) != test.expected { 136 | t.Fatalf("Expected %v but got %v", test.expected, string(bs)) 137 | } 138 | }) 139 | } 140 | } 141 | 142 | func TestUnmarshalProject(t *testing.T) { 143 | tests := []struct { 144 | note string 145 | input string 146 | expected *Project 147 | }{ 148 | { 149 | note: "no dependencies", 150 | input: `name: test_project 151 | version: 0.0.1 152 | source: src 153 | `, 154 | expected: &Project{ 155 | Name: "test_project", 156 | Version: "0.0.1", 157 | SourceDirs: []string{"src"}, 158 | }, 159 | }, 160 | { 161 | note: "file dependency with only simplified name & location", 162 | input: `name: test_project 163 | version: 0.0.1 164 | source: src 165 | dependencies: 166 | foo: file://dev/null 167 | `, 168 | expected: &Project{ 169 | Name: "test_project", 170 | Version: "0.0.1", 171 | SourceDirs: []string{"src"}, 172 | 173 | Dependencies: Dependencies{ 174 | "foo": Dependency{ 175 | Name: "foo", 176 | DependencyInfo: DependencyInfo{ 177 | Location: "file://dev/null", 178 | Namespace: "foo", 179 | }, 180 | }, 181 | }, 182 | }, 183 | }, 184 | { 185 | note: "file dependency with only name & location", 186 | input: `name: test_project 187 | version: 0.0.1 188 | source: src 189 | dependencies: 190 | foo: 191 | location: file://dev/null 192 | `, 193 | expected: &Project{ 194 | Name: "test_project", 195 | Version: "0.0.1", 196 | SourceDirs: []string{"src"}, 197 | 198 | Dependencies: Dependencies{ 199 | "foo": Dependency{ 200 | Name: "foo", 201 | DependencyInfo: DependencyInfo{ 202 | Location: "file://dev/null", 203 | Namespace: "foo", 204 | }, 205 | }, 206 | }, 207 | }, 208 | }, 209 | { 210 | note: "file dependency with no namespace", 211 | input: `name: test_project 212 | version: 0.0.1 213 | source: src 214 | dependencies: 215 | foo: 216 | location: file://dev/null 217 | namespace: false 218 | `, 219 | expected: &Project{ 220 | Name: "test_project", 221 | Version: "0.0.1", 222 | SourceDirs: []string{"src"}, 223 | Dependencies: Dependencies{ 224 | "foo": Dependency{ 225 | Name: "foo", 226 | DependencyInfo: DependencyInfo{ 227 | Location: "file://dev/null", 228 | Namespace: "", 229 | }, 230 | }, 231 | }, 232 | }, 233 | }, 234 | { 235 | note: "file dependency with named namespace", 236 | input: `name: test_project 237 | version: 0.0.1 238 | source: src 239 | dependencies: 240 | foo: 241 | location: file://dev/null 242 | namespace: bar 243 | `, 244 | expected: &Project{ 245 | Name: "test_project", 246 | Version: "0.0.1", 247 | SourceDirs: []string{"src"}, 248 | Dependencies: Dependencies{ 249 | "foo": Dependency{ 250 | Name: "foo", 251 | DependencyInfo: DependencyInfo{ 252 | Location: "file://dev/null", 253 | Namespace: "bar", 254 | }, 255 | }, 256 | }, 257 | }, 258 | }, 259 | { 260 | note: "git dependency with only name & location", 261 | input: `name: test_project 262 | version: 0.0.1 263 | source: src 264 | dependencies: 265 | foo: git+https://example.com/my/repo 266 | `, 267 | expected: &Project{ 268 | Name: "test_project", 269 | Version: "0.0.1", 270 | SourceDirs: []string{"src"}, 271 | Dependencies: Dependencies{ 272 | "foo": Dependency{ 273 | Name: "foo", 274 | DependencyInfo: DependencyInfo{ 275 | Location: "git+https://example.com/my/repo", 276 | Namespace: "foo", 277 | }, 278 | }, 279 | }, 280 | }, 281 | }, 282 | } 283 | 284 | for _, test := range tests { 285 | t.Run(test.note, func(t *testing.T) { 286 | var project Project 287 | err := yaml.Unmarshal([]byte(test.input), &project) 288 | if err != nil { 289 | t.Fatal(err) 290 | } 291 | 292 | if !reflect.DeepEqual(&project, test.expected) { 293 | t.Fatalf("Expected %v but got %v", test.expected, project) 294 | } 295 | }) 296 | } 297 | } 298 | 299 | // proj 300 | // +-- dep_a (no opa.project) 301 | // +-- dep_b (opa.project, no source) 302 | // | +-- dep_b1 (no opa.project) 303 | // | +-- dep_b2 (opa.project, source) 304 | // +-- dep_c (opa.project, source) 305 | // . +-- dep_c1 (opa.project, no source) 306 | // . +-- dep_c2 (opa.project, source) 307 | func TestReadProjectFromFile(t *testing.T) { 308 | depA := DepId("dep_a", "file://dep_a") 309 | depB := DepId("dep_b", "file://dep_b") 310 | depB1 := DepId("dep_b.dep_b1", "file://dep_b1") 311 | depB2 := DepId("dep_b.dep_b2", "file://dep_b2") 312 | depC := DepId("dep_c", "file://dep_c") 313 | depC1 := DepId("dep_c.dep_c1", "file://dep_c1") 314 | depC2 := DepId("dep_c.dep_c2", "file://dep_c2") 315 | 316 | files := map[string]string{ 317 | "opa.project": `name: proj 318 | source: src 319 | dependencies: 320 | dep_a: file://dep_a 321 | dep_b: file://dep_b 322 | dep_c: file://dep_c 323 | `, 324 | "src/policy.rego": `package test`, 325 | filepath.Join(".opa", "dependencies", depA, "policy.rego"): `package dep_a`, 326 | filepath.Join(".opa", "dependencies", depB, "opa.project"): `name: dep_b 327 | dependencies: 328 | dep_b1: file://dep_b1 329 | dep_b2: file://dep_b2`, 330 | filepath.Join(".opa", "dependencies", depB1, "policy.rego"): `package dep_b1`, 331 | filepath.Join(".opa", "dependencies", depB2, "opa.project"): `name: dep_b2 332 | source: foo`, 333 | filepath.Join(".opa", "dependencies", depB2, "foo", "policy.rego"): `package dep_b2`, 334 | filepath.Join(".opa", "dependencies", depC, "opa.project"): `name: dep_c 335 | source: bar 336 | dependencies: 337 | dep_c1: file://dep_c1 338 | dep_c2: file://dep_c2`, 339 | filepath.Join(".opa", "dependencies", depC, "bar", "policy.rego"): `package dep_c`, 340 | filepath.Join(".opa", "dependencies", depC1, "opa.project"): `name: dep_c1`, 341 | filepath.Join(".opa", "dependencies", depC1, "policy.rego"): `package dep_c1`, 342 | filepath.Join(".opa", "dependencies", depC2, "opa.project"): `name: dep_c2 343 | source: baz`, 344 | filepath.Join(".opa", "dependencies", depC2, "baz", "policy.rego"): `package dep_c2`, 345 | } 346 | err := withTempFiles(files, func(path string) { 347 | fmt.Println(path) 348 | project, err := ReadProjectFromFile(path, false) 349 | if err != nil { 350 | t.Fatal(err) 351 | } 352 | if err := project.Load(); err != nil { 353 | t.Fatal(err) 354 | } 355 | dataLocations, err := project.DataLocations() 356 | if err != nil { 357 | t.Fatal(err) 358 | } 359 | 360 | _ = project.PrintTree(os.Stdout) 361 | 362 | expected := []string{ 363 | filepath.Join(path, "src"), 364 | filepath.Join(path, ".opa", "dependencies", depA), 365 | filepath.Join(path, ".opa", "dependencies", depB), 366 | filepath.Join(path, ".opa", "dependencies", depB1), 367 | filepath.Join(path, ".opa", "dependencies", depB2, "foo"), 368 | filepath.Join(path, ".opa", "dependencies", depC, "bar"), 369 | filepath.Join(path, ".opa", "dependencies", depC1), 370 | filepath.Join(path, ".opa", "dependencies", depC2, "baz"), 371 | } 372 | 373 | if len(dataLocations) != len(expected) { 374 | t.Fatalf("Expected\n\n%v\n\nbut got\n\n%v", expected, dataLocations) 375 | } 376 | for _, e := range expected { 377 | found := false 378 | for _, d := range dataLocations { 379 | if e == d { 380 | found = true 381 | break 382 | } 383 | } 384 | if !found { 385 | t.Fatalf("Expected\n\n%v\n\nbut got\n\n%v", expected, dataLocations) 386 | } 387 | } 388 | }) 389 | if err != nil { 390 | t.Fatal(err) 391 | } 392 | } 393 | 394 | func withTempFiles(files map[string]string, f func(string)) error { 395 | root, err := os.MkdirTemp("", "test-") 396 | if err != nil { 397 | return err 398 | } 399 | 400 | cleanup := func() { 401 | _ = os.RemoveAll(root) 402 | } 403 | defer cleanup() 404 | 405 | for path, content := range files { 406 | dir := filepath.Dir(path) 407 | if err := os.MkdirAll(filepath.Join(root, dir), 0755); err != nil { 408 | return err 409 | } 410 | if err := os.WriteFile(filepath.Join(root, path), []byte(content), 0644); err != nil { 411 | return err 412 | } 413 | } 414 | 415 | f(root) 416 | return nil 417 | } 418 | -------------------------------------------------------------------------------- /utils/opa.go: -------------------------------------------------------------------------------- 1 | package utils 2 | 3 | import ( 4 | "fmt" 5 | "github.com/johanfylling/odm/printer" 6 | "os" 7 | ) 8 | 9 | type Opa struct { 10 | location string 11 | dataLocations []string 12 | entrypoints []string 13 | target string 14 | } 15 | 16 | func NewOpa(dataLocations ...string) *Opa { 17 | location, ok := os.LookupEnv("OPA_PATH") 18 | if !ok { 19 | location = "opa" 20 | } 21 | 22 | printer.Debug("Creating OPA instance\nlocation: %s\ndata: %v", location, dataLocations) 23 | 24 | return &Opa{ 25 | location: location, 26 | dataLocations: dataLocations, 27 | } 28 | } 29 | 30 | func (o *Opa) WithEntrypoints(entrypoints []string) *Opa { 31 | cpy := *o 32 | cpy.entrypoints = entrypoints 33 | return &cpy 34 | } 35 | 36 | func (o *Opa) WithTarget(target string) *Opa { 37 | cpy := *o 38 | cpy.target = target 39 | return &cpy 40 | } 41 | 42 | func (o *Opa) Eval(passThroughArgs ...string) (string, error) { 43 | printer.Info("Running OPA eval") 44 | 45 | opaArgs := make([]string, 0, 1+2*len(o.dataLocations)+len(passThroughArgs)) 46 | opaArgs = append(opaArgs, "eval") 47 | 48 | for _, location := range o.dataLocations { 49 | opaArgs = append(opaArgs, "-d", location) 50 | } 51 | opaArgs = append(opaArgs, passThroughArgs...) 52 | 53 | return RunCommand(o.location, opaArgs...) 54 | } 55 | 56 | func (o *Opa) Test(passThroughArgs ...string) (string, error) { 57 | printer.Info("Running OPA test") 58 | 59 | opaArgs := make([]string, 0, 1+len(o.dataLocations)+len(passThroughArgs)) 60 | opaArgs = append(opaArgs, "test") 61 | 62 | for _, location := range o.dataLocations { 63 | opaArgs = append(opaArgs, location) 64 | } 65 | opaArgs = append(opaArgs, passThroughArgs...) 66 | 67 | return RunCommand(o.location, opaArgs...) 68 | } 69 | 70 | func (o *Opa) Build(outputPath string, passThroughFlags ...string) (string, error) { 71 | printer.Info("Running OPA build") 72 | printer.Debug("Output bundle path: %s", outputPath) 73 | 74 | opaArgs := prefixEntrypoints(o.entrypoints, passThroughFlags) 75 | opaArgs = prefixOutput(outputPath, opaArgs) 76 | opaArgs = prefixTarget(o.target, opaArgs) 77 | // locations must be first in the list of arguments, so prefixed last 78 | opaArgs = prefixDataLocations(o.dataLocations, opaArgs, false) 79 | 80 | return runOpaCommand(o.location, "build", opaArgs...) 81 | } 82 | 83 | func (o *Opa) Refactor(fromPackage, toPackage string) error { 84 | printer.Info("Running OPA refactor") 85 | printer.Debug("From package: %s", fromPackage) 86 | printer.Debug("To package: %s", toPackage) 87 | 88 | mapping := fmt.Sprintf("%s:%s", fromPackage, toPackage) 89 | 90 | opaArgs := make([]string, 0, 4+len(o.dataLocations)) 91 | opaArgs = append(opaArgs, "move") 92 | opaArgs = append(opaArgs, o.dataLocations...) 93 | opaArgs = append(opaArgs, "-w", "-p", mapping) 94 | 95 | _, err := runOpaCommand(o.location, "refactor", opaArgs...) 96 | return err 97 | } 98 | 99 | func runOpaCommand(opaLocation string, command string, flags ...string) (string, error) { 100 | opaArgs := make([]string, 0, 1+len(flags)) 101 | opaArgs = append(opaArgs, command) 102 | opaArgs = append(opaArgs, flags...) 103 | 104 | return RunCommand(opaLocation, opaArgs...) 105 | } 106 | 107 | func prefixDataLocations(dataLocations []string, flags []string, namedFlag bool) []string { 108 | multiplier := 1 109 | if namedFlag { 110 | multiplier = 2 111 | } 112 | 113 | newFlags := make([]string, 0, len(dataLocations)*multiplier+len(flags)) 114 | for _, location := range dataLocations { 115 | if namedFlag { 116 | newFlags = append(newFlags, "-d", location) 117 | } else { 118 | newFlags = append(newFlags, location) 119 | } 120 | } 121 | 122 | return append(newFlags, flags...) 123 | } 124 | 125 | func prefixEntrypoints(entrypoints []string, flags []string) []string { 126 | newFlags := make([]string, 0, len(entrypoints)*2+len(flags)) 127 | for _, entrypoint := range entrypoints { 128 | newFlags = append(newFlags, "-e", entrypoint) 129 | } 130 | 131 | return append(newFlags, flags...) 132 | } 133 | 134 | func prefixOutput(outputPath string, flags []string) []string { 135 | newFlags := make([]string, 0, 2+len(flags)) 136 | if !Contains(flags, "-o") && !Contains(flags, "--output") { 137 | newFlags = append(newFlags, "-o", outputPath) 138 | } else if outputPath != "" { 139 | printer.Debug("Output path present on pass-through flags to OPA, ignoring configured output path") 140 | } 141 | 142 | return append(newFlags, flags...) 143 | } 144 | 145 | func prefixTarget(target string, flags []string) []string { 146 | if target == "" { 147 | return flags 148 | } 149 | newFlags := make([]string, 0, 2+len(flags)) 150 | if !Contains(flags, "-t") && !Contains(flags, "--target") { 151 | newFlags = append(newFlags, "-t", target) 152 | } else if target != "" { 153 | printer.Debug("Target present on pass-through flags to OPA, ignoring configured target") 154 | } 155 | 156 | return append(newFlags, flags...) 157 | } 158 | -------------------------------------------------------------------------------- /utils/utils.go: -------------------------------------------------------------------------------- 1 | package utils 2 | 3 | import ( 4 | "bytes" 5 | "fmt" 6 | "github.com/johanfylling/odm/printer" 7 | "net/url" 8 | "os" 9 | "os/exec" 10 | "path/filepath" 11 | "strings" 12 | ) 13 | 14 | func FileExists(path string) bool { 15 | _, err := os.Stat(path) 16 | return !os.IsNotExist(err) 17 | } 18 | 19 | func FilterExistingFiles(dirs []string) []string { 20 | var result []string 21 | for _, dir := range dirs { 22 | if FileExists(dir) { 23 | result = append(result, dir) 24 | } 25 | } 26 | return result 27 | } 28 | 29 | func IsDir(path string) bool { 30 | info, err := os.Stat(path) 31 | if err != nil { 32 | return false 33 | } 34 | return info.IsDir() 35 | } 36 | 37 | func MustBeDir(path string) error { 38 | if !IsDir(path) { 39 | if absPath, err := filepath.Abs(path); err != nil { 40 | return fmt.Errorf("'%s' is not a directory", path) 41 | } else { 42 | return fmt.Errorf("'%s' is not a directory", absPath) 43 | } 44 | } 45 | return nil 46 | } 47 | 48 | func MakeDir(path string) error { 49 | if FileExists(path) { 50 | return MustBeDir(path) 51 | } 52 | return os.MkdirAll(path, 0755) 53 | } 54 | 55 | func GetFileName(path string) string { 56 | info, err := os.Stat(path) 57 | if err != nil { 58 | return "" 59 | } 60 | return info.Name() 61 | } 62 | 63 | func GetParentDir(path string) string { 64 | pathComponents := strings.Split(path, "/") 65 | return strings.Join(pathComponents[:len(pathComponents)-1], "/") 66 | } 67 | 68 | func NormalizeFilePath(path string) (string, error) { 69 | if strings.HasPrefix(path, "file:/") { 70 | u, err := url.Parse(path) 71 | if err != nil { 72 | return "", err 73 | } 74 | 75 | if u.Host != "" { 76 | return fmt.Sprintf("/%s%s", u.Host, u.Path), nil 77 | } else { 78 | return strings.TrimPrefix(u.Path, "/"), nil 79 | } 80 | } 81 | 82 | return path, nil 83 | } 84 | 85 | func CopyAll(src string, dstDir string, exclude []string, ignoreEmptyFiles bool) error { 86 | if !FileExists(src) { 87 | return fmt.Errorf("source file/directory %s does not exist", src) 88 | } 89 | 90 | if err := os.MkdirAll(dstDir, 0755); err != nil { 91 | return fmt.Errorf("failed to create destination directory %s: %w", dstDir, err) 92 | } 93 | 94 | info, err := os.Stat(src) 95 | if err != nil { 96 | return fmt.Errorf("failed to stat source file/directory %s: %w", src, err) 97 | } 98 | if info.IsDir() { 99 | printer.Debug("Copying directory", src, "to", dstDir) 100 | children, err := os.ReadDir(src) 101 | if err != nil { 102 | return fmt.Errorf("failed to read directory %s: %w", src, err) 103 | } 104 | 105 | for _, child := range children { 106 | if contains(exclude, child.Name()) { 107 | printer.Debug("Skipping excluded file", child.Name()) 108 | continue 109 | } 110 | 111 | var dst string 112 | if child.IsDir() { 113 | dst = dstDir + "/" + child.Name() 114 | } else { 115 | dst = dstDir 116 | } 117 | 118 | if err := CopyAll(src+"/"+child.Name(), dst, exclude, ignoreEmptyFiles); err != nil { 119 | return err 120 | } 121 | } 122 | } else { 123 | dstFile := dstDir + "/" + info.Name() 124 | printer.Debug("Copying file %s to %s", src, dstFile) 125 | data, err := os.ReadFile(src) 126 | if err != nil { 127 | return fmt.Errorf("failed to read file %s: %w", src, err) 128 | } 129 | if ignoreEmptyFiles && len(data) == 0 { 130 | printer.Debug("Skipping empty file", src) 131 | return nil 132 | } 133 | if err := os.WriteFile(dstFile, data, 0644); err != nil { 134 | return fmt.Errorf("failed to write file %s: %w", dstFile, err) 135 | } 136 | } 137 | 138 | return nil 139 | } 140 | 141 | func contains(arr []string, str string) bool { 142 | for _, item := range arr { 143 | if item == str { 144 | return true 145 | } 146 | } 147 | return false 148 | } 149 | 150 | func RunCommand(command string, args ...string) (string, error) { 151 | printer.Debug("Executing '%s' with args: %s", command, args) 152 | cmd := exec.Command(command, args...) 153 | var outb, errb bytes.Buffer 154 | cmd.Stdout = &outb 155 | cmd.Stderr = &errb 156 | if err := cmd.Run(); err != nil { 157 | if errb.Len() != 0 { 158 | return "", fmt.Errorf("%s", errb.String()) 159 | } else { 160 | return "", fmt.Errorf("%s", outb.String()) 161 | } 162 | } else { 163 | return outb.String(), nil 164 | } 165 | } 166 | 167 | func Contains[T comparable](slice []T, item T) bool { 168 | for _, i := range slice { 169 | if i == item { 170 | return true 171 | } 172 | } 173 | return false 174 | } 175 | --------------------------------------------------------------------------------