-
-
Notifications
You must be signed in to change notification settings - Fork 67
feat(cli): machine-readable output for editor and automation integrations #499
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
Harsh4902
merged 1 commit into
microcks:master
from
Caesarsage:feat/vscode-service-test-json-commands
Aug 13, 2026
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /* | ||
| * Copyright The Microcks 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 ( | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/microcks/microcks-cli/pkg/errors" | ||
| "github.com/microcks/microcks-cli/pkg/output" | ||
| "github.com/microcks/microcks-cli/version" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| const capabilitiesSchemaVersion = "v1" | ||
|
|
||
| var supportedCapabilities = []string{ | ||
| "auth.login", | ||
| "auth.login.sso", | ||
| "auth.logout", | ||
| "context.list", | ||
| "context.list.json", | ||
| "context.use", | ||
| "context.use.json", | ||
| "context.delete", | ||
| "context.delete.json", | ||
| "instance.start", | ||
| "instance.start.json", | ||
| "instance.stop", | ||
| "artifact.import.file", | ||
| "artifact.import.file.json", | ||
| "artifact.import.file.watch", | ||
| "artifact.import.directory", | ||
| "artifact.import.url", | ||
| "service.list.json", | ||
| "service.get.json", | ||
| "test.run", | ||
| "test.run.output.json", | ||
| "test.run.output.yaml", | ||
| "test.run.output.github-actions", | ||
| "test.dry-run", | ||
| "test.dry-run.watch", | ||
| "test.dry-run.watch.events.json", | ||
| "test.list.json", | ||
| "test.get.json", | ||
| } | ||
|
|
||
| type capabilitiesDocument struct { | ||
| SchemaVersion string `json:"schemaVersion"` | ||
| CLIVersion string `json:"cliVersion"` | ||
| Capabilities []string `json:"capabilities"` | ||
| } | ||
|
|
||
| func NewCapabilitiesCommand() *cobra.Command { | ||
| var outputFormat string | ||
|
|
||
| command := &cobra.Command{ | ||
| Use: "capabilities", | ||
| Short: "List machine-readable Microcks CLI capabilities", | ||
| Args: cobra.NoArgs, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| if !output.IsTextOrJSON(outputFormat) { | ||
| return errors.Wrapf(errors.KindUsage, "--output must be one of: text, json") | ||
| } | ||
|
|
||
| document := capabilitiesDocument{ | ||
| SchemaVersion: capabilitiesSchemaVersion, | ||
| CLIVersion: version.Version, | ||
| Capabilities: supportedCapabilities, | ||
| } | ||
| if outputFormat == "json" { | ||
| return errors.Wrap( | ||
| errors.KindGeneric, | ||
| output.WriteJSON(os.Stdout, document), | ||
| ) | ||
| } | ||
|
|
||
| for _, capability := range document.Capabilities { | ||
| if _, err := fmt.Fprintln(os.Stdout, capability); err != nil { | ||
| return errors.Wrap( | ||
| errors.KindEnvironment, | ||
| fmt.Errorf("writing capabilities output: %w", err), | ||
| ) | ||
| } | ||
| } | ||
| return nil | ||
| }, | ||
| } | ||
| command.Flags().StringVar(&outputFormat, "output", "text", "Output format: text or json") | ||
| return command | ||
| } | ||
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,88 @@ | ||
| /* | ||
| * Copyright The Microcks 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 ( | ||
| "encoding/json" | ||
| "slices" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestCapabilitiesCommandOutputsJSON(t *testing.T) { | ||
| out, err := executeCLIForTest(t, "capabilities", "--output", "json") | ||
| if err != nil { | ||
| t.Fatalf("command returned error: %v", err) | ||
| } | ||
|
|
||
| var document capabilitiesDocument | ||
| if err := json.Unmarshal([]byte(out), &document); err != nil { | ||
| t.Fatalf("output is not valid JSON: %v", err) | ||
| } | ||
| if document.SchemaVersion != capabilitiesSchemaVersion { | ||
| t.Fatalf("unexpected schema version: %s", document.SchemaVersion) | ||
| } | ||
| if document.CLIVersion == "" { | ||
| t.Fatal("expected a CLI version") | ||
| } | ||
| expectedCapabilities := []string{ | ||
| "auth.login", | ||
| "auth.login.sso", | ||
| "auth.logout", | ||
| "context.list", | ||
| "context.list.json", | ||
| "context.use", | ||
| "context.use.json", | ||
| "context.delete", | ||
| "context.delete.json", | ||
| "instance.start", | ||
| "instance.start.json", | ||
| "instance.stop", | ||
| "artifact.import.file", | ||
| "artifact.import.file.json", | ||
| "artifact.import.file.watch", | ||
| "artifact.import.directory", | ||
| "artifact.import.url", | ||
| "service.list.json", | ||
| "service.get.json", | ||
| "test.run", | ||
| "test.run.output.json", | ||
| "test.run.output.yaml", | ||
| "test.run.output.github-actions", | ||
| "test.dry-run", | ||
| "test.dry-run.watch", | ||
| "test.dry-run.watch.events.json", | ||
| "test.list.json", | ||
| "test.get.json", | ||
| } | ||
| if !slices.Equal(document.Capabilities, expectedCapabilities) { | ||
| t.Fatalf("unexpected capabilities:\n got: %#v\nwant: %#v", document.Capabilities, expectedCapabilities) | ||
| } | ||
|
|
||
| seen := make(map[string]struct{}, len(document.Capabilities)) | ||
| for _, capability := range document.Capabilities { | ||
| if _, duplicate := seen[capability]; duplicate { | ||
| t.Errorf("duplicate capability %q", capability) | ||
| } | ||
| seen[capability] = struct{}{} | ||
| } | ||
| } | ||
|
|
||
| func TestCapabilitiesCommandRejectsUnsupportedOutput(t *testing.T) { | ||
| _, err := executeCLIForTest(t, "capabilities", "--output", "yaml") | ||
| if err == nil { | ||
| t.Fatal("expected unsupported output format to fail") | ||
| } | ||
| } |
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,81 @@ | ||
| /* | ||
| * Copyright The Microcks 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/microcks/microcks-cli/pkg/config" | ||
| "github.com/microcks/microcks-cli/pkg/connectors" | ||
| "github.com/microcks/microcks-cli/pkg/errors" | ||
| ) | ||
|
|
||
| func newCommandClient(globalClientOpts *connectors.ClientOptions) (connectors.MicrocksClient, string, error) { | ||
| config.InsecureTLS = globalClientOpts.InsecureTLS | ||
| config.CaCertPaths = globalClientOpts.CaCertPaths | ||
| config.Verbose = globalClientOpts.Verbose | ||
|
|
||
| if globalClientOpts.ServerAddr != "" { | ||
| mc, err := connectors.NewMicrocksClient(globalClientOpts.ServerAddr) | ||
| if err != nil { | ||
| return nil, "", err | ||
| } | ||
|
|
||
| if globalClientOpts.ClientId != "" && globalClientOpts.ClientSecret != "" { | ||
| keycloakURL, err := mc.GetKeycloakURL() | ||
| if err != nil { | ||
| return nil, "", err | ||
| } | ||
|
|
||
| oauthToken := "unauthenticated-token" | ||
| if keycloakURL != "null" { | ||
| kc, err := connectors.NewKeycloakClient(keycloakURL, globalClientOpts.ClientId, globalClientOpts.ClientSecret) | ||
| if err != nil { | ||
| return nil, "", err | ||
| } | ||
|
|
||
| oauthToken, err = kc.ConnectAndGetToken() | ||
| if err != nil { | ||
| return nil, "", err | ||
| } | ||
| } | ||
| mc.SetOAuthToken(oauthToken) | ||
| } | ||
| return mc, globalClientOpts.ServerAddr, nil | ||
| } | ||
|
|
||
| localConfig, err := config.ReadLocalConfig(globalClientOpts.ConfigPath) | ||
| if err != nil { | ||
| return nil, "", err | ||
| } | ||
| if localConfig == nil { | ||
| return nil, "", errors.Wrapf(errors.KindUsage, "please login to perform this operation") | ||
| } | ||
|
|
||
| clientOpts := *globalClientOpts | ||
| if clientOpts.Context == "" { | ||
| clientOpts.Context = localConfig.CurrentContext | ||
| } | ||
|
|
||
| mc, err := connectors.NewClient(clientOpts) | ||
| if err != nil { | ||
| return nil, "", err | ||
| } | ||
|
|
||
| ctx, err := localConfig.ResolveContext(clientOpts.Context) | ||
| if err != nil { | ||
| return nil, "", errors.Wrap(errors.KindNotFound, err) | ||
| } | ||
| return mc, ctx.Server.Server, 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.
Uh oh!
There was an error while loading. Please reload this page.