// Return the given interface as a YAML byte slice. func toYaml(i interface{}) []byte { data, err := yaml.Marshal(i) if err != nil { panic(err) } return data }
func yamlFn(v interface{}) (string, error) { data, err := yaml.Marshal(v) if err != nil { return "", err } return string(data), nil }
func (metadata *RightScriptMetadata) WriteTo(script io.Writer) (n int64, err error) { if metadata.Comment == "" { metadata.Comment = "#" } if metadata.Inputs == nil { metadata.Inputs = InputMap{} } c, err := fmt.Fprintf(script, "%s ---\n", metadata.Comment) if n += int64(c); err != nil { return } yml, err := yaml.Marshal(metadata) scanner := bufio.NewScanner(bytes.NewBuffer(yml)) for scanner.Scan() { c, err = fmt.Fprintf(script, "%s %s\n", metadata.Comment, scanner.Text()) if n += int64(c); err != nil { return } } if err = scanner.Err(); err != nil { return } c, err = fmt.Fprintf(script, "%s ...\n", metadata.Comment) if n += int64(c); err != nil { return } return }
// Execute runs the command func (c *CommandPush) Execute(b *Build) (State, error) { if len(c.cfg.args) != 1 { return b.state, fmt.Errorf("PUSH requires exactly one argument") } if b.state.ImageID == "" { return b.state, fmt.Errorf("Cannot PUSH empty image") } if err := b.client.TagImage(b.state.ImageID, c.cfg.args[0]); err != nil { return b.state, err } image := imagename.NewFromString(c.cfg.args[0]) artifact := imagename.Artifact{ Name: image, Pushed: b.cfg.Push, Tag: image.GetTag(), ImageID: b.state.ImageID, BuildTime: time.Now(), } // push image and add some lines to artifacts if b.cfg.Push { digest, err := b.client.PushImage(image.String()) if err != nil { return b.state, err } artifact.Digest = digest artifact.Addressable = fmt.Sprintf("%s@%s", image.NameWithRegistry(), digest) } else { log.Infof("| Don't push. Pass --push flag to actually push to the registry") } // Publish artifact files if b.cfg.ArtifactsPath != "" { if err := os.MkdirAll(b.cfg.ArtifactsPath, 0755); err != nil { return b.state, fmt.Errorf("Failed to create directory %s for the artifacts, error: %s", b.cfg.ArtifactsPath, err) } filePath := filepath.Join(b.cfg.ArtifactsPath, artifact.GetFileName()) artifacts := imagename.Artifacts{ []imagename.Artifact{artifact}, } content, err := yaml.Marshal(artifacts) if err != nil { return b.state, err } if err := ioutil.WriteFile(filePath, content, 0644); err != nil { return b.state, fmt.Errorf("Failed to write artifact file %s, error: %s", filePath, err) } log.Infof("| Saved artifact file %s", filePath) log.Debugf("Artifact properties: %# v", pretty.Formatter(artifact)) } return b.state, nil }
func yamlFn(args ...interface{}) (result string, err error) { var ( i = 0 input interface{} ) if len(args) == 1 { input = args[0] } else if len(args) == 2 { if i, err = interfaceToInt(args[0]); err != nil { return "", err } input = args[1] } else { return "", fmt.Errorf("yaml helper expects from 1 to 2 arguments, %d given", len(args)) } data, err := yaml.Marshal(input) if err != nil { return "", err } result = string(data) if i > 0 { result = indent(strings.Repeat(" ", i), result) } return result, nil }
// CreateContainerOptions returns create configuration eatable by go-dockerclient func (a *Container) CreateContainerOptions() (*docker.CreateContainerOptions, error) { apiConfig := a.Config.GetAPIConfig() yamlData, err := yaml.Marshal(a.Config) if err != nil { return nil, err } // Copy labels because we want to assign some extra stuff labels := map[string]string{} for k, v := range apiConfig.Labels { labels[k] = v } labels["rocker-compose-id"] = util.GenerateRandomID() labels["rocker-compose-config"] = string(yamlData) apiConfig.Labels = labels apiConfig.Image = a.Image.String() return &docker.CreateContainerOptions{ Name: a.Name.String(), Config: apiConfig, HostConfig: a.Config.GetAPIHostConfig(), }, nil }
func (c *Config) Dump() (yamlDoc string, err error) { defer errs.PassE(&err) buf, err := yaml.Marshal(c.ast) errs.CheckE(err) yamlDoc = string(buf) return }
func TestYamlLink(t *testing.T) { assertions := map[string]string{ "platform.statsd": "platform.statsd:statsd", "platform.statsd:metrics": "platform.statsd:metrics", "/platform.statsd": "platform.statsd:statsd", "/platform.statsd:metrics": "platform.statsd:metrics", "statsd": "statsd:statsd", "statsd:metrics": "statsd:metrics", ".statsd": "statsd:statsd", ".statsd:metrics": "statsd:metrics", "": "\"\"", } for inYaml, outYaml := range assertions { v := Link{} if err := yaml.Unmarshal([]byte(inYaml), &v); err != nil { t.Fatal(err) } data, err := yaml.Marshal(v) if err != nil { t.Fatal(err) } assert.Equal(t, outYaml, strings.TrimSpace(string(data))) } }
func (s *Supervisor) saveDB() error { s.mu.Lock() defer s.mu.Unlock() data, err := yaml.Marshal(s.programs()) if err != nil { return err } return ioutil.WriteFile(s.programPath(), data, 0644) }
func TestImagename_ToYaml(t *testing.T) { value := struct { Name *ImageName }{ NewFromString("hub/ns/name:1"), } data, err := yaml.Marshal(value) if err != nil { t.Fatal(err) } assert.Equal(t, "name: hub/ns/name:1\n", string(data)) }
func (config *ConfigViper) ShowConfiguration(output io.Writer) error { // Check if config file exists if _, err := os.Stat(config.ConfigFileUsed()); err != nil { return err } yml, err := yaml.Marshal(config.AllSettings()) if err != nil { return err } output.Write(yml) return nil }
func (a *yamlTestCases) run(t *testing.T) error { for inYaml, outYaml := range a.assertions { v := &Container{} if err := yaml.Unmarshal([]byte(inYaml), v); err != nil { return fmt.Errorf("Failed unmarshal for test %q, error: %s", inYaml, err) } data, err := yaml.Marshal(v) if err != nil { return fmt.Errorf("Failed marshal for test %q, error: %s", inYaml, err) } assert.Equal(t, outYaml, strings.TrimSpace(string(data))) } return nil }
func compareYaml(name string, a, b *Container) (bool, error) { av := reflect.Indirect(reflect.ValueOf(a)).FieldByName(name) bv := reflect.Indirect(reflect.ValueOf(b)).FieldByName(name) isSlice := av.Type().Kind() == reflect.Slice isMap := av.Type().Kind() == reflect.Map // empty values and nil pointer should be considered equal if av.IsNil() && !isSlice && !isMap { av = reflect.New(av.Type().Elem()) } if bv.IsNil() && !isSlice && !isMap { bv = reflect.New(bv.Type().Elem()) } // sort lists which should not consider different order to be a change if isSlice && name != "Entrypoint" && name != "Cmd" { aSorted := newYamlSortable(av) sort.Sort(aSorted) av = reflect.ValueOf(aSorted) bSorted := newYamlSortable(bv) sort.Sort(bSorted) bv = reflect.ValueOf(bSorted) } yml1, err := yaml.Marshal(av.Interface()) if err != nil { return false, err } yml2, err := yaml.Marshal(bv.Interface()) if err != nil { return false, err } return string(yml1) == string(yml2), nil }
func (self *Api) UpdateAuth(token *oauth2.Token) error { // Save auth data log.WithFields(log.Fields{"Target": self.AuthFile}).Debug("Saving authentication data") yml, err := yaml.Marshal(&token) if err != nil { return err } err = ioutil.WriteFile(self.AuthFile, yml, 0600) if err != nil { return err } return nil }
// WriteConfig writes config from file to structure func WriteConfig(config Config, fname string) error { yamlConfig := &yamlConfig{} yamlConfig.Services = make([]yamlServiceConfig, len(config.Services)) i := 0 for k, v := range config.Services { ysc := &yamlServiceConfig{} ysc.Service = k ysc.Api = v.Common.Api ysc.Config = v.ServiceSpecific yamlConfig.Services[i] = *ysc i++ } b, err := yaml.Marshal(yamlConfig) if err != nil { return err } return ioutil.WriteFile(fname, b, 0777) }
func readConf(filename string) (c Configuration, err error) { // initial default value c.Server.Addr = ":11313" // in memory of 08-31 13:13 c.Client.ServerURL = "http://localhost:11313" data, err := ioutil.ReadFile(filename) if err != nil { data = []byte("") } err = yaml.Unmarshal(data, &c) if err != nil { return } cfgDir := filepath.Dir(filename) if !IsDir(cfgDir) { os.MkdirAll(cfgDir, 0755) } data, _ = yaml.Marshal(c) err = ioutil.WriteFile(filename, data, 0644) return }
func TestYamlPortBinding(t *testing.T) { assertions := map[string]string{ "": "\"\"", "8000": "8000/tcp", "8125/udp": "8125/udp", "8081:8000": "8081:8000/tcp", "8126:8125/udp": "8126:8125/udp", "0.0.0.0::5959": "0.0.0.0::5959/tcp", "0.0.0.0:8081:80": "0.0.0.0:8081:80/tcp", "0.0.0.0:8126:8125/udp": "0.0.0.0:8126:8125/udp", } for inYaml, outYaml := range assertions { v := PortBinding{} if err := yaml.Unmarshal([]byte(inYaml), &v); err != nil { t.Fatal(err) } data, err := yaml.Marshal(v) if err != nil { t.Fatal(err) } assert.Equal(t, outYaml, strings.TrimSpace(string(data))) } }
// Process processes the code yielding a []byte of the YAML // representation of the Swagger defintion. The processing rules are // as follows: // 1. For each common.Service found, go over its Routes (whatever Routes() method yields) // and based on that create the paths and operations. // 2. For structs, field-level comments are taken from the description value of // "romana" structure tag. For example: // type Policy struct { // Direction string `json:"direction,omitempty" romana:"desc:Direction is one of 'ingress' or egress'."` // ... // } func (rd *Swaggerer) Process() ([]byte, error) { rd.init() rd.swagger.Paths = rd.getPaths() json, err := yaml.Marshal(rd.swagger) return json, err }
func (items yamlSortable) Less(i, j int) bool { yml1, _ := yaml.Marshal(items[i]) yml2, _ := yaml.Marshal(items[j]) return string(yml1) < string(yml2) }
// Obtain input via STDIN then print out to config file // Example of config file // login: // default_account: acct1 // accounts: // acct1: // id: 67972 // host: us-3.rightscale.com // refresh_token: abc123abc123abc123abc123abc123abc123abc1 // acct2: // id: 60073 // host: us-4.rightscale.com // refresh_token: zxy987zxy987zxy987zxy987xzy987zxy987xzy9 func (config *ConfigViper) SetAccount(name string, setDefault bool, input io.Reader, output io.Writer) error { // if the default account isn't set we should set it to the account we are setting if !config.IsSet("login.default_account") { setDefault = true } // get the settings and specifically the login settings into a map we can manipulate and marshal to YAML unhindered // by the meddling of the Viper settings := config.AllSettings() loginSettings := settings["login"].(map[interface{}]interface{}) // set the default account if we want or need to if setDefault { loginSettings["default_account"] = name } // get the previous value for the named account if it exists and construct a new account to populate oldAccount, ok := config.Accounts[name] newAccount := &Account{} // prompt for the account ID and use the old value if nothing is entered fmt.Fprint(output, "Account ID") if ok { fmt.Fprintf(output, " (%d)", oldAccount.Id) } fmt.Fprint(output, ": ") fmt.Fscanln(input, &newAccount.Id) if ok && newAccount.Id == 0 { newAccount.Id = oldAccount.Id } // prompt for the API endpoint host and use the old value if nothing is entered fmt.Fprint(output, "API endpoint host") if ok { fmt.Fprintf(output, " (%s)", oldAccount.Host) } fmt.Fprint(output, ": ") fmt.Fscanln(input, &newAccount.Host) if ok && newAccount.Host == "" { newAccount.Host = oldAccount.Host } // prompt for the refresh token and use the old value if nothing is entered fmt.Fprint(output, "Refresh token") if ok { fmt.Fprintf(output, " (%s)", oldAccount.RefreshToken) } fmt.Fprint(output, ": ") fmt.Fscanln(input, &newAccount.RefreshToken) if ok && newAccount.RefreshToken == "" { newAccount.RefreshToken = oldAccount.RefreshToken } // add the new account to the map of accounts overwriting any old value accounts := loginSettings["accounts"].(map[interface{}]interface{}) accounts[name] = newAccount // render the settings map as YAML yml, err := yaml.Marshal(settings) if err != nil { return err } configPath := config.ConfigFileUsed() // back up the current config file before writing a new one or if one does not exist, make sure the directory exists if err := os.Rename(configPath, configPath+".bak"); err != nil { if os.IsNotExist(err) { if err := os.MkdirAll(filepath.Dir(configPath), 0700); err != nil { return err } } else { return err } } // create a new config file which only the current user can read or write configFile, err := os.OpenFile(configPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) if err != nil { return err } defer configFile.Close() // write the YAML into the config file if _, err := configFile.Write(yml); err != nil { return err } return nil }
func pinCommand(ctx *cli.Context) { initLogs(ctx) var ( vars template.Vars data []byte output = ctx.String("output") format = ctx.String("type") local = ctx.BoolT("local") hub = ctx.BoolT("hub") fd = os.Stdout ) if output == "-" && !ctx.GlobalIsSet("verbose") { log.SetLevel(log.WarnLevel) } dockerCli := initDockerClient(ctx) config := initComposeConfig(ctx, dockerCli) auth := initAuthConfig(ctx) compose, err := compose.New(&compose.Config{ Manifest: config, Docker: dockerCli, Auth: auth, }) if err != nil { log.Fatal(err) } if vars, err = compose.PinAction(local, hub); err != nil { log.Fatal(err) } if output != "-" { if fd, err = os.Create(output); err != nil { log.Fatal(err) } defer fd.Close() if ext := filepath.Ext(output); !ctx.IsSet("type") && ext == ".json" { format = "json" } } switch format { case "yaml": if data, err = yaml.Marshal(vars); err != nil { log.Fatal(err) } case "json": if data, err = json.Marshal(vars); err != nil { log.Fatal(err) } default: log.Fatalf("Possible tyoes are `yaml` and `json`, unknown type `%s`", format) } if _, err := io.Copy(fd, bytes.NewReader(data)); err != nil { log.Fatal(err) } }
func main() { func() { rs := make(map[string]interface{}) rs["Go"] = "Google" d, err := yaml.Marshal(rs) if err != nil { panic(err) } fmt.Println() fmt.Printf("yaml.Marshal with map: %+v\n", string(d)) // yaml.Marshal with map: Go: Google }() func() { rs := make(map[interface{}]interface{}) if err := yaml.Unmarshal([]byte(sourceData), &rs); err != nil { panic(err) } fmt.Println() fmt.Printf("yaml.Unmarshal with map: %+v\n", rs) // yaml.Unmarshal with map: map[platform:map[desktop:map[] mobile:map[] // ... for k := range rs { switch t := k.(type) { case string: fmt.Println(t) } } }() func() { rs := struct { Name string Number int `num` Category struct { Location string } `Category` }{} rs.Name = "Google" rs.Category = struct { Location string }{ Location: "USA", } d, err := yaml.Marshal(rs) if err != nil { panic(err) } fmt.Println() fmt.Printf("yaml.Marshal with struct:\n%+v\n", string(d)) // yaml.Marshal with struct: /* name: Google num: 0 Category: location: USA */ }() func() { rs := data{} if err := yaml.Unmarshal([]byte(sourceData), &rs); err != nil { panic(err) } fmt.Println() fmt.Printf("yaml.Unmarshal with struct: %+v\n", rs) // yaml.Unmarshal with struct: {ProductType:[{PartID:6496293} {PartID:6493671}]} }() fmt.Println() fmt.Println("isJSON:", isJSON([]byte(sourceData))) // false }
func (m *Migration) ConvertToYaml() (string, error) { yamlBytes, err := yaml.Marshal(m.Content) return string(yamlBytes), err }
func stDownload(href, downloadTo string, usePublished bool, downloadMciSettings bool, scriptPath string) { client, _ := Config.Account.Client15() stLocator := client.ServerTemplateLocator(href) st, err := stLocator.Show(rsapi.APIParams{"view": "inputs_2_0"}) if err != nil { fatalError("Could not find ServerTemplate with href %s: %s", href, err.Error()) } if downloadTo == "" { downloadTo = cleanFileName(st.Name) + ".yml" } else if isDirectory(downloadTo) { downloadTo = filepath.Join(downloadTo, cleanFileName(st.Name)+".yml") } fmt.Printf("Downloading '%s' to '%s'\n", st.Name, downloadTo) //------------------------------------- // MultiCloudImages //------------------------------------- mcis, err := downloadMultiCloudImages(st, downloadMciSettings) if err != nil { fatalError("Could not get MCIs from API: %s", err.Error()) } //------------------------------------- // RightScripts //------------------------------------- rbLocator := client.RunnableBindingLocator(getLink(st.Links, "runnable_bindings")) rbs, err := rbLocator.Index(rsapi.APIParams{}) if err != nil { fatalError("Could not find attached RightScripts with href %s: %s", rbLocator.Href, err.Error()) } rightScripts := make(map[string][]*RightScript) countBySequence := make(map[string]int) seenRightscript := make(map[string]*RightScript) for _, rb := range rbs { countBySequence[strings.Title(rb.Sequence)] += 1 seenRightscript[getLink(rb.Links, "right_script")] = nil } for sequenceType, count := range countBySequence { rightScripts[sequenceType] = make([]*RightScript, count) } fmt.Printf("Downloading %d attached RightScripts:\n", len(seenRightscript)) for _, rb := range rbs { rsHref := getLink(rb.Links, "right_script") if rsHref == "" { fatalError("Could not download ServerTemplate, it has attached cookbook recipes, which are not supported by this tool.\n") } if scr, ok := seenRightscript[rsHref]; ok && scr != nil { rightScripts[strings.Title(rb.Sequence)][rb.Position-1] = scr continue } newScript := RightScript{ Type: LocalRightScript, Path: cleanFileName(rb.RightScript.Name), } if usePublished { // We repull the rightscript here to get the description field, which we need to break ties between // similarly named publications! rsLoc := client.RightScriptLocator(rsHref) rs, err := rsLoc.Show(rsapi.APIParams{}) if err != nil { fatalError("Could not get RightScript %s: %s\n", rsHref, err.Error()) } pub, err := findPublication("RightScript", rs.Name, rs.Revision, map[string]string{`Description`: rs.Description}) if err != nil { fatalError("Error finding publication: %s\n", err.Error()) } if pub != nil { fmt.Printf("Not downloading '%s' to disk, using Revision %s, Publisher '%s' from the MultiCloud Marketplace\n", rs.Name, formatRev(rs.Revision), pub.Publisher) newScript = RightScript{ Type: PublishedRightScript, Name: pub.Name, Revision: pub.Revision, Publisher: pub.Publisher, } } } rightScripts[strings.Title(rb.Sequence)][rb.Position-1] = &newScript if newScript.Type == LocalRightScript { if scriptPath == "" { downloadedTo := rightScriptDownload(rsHref, filepath.Dir(downloadTo)) newScript.Path = strings.TrimPrefix(downloadedTo, filepath.Dir(downloadTo)+string(filepath.Separator)) } else { // Create scripts directory err := os.MkdirAll(filepath.Join(filepath.Dir(downloadTo), scriptPath), 0755) if err != nil { fatalError("Error creating directory: %s", err.Error()) } downloadedTo := rightScriptDownload(rsHref, filepath.Join(filepath.Dir(downloadTo), scriptPath)) newScript.Path = strings.TrimPrefix(downloadedTo, filepath.Dir(downloadTo)+string(filepath.Separator)) } } seenRightscript[rsHref] = &newScript } //------------------------------------- // Alerts //------------------------------------- alerts, err := downloadAlerts(st) if err != nil { fatalError("Could not get Alerts from API: %s", err.Error()) } //------------------------------------- // Inputs //------------------------------------- stInputs := make(map[string]*InputValue) for _, inputHash := range st.Inputs { iv, err := parseInputValue(inputHash["value"]) if err != nil { fatalError("Error parsing input value from API:", err.Error()) } // The API returns "inherit" values as "blank" values. Blank really means an // empty text string, which is usually not what was meant -- usually people // didn't mean to set the input at the ST level, in which case the // RightScript sets the value. Note that the user can still set the input to // blank at the RightScript level so it isn't much of a limitation this // occassionally doesn't always get set correctly on download // TBD: this can be improved slightly -- we can cross check the input with // the rightscript it came form. If its the same, we inherit. If we can // assume the ST overrides and use that value. if iv.Type != "blank" { stInputs[inputHash["name"]] = iv } } //------------------------------------- // ServerTemplate YAML itself finally //------------------------------------- stDef := ServerTemplate{ Name: st.Name, Description: removeCarriageReturns(st.Description), Inputs: stInputs, MultiCloudImages: mcis, RightScripts: rightScripts, Alerts: alerts, } bytes, err := yaml.Marshal(&stDef) if err != nil { fatalError("Creating yaml failed: %s", err.Error()) } err = ioutil.WriteFile(downloadTo, bytes, 0644) if err != nil { fatalError("Could not create file: %s", err.Error()) } fmt.Printf("Finished downloading '%s' to '%s'\n", st.Name, downloadTo) }
// MarshalYAML serializes ImageName to YAML string func (img ImageName) MarshalYAML() ([]byte, error) { return yaml.Marshal(img.String()) }