|
| 1 | +/* |
| 2 | + * Copyright The Microcks Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package cmd |
| 17 | + |
| 18 | +import ( |
| 19 | + "fmt" |
| 20 | + "os" |
| 21 | + "strings" |
| 22 | + "text/tabwriter" |
| 23 | + |
| 24 | + "github.com/microcks/microcks-cli/pkg/config" |
| 25 | + "github.com/microcks/microcks-cli/pkg/connectors" |
| 26 | + "github.com/microcks/microcks-cli/pkg/errors" |
| 27 | + "github.com/spf13/cobra" |
| 28 | +) |
| 29 | + |
| 30 | +func NewServicesCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command { |
| 31 | + servicesCmd := &cobra.Command{ |
| 32 | + Use: "services", |
| 33 | + Short: "Manage Microcks services", |
| 34 | + Long: `Manage Microcks services`, |
| 35 | + Run: func(cmd *cobra.Command, args []string) { |
| 36 | + cmd.HelpFunc()(cmd, args) |
| 37 | + }, |
| 38 | + } |
| 39 | + |
| 40 | + servicesCmd.AddCommand(newServicesListCommand(globalClientOpts)) |
| 41 | + return servicesCmd |
| 42 | +} |
| 43 | + |
| 44 | +func newServicesListCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command { |
| 45 | + var ( |
| 46 | + page int |
| 47 | + size int |
| 48 | + ) |
| 49 | + |
| 50 | + listCmd := &cobra.Command{ |
| 51 | + Use: "list", |
| 52 | + Short: "List services imported in Microcks", |
| 53 | + Long: `List services imported in Microcks`, |
| 54 | + Example: `# List services using current context |
| 55 | +microcks services list |
| 56 | +
|
| 57 | +# List services with pagination |
| 58 | +microcks services list --page 1 --size 10`, |
| 59 | + Run: func(cmd *cobra.Command, args []string) { |
| 60 | + config.InsecureTLS = globalClientOpts.InsecureTLS |
| 61 | + config.CaCertPaths = globalClientOpts.CaCertPaths |
| 62 | + config.Verbose = globalClientOpts.Verbose |
| 63 | + |
| 64 | + var mc connectors.MicrocksClient |
| 65 | + |
| 66 | + if globalClientOpts.ServerAddr != "" && globalClientOpts.ClientId != "" && globalClientOpts.ClientSecret != "" { |
| 67 | + mc = connectors.NewMicrocksClient(globalClientOpts.ServerAddr) |
| 68 | + |
| 69 | + keycloakURL, err := mc.GetKeycloakURL() |
| 70 | + if err != nil { |
| 71 | + fmt.Printf("Got error when invoking Microcks client retrieving config: %s", err) |
| 72 | + os.Exit(1) |
| 73 | + } |
| 74 | + |
| 75 | + var oauthToken string = "unauthenticated-token" |
| 76 | + if keycloakURL != "null" { |
| 77 | + kc := connectors.NewKeycloakClient(keycloakURL, globalClientOpts.ClientId, globalClientOpts.ClientSecret) |
| 78 | + oauthToken, err = kc.ConnectAndGetToken() |
| 79 | + if err != nil { |
| 80 | + fmt.Printf("Got error when invoking Keycloak client: %s", err) |
| 81 | + os.Exit(1) |
| 82 | + } |
| 83 | + } |
| 84 | + mc.SetOAuthToken(oauthToken) |
| 85 | + } else { |
| 86 | + localConfig, err := config.ReadLocalConfig(globalClientOpts.ConfigPath) |
| 87 | + if err != nil { |
| 88 | + fmt.Println(err) |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + if localConfig == nil { |
| 93 | + fmt.Println("Please login to perform operation...") |
| 94 | + return |
| 95 | + } |
| 96 | + |
| 97 | + if globalClientOpts.Context == "" { |
| 98 | + globalClientOpts.Context = localConfig.CurrentContext |
| 99 | + } |
| 100 | + |
| 101 | + mc, err = connectors.NewClient(*globalClientOpts) |
| 102 | + if err != nil { |
| 103 | + fmt.Printf("error %v", err) |
| 104 | + return |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + services, err := mc.GetServices(page, size) |
| 109 | + if err != nil { |
| 110 | + fmt.Printf("Got error when listing services: %s", err) |
| 111 | + os.Exit(1) |
| 112 | + } |
| 113 | + |
| 114 | + if len(services) == 0 { |
| 115 | + fmt.Println("No services found") |
| 116 | + return |
| 117 | + } |
| 118 | + |
| 119 | + w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) |
| 120 | + defer func() { _ = w.Flush() }() |
| 121 | + columnNames := []string{"NAME", "VERSION", "TYPE"} |
| 122 | + _, err = fmt.Fprintf(w, "%s\n", strings.Join(columnNames, "\t")) |
| 123 | + errors.CheckError(err) |
| 124 | + |
| 125 | + for _, svc := range services { |
| 126 | + _, err = fmt.Fprintf(w, "%s\t%s\t%s\n", svc.Name, svc.Version, svc.Type) |
| 127 | + errors.CheckError(err) |
| 128 | + } |
| 129 | + }, |
| 130 | + } |
| 131 | + |
| 132 | + listCmd.Flags().IntVar(&page, "page", 0, "Page of services to retrieve (0-indexed)") |
| 133 | + listCmd.Flags().IntVar(&size, "size", 20, "Number of services per page") |
| 134 | + return listCmd |
| 135 | +} |
0 commit comments