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

Add support for Contentful CLI #343

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions plugins/contentful/contentful.go
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(),
Comment on lines +16 to +17
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

login and logout shouldn't authenticate either, I assume.

),
Uses: []schema.CredentialUsage{
{
Name: credname.PersonalAccessToken,
},
},
}
}
81 changes: 81 additions & 0 deletions plugins/contentful/personal_access_token.go
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use TryContentfulConfigFile() directly, no need for TryAll if there is only one importer.

}

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"`
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar question for ActiveEnvironmentId.

}

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, "", " ")
Copy link
Member

Choose a reason for hiding this comment

The 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
}
41 changes: 41 additions & 0 deletions plugins/contentful/personal_access_token_test.go
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this TODO still be here?

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",
},
},
},
},
})
}
22 changes: 22 additions & 0 deletions plugins/contentful/plugin.go
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(),
},
}
}
5 changes: 5 additions & 0 deletions plugins/contentful/test-fixtures/contentfulrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"managementToken": "CFPAT-XArJuIgiqjflHUGePPYbILIKOUzdFv2jTJMNEXAMPLE",
"activeEnvironmentId": "master",
"host": "api.contentful.com"
}