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

Fix index generator module #4

Merged
merged 1 commit into from
Oct 15, 2020
Merged
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
57 changes: 9 additions & 48 deletions index/generator/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,19 @@ limitations under the License.
package cmd

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/spf13/cobra"
"gopkg.in/yaml.v2"

"github.com/devfile/registry-support/index/generator/schema"
"github.com/devfile/registry-support/index/generator/library"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
)

const (
shortDesc = "Generate index file"
longDesc = "Generate index file based on the index schema and registry devfiles"
meta = "meta.yaml"
devfile = "devfile.yaml"
)

var cfgFile string
Expand All @@ -48,9 +42,15 @@ var rootCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
registryDirPath := args[0]
indexFilePath := args[1]
err := generateIndex(registryDirPath, indexFilePath)

index, err := library.GenerateIndexStruct(registryDirPath)
if err != nil {
fmt.Errorf("failed to generate index file: %v", err)
fmt.Printf("failed to generate index struct: %v", err)
}

err = library.CreateIndexFile(index, indexFilePath)
if err != nil {
fmt.Printf("failed to create index file: %v", err)
}
},
}
Expand Down Expand Up @@ -103,42 +103,3 @@ func initConfig() {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}

func generateIndex(registryDirPath string, indexFilePath string) error {
registryDir, err := ioutil.ReadDir(registryDirPath)
if err != nil {
fmt.Errorf("failed to read registry directory %s: %v", registryDirPath, err)
}

var index []schema.Schema
for _, devfileDir := range registryDir {
if !devfileDir.IsDir() {
fmt.Errorf("%s is not a directory: %v", filepath.Join(registryDirPath, devfileDir.Name()), err)
}

metaFilePath := filepath.Join(registryDirPath, devfileDir.Name(), meta)
bytes, err := ioutil.ReadFile(metaFilePath)
if err != nil {
fmt.Errorf("failed to read %s: %v", metaFilePath, err)
}
var indexComponent schema.Schema
err = yaml.Unmarshal(bytes, &indexComponent)
if err != nil {
fmt.Errorf("failed to unmarshal %s data: %v", metaFilePath, err)
}
indexComponent.Links = schema.Links{
Self: fmt.Sprintf("%s/%s:%s", "devfile-catalog", indexComponent.Name, "latest"),
}
index = append(index, indexComponent)
}

bytes, err := json.MarshalIndent(index, "", " ")
if err != nil {
fmt.Errorf("failed to marshal %s data: %v", indexFilePath, err)
}
err = ioutil.WriteFile(indexFilePath, bytes, 0644)
if err != nil {
fmt.Errorf("failed to write %s: %v", indexFilePath, err)
}
return nil
}
Binary file modified index/generator/index-generator
Binary file not shown.
63 changes: 63 additions & 0 deletions index/generator/library/library.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package library

import (
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"

"github.com/devfile/registry-support/index/generator/schema"
"gopkg.in/yaml.v2"
)

const (
meta = "meta.yaml"
devfile = "devfile.yaml"
)

// GenerateIndexStruct parses registry then generates index struct according to the schema
func GenerateIndexStruct(registryDirPath string) ([]schema.Schema, error) {
registryDir, err := ioutil.ReadDir(registryDirPath)
if err != nil {
return nil, fmt.Errorf("failed to read registry directory %s: %v", registryDirPath, err)
}

var index []schema.Schema
for _, devfileDir := range registryDir {
if !devfileDir.IsDir() {
return nil, fmt.Errorf("%s is not a directory", filepath.Join(registryDirPath, devfileDir.Name()))
}

metaFilePath := filepath.Join(registryDirPath, devfileDir.Name(), meta)
bytes, err := ioutil.ReadFile(metaFilePath)
if err != nil {
return nil, fmt.Errorf("failed to read %s: %v", metaFilePath, err)
}
var indexComponent schema.Schema
err = yaml.Unmarshal(bytes, &indexComponent)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal %s data: %v", metaFilePath, err)
}
indexComponent.Links = schema.Links{
Self: fmt.Sprintf("%s/%s:%s", "devfile-catalog", indexComponent.Name, "latest"),
}
index = append(index, indexComponent)
}

return index, nil
}

// CreateIndexFile creates index file in disk
func CreateIndexFile(index []schema.Schema, indexFilePath string) error {
bytes, err := json.MarshalIndent(index, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal %s data: %v", indexFilePath, err)
}

err = ioutil.WriteFile(indexFilePath, bytes, 0644)
if err != nil {
return fmt.Errorf("failed to write %s: %v", indexFilePath, err)
}

return nil
}