-
-
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 ability to load content from folders (#78)
* feat: add ability to load content from folders * added folder handler and fixed error wrapping * added lint fixes
- Loading branch information
Showing
6 changed files
with
216 additions
and
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1" | ||
"k8s.io/apimachinery/pkg/util/yaml" | ||
) | ||
|
||
type FileHandler struct { | ||
location string | ||
} | ||
|
||
func (h *FileHandler) CRDs() ([]*v1beta1.CustomResourceDefinition, error) { | ||
if _, err := os.Stat(h.location); os.IsNotExist(err) { | ||
return nil, fmt.Errorf("file under '%s' does not exist", h.location) | ||
} | ||
content, err := os.ReadFile(h.location) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read file: %w", err) | ||
} | ||
|
||
crd := &v1beta1.CustomResourceDefinition{} | ||
if err := yaml.Unmarshal(content, crd); err != nil { | ||
return nil, fmt.Errorf("failed to unmarshal into custom resource definition: %w", err) | ||
} | ||
|
||
return []*v1beta1.CustomResourceDefinition{crd}, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
|
||
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1" | ||
"k8s.io/apimachinery/pkg/util/yaml" | ||
) | ||
|
||
type FolderHandler struct { | ||
location string | ||
} | ||
|
||
func (h *FolderHandler) CRDs() ([]*v1beta1.CustomResourceDefinition, error) { | ||
if _, err := os.Stat(h.location); os.IsNotExist(err) { | ||
return nil, fmt.Errorf("file under '%s' does not exist", h.location) | ||
} | ||
|
||
var crds []*v1beta1.CustomResourceDefinition | ||
|
||
if err := filepath.Walk(h.location, func(path string, info fs.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if info.IsDir() { | ||
return nil | ||
} | ||
|
||
if filepath.Ext(path) != ".yaml" { | ||
fmt.Fprintln(os.Stderr, "skipping file "+path) | ||
|
||
return nil | ||
} | ||
|
||
content, err := os.ReadFile(path) | ||
if err != nil { | ||
return fmt.Errorf("failed to read file: %w", err) | ||
} | ||
|
||
crd := &v1beta1.CustomResourceDefinition{} | ||
if err := yaml.Unmarshal(content, crd); err != nil { | ||
fmt.Fprintln(os.Stderr, "skipping none CRD file: "+path) | ||
|
||
return nil //nolint:nilerr // intentional | ||
} | ||
|
||
crds = append(crds, crd) | ||
|
||
return nil | ||
}); err != nil { | ||
return nil, fmt.Errorf("failed to walk the selected folder: %w", err) | ||
} | ||
|
||
return crds, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/Skarlso/crd-to-sample-yaml/pkg/fetcher" | ||
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1" | ||
"k8s.io/apimachinery/pkg/util/yaml" | ||
) | ||
|
||
type URLHandler struct { | ||
url string | ||
} | ||
|
||
func (h *URLHandler) CRDs() ([]*v1beta1.CustomResourceDefinition, error) { | ||
client := http.DefaultClient | ||
client.Timeout = 10 * time.Second | ||
|
||
f := fetcher.NewFetcher(client) | ||
content, err := f.Fetch(h.url) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to fetch content: %w", err) | ||
} | ||
|
||
crd := &v1beta1.CustomResourceDefinition{} | ||
if err := yaml.Unmarshal(content, crd); err != nil { | ||
return nil, fmt.Errorf("failed to unmarshal into custom resource definition: %w", err) | ||
} | ||
|
||
return []*v1beta1.CustomResourceDefinition{crd}, 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