Skip to content

Commit

Permalink
use go
Browse files Browse the repository at this point in the history
  • Loading branch information
coreydaley committed Jun 25, 2020
1 parent 8b75cb6 commit 8f7466c
Show file tree
Hide file tree
Showing 11 changed files with 565 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
tmp
_output
library
14 changes: 14 additions & 0 deletions Dockerfile
@@ -0,0 +1,14 @@
# Dockerfile used to verify openshift/library via ci-operator
FROM registry.svc.ci.openshift.org/openshift/release:golang-1.13 as builder
WORKDIR /go/src/github.com/openshift/library
COPY . .
RUN make verify-gofmt
RUN make build

FROM registry.svc.ci.openshift.org/openshift/origin-v4.0:base
RUN yum install -y git make
COPY --from=builder /go/src/github.com/openshift/library /go/src/github.com/openshift/library
RUN chmod 777 /go/src/github.com/openshift/library
WORKDIR /go/src/github.com/openshift/library
ENTRYPOINT []
CMD ["make", "verify-pullrequest"]
43 changes: 43 additions & 0 deletions Makefile
@@ -0,0 +1,43 @@
# If no documents are specified, use these as the default
DOCUMENTS ?= official,community

ifeq ($(MATCHALL),true)
MATCHALLTAGS=--match-all
endif

.DEFAULT_GOAL := help

verify: verify-gofmt verify-pullrequest ## Run verifications. Example: make verify
.PHONY: verify

verify-gofmt: ## Run gofmt verification. Example: make verify-gofmt
hack/verify-gofmt.sh
.PHONY: verify-gofmt

verify-pullrequest: ## Run pull request verification. Example: make verify-pullrequest
hack/verify-pullrequest.sh $(DOCUMENTS)
.PHONY: verify-pullrequest

# Using -race here since we are running concurrently
build: ## Build the library executable. Example: make build
go version
go build -race
.PHONY: build

import: ## Run the import script. Example: make import
./library import --documents=$(DOCUMENTS) --tags=$(TAGS) $(MATCHALLTAGS) --dir=$(DIR)
.PHONY: import

vendor: ## Vendor Go Dependencies. Example: make vendor
go mod vendor
.PHONY: vendor

clean: ## Clean up the workspace. Example: make clean
rm -f library
rm -rf _output/
.PHONY: clean

help: ## Print this help. Example: make help
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
.PHONY: help

22 changes: 20 additions & 2 deletions README.md
Expand Up @@ -9,7 +9,6 @@ This repository contains a curated set of image streams and templates for OpenSh
- [Official](#official)
- [Community](#community)
- [Building the Library](#building-the-library)
- [Python Dependencies](#python-dependencies)
- [Running the Script](#running-the-script)
- [Verifying Your Updates](#verifying-your-updates)
- [Contributing](#contributing)
Expand Down Expand Up @@ -45,9 +44,28 @@ Community templates and image streams are **not** provided or supported by Red H

## Building the Library

### Running the Script
You must build the library executable before you can run the import.

$ make build

### Running the Script
# Imports the official.yaml and community.yaml without any
# additional flags or filters
$ make import

# Imports the templates and imagestreams into some_dir
$ make import DIR=some_dir

# Imports only the foo.yaml and bar.yaml documents
$ make import DOCUMENTS=foo.yaml,bar.yaml

# Imports only the templates and imagestreams tagged with
# tag1 OR tag2
$ make import TAGS=tag1,tag2

# Imports only the templates and imagestreams tagged with
# tag1 AND tag2
$ make import TAGS=tag1,tag2 MATCHALL=true

## Verifying Your Updates

Expand Down
34 changes: 34 additions & 0 deletions api/library/v1/types.go
@@ -0,0 +1,34 @@
package v1

// ItemImageStream is an object that describes an OpenShift ImageStream that we need to import
type ItemImageStream struct {
Location string `yaml:"location"`
Docs string `yaml:"docs"`
Regex string `yaml:"regex"`
Suffix string `yaml:"suffix"`
Tags []string `yaml:"tags"`
}

// ItemTemplate is an object that describes an OpenShift Template that we need to import
type ItemTemplate struct {
Location string `yaml:"location"`
Docs string `yaml:"docs"`
Regex string `yaml:"regex"`
Suffix string `yaml:"suffix"`
Tags []string `yaml:"tags"`
}

// Item is a container that holds ItemImageStreams and ItemTemplates
type Item struct {
ImageStreams []ItemImageStream `yaml:"imagestreams"`
Templates []ItemTemplate `yaml:"templates"`
}

type Document struct {
Variables map[string]string
}

// DocumentData is the data section of a library document
type DocumentData struct {
Data map[string]Item
}
76 changes: 76 additions & 0 deletions cmd/common.go
@@ -0,0 +1,76 @@
package cmd

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"

"github.com/ghodss/yaml"

imageapiv1 "github.com/openshift/api/image/v1"
templateapiv1 "github.com/openshift/api/template/v1"
)

func writeToFile(data []byte, filePath string) error {
return ioutil.WriteFile(filePath, data, os.ModePerm)
}

func replaceVariables(d *[]byte, v map[string]string) error {
for k, v := range v {
*d = []byte(strings.ReplaceAll(string(*d), fmt.Sprintf("{%s}", k), fmt.Sprintf("%s", v)))
}

return nil
}

func fetchURL(path string) ([]byte, error) {
resp, err := http.Get(path)
if err != nil {
return []byte{}, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return []byte{}, fmt.Errorf("%d", resp.StatusCode)
}

return ioutil.ReadAll(resp.Body)
}

func unMarshalTemplate(body []byte, t *templateapiv1.Template) error {
body, err := yaml.YAMLToJSON(body)
if err != nil {
return fmt.Errorf("Unable to convert yaml to json: %v", err)
}
if jerr := json.Unmarshal(body, &t); jerr != nil {
return fmt.Errorf("Error unmarshaling data: %v", err)

}
return nil
}

func unMarshalImageStream(body []byte, is *imageapiv1.ImageStream) error {
body, err := yaml.YAMLToJSON(body)
if err != nil {
return fmt.Errorf("Unable to convert yaml to json: %v", err)
}
if jerr := json.Unmarshal(body, &is); jerr != nil {
return fmt.Errorf("Error unmarshaling data: %v", err)

}
return nil
}

func unMarshalImageStreamList(body []byte, isl *imageapiv1.ImageStreamList) error {
body, err := yaml.YAMLToJSON(body)
if err != nil {
return fmt.Errorf("Unable to convert yaml to json: %v", err)
}
if jerr := json.Unmarshal(body, &isl); jerr != nil {
return fmt.Errorf("Error unmarshaling data: %v", err)

}
return nil
}

0 comments on commit 8f7466c

Please sign in to comment.