-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from 90poe/issue-10/plugins-validate-cmd
feat: plugins validate cmd
- Loading branch information
Showing
11 changed files
with
350 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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,33 @@ | ||
## connectctl plugins validate | ||
|
||
Validates connector config | ||
|
||
### Synopsis | ||
|
||
Validate the provided configuration values against the configuration definition. This API performs per config validation, outputs suggested values and error messages during validation. | ||
It exits with code 1 if config is invalid. | ||
|
||
|
||
``` | ||
connectctl plugins validate [flags] | ||
``` | ||
|
||
### Options | ||
|
||
``` | ||
-c, --cluster string the URL of the connect cluster (required) | ||
-h, --help help for validate | ||
-i, --input string Input data in json format (required) | ||
-o, --output string specify the output format (valid options: json, table) (default "json") | ||
-q, --quiet disable output logging | ||
``` | ||
### Options inherited from parent commands | ||
|
||
``` | ||
-l, --loglevel loglevel Specify the loglevel for the program (default info) | ||
--logfile Specify a file to output logs to | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [connectctl plugins](connectctl_plugins.md) - Manage plugins |
This file contains 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 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,27 @@ | ||
package ctl | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
|
||
"github.com/jedib0t/go-pretty/table" | ||
) | ||
|
||
func PrintAsJSON(data interface{}) error { | ||
b, err := json.MarshalIndent(data, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
os.Stdout.Write(b) | ||
return nil | ||
} | ||
|
||
func PrintAsTable(handler func(table.Writer)) { | ||
t := table.NewWriter() | ||
t.SetOutputMirror(os.Stdout) | ||
|
||
handler(t) | ||
|
||
t.Render() | ||
} |
This file contains 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 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,117 @@ | ||
package plugins | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/90poe/connectctl/internal/ctl" | ||
"github.com/90poe/connectctl/internal/version" | ||
"github.com/90poe/connectctl/pkg/client/connect" | ||
"github.com/90poe/connectctl/pkg/manager" | ||
"github.com/jedib0t/go-pretty/table" | ||
"github.com/pkg/errors" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
type validatePluginsCmdParams struct { | ||
ClusterURL string | ||
Input string | ||
Output string | ||
Quiet bool | ||
} | ||
|
||
func validatePluginsCmd() *cobra.Command { | ||
params := &validatePluginsCmdParams{} | ||
|
||
validateCmd := &cobra.Command{ | ||
Use: "validate", | ||
Short: "Validates plugin config", | ||
Long: "", | ||
RunE: func(cmd *cobra.Command, _ []string) error { | ||
return doValidatePlugins(cmd, params) | ||
}, | ||
} | ||
|
||
ctl.AddClusterFlag(validateCmd, true, ¶ms.ClusterURL) | ||
ctl.AddInputFlag(validateCmd, true, ¶ms.Input) | ||
ctl.AddOutputFlags(validateCmd, ¶ms.Output) | ||
ctl.AddQuietFlag(validateCmd, ¶ms.Quiet) | ||
|
||
return validateCmd | ||
} | ||
|
||
func doValidatePlugins(_ *cobra.Command, params *validatePluginsCmdParams) error { | ||
var inputConfig connect.ConnectorConfig | ||
if err := json.Unmarshal([]byte(params.Input), &inputConfig); err != nil { | ||
return errors.Wrap(err, "error parsing input connector config") | ||
} | ||
|
||
config := &manager.Config{ | ||
ClusterURL: params.ClusterURL, | ||
Version: version.Version, | ||
} | ||
|
||
userAgent := fmt.Sprintf("90poe.io/connectctl/%s", version.Version) | ||
|
||
client, err := connect.NewClient(params.ClusterURL, connect.WithUserAgent(userAgent)) | ||
if err != nil { | ||
return errors.Wrap(err, "error creating connect client") | ||
} | ||
|
||
mngr, err := manager.NewConnectorsManager(client, config) | ||
if err != nil { | ||
return errors.Wrap(err, "error creating connectors manager") | ||
} | ||
|
||
validation, err := mngr.ValidatePlugins(inputConfig) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !params.Quiet { | ||
switch params.Output { | ||
case "json": | ||
if err = ctl.PrintAsJSON(validation); err != nil { | ||
return errors.Wrap(err, "error printing validation results as JSON") | ||
} | ||
|
||
case "table": | ||
printAsTable(validation) | ||
|
||
default: | ||
return fmt.Errorf("invalid output format specified: %s", params.Output) | ||
} | ||
} | ||
|
||
if validation.ErrorCount > 0 { | ||
return fmt.Errorf("detected %d errors in the configuation", validation.ErrorCount) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func printAsTable(validation *connect.ConfigValidation) { | ||
ctl.PrintAsTable(func(t table.Writer) { | ||
t.Style().Options.SeparateRows = true | ||
t.AppendHeader(table.Row{"Name", "Spec", "Value", "Errors"}) | ||
|
||
for _, info := range validation.Configs { | ||
spec := fmt.Sprintf( | ||
"default: %s\nrequired: %v", | ||
ctl.StrPtrToStr(info.Definition.DefaultValue), | ||
info.Definition.Required, | ||
) | ||
|
||
errors := strings.Join(info.Value.Errors, "\n") | ||
|
||
t.AppendRow(table.Row{ | ||
info.Definition.Name, | ||
spec, | ||
ctl.StrPtrToStr(info.Value.Value), | ||
errors, | ||
}) | ||
} | ||
}) | ||
} |
This file contains 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,9 @@ | ||
package ctl | ||
|
||
func StrPtrToStr(str *string) string { | ||
if str == nil { | ||
return "null" | ||
} | ||
|
||
return *str | ||
} |
This file contains 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 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
Oops, something went wrong.