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 make registry.json command #347

Merged
merged 5 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ registry:
validate: registry
go run cmd/contrib/main.go $@

registry.json: registry
go run cmd/contrib/main.go $@

$(plugins_dir):
mkdir -p $(plugins_dir)
chmod 700 $(plugins_dir)
Expand Down
16 changes: 16 additions & 0 deletions cmd/contrib/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ func main() {
}
return
}

if command == "registry.json" {
err := generateRegistryJSON()
if err != nil {
log.Fatal(err)
}
return
}
}

func isPluginCommand(command string) (isPluginCommand bool, pluginName string, pluginCommand string) {
Expand Down Expand Up @@ -620,3 +628,11 @@ func transformCredentialName(ans any) (newAns any) {

return ans
}

func generateRegistryJSON() error {
b, err := plugins.RegistryJSON()
if err != nil {
return err
}
return os.WriteFile(filepath.Join("plugins", "registry.json"), b, 0600)
}
21 changes: 21 additions & 0 deletions plugins/registry.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
package plugins

import (
"encoding/json"
"fmt"
"strings"
"time"

"github.com/1Password/shell-plugins/sdk/schema"
)

var registry = []schema.Plugin{}

func RegistryJSON() ([]byte, error) {
var listToJSON []schema.Plugin

// Only include plugins that contain at least one credential.
for _, p := range List() {
if len(p.Credentials) > 0 {
listToJSON = append(listToJSON, p)
}
}
timedRegistry := struct {
Plugins []schema.Plugin `json:"registry"`
Timestamp time.Time `json:"timestamp"`
}{
Plugins: listToJSON,
Timestamp: time.Now(),
}
return json.MarshalIndent(timedRegistry, "", "\t")
}

func List() []schema.Plugin {
list := make([]schema.Plugin, len(registry))
copy(list, registry)
Expand Down
21 changes: 21 additions & 0 deletions sdk/schema/plugin.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package schema

import (
"encoding/json"
"fmt"
"net/url"
)
Expand Down Expand Up @@ -117,3 +118,23 @@ func (p Plugin) DeepValidate() []ValidationReport {

return reports
}

func (p Plugin) MarshalJSON() ([]byte, error) {
if len(p.Credentials) == 0 {
return nil, nil
}
plugin := struct {
PlatformName string `json:"platform_name"`
ManagementURL string `json:"management_url,omitempty"`
}{
PlatformName: p.Platform.Name,
ManagementURL: func() string {
if p.Credentials[0].ManagementURL != nil {
return p.Credentials[0].ManagementURL.String()
hculea marked this conversation as resolved.
Show resolved Hide resolved
} else {
return ""
}
}(),
}
return json.Marshal(plugin)
}