-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add config file for optional groupping
- Loading branch information
Showing
13 changed files
with
288 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"k8s.io/apimachinery/pkg/util/yaml" | ||
|
||
"github.com/Skarlso/crd-to-sample-yaml/pkg" | ||
) | ||
|
||
type ConfigHandler struct { | ||
configFileLocation string | ||
} | ||
|
||
func (h *ConfigHandler) CRDs() ([]*pkg.SchemaType, error) { | ||
if _, err := os.Stat(h.configFileLocation); os.IsNotExist(err) { | ||
return nil, fmt.Errorf("file under '%s' does not exist", h.configFileLocation) | ||
} | ||
content, err := os.ReadFile(h.configFileLocation) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read file: %w", err) | ||
} | ||
|
||
configFile := &pkg.RenderConfig{} | ||
if err = yaml.Unmarshal(content, configFile); err != nil { | ||
return nil, fmt.Errorf("failed to unmarshal config file: %w", err) | ||
} | ||
|
||
// for each api group, call file handler and folder handler and gather all | ||
// the CRDs. | ||
var result []*pkg.SchemaType | ||
|
||
for _, group := range configFile.ApiGroups { | ||
for _, file := range group.Files { | ||
handler := FileHandler{location: file, group: group.Name} | ||
fileResults, err := handler.CRDs() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to process CRDs for files in groups %s: %w", group.Name, err) | ||
} | ||
|
||
result = append(result, fileResults...) | ||
} | ||
|
||
for _, folder := range group.Folders { | ||
handler := FolderHandler{location: folder, group: group.Name} | ||
folderResults, err := handler.CRDs() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to process CRDs for folders %s: %w", handler.location, err) | ||
} | ||
|
||
result = append(result, folderResults...) | ||
} | ||
} | ||
|
||
return result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package pkg | ||
|
||
// ApiGroups defines groups by which grouping will happen in the resulting HTML output. | ||
type ApiGroups struct { | ||
Name string `json:"name"` | ||
Description string `json:"description"` | ||
Files []string `json:"files,omitempty"` | ||
Folders []string `json:"folders,omitempty"` | ||
} | ||
|
||
// RenderConfig defines a configuration for the resulting rendered HTML content. | ||
type RenderConfig struct { | ||
ApiGroups []ApiGroups `json:"apiGroups"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.