Commit cbad6f91 authored by michaelguarino's avatar michaelguarino
Browse files

Add repo attrs target

No related merge requests found
Showing with 146 additions and 8 deletions
+146 -8
......@@ -2,8 +2,11 @@ package api
import (
"fmt"
"os"
"path/filepath"
"gopkg.in/yaml.v2"
"github.com/michaeljguarino/graphql"
)
type ResourceDefinitionInput struct {
......@@ -30,10 +33,12 @@ type IntegrationInput struct {
}
type RepositoryInput struct {
Dashboards []struct {
Name string
UID string `json:"uid"`
}
Name string
Description string
Tags []Tag `json:"tags,omitempty" yaml:"tags"`
Icon string `json:"icon,omitempty" yaml:"icon"`
DarkIcon string `json:"darkIcon,omitempty" yaml:"darkIcon"`
Category string
}
const updateRepository = `
......@@ -44,6 +49,12 @@ const updateRepository = `
}
`
const upsertRepository = `
mutation UpsertRepository($name: String!, $publisher: String!, $attributes: RepositoryAttributes!) {
upsertRepository(name: $name, publisher: $publisher, attributes: $attributes) { id }
}
`
const createIntegration = `
mutation CreateIntegration($name: String!, $attrs: IntegrationAttributes!) {
createIntegration(repositoryName: $name, attributes: $attrs) {
......@@ -113,6 +124,51 @@ func (client *Client) UpdateRepository(name string, input RepositoryInput) (stri
return resp.Id, err
}
func (client *Client) CreateRepository(name, publisher string, input RepositoryInput) error {
var resp struct {
UpsertRepository struct {
Id string
}
}
req := client.Build(upsertRepository)
req.Var("name", name)
req.Var("publisher", publisher)
ok, err := getIconReader(input.Icon, "icon", req)
if err != nil {
return err
}
if ok {
input.Icon = "icon"
}
ok, err = getIconReader(input.DarkIcon, "darkicon", req)
if err != nil {
return err
}
if ok {
input.DarkIcon = "darkicon"
}
req.Var("attributes", input)
return client.Run(req, &resp)
}
func getIconReader(icon, field string, req *graphql.Request) (bool, error) {
if icon == "" {
return false, nil
}
file, err := filepath.Abs(icon)
if err != nil {
return false, err
}
f, err := os.Open(file)
req.File(field, file, f)
return true, err
}
func ConstructRepositoryInput(marshalled []byte) (input RepositoryInput, err error) {
err = yaml.Unmarshal(marshalled, &input)
return
......
......@@ -57,14 +57,12 @@ func mkSha(file string) (sha string, err error) {
return
}
readmePath, _ := filepath.Abs(input.Readme)
readme, err := utils.Sha256(readmePath)
readme, err := fileSha(input.Readme)
if err != nil {
return
}
blobPath, _ := filepath.Abs(input.Blob)
blob, err := utils.Sha256(blobPath)
blob, err := fileSha(input.Blob)
if err != nil {
return
}
......
package pluralfile
import (
"fmt"
"io/ioutil"
"path/filepath"
"github.com/pluralsh/plural/pkg/api"
"github.com/pluralsh/plural/pkg/utils"
)
type RepoAttrs struct {
File string
Publisher string
}
func (a *RepoAttrs) Type() ComponentName {
return REPO_ATTRS
}
func (a *RepoAttrs) Key() string {
return fmt.Sprintf("%s_%s", a.File, a.Publisher)
}
func (a *RepoAttrs) Push(repo string, sha string) (string, error) {
fullPath, _ := filepath.Abs(a.File)
contents, err := ioutil.ReadFile(fullPath)
if err != nil {
return "", err
}
input, err := api.ConstructRepositoryInput(contents)
if err != nil {
return "", err
}
newsha, err := a.mkSha(fullPath, input)
if err != nil || newsha == sha {
utils.Highlight("No change for %s\n", a.File)
return sha, err
}
client := api.NewClient()
return newsha, client.CreateRepository(repo, a.Publisher, input)
}
func (a *RepoAttrs) mkSha(fullPath string, input api.RepositoryInput) (sha string, err error) {
base, err := utils.Sha256(fullPath)
if err != nil {
return
}
iconSha, err := fileSha(input.Icon)
if err != nil {
return
}
darkIconSha, err := fileSha(input.DarkIcon)
if err != nil {
return
}
sha = utils.Sha([]byte(fmt.Sprintf("%s:%s:%s", base, iconSha, darkIconSha)))
return
}
func fileSha(path string) (string, error) {
if path == "" {
return "", nil
}
fpath, _ := filepath.Abs(path)
return utils.Sha256(fpath)
}
......@@ -18,6 +18,7 @@ type Lockfile struct {
Crd map[string]string
Ird map[string]string
Tag map[string]string
Attrs map[string]string
}
func lock() *Lockfile {
......@@ -30,6 +31,7 @@ func lock() *Lockfile {
Crd: map[string]string{},
Ird: map[string]string{},
Tag: map[string]string{},
Attrs: map[string]string{},
}
}
......@@ -89,6 +91,9 @@ func (lock *Lockfile) getSha(name ComponentName, key string) string {
case TAG:
sha, _ := lock.Tag[key]
return sha
case REPO_ATTRS:
sha, _ := lock.Tag[key]
return sha
default:
return ""
}
......@@ -113,6 +118,8 @@ func (lock *Lockfile) addSha(name ComponentName, key string, sha string) {
lock.Ird[key] = sha
case TAG:
lock.Tag[key] = sha
case REPO_ATTRS:
lock.Attrs[key] = sha
default:
return
}
......
......@@ -25,6 +25,7 @@ const (
IRD ComponentName = "ird"
COMMAND ComponentName = "run"
TAG ComponentName = "tag"
REPO_ATTRS ComponentName = "attrs"
)
type Component interface {
......@@ -153,6 +154,10 @@ func Parse(f string) (*Pluralfile, error) {
return plrl, err
}
plrl.Components = append(plrl.Components, tags...)
case "attributes":
pub, file := splitline[1], splitline[2]
plrl.Components = append(plrl.Components, &RepoAttrs{File: file, Publisher: pub})
default:
continue
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment