-
Notifications
You must be signed in to change notification settings - Fork 48
feat(cli): add apply command #2804
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+802
−38
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // | ||
| // Copyright 2026 The Chainloop Authors. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "github.com/chainloop-dev/chainloop/app/cli/pkg/action" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func newApplyCmd() *cobra.Command { | ||
| var filePath string | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "apply", | ||
| Short: "Apply resources from YAML files", | ||
| Long: `Apply resources from a YAML file or directory. | ||
| Supports multi-document YAML files. Each document must have a 'kind' field.`, | ||
| Example: ` # Apply resources from a single file | ||
| chainloop apply -f my-contract.yaml | ||
|
|
||
| # Apply resources from a directory | ||
| chainloop apply -f ./contracts/`, | ||
| RunE: func(cmd *cobra.Command, _ []string) error { | ||
| results, err := action.NewApply(ActionOpts).Run(cmd.Context(), filePath) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| logger.Info().Msgf("%d contracts applied", len(results)) | ||
|
|
||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().StringVarP(&filePath, "file", "f", "", "path to a YAML file or directory") | ||
| cobra.CheckErr(cmd.MarkFlagRequired("file")) | ||
|
|
||
| return cmd | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,210 @@ | ||
| // | ||
| // Copyright 2026 The Chainloop Authors. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package action | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| pb "github.com/chainloop-dev/chainloop/app/controlplane/api/controlplane/v1" | ||
| "google.golang.org/grpc" | ||
| "gopkg.in/yaml.v3" | ||
| ) | ||
|
|
||
| const ( | ||
| KindContract = "Contract" | ||
| ) | ||
|
|
||
| // ApplyResult holds the outcome of a successfully applied resource document | ||
| type ApplyResult struct { | ||
| Kind string | ||
| Name string | ||
| } | ||
|
|
||
| // YAMLDoc holds a parsed YAML document with its kind and raw bytes | ||
| type YAMLDoc struct { | ||
| Kind string | ||
| Name string | ||
| RawData []byte | ||
| } | ||
|
|
||
| // Apply handles applying resources from YAML files | ||
| type Apply struct { | ||
| cfg *ActionsOpts | ||
| } | ||
|
|
||
| // NewApply creates a new Apply action | ||
| func NewApply(cfg *ActionsOpts) *Apply { | ||
| return &Apply{cfg: cfg} | ||
| } | ||
|
|
||
| // Run applies all resources found in the given path (file or directory) | ||
| func (a *Apply) Run(ctx context.Context, path string) ([]*ApplyResult, error) { | ||
| docs, err := ParseYAMLPath(path) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // Apply contracts | ||
| var results []*ApplyResult | ||
| for _, doc := range docs { | ||
| result := &ApplyResult{Kind: doc.Kind, Name: doc.Name} | ||
| switch doc.Kind { | ||
| case KindContract: | ||
| if err := ApplyContractFromRawData(ctx, a.cfg.CPConnection, doc.RawData); err != nil { | ||
| return results, fmt.Errorf("%s/%s: %w", doc.Kind, doc.Name, err) | ||
cubic-dev-ai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| default: | ||
| return results, fmt.Errorf("unsupported kind %q", doc.Kind) | ||
| } | ||
| results = append(results, result) | ||
| } | ||
|
|
||
| return results, nil | ||
| } | ||
|
|
||
| // ParseYAMLPath collects all YAML files from a path (file or directory), | ||
| // reads them, and splits multi-document files into individual YAMLDoc entries. | ||
| func ParseYAMLPath(path string) ([]*YAMLDoc, error) { | ||
matiasinsaurralde marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| files, err := CollectYAMLFiles(path) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if len(files) == 0 { | ||
| return nil, fmt.Errorf("no YAML files found in %q", path) | ||
| } | ||
|
|
||
| var allDocs []*YAMLDoc | ||
| for _, f := range files { | ||
| rawData, err := os.ReadFile(f) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("reading file %s: %w", f, err) | ||
| } | ||
|
|
||
| docs, err := SplitYAMLDocuments(rawData) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("parsing file %s: %w", f, err) | ||
| } | ||
|
|
||
| allDocs = append(allDocs, docs...) | ||
| } | ||
|
|
||
| return allDocs, nil | ||
| } | ||
|
|
||
| // ApplyContractFromRawData applies a single contract document using the gRPC client. | ||
| func ApplyContractFromRawData(ctx context.Context, conn *grpc.ClientConn, rawData []byte) error { | ||
| client := pb.NewWorkflowContractServiceClient(conn) | ||
|
|
||
| _, err := client.Apply(ctx, &pb.WorkflowContractServiceApplyRequest{ | ||
| RawSchema: rawData, | ||
| }) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to apply contract: %w", err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // CollectYAMLFiles returns YAML file paths from the given path. | ||
| // If path is a file, it returns that file. If a directory, it walks recursively. | ||
| func CollectYAMLFiles(path string) ([]string, error) { | ||
| info, err := os.Stat(path) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("accessing path %q: %w", path, err) | ||
| } | ||
|
|
||
| if !info.IsDir() { | ||
| return []string{path}, nil | ||
| } | ||
|
|
||
| var files []string | ||
| err = filepath.WalkDir(path, func(p string, d os.DirEntry, err error) error { | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if d.IsDir() { | ||
| return nil | ||
| } | ||
| ext := strings.ToLower(filepath.Ext(p)) | ||
| if ext == ".yaml" || ext == ".yml" { | ||
| files = append(files, p) | ||
| } | ||
| return nil | ||
| }) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("walking directory %q: %w", path, err) | ||
| } | ||
|
|
||
| return files, nil | ||
| } | ||
|
|
||
| // SplitYAMLDocuments splits a potentially multi-document YAML file into individual documents, | ||
| // extracting kind and name from each. | ||
| func SplitYAMLDocuments(rawData []byte) ([]*YAMLDoc, error) { | ||
| decoder := yaml.NewDecoder(bytes.NewReader(rawData)) | ||
|
|
||
| var docs []*YAMLDoc | ||
| for { | ||
| var node yaml.Node | ||
| if err := decoder.Decode(&node); err != nil { | ||
| if errors.Is(err, io.EOF) { | ||
| break | ||
| } | ||
| return nil, fmt.Errorf("decoding YAML document: %w", err) | ||
| } | ||
|
|
||
| // Marshal node back to bytes for the per-resource apply | ||
| docBytes, err := yaml.Marshal(&node) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("marshalling YAML node: %w", err) | ||
| } | ||
|
|
||
| // Extract kind and name via partial unmarshal | ||
| var header struct { | ||
| Kind string `yaml:"kind"` | ||
| Metadata struct { | ||
| Name string `yaml:"name"` | ||
| } `yaml:"metadata"` | ||
| } | ||
| if err := yaml.Unmarshal(docBytes, &header); err != nil { | ||
| return nil, fmt.Errorf("extracting document kind: %w", err) | ||
| } | ||
|
|
||
| if header.Kind == "" { | ||
Piskoo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return nil, fmt.Errorf("missing 'kind' field in YAML document") | ||
| } | ||
|
|
||
| if header.Metadata.Name == "" { | ||
| return nil, fmt.Errorf("missing 'metadata.name' field in YAML document of kind %q", header.Kind) | ||
| } | ||
|
|
||
| docs = append(docs, &YAMLDoc{ | ||
| Kind: header.Kind, | ||
| Name: header.Metadata.Name, | ||
| RawData: docBytes, | ||
| }) | ||
| } | ||
|
|
||
| return docs, nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since OSS only supports declarative workflow contracts, I think this command should be placed in
chainloop wf contract apply.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already have
wf contract apply, this one is there to expose generic apply for all resources in chainloop, it will be extended in CLI EE. We can refactorwf contract applyin a separate task so it uses the new endpoint, although if we do that, we will lose the capability scoping contracts per project. We don't support projects in the schema