// EncodeToStream serializes the provided object to the given writer. Overrides is ignored. func (s *Serializer) EncodeToStream(obj runtime.Object, w io.Writer, overrides ...unversioned.GroupVersion) error { if s.yaml { json, err := json.Marshal(obj) if err != nil { return err } data, err := yaml.JSONToYAML(json) if err != nil { return err } _, err = w.Write(data) return err } if s.pretty { data, err := json.MarshalIndent(obj, "", " ") if err != nil { return err } _, err = w.Write(data) return err } encoder := json.NewEncoder(w) return encoder.Encode(obj) }
func JSONToYAMLOrDie(in []byte) []byte { data, err := yaml.JSONToYAML(in) if err != nil { panic(err) } return data }
func testObjects(t *testing.T, list *api.List, fixtureFilename string) { filename := filepath.Join("../../../../test/fixtures/bootstrappolicy", fixtureFilename) expectedYAML, err := ioutil.ReadFile(filename) if err != nil { t.Fatal(err) } if err := runtime.EncodeList(api.Codecs.LegacyCodec(v1.SchemeGroupVersion), list.Items); err != nil { t.Fatal(err) } jsonData, err := runtime.Encode(api.Codecs.LegacyCodec(v1.SchemeGroupVersion), list) if err != nil { t.Fatal(err) } yamlData, err := yaml.JSONToYAML(jsonData) if err != nil { t.Fatal(err) } if string(yamlData) != string(expectedYAML) { fmt.Println("Bootstrap policy data does not match the test fixture in " + filename) fmt.Println("If the change is expected, update the fixture with this bootstrap policy data:") fmt.Println("-----------------------------------") fmt.Println(string(yamlData)) fmt.Println("-----------------------------------") t.Errorf("Diff between bootstrap data and fixture data in %s:\n-------------\n%s", filename, util.StringDiff(string(yamlData), string(expectedYAML))) } }
func jsonToYAML(j []byte) []byte { y, err := yaml.JSONToYAML(j) if err != nil { panic(fmt.Sprintf("json to yaml failed: %v", err)) } return y }
func jsonToYAML(j []byte) ([]byte, error) { y, err := yaml.JSONToYAML(j) if err != nil { return nil, fmt.Errorf("json to yaml failed: %v\n%v\n", err, j) } return y, nil }
func json2Yaml(json []byte) (s string) { if y, err := yaml.JSONToYAML(json); err != nil { s = err.Error() } else { s = string(y) } return }
// Write serializes the config to yaml. // Encapsulates serialization without assuming the destination is a file. func Write(config clientcmdapi.Config) ([]byte, error) { json, err := clientcmdlatest.Codec.Encode(&config) if err != nil { return nil, err } content, err := yaml.JSONToYAML(json) if err != nil { return nil, err } return content, nil }
func JsonToYaml(inputBytes []byte) []byte { buffer, err := yaml.JSONToYAML(inputBytes) if err != nil { fmt.Println("JSON -> YAML convert error.") fmt.Printf("err: %v\n", err) os.Exit(1) } return buffer }
func writeYAML(obj runtime.Object) ([]byte, error) { json, err := Codec.Encode(obj) if err != nil { return nil, err } content, err := yaml.JSONToYAML(json) if err != nil { return nil, err } return content, err }
func ExampleMergingSomeWithConflict() { commandLineFile, _ := ioutil.TempFile("", "") defer os.Remove(commandLineFile.Name()) envVarFile, _ := ioutil.TempFile("", "") defer os.Remove(envVarFile.Name()) WriteToFile(testConfigAlfa, commandLineFile.Name()) WriteToFile(testConfigConflictAlfa, envVarFile.Name()) loadingRules := ClientConfigLoadingRules{ CommandLinePath: commandLineFile.Name(), EnvVarPath: envVarFile.Name(), } mergedConfig, err := loadingRules.Load() json, err := clientcmdlatest.Codec.Encode(mergedConfig) if err != nil { fmt.Printf("Unexpected error: %v", err) } output, err := yaml.JSONToYAML(json) if err != nil { fmt.Printf("Unexpected error: %v", err) } fmt.Printf("%v", string(output)) // Output: // apiVersion: v1 // clusters: // - cluster: // server: http://cow.org:8080 // name: cow-cluster // - cluster: // insecure-skip-tls-verify: true // server: http://donkey.org:8080 // name: donkey-cluster // contexts: // - context: // cluster: cow-cluster // namespace: hammer-ns // user: red-user // name: federal-context // current-context: federal-context // kind: Config // preferences: {} // users: // - name: red-user // user: // token: red-token // - name: yellow-user // user: // token: yellow-token }
// writeYAML writes the specified object to a byte array as yaml. func writeYAML(obj runtime.Object) ([]byte, error) { json, err := runtime.Encode(api.Codecs.LegacyCodec(), obj) if err != nil { return nil, err } content, err := yaml.JSONToYAML(json) if err != nil { return nil, err } return content, err }
func writeYAML(obj runtime.Object) ([]byte, error) { json, err := runtime.Encode(serializer.NewCodecFactory(internal.Scheme).LegacyCodec(v1.SchemeGroupVersion), obj) if err != nil { return nil, err } content, err := yaml.JSONToYAML(json) if err != nil { return nil, err } return content, err }
// CmdShow implements the 'show' command func CmdShow(c *cli.Context) { utils.CheckRequiredFlags(c, []string{"kubeware"}) kubeware := os.Getenv("KDEPLOY_KUBEWARE") localKubePath, err := fetchers.Fetch(kubeware) if err != nil { log.Fatal(fmt.Errorf("Could not fetch kubeware: '%s' (%v)", kubeware, err)) } log.Debugf("Going to parse kubeware in %s", localKubePath) metadata := template.ParseMetadata(localKubePath) defaults, err := metadata.AttributeDefaults() utils.CheckError(err) log.Debugf("Building attributes") attributes := template.BuildAttributes(os.Getenv("KDEPLOY_ATTRIBUTE"), defaults) log.Debugf("Parsing services") servicesSpecs, err := metadata.ParseServices(attributes) utils.CheckError(err) log.Debugf("Parsing controllers") controllersSpecs, err := metadata.ParseControllers(attributes) utils.CheckError(err) // print resolved resources for _, s := range servicesSpecs { y, err := gyml.JSONToYAML([]byte(s)) utils.CheckError(err) fmt.Println(string(y)) } for _, c := range controllersSpecs { y, err := gyml.JSONToYAML([]byte(c)) utils.CheckError(err) fmt.Println(string(y)) } }
//go:generate goversion -major=1 -minor=0 -patch=0 func main() { bytes, err := ioutil.ReadAll(os.Stdin) if err != nil { log.Panicln(err) } json, err := yaml.JSONToYAML(bytes) if err != nil { log.Panicln(err) } print(string(json)) }
func Example_noMergingOnExplicitPaths() { commandLineFile, _ := ioutil.TempFile("", "") defer os.Remove(commandLineFile.Name()) envVarFile, _ := ioutil.TempFile("", "") defer os.Remove(envVarFile.Name()) WriteToFile(testConfigAlfa, commandLineFile.Name()) WriteToFile(testConfigConflictAlfa, envVarFile.Name()) loadingRules := ClientConfigLoadingRules{ ExplicitPath: commandLineFile.Name(), Precedence: []string{envVarFile.Name()}, } mergedConfig, err := loadingRules.Load() json, err := runtime.Encode(clientcmdlatest.Codec, mergedConfig) if err != nil { fmt.Printf("Unexpected error: %v", err) } output, err := yaml.JSONToYAML(json) if err != nil { fmt.Printf("Unexpected error: %v", err) } fmt.Printf("%v", string(output)) // Output: // apiVersion: v1 // clusters: // - cluster: // server: http://cow.org:8080 // name: cow-cluster // contexts: // - context: // cluster: cow-cluster // namespace: hammer-ns // user: red-user // name: federal-context // current-context: "" // kind: Config // preferences: {} // users: // - name: red-user // user: // token: red-token }
func testObjects(t *testing.T, list *api.List, fixtureFilename string) { filename := filepath.Join("testdata", fixtureFilename) expectedYAML, err := ioutil.ReadFile(filename) if err != nil { t.Fatal(err) } if err := runtime.EncodeList(api.Codecs.LegacyCodec(v1.SchemeGroupVersion, rbacv1alpha1.SchemeGroupVersion), list.Items); err != nil { t.Fatal(err) } jsonData, err := runtime.Encode(api.Codecs.LegacyCodec(v1.SchemeGroupVersion, rbacv1alpha1.SchemeGroupVersion), list) if err != nil { t.Fatal(err) } yamlData, err := yaml.JSONToYAML(jsonData) if err != nil { t.Fatal(err) } if string(yamlData) != string(expectedYAML) { t.Errorf("Bootstrap policy data does not match the test fixture in %s", filename) const updateEnvVar = "UPDATE_BOOTSTRAP_POLICY_FIXTURE_DATA" if os.Getenv(updateEnvVar) == "true" { if err := ioutil.WriteFile(filename, []byte(yamlData), os.FileMode(0755)); err == nil { t.Logf("Updated data in %s", filename) t.Logf("Verify the diff, commit changes, and rerun the tests") } else { t.Logf("Could not update data in %s: %v", filename, err) } } else { t.Logf("Diff between bootstrap data and fixture data in %s:\n-------------\n%s", filename, diff.StringDiff(string(yamlData), string(expectedYAML))) t.Logf("If the change is expected, re-run with %s=true to update the fixtures", updateEnvVar) } } }
func ExampleMergingEverythingNoConflicts() { commandLineFile, _ := ioutil.TempFile("", "") defer os.Remove(commandLineFile.Name()) envVarFile, _ := ioutil.TempFile("", "") defer os.Remove(envVarFile.Name()) currentDirFile, _ := ioutil.TempFile("", "") defer os.Remove(currentDirFile.Name()) homeDirFile, _ := ioutil.TempFile("", "") defer os.Remove(homeDirFile.Name()) WriteToFile(testConfigAlfa, commandLineFile.Name()) WriteToFile(testConfigBravo, envVarFile.Name()) WriteToFile(testConfigCharlie, currentDirFile.Name()) WriteToFile(testConfigDelta, homeDirFile.Name()) loadingRules := ClientConfigLoadingRules{ CommandLinePath: commandLineFile.Name(), EnvVarPath: envVarFile.Name(), CurrentDirectoryPath: currentDirFile.Name(), HomeDirectoryPath: homeDirFile.Name(), } mergedConfig, err := loadingRules.Load() json, err := clientcmdlatest.Codec.Encode(mergedConfig) if err != nil { fmt.Printf("Unexpected error: %v", err) } output, err := yaml.JSONToYAML(json) if err != nil { fmt.Printf("Unexpected error: %v", err) } fmt.Printf("%v", string(output)) // Output: // apiVersion: v1 // clusters: // - cluster: // server: http://chicken.org:8080 // name: chicken-cluster // - cluster: // server: http://cow.org:8080 // name: cow-cluster // - cluster: // server: http://horse.org:8080 // name: horse-cluster // - cluster: // server: http://pig.org:8080 // name: pig-cluster // contexts: // - context: // cluster: cow-cluster // namespace: hammer-ns // user: red-user // name: federal-context // - context: // cluster: chicken-cluster // namespace: plane-ns // user: blue-user // name: gothic-context // - context: // cluster: pig-cluster // namespace: saw-ns // user: black-user // name: queen-anne-context // - context: // cluster: horse-cluster // namespace: chisel-ns // user: green-user // name: shaker-context // current-context: "" // kind: Config // preferences: {} // users: // - name: black-user // user: // token: black-token // - name: blue-user // user: // token: blue-token // - name: green-user // user: // token: green-token // - name: red-user // user: // token: red-token }
func (s *Server) Save(request *shared.SaveRequest, response *shared.SaveResponse) error { if !s.auth.Auth([]byte(request.Path), request.Hash) { return kerr.New("GIONMMGOWA", "Auth failed") } pkg, err := pkghelp.Scan(s.ctx, request.Path) if err != nil { return kerr.Wrap("YKYVDSDGNV", err) } localContext := envctx.NewContext(s.ctx, pkg.Env) for _, info := range request.Files { // Check we only have yml, yaml or json extension. ext := filepath.Ext(info.File) if ext != ".json" && ext != ".yml" && ext != ".yaml" { return kerr.New("NDTPTCDOET", "Unsupported extension %s in %s", ext, info.File) } // Check the bytes are well formed json... o := &system.ObjectStub{} if err := system.Unmarshal(localContext, info.Bytes, o); err != nil { return kerr.Wrap("QISVPOXTCJ", err) } // Check type field exists if o.Type == nil { return kerr.New("PHINYFTGEC", "%s has no type", info.File) } // Check id field exists apart from system:package type. if o.Id == nil && *o.Type != *system.NewReference("kego.io/system", "package") { return kerr.New("NNOEQPRQXS", "%s has no id", info.File) } // Convert output to YAML if needed. output := info.Bytes if ext == ".yaml" || ext == ".yml" { var err error if output, err = yaml.JSONToYAML(output); err != nil { return kerr.Wrap("EAMEWSCAGB", err) } } var mode os.FileMode var full string file := pkg.File(info.File) if file != nil { // The file already exists, so we should use the existing filemode full = file.AbsoluteFilepath fs, err := os.Stat(full) if err != nil { return kerr.Wrap("VLIJSSVSXU", err) } mode = fs.Mode() } else { if full, err = pkghelp.Check(pkg.Env.Dir, info.File, pkg.Env.Recursive); err != nil { return kerr.Wrap("YSQEHPFIVF", err) } mode = 0644 if _, err := os.Stat(full); err == nil || !os.IsNotExist(err) { return kerr.New("XOEPAUNCXB", "Can't overwrite %s - existing file is not a valid ke data file", info.File) } } if err := ioutil.WriteFile(full, output, mode); err != nil { return kerr.Wrap("KPDYGCYOYQ", err) } response.Files = append(response.Files, shared.SaveResponseFile{ File: info.File, Hash: info.Hash, }) } return nil }
func yamlFormatter(data []byte) { out, err := yaml.JSONToYAML(data) Check(err) fmt.Print(string(out)) }
func main() { runtime.GOMAXPROCS(runtime.NumCPU()) flag.CommandLine.SetNormalizeFunc(util.WordSepNormalizeFunc) flag.Parse() if *rewrite != "" { *inputSource = *rewrite *outputDest = *rewrite } var in io.Reader if *inputSource == "-" { in = os.Stdin } else { f, err := os.Open(*inputSource) if err != nil { log.Fatalf("Couldn't open %q: %q", *inputSource, err) } defer f.Close() in = f } data, err := ioutil.ReadAll(in) if err != nil { log.Fatalf("Couldn't read from input: %q", err) } isYAML := isYAML(data) if isYAML { data, err = yaml.YAMLToJSON(data) if err != nil { log.Fatalf("Failed to convert YAML to JSON: %q", err) } } obj, err := api.Scheme.Decode(data) if err != nil { log.Fatalf("Couldn't decode input: %q", err) } outData, err := api.Scheme.EncodeToVersion(obj, *outputVersion) if err != nil { log.Fatalf("Failed to encode to version %q: %q", *outputVersion, err) } if isYAML { outData, err = yaml.JSONToYAML(outData) if err != nil { log.Fatalf("Failed to convert to YAML: %q", err) } } else if true { // TODO: figure out if input JSON was pretty. var buf bytes.Buffer err = json.Indent(&buf, outData, "", " ") if err != nil { log.Fatalf("Failed to indent JSON: %q", err) } outData = buf.Bytes() } var out io.Writer if *outputDest == "-" { out = os.Stdout } else { f, err := os.Create(*outputDest) if err != nil { log.Fatalf("Couldn't open %q: %q", *outputDest, err) } defer f.Close() out = f } if _, err = out.Write(outData); err != nil { log.Fatalf("Failed to write: %q", err) } }
func main() { viper.SetConfigType("yaml") viper.AddConfigPath(".") viper.SetConfigName("config") // name of config file (without extension) err := viper.ReadInConfig() // Find and read the config file if err != nil { // Handle errors reading the config file panic(fmt.Errorf("Fatal error config file: %s \n", err)) } file2, err := ioutil.ReadFile(viper.GetString("yaml")) if err != nil { // Handle errors reading the config file panic(fmt.Errorf("Fatal error mangas file: %s \n", err)) } mangas := &Mangas{} file, _ := yaml.YAMLToJSON(file2) json.Unmarshal(file, &mangas) var syncMalCmd = &cobra.Command{ Use: "syncMal", Short: "Sync MyAnimelist manga list with config file", Long: `Sync MyAnimelist manga list to config file in Yaml. It will make the difference between what alreay there.`, Run: func(cmd *cobra.Command, args []string) { c := mal.NewClient() c.SetCredentials(viper.GetString("myanimelist.login"), viper.GetString("myanimelist.password")) c.SetUserAgent(viper.GetString("myanimelist.apikey")) list, _, _ := c.Manga.List("Noriak") for _, manga := range list.Manga { if manga.MyStatus == 1 { mangaName := manga.SeriesTitle mangaId := manga.SeriesMangaDBID mangaIdString := strconv.Itoa(mangaId) malLastChapter := manga.MyReadChapters lastChapterRead := malLastChapter _, ok := mangas.Mangaseries[mangaIdString] mangaYaml := &Detail{} mangaYaml.Name = mangaName mangaYaml.Dir = mangaName mangaYaml.Lastchapterdownloaded = 0 mangaYaml.Url = "" mangaYaml.RawUrl = "" mangaYaml.Reading = "" if ok { yamlLastChapterDownloaded := mangas.Mangaseries[mangaIdString].Lastchapterdownloaded mangaYaml.Lastchapterdownloaded = yamlLastChapterDownloaded yamlUrl := mangas.Mangaseries[mangaIdString].Url mangaYaml.Url = yamlUrl yamlRawUrl := mangas.Mangaseries[mangaIdString].RawUrl mangaYaml.RawUrl = yamlRawUrl yamlReading := mangas.Mangaseries[mangaIdString].Reading mangaYaml.Reading = yamlReading yamlDir := mangas.Mangaseries[mangaIdString].Dir if yamlDir != "" { mangaYaml.Dir = yamlDir } yamlLastChapterRead := mangas.Mangaseries[mangaIdString].Lastchapterread if yamlLastChapterRead > malLastChapter { //update mal lastChapterRead = yamlLastChapterRead _, err := c.Manga.Update(mangaId, mal.MangaEntry{Status: "reading", Chapter: lastChapterRead}) if err != nil { fmt.Println("Error update chapter read to myanimelist") } else { fmt.Println(mangaName + " updated") } } } mangaYaml.Lastchapterread = lastChapterRead mangas.Mangaseries[mangaIdString] = *mangaYaml } } //Savetofile b, _ := json.Marshal(mangas) y, _ := yaml.JSONToYAML(b) ioutil.WriteFile(viper.GetString("yaml"), y, 0777) }, } var downloadCmd = &cobra.Command{ Use: "download", Short: "Download new chapters", Long: `Try to see if there is new chapters and download them. It will possible to read them after.`, Run: func(cmd *cobra.Command, args []string) { // Var for mangadownloader //outFlag := flag.String("out", "", "Output directory") cbzFlag := flag.Bool("cbz", true, "CBZ") pageDigitCountFlag := flag.Int("pagedigitcount", 4, "Page digit count") httpRetryFlag := flag.Int("httpretry", 10, "Http retry") parallelChapterFlag := flag.Int("parallelchapter", 2, "Parallel chapter") parallelPageFlag := flag.Int("parallelpage", 4, "Parallel page") flag.Parse() //out := *outFlag options := &mangadownloader.Options{ Cbz: *cbzFlag, PageDigitCount: *pageDigitCountFlag, ParallelChapter: *parallelChapterFlag, ParallelPage: *parallelPageFlag, } md := mangadownloader.CreateDefaultMangeDownloader() md.HttpRetry = *httpRetryFlag for key, value := range mangas.Mangaseries { mangaName := value.Name mangaDir := value.Dir lastChapterDownloaded := value.Lastchapterdownloaded urlManga := value.Url if urlManga != "" { fmt.Println("Downloading " + mangaName) u, err := url.Parse(urlManga) if err != nil { panic(err) } o, err := md.Identify(u) if err != nil { panic(err) } dirOut := viper.GetString("downloadDir") + mangaDir + "/" _ = md.DownloadManga(o.(*mangadownloader.Manga), "./mangas/", options) files, _ := ioutil.ReadDir(dirOut) var cbzFiles []os.FileInfo for _, f := range files { if strings.Contains(f.Name(), ".cbz") { cbzFiles = append(cbzFiles, f) } } sort.Sort(byName(cbzFiles)) newChapterCbz := "" for _, f := range cbzFiles { newChapterCbz = f.Name() } newChapter := strings.Split(newChapterCbz, ".") newChapterInt, _ := strconv.Atoi(newChapter[0]) if newChapterInt > lastChapterDownloaded { lastChapterDownloaded = newChapterInt } value.Lastchapterdownloaded = lastChapterDownloaded mangas.Mangaseries[key] = value //Savetofile b, _ := json.Marshal(mangas) y, _ := yaml.JSONToYAML(b) ioutil.WriteFile(viper.GetString("yaml"), y, 0777) fmt.Println("Finish downloading " + mangaName) } if value.RawUrl != "" && value.Reading != "" { cmd := exec.Command("python2", "download_raw.py", value.RawUrl, value.Reading, viper.GetString("downloadDir")) if err := cmd.Start(); err != nil { panic(err) } } } }, } var readCmd = &cobra.Command{ Use: "read", Short: "Read new downloaded chapter", Long: `Read new downloaded Mangas. It will also permit to save the new state of reading.`, Run: func(cmd *cobra.Command, args []string) { id := 1 var listMangas []string for key, value := range mangas.Mangaseries { mangaName := value.Name lastChapterDownloaded := value.Lastchapterdownloaded lastChapterRead := value.Lastchapterread if lastChapterDownloaded != lastChapterRead { fmt.Printf("%3d | %3d/%3d | %s\n", id, lastChapterRead, lastChapterDownloaded, mangaName) listMangas = append(listMangas, key) id = id + 1 } } fmt.Print("Enter id of Manga to read: ") var input int fmt.Scanln(&input) idMangaToRead := listMangas[input-1] manga := mangas.Mangaseries[idMangaToRead] mangaDir := manga.Dir lastChapter := manga.Lastchapterread lastChapter = lastChapter + 1 lastChapterStr := fmt.Sprintf("%03d", lastChapter) linkChapter := viper.GetString("downloadDir") + mangaDir + "/" + lastChapterStr + ".cbz" //Exec Mcomix cmdMcomix := exec.Command("mcomix", "-w", linkChapter) if err := cmdMcomix.Start(); err != nil { panic(err) } fmt.Print("Enter last chapter read: ") var newLastChapter int fmt.Scanln(&newLastChapter) manga.Lastchapterread = newLastChapter mangas.Mangaseries[idMangaToRead] = manga //Savetofile b, _ := json.Marshal(mangas) y, _ := yaml.JSONToYAML(b) ioutil.WriteFile(viper.GetString("yaml"), y, 0777) }, } var rootCmd = &cobra.Command{Use: "hikikomori"} rootCmd.AddCommand(syncMalCmd) rootCmd.AddCommand(downloadCmd) rootCmd.AddCommand(readCmd) rootCmd.Execute() }