Skip to content

Commit

Permalink
Add boilerplate code for generating a json-formatted registry
Browse files Browse the repository at this point in the history
  • Loading branch information
hculea committed Jul 10, 2023
1 parent a39d64a commit 58351dd
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
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", "plugins.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"`
}{
PlatformName: p.Platform.Name,
ManagementURL: func() string {
if p.Credentials[0].ManagementURL != nil {
return p.Credentials[0].ManagementURL.String()
} else {
return ""
}
}(),
}
return json.Marshal(plugin)
}

0 comments on commit 58351dd

Please sign in to comment.