-
Notifications
You must be signed in to change notification settings - Fork 172
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
Add support for Contentful CLI #343
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package contentful | ||
|
||
import ( | ||
"github.com/1Password/shell-plugins/sdk" | ||
"github.com/1Password/shell-plugins/sdk/needsauth" | ||
"github.com/1Password/shell-plugins/sdk/schema" | ||
"github.com/1Password/shell-plugins/sdk/schema/credname" | ||
) | ||
|
||
func ContentfulCLI() schema.Executable { | ||
return schema.Executable{ | ||
Name: "Contentful CLI", | ||
Runs: []string{"contentful"}, | ||
DocsURL: sdk.URL("https://www.contentful.com/developers/docs/tutorials/cli/"), | ||
NeedsAuth: needsauth.IfAll( | ||
needsauth.NotForHelpOrVersion(), | ||
needsauth.NotWithoutArgs(), | ||
), | ||
Uses: []schema.CredentialUsage{ | ||
{ | ||
Name: credname.PersonalAccessToken, | ||
}, | ||
}, | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package contentful | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
|
||
"github.com/1Password/shell-plugins/sdk" | ||
"github.com/1Password/shell-plugins/sdk/importer" | ||
"github.com/1Password/shell-plugins/sdk/provision" | ||
"github.com/1Password/shell-plugins/sdk/schema" | ||
"github.com/1Password/shell-plugins/sdk/schema/credname" | ||
"github.com/1Password/shell-plugins/sdk/schema/fieldname" | ||
) | ||
|
||
func PersonalAccessToken() schema.CredentialType { | ||
return schema.CredentialType{ | ||
Name: credname.PersonalAccessToken, | ||
DocsURL: sdk.URL("https://www.contentful.com/developers/docs/references/content-management-api/#/reference/personal-access-tokens"), | ||
Fields: []schema.CredentialField{ | ||
{ | ||
Name: fieldname.Token, | ||
MarkdownDescription: "Personal Access Token used to authenticate to Contentful.", | ||
Secret: true, | ||
Composition: &schema.ValueComposition{ | ||
Length: 49, | ||
Prefix: "CFPAT-", | ||
Charset: schema.Charset{ | ||
Uppercase: true, | ||
Lowercase: true, | ||
Digits: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
DefaultProvisioner: provision.TempFile( | ||
contentfulConfig, | ||
provision.AtFixedPath("~/.contentfulrc.json"), | ||
), | ||
Importer: importer.TryAll( | ||
TryContentfulConfigFile(), | ||
)} | ||
Comment on lines
+39
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's use |
||
} | ||
|
||
func TryContentfulConfigFile() sdk.Importer { | ||
return importer.TryFile("~/.contentfulrc.json", func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) { | ||
var config Config | ||
if err := contents.ToJSON(&config); err != nil { | ||
out.AddError(err) | ||
return | ||
} | ||
|
||
if config.ManagementToken == "" { | ||
return | ||
} | ||
|
||
out.AddCandidate(sdk.ImportCandidate{ | ||
Fields: map[sdk.FieldName]string{ | ||
fieldname.Token: config.ManagementToken, | ||
}, | ||
}) | ||
}) | ||
} | ||
|
||
type Config struct { | ||
ManagementToken string `json:"managementToken"` | ||
ActiveEnvironmentId string `json:"activeEnvironmentId"` | ||
Host string `json:"host"` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the host something we could import from the json config file as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar question for |
||
} | ||
|
||
func contentfulConfig(in sdk.ProvisionInput) ([]byte, error) { | ||
config := Config{ | ||
ManagementToken: in.ItemFields[fieldname.Token], | ||
ActiveEnvironmentId: "master", | ||
Host: "api.contentful.com", | ||
} | ||
contents, err := json.MarshalIndent(&config, "", " ") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for indentation, since this only produces a file meant to be consumed in a programmatic fashion. 😄 |
||
if err != nil { | ||
return nil, err | ||
} | ||
return []byte(contents), nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package contentful | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/1Password/shell-plugins/sdk" | ||
"github.com/1Password/shell-plugins/sdk/plugintest" | ||
"github.com/1Password/shell-plugins/sdk/schema/fieldname" | ||
) | ||
|
||
func TestPersonalAccessTokenProvisioner(t *testing.T) { | ||
plugintest.TestProvisioner(t, PersonalAccessToken().DefaultProvisioner, map[string]plugintest.ProvisionCase{ | ||
"default": { | ||
ItemFields: map[sdk.FieldName]string{ // TODO: Check if this is correct | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this |
||
fieldname.Token: "lDoY0qLbGt9Se8K71O42xI3y6XPxNSfFop4cjHHq1CEXAMPLE", | ||
}, | ||
ExpectedOutput: sdk.ProvisionOutput{ | ||
Environment: map[string]string{ | ||
"CONTENTFUL_TOKEN": "lDoY0qLbGt9Se8K71O42xI3y6XPxNSfFop4cjHHq1CEXAMPLE", | ||
}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestPersonalAccessTokenImporter(t *testing.T) { | ||
plugintest.TestImporter(t, PersonalAccessToken().Importer, map[string]plugintest.ImportCase{ | ||
"config file": { | ||
Files: map[string]string{ | ||
"~/.contentfulrc.json": plugintest.LoadFixture(t, "contentfulrc.json"), | ||
}, | ||
ExpectedCandidates: []sdk.ImportCandidate{ | ||
{ | ||
Fields: map[sdk.FieldName]string{ | ||
fieldname.Key: "CFPAT-XArJuIgiqjflHUGePPYbILIKOUzdFv2jTJMNEXAMPLE", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package contentful | ||
|
||
import ( | ||
"github.com/1Password/shell-plugins/sdk" | ||
"github.com/1Password/shell-plugins/sdk/schema" | ||
) | ||
|
||
func New() schema.Plugin { | ||
return schema.Plugin{ | ||
Name: "contentful", | ||
Platform: schema.PlatformInfo{ | ||
Name: "Contentful", | ||
Homepage: sdk.URL("https://contentful.com"), | ||
}, | ||
Credentials: []schema.CredentialType{ | ||
PersonalAccessToken(), | ||
}, | ||
Executables: []schema.Executable{ | ||
ContentfulCLI(), | ||
}, | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"managementToken": "CFPAT-XArJuIgiqjflHUGePPYbILIKOUzdFv2jTJMNEXAMPLE", | ||
"activeEnvironmentId": "master", | ||
"host": "api.contentful.com" | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
login
andlogout
shouldn't authenticate either, I assume.