Skip to content

Commit

Permalink
support reading from stdin and URL (#19)
Browse files Browse the repository at this point in the history
Signed-off-by: Nandor Kracser <[email protected]>
  • Loading branch information
bonifaido authored Jun 24, 2020
1 parent 0ca2aa7 commit b084d55
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions kurun.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/http"
"os"
"os/exec"
"os/signal"
Expand Down Expand Up @@ -497,13 +497,33 @@ var applyCmd = &cobra.Command{

var rawResources [][]byte

for _, manifest := range files {
manifestData, err := ioutil.ReadFile(manifest)
if err != nil {
return err
for _, file := range files {

var manifest io.Reader

if strings.HasPrefix(file, "http://") || strings.HasPrefix(file, "https://") {
resp, err := http.Get(file)
if err != nil {
return err
}
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return fmt.Errorf("unable to read URL %s, server reported %d", file, resp.StatusCode)
}

defer resp.Body.Close()
manifest = resp.Body

} else if file == "-" {
manifest = os.Stdin
} else {
var err error
manifest, err = os.Open(file)
if err != nil {
return err
}
}

decoder := k8sYaml.NewYAMLOrJSONDecoder(bytes.NewBuffer(manifestData), 4096)
decoder := k8sYaml.NewYAMLOrJSONDecoder(manifest, 4096)

var obj *unstructured.Unstructured

Expand Down Expand Up @@ -635,7 +655,7 @@ func main() {
portForwardCmd.PersistentFlags().StringVar(&tlsSecret, "tlssecret", "", "Use the certs for ghostunnel")
portForwardCmd.PersistentFlags().IntVar(&localPort, "localport", 8444, "Local port to use for port-forwarding")

applyCmd.PersistentFlags().StringSliceVarP(&files, "filename", "f", []string{}, "Filename, directory, or URL to files to use to create the resource")
applyCmd.PersistentFlags().StringSliceVarP(&files, "filename", "f", []string{}, "Filename, or URL to files to use to create the resource (use - for STDIN)")

rootCmd := &cobra.Command{Use: "kurun"}
rootCmd.PersistentFlags().StringVar(&namespace, "namespace", "default", "Namespace to use for the Pod/Service")
Expand Down

0 comments on commit b084d55

Please sign in to comment.