-
Notifications
You must be signed in to change notification settings - Fork 4
Adds project apply command
#257
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
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
47db3ec
added apply command
3c1164e
removed support to --version flag
da50565
added support to yaml files
fcaca4d
working on apply config
67c5a31
Merge remote-tracking branch 'origin/main' into feat/project-config-a…
4c13d0f
code review
65f05a4
cleanup
cbe11c7
code review
144b2b0
added optional --message flag
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
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,159 @@ | ||
| // Copyright Mia srl | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| // 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 project | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/mia-platform/miactl/internal/client" | ||
| "github.com/mia-platform/miactl/internal/clioptions" | ||
| "github.com/mia-platform/miactl/internal/files" | ||
| "github.com/mia-platform/miactl/internal/resources" | ||
| "github.com/mia-platform/miactl/internal/resources/configuration" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| const ( | ||
| applyProjectCmdUsage = "apply" | ||
| applyProjectCmdShort = "Apply a Project configuration" | ||
| applyProjectCmdLong = `Apply a Project configuration from a file. | ||
|
|
||
| The configuration file should contain a complete project configuration in JSON or YAML format. | ||
| This command will replace the current project configuration with the one provided in the file. | ||
| ` | ||
| ) | ||
|
|
||
| type applyProjectOptions struct { | ||
| ProjectID string | ||
| RevisionName string | ||
| FilePath string | ||
| Title string | ||
| } | ||
|
|
||
| // ApplyCmd returns a cobra command for applying a project configuration | ||
| func ApplyCmd(options *clioptions.CLIOptions) *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: applyProjectCmdUsage, | ||
| Short: applyProjectCmdShort, | ||
| Long: applyProjectCmdLong, | ||
| RunE: func(cmd *cobra.Command, _ []string) error { | ||
| restConfig, err := options.ToRESTConfig() | ||
| cobra.CheckErr(err) | ||
|
|
||
| client, err := client.APIClientForConfig(restConfig) | ||
| cobra.CheckErr(err) | ||
|
|
||
| cmdOptions := applyProjectOptions{ | ||
| RevisionName: options.Revision, | ||
| ProjectID: restConfig.ProjectID, | ||
| FilePath: options.InputFilePath, | ||
| Title: options.Message, | ||
| } | ||
|
|
||
| return handleApplyProjectConfigurationCmd(cmd.Context(), client, cmdOptions) | ||
| }, | ||
| } | ||
|
|
||
| flags := cmd.Flags() | ||
| options.AddProjectFlags(flags) | ||
| options.AddRevisionFlags(flags) | ||
|
|
||
| flags.StringVarP(&options.Message, "message", "m", "", "the message to use when saving the configuration") | ||
|
|
||
| // file path flag is required | ||
| flags.StringVarP(&options.InputFilePath, "file", "f", "", "path to JSON/YAML file containing the project configuration") | ||
| if err := cmd.MarkFlagRequired("file"); err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func handleApplyProjectConfigurationCmd(ctx context.Context, client *client.APIClient, options applyProjectOptions) error { | ||
| err := validateApplyProjectOptions(options) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| err = applyConfiguration(ctx, client, options) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to apply project configuration: %w", err) | ||
| } | ||
|
|
||
| fmt.Println("Project configuration applied successfully") | ||
| return nil | ||
| } | ||
|
|
||
| func validateApplyProjectOptions(options applyProjectOptions) error { | ||
| if len(options.ProjectID) == 0 { | ||
| return fmt.Errorf("missing project name, please provide a project name as argument") | ||
| } | ||
|
|
||
| if len(options.FilePath) == 0 { | ||
| return fmt.Errorf("missing file path, please provide a file path with the -f flag") | ||
| } | ||
|
|
||
| if len(options.RevisionName) == 0 { | ||
| return fmt.Errorf("missing revision name, please provide a revision name") | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func applyConfiguration(ctx context.Context, client *client.APIClient, options applyProjectOptions) error { | ||
| ref, err := configuration.GetEncodedRevisionRef(options.RevisionName) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| projectConfig := make(map[string]any) | ||
| if err := files.ReadFile(options.FilePath, &projectConfig); err != nil { | ||
| return fmt.Errorf("failed to read project configuration file: %w", err) | ||
| } | ||
|
|
||
| structuredConfig, err := configuration.BuildDescribeConfiguration(projectConfig) | ||
| if err != nil { | ||
| return fmt.Errorf("cannot parse project configuration: %w", err) | ||
| } | ||
|
|
||
| applyConfig := configuration.BuildApplyRequest(structuredConfig) | ||
|
|
||
| if options.Title != "" { | ||
| applyConfig = applyConfig.WithTitle(options.Title) | ||
| } | ||
|
|
||
| body, err := resources.EncodeResourceToJSON(applyConfig) | ||
| if err != nil { | ||
| return fmt.Errorf("cannot encode project configuration: %w", err) | ||
| } | ||
|
|
||
| endpoint := fmt.Sprintf("/api/backend/projects/%s/%s/configuration", options.ProjectID, ref) | ||
| response, err := client. | ||
| Post(). | ||
| APIPath(endpoint). | ||
| Body(body). | ||
| Do(ctx) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to apply project configuration: %w", err) | ||
| } | ||
| if err := response.Error(); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.