Skip to content
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

Derive field name in make new-plugin more intelligently #72

Merged
merged 1 commit into from
Dec 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 24 additions & 8 deletions cmd/contrib/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ func newPlugin() error {
PlatformNameUpperCamelCase string
ValueComposition schema.ValueComposition
FieldName string
FieldNameUpperCamelCase string
CredentialEnvVarName string
IsNewCredentialName bool
CredentialNameUpperCamelCase string
Expand All @@ -173,7 +174,6 @@ func newPlugin() error {

result.CredentialNameUpperCamelCase = strings.Join(credNameSplit, "")
result.CredentialNameSnakeCase = strings.ToLower(strings.Join(credNameSplit, "_"))
result.CredentialEnvVarName = strings.ToUpper(result.Name + "_" + result.CredentialNameSnakeCase)

result.IsNewCredentialName = true
for _, existing := range credname.ListAll() {
Expand All @@ -183,8 +183,24 @@ func newPlugin() error {
}
}

// As a placeholder, assume the field name is the last word of the credential name, e.g. "Token"
result.FieldName = credNameSplit[len(credNameSplit)-1]
// As a placeholder, assume the field name is the short version (max 7 chars) of the credential name, starting
// from the last word. For example:
// "Personal Access Token" => "Token"
// "Secret Key" => "Key"
// "API Key" => "API Key"
var fieldNameSplit []string
lengthCutoff := 7
for i := range credNameSplit {
word := credNameSplit[len(credNameSplit)-1-i]
if len(strings.Join(append(fieldNameSplit, word), " ")) > lengthCutoff {
break
}

fieldNameSplit = append([]string{word}, fieldNameSplit...)
}
result.FieldName = strings.Join(fieldNameSplit, " ")
result.FieldNameUpperCamelCase = strings.Join(fieldNameSplit, "")
result.CredentialEnvVarName = strings.ToUpper(strings.Join(append([]string{result.Name}, fieldNameSplit...), "_"))

relativeDirPath := filepath.Join("plugins", result.Name)
err = os.MkdirAll(relativeDirPath, 0777)
Expand Down Expand Up @@ -345,7 +361,7 @@ func {{ .CredentialNameUpperCamelCase }}() schema.CredentialType {
ManagementURL: sdk.URL("https://console.{{ .Name }}.com/user/security/tokens"), // TODO: Replace with actual URL
Fields: []schema.CredentialField{
{
Name: fieldname.{{ .FieldName }},
Name: fieldname.{{ .FieldNameUpperCamelCase }},
MarkdownDescription: "{{ .FieldName }} used to authenticate to {{ .PlatformName }}.",
Secret: true,
{{- if .ValueComposition.Length }}
Expand Down Expand Up @@ -382,7 +398,7 @@ func {{ .CredentialNameUpperCamelCase }}() schema.CredentialType {
}

var defaultEnvVarMapping = map[string]sdk.FieldName{
"{{ .CredentialEnvVarName }}": fieldname.{{ .FieldName }}, // TODO: Check if this is correct
"{{ .CredentialEnvVarName }}": fieldname.{{ .FieldNameUpperCamelCase }}, // TODO: Check if this is correct
florisvdg marked this conversation as resolved.
Show resolved Hide resolved
}

// TODO: Check if the platform stores the {{ .CredentialName }} in a local config file, and if so,
Expand All @@ -395,21 +411,21 @@ func Try{{ .PlatformNameUpperCamelCase }}ConfigFile() sdk.Importer {
// return
// }

// if config.{{ .FieldName }} == "" {
// if config.{{ .FieldNameUpperCamelCase }} == "" {
// return
// }

// out.AddCandidate(sdk.ImportCandidate{
// Fields: map[sdk.FieldName]string{
// fieldname.{{ .FieldName }}: config.{{ .FieldName }},
// fieldname.{{ .FieldNameUpperCamelCase }}: config.{{ .FieldNameUpperCamelCase }},
// },
// })
})
}

// TODO: Implement the config file schema
// type Config struct {
// {{ .FieldName }} string
// {{ .FieldNameUpperCamelCase }} string
// }
`,
}
Expand Down