Created
May 23, 2017 08:34
-
-
Save maleck13/fac92043989a8a425c247e5e6f4f0432 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Copyright © 2017 NAME HERE <EMAIL ADDRESS> | |
// | |
// Licensed under the Apache License, Version 2.0 (the "License"); | |
// you may not use this file except in compliance with the License. | |
// You may obtain a copy of the License at | |
// | |
// http://www.apache.org/licenses/LICENSE-2.0 | |
// | |
// Unless required by applicable law or agreed to in writing, software | |
// distributed under the License is distributed on an "AS IS" BASIS, | |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
// See the License for the specific language governing permissions and | |
// limitations under the License. | |
package cmd | |
import ( | |
"io" | |
"log" | |
"os" | |
"io/ioutil" | |
"reflect" | |
"fmt" | |
authapi "github.com/openshift/origin/pkg/authorization/api" | |
authapi1 "github.com/openshift/origin/pkg/authorization/api/v1" | |
"github.com/pkg/errors" | |
"github.com/spf13/cobra" | |
kapi "k8s.io/kubernetes/pkg/api" | |
kapi1 "k8s.io/kubernetes/pkg/api/v1" | |
kauth "k8s.io/kubernetes/pkg/apis/authorization" | |
kauth1 "k8s.io/kubernetes/pkg/apis/authorization/v1beta1" | |
"k8s.io/kubernetes/pkg/runtime" | |
) | |
// convertCmd represents the convert command | |
var convertCmd = &cobra.Command{ | |
Use: "convert", | |
Short: "A brief description of your command", | |
Long: `A longer description that spans multiple lines and likely contains examples | |
and usage of using your command. For example: | |
Cobra is a CLI library for Go that empowers applications. | |
This application is a tool to generate the needed files | |
to quickly create a Cobra application.`, | |
Run: func(cmd *cobra.Command, args []string) { | |
// TODO: Work your own magic here | |
in, err := cmd.Flags().GetString("in") | |
if err != nil { | |
log.Fatalf("failed to get flag in %v", err) | |
} | |
f, err := os.Open(in) | |
if err != nil { | |
log.Fatalf("failed to open file %s error %v", in, err) | |
} | |
defer f.Close() | |
def, err := Parse(f) | |
if err != nil { | |
log.Fatalf("failed to parse definitons %v", err) | |
} | |
log.Println(def.Items) | |
for _, item := range def.Items { | |
log.Println(reflect.TypeOf(item)) | |
} | |
//kapi.Scheme.Convert() | |
}, | |
} | |
func Parse(in io.Reader) (*kapi.List, error) { | |
data, err := ioutil.ReadAll(in) | |
if err != nil { | |
return nil, errors.Wrap(err, "failed to read all from reader") | |
} | |
dec := kapi.Codecs.UniversalDecoder() | |
udec := runtime.UnstructuredJSONScheme | |
ob, err := runtime.Decode(dec, data) | |
if err != nil { | |
return nil, err | |
} | |
list, ok := ob.(*kapi.List) | |
if !ok { | |
return nil, errors.New(fmt.Sprintf("expected a list but got a %v ", reflect.TypeOf(ob))) | |
} | |
errs := runtime.DecodeList(list.Items, kapi.Codecs.UniversalDecoder(), udec) | |
if len(errs) > 0 { | |
return nil, errs[0] | |
} | |
return list, nil | |
} | |
func init() { | |
authapi.AddToScheme(kapi.Scheme) | |
authapi1.AddToScheme(kapi.Scheme) | |
kauth.AddToScheme(kapi.Scheme) | |
kauth1.AddToScheme(kapi.Scheme) | |
kapi.AddToScheme(kapi.Scheme) | |
kapi1.AddToScheme(kapi.Scheme) | |
RootCmd.AddCommand(convertCmd) | |
convertCmd.Flags().StringP("in", "i", "", "input file") | |
// Here you will define your flags and configuration settings. | |
// Cobra supports Persistent Flags which will work for this command | |
// and all subcommands, e.g.: | |
// convertCmd.PersistentFlags().String("foo", "", "A help for foo") | |
// Cobra supports local flags which will only run when this command | |
// is called directly, e.g.: | |
// convertCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment