|
| 1 | +// Copyright (c) ClaceIO, LLC |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package main |
| 5 | + |
| 6 | +import ( |
| 7 | + "cmp" |
| 8 | + "encoding/json" |
| 9 | + "fmt" |
| 10 | + "net/url" |
| 11 | + "strconv" |
| 12 | + "strings" |
| 13 | + |
| 14 | + "github.com/openrundev/openrun/internal/types" |
| 15 | + "github.com/urfave/cli/v2" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + SOURCE_URL_FLAG = "source-url" |
| 20 | + PROVIDER_VERSION_FLAG = "version" |
| 21 | +) |
| 22 | + |
| 23 | +func initProviderCommand(commonFlags []cli.Flag, clientConfig *types.ClientConfig) *cli.Command { |
| 24 | + return &cli.Command{ |
| 25 | + Name: "provider", |
| 26 | + Usage: "Manage out-of-process binding providers", |
| 27 | + Subcommands: []*cli.Command{ |
| 28 | + providerInstallCommand(commonFlags, clientConfig), |
| 29 | + providerUninstallCommand(commonFlags, clientConfig), |
| 30 | + providerListCommand(commonFlags, clientConfig), |
| 31 | + }, |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func providerInstallCommand(commonFlags []cli.Flag, clientConfig *types.ClientConfig) *cli.Command { |
| 36 | + flags := make([]cli.Flag, 0, len(commonFlags)+2) |
| 37 | + flags = append(flags, commonFlags...) |
| 38 | + flags = append(flags, newStringFlag(SOURCE_URL_FLAG, "", "The provider binary source: an http(s) url (supports {version}, {os} and {arch} placeholders) or a server-local file path", "")) |
| 39 | + flags = append(flags, newStringFlag(PROVIDER_VERSION_FLAG, "", "The provider version to install", "")) |
| 40 | + |
| 41 | + return &cli.Command{ |
| 42 | + Name: "install", |
| 43 | + Usage: "Install (or update) an out-of-process binding provider", |
| 44 | + Flags: flags, |
| 45 | + ArgsUsage: "<provider_name>", |
| 46 | + UsageText: `args: <provider_name> |
| 47 | +
|
| 48 | +The provider binary is downloaded (or copied), verified, registered in the |
| 49 | +metadata database and its service types become available for service create. |
| 50 | +
|
| 51 | +Examples: |
| 52 | + openrun provider install mongodb --source-url https://github.com/openrundev/openrun-bindings/releases/download/mongodb%2F{version}/openrun-binding-mongodb-{os}-{arch} --version v0.1.0 |
| 53 | + openrun provider install redis --source-url /tmp/openrun-binding-redis |
| 54 | +`, |
| 55 | + Action: func(cCtx *cli.Context) error { |
| 56 | + if cCtx.NArg() != 1 { |
| 57 | + return fmt.Errorf("expected one arg: <provider_name>") |
| 58 | + } |
| 59 | + request := types.ProviderInstallRequest{ |
| 60 | + Name: cCtx.Args().First(), |
| 61 | + SourceURL: cCtx.String(SOURCE_URL_FLAG), |
| 62 | + Version: cCtx.String(PROVIDER_VERSION_FLAG), |
| 63 | + } |
| 64 | + if request.SourceURL == "" { |
| 65 | + return fmt.Errorf("--%s is required", SOURCE_URL_FLAG) |
| 66 | + } |
| 67 | + |
| 68 | + client := newHttpClient(clientConfig) |
| 69 | + var response types.BindingProvider |
| 70 | + if err := client.Post("/_openrun/provider", url.Values{}, &request, &response); err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + |
| 74 | + printStdout(cCtx, "Provider %s %s installed, service types: %s\n", |
| 75 | + response.Name, response.Version, strings.Join(response.ServiceTypes, ", ")) |
| 76 | + return nil |
| 77 | + }, |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func providerUninstallCommand(commonFlags []cli.Flag, clientConfig *types.ClientConfig) *cli.Command { |
| 82 | + flags := make([]cli.Flag, 0, len(commonFlags)+1) |
| 83 | + flags = append(flags, commonFlags...) |
| 84 | + flags = append(flags, newBoolFlag("force", "", "Uninstall even if services of the provider's types exist", false)) |
| 85 | + |
| 86 | + return &cli.Command{ |
| 87 | + Name: "uninstall", |
| 88 | + Usage: "Uninstall an out-of-process binding provider", |
| 89 | + Flags: flags, |
| 90 | + ArgsUsage: "<provider_name>", |
| 91 | + Action: func(cCtx *cli.Context) error { |
| 92 | + if cCtx.NArg() != 1 { |
| 93 | + return fmt.Errorf("expected one arg: <provider_name>") |
| 94 | + } |
| 95 | + name := cCtx.Args().First() |
| 96 | + |
| 97 | + values := url.Values{} |
| 98 | + values.Add("name", name) |
| 99 | + values.Add("force", strconv.FormatBool(cCtx.Bool("force"))) |
| 100 | + |
| 101 | + client := newHttpClient(clientConfig) |
| 102 | + var response map[string]any |
| 103 | + if err := client.Delete("/_openrun/provider", values, &response); err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + |
| 107 | + printStdout(cCtx, "Provider %s uninstalled\n", name) |
| 108 | + return nil |
| 109 | + }, |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +func providerListCommand(commonFlags []cli.Flag, clientConfig *types.ClientConfig) *cli.Command { |
| 114 | + flags := make([]cli.Flag, 0, len(commonFlags)+1) |
| 115 | + flags = append(flags, commonFlags...) |
| 116 | + flags = append(flags, newStringFlag("format", "f", "The display format. Valid options are table, basic, csv, json, jsonl and jsonl_pretty", "")) |
| 117 | + |
| 118 | + return &cli.Command{ |
| 119 | + Name: "list", |
| 120 | + Usage: "List installed binding providers", |
| 121 | + Flags: flags, |
| 122 | + Action: func(cCtx *cli.Context) error { |
| 123 | + client := newHttpClient(clientConfig) |
| 124 | + var response []types.BindingProvider |
| 125 | + if err := client.Get("/_openrun/providers", url.Values{}, &response); err != nil { |
| 126 | + return err |
| 127 | + } |
| 128 | + |
| 129 | + printProviderList(cCtx, response, cmp.Or(cCtx.String("format"), clientConfig.Client.DefaultFormat)) |
| 130 | + return nil |
| 131 | + }, |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func printProviderList(cCtx *cli.Context, providers []types.BindingProvider, format string) { |
| 136 | + switch format { |
| 137 | + case FORMAT_JSON: |
| 138 | + enc := json.NewEncoder(cCtx.App.Writer) |
| 139 | + enc.SetIndent("", " ") |
| 140 | + enc.Encode(providers) //nolint:errcheck |
| 141 | + case FORMAT_JSONL: |
| 142 | + enc := json.NewEncoder(cCtx.App.Writer) |
| 143 | + for _, p := range providers { |
| 144 | + enc.Encode(p) //nolint:errcheck |
| 145 | + } |
| 146 | + case FORMAT_JSONL_PRETTY: |
| 147 | + enc := json.NewEncoder(cCtx.App.Writer) |
| 148 | + enc.SetIndent("", " ") |
| 149 | + for _, p := range providers { |
| 150 | + enc.Encode(p) //nolint:errcheck |
| 151 | + } |
| 152 | + case FORMAT_CSV: |
| 153 | + for _, p := range providers { |
| 154 | + printStdout(cCtx, "%s,%s,%s\n", p.Name, p.Version, strings.Join(p.ServiceTypes, " ")) |
| 155 | + } |
| 156 | + default: |
| 157 | + formatStr := "%-20s %-15s %-30s %-s\n" |
| 158 | + printStdout(cCtx, formatStr, "Name", "Version", "ServiceTypes", "SourceURL") |
| 159 | + for _, p := range providers { |
| 160 | + printStdout(cCtx, formatStr, p.Name, p.Version, strings.Join(p.ServiceTypes, ", "), p.SourceURL) |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments