Ejemplo n.º 1
0
func TestIntOrStringUnmarshalYAML(t *testing.T) {
	{
		yamlCodeInt := "val: 123\n"

		var result IntOrStringHolder
		if err := yaml.Unmarshal([]byte(yamlCodeInt), &result); err != nil {
			t.Errorf("Failed to unmarshal: %v", err)
		}
		if result.IOrS.Kind != IntstrInt || result.IOrS.IntVal != 123 {
			t.Errorf("Failed to unmarshal int-typed IntOrString: %v", result)
		}
	}

	{
		yamlCodeStr := "val: \"123\"\n"

		var result IntOrStringHolder
		if err := yaml.Unmarshal([]byte(yamlCodeStr), &result); err != nil {
			t.Errorf("Failed to unmarshal: %v", err)
		}
		if result.IOrS.Kind != IntstrString || result.IOrS.StrVal != "123" {
			t.Errorf("Failed to unmarshal string-typed IntOrString: %v", result)
		}
	}
}
Ejemplo n.º 2
0
func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	defer httplog.MakeLogged(req, &w).Log()

	u, err := url.ParseRequestURI(req.RequestURI)
	if err != nil {
		s.error(w, err)
		return
	}
	// TODO: use an http.ServeMux instead of a switch.
	switch {
	case u.Path == "/container" || u.Path == "/containers":
		defer req.Body.Close()
		data, err := ioutil.ReadAll(req.Body)
		if err != nil {
			s.error(w, err)
			return
		}
		if u.Path == "/container" {
			// This is to provide backward compatibility. It only supports a single manifest
			var manifest api.ContainerManifest
			err = yaml.Unmarshal(data, &manifest)
			if err != nil {
				s.error(w, err)
				return
			}
			s.UpdateChannel <- manifestUpdate{httpServerSource, []api.ContainerManifest{manifest}}
		} else if u.Path == "/containers" {
			var manifests []api.ContainerManifest
			err = yaml.Unmarshal(data, &manifests)
			if err != nil {
				s.error(w, err)
				return
			}
			s.UpdateChannel <- manifestUpdate{httpServerSource, manifests}
		}
	case u.Path == "/podInfo":
		podID := u.Query().Get("podID")
		if len(podID) == 0 {
			http.Error(w, "Missing 'podID=' query entry.", http.StatusBadRequest)
			return
		}
		info, err := s.Kubelet.GetPodInfo(podID)
		if err != nil {
			s.error(w, err)
			return
		}
		data, err := json.Marshal(info)
		if err != nil {
			s.error(w, err)
			return
		}
		w.WriteHeader(http.StatusOK)
		w.Header().Add("Content-type", "application/json")
		w.Write(data)
	case strings.HasPrefix(u.Path, "/stats"):
		s.serveStats(w, req)
	default:
		s.DelegateHandler.ServeHTTP(w, req)
	}
}
Ejemplo n.º 3
0
// Decode converts a JSON string back into a pointer to an api object. Deduces the type
// based upon the Kind field (set by encode).
func Decode(data []byte) (interface{}, error) {
	findKind := struct {
		Kind string `json:"kind,omitempty" yaml:"kind,omitempty"`
	}{}
	// yaml is a superset of json, so we use it to decode here. That way, we understand both.
	err := yaml.Unmarshal(data, &findKind)
	if err != nil {
		return nil, fmt.Errorf("couldn't get kind: %#v", err)
	}
	objType, found := knownTypes[findKind.Kind]
	if !found {
		return nil, fmt.Errorf("%v is not a known type", findKind.Kind)
	}
	obj := reflect.New(objType).Interface()
	err = yaml.Unmarshal(data, obj)
	if err != nil {
		return nil, err
	}
	_, jsonBase, err := nameAndJSONBase(obj)
	if err != nil {
		return nil, err
	}
	// Don't leave these set. Track type with go's type.
	jsonBase.Kind = ""
	return obj, nil
}
Ejemplo n.º 4
0
func TestPathMatcherFactoryBadConfig(t *testing.T) {
	config := []byte(`
paths:
  - "/v1.1/push/events/.*"
  - "/v2.1/.*/events$"
`)
	pathConfig := TestPathMatcherConfig{}
	yaml.Unmarshal(config, &pathConfig)

	factory := pathMatcherFactory{}
	_, err := factory.Create(pathConfig.Paths)
	if err == nil {
		t.Error("Expected error when headers have no name")
	}

	config = []byte(`
paths:
  match_any: "hello"
`)
	yaml.Unmarshal(config, &pathConfig)
	_, err = factory.Create(pathConfig.Paths)
	if err == nil {
		t.Error("Expected error when headers have no name")
	}
}
Ejemplo n.º 5
0
func main() {
	if len(os.Args) != 3 {
		checkErr(fmt.Errorf(usage))
	}
	specFilename := os.Args[1]
	configFilename := os.Args[2]

	specData, err := ReadConfigData(specFilename)
	checkErr(err)

	spec := EnscopeSpec{}
	err = yaml.Unmarshal(specData, &spec)
	checkErr(err)

	configData, err := ReadConfigData(configFilename)
	checkErr(err)

	var data interface{}

	err = yaml.Unmarshal([]byte(configData), &data)
	checkErr(err)

	xData, err := enscope("", spec, data)
	checkErr(err)

	out, err := yaml.Marshal(xData)
	checkErr(err)

	fmt.Print(string(out))
}
Ejemplo n.º 6
0
func TestHeaderMatcherFactoryBadData(t *testing.T) {
	config := []byte(`
headers:
  match_any:
    - "Authorization": "Bearer.*"
    - name: "X-Forwarded-For"
`)
	headerConfig := TestHeaderConfig{}
	yaml.Unmarshal(config, &headerConfig)

	factory := headerMatcherFactory{}
	if _, err := factory.Create(headerConfig.Headers); err == nil {
		t.Error("Expected error when headers have no name")
	}

	config = []byte(`
headers:
  - "Authorization": "Bearer.*"
  - name: "X-Forwarded-For"
`)
	yaml.Unmarshal(config, &headerConfig)
	if _, err := factory.Create(headerConfig.Headers); err == nil {
		t.Error("expected error when match_any is missing")
	}
}
Ejemplo n.º 7
0
// DecodeInto parses a YAML or JSON string and stores it in obj. Returns an error
// if data.Kind is set and doesn't match the type of obj. Obj should be a
// pointer to an api type.
// If obj's version doesn't match that in data, an attempt will be made to convert
// data into obj's version.
func (s *Scheme) DecodeInto(data []byte, obj interface{}) error {
	if len(data) == 0 {
		// This is valid YAML, but it's a bad idea not to return an error
		// for an empty string-- that's almost certainly not what the caller
		// was expecting.
		return errors.New("empty input")
	}
	dataVersion, dataKind, err := s.DataVersionAndKind(data)
	if err != nil {
		return err
	}
	objVersion, objKind, err := s.ObjectVersionAndKind(obj)
	if err != nil {
		return err
	}
	if dataKind == "" {
		// Assume objects with unset Kind fields are being unmarshalled into the
		// correct type.
		dataKind = objKind
	}
	if dataKind != objKind {
		return fmt.Errorf("data of kind '%v', obj of type '%v'", dataKind, objKind)
	}
	if dataVersion == "" {
		// Assume objects with unset Version fields are being unmarshalled into the
		// correct type.
		dataVersion = objVersion
	}

	if objVersion == dataVersion {
		// Easy case!
		err = yaml.Unmarshal(data, obj)
		if err != nil {
			return err
		}
	} else {
		external, err := s.NewObject(dataVersion, dataKind)
		if err != nil {
			return fmt.Errorf("unable to create new object of type ('%s', '%s')", dataVersion, dataKind)
		}
		// yaml is a superset of json, so we use it to decode here. That way,
		// we understand both.
		err = yaml.Unmarshal(data, external)
		if err != nil {
			return err
		}
		err = s.converter.Convert(external, obj, 0, s.generateConvertMeta(dataVersion, objVersion))
		if err != nil {
			return err
		}
	}

	// Version and Kind should be blank in memory.
	return s.SetVersionAndKind("", "", obj)
}
Ejemplo n.º 8
0
func (kl *Kubelet) extractFromHTTP(url string, updateChannel chan<- manifestUpdate) error {
	request, err := http.NewRequest("GET", url, nil)
	if err != nil {
		return err
	}
	response, err := http.DefaultClient.Do(request)
	if err != nil {
		return err
	}
	defer response.Body.Close()
	data, err := ioutil.ReadAll(response.Body)
	if err != nil {
		return err
	}
	if len(data) == 0 {
		return fmt.Errorf("zero-length data received from %v", url)
	}

	// First try as if it's a single manifest
	var manifest api.ContainerManifest
	singleErr := yaml.Unmarshal(data, &manifest)
	if singleErr == nil && manifest.Version == "" {
		// If data is a []ContainerManifest, trying to put it into a ContainerManifest
		// will not give an error but also won't set any of the fields.
		// Our docs say that the version field is mandatory, so using that to judge wether
		// this was actually successful.
		singleErr = fmt.Errorf("got blank version field")
	}
	if singleErr == nil {
		updateChannel <- manifestUpdate{httpClientSource, []api.ContainerManifest{manifest}}
		return nil
	}

	// That didn't work, so try an array of manifests.
	var manifests []api.ContainerManifest
	multiErr := yaml.Unmarshal(data, &manifests)
	// We're not sure if the person reading the logs is going to care about the single or
	// multiple manifest unmarshalling attempt, so we need to put both in the logs, as is
	// done at the end. Hence not returning early here.
	if multiErr == nil && len(manifests) == 0 {
		multiErr = fmt.Errorf("no elements in ContainerManifest array")
	}
	if multiErr == nil && manifests[0].Version == "" {
		multiErr = fmt.Errorf("got blank version field")
	}
	if multiErr == nil {
		updateChannel <- manifestUpdate{httpClientSource, manifests}
		return nil
	}
	return fmt.Errorf("%v: received '%v', but couldn't parse as a "+
		"single manifest (%v: %#v) or as multiple manifests (%v: %#v).\n",
		url, string(data), singleErr, manifest, multiErr, manifests)
}
Ejemplo n.º 9
0
func TestYAMLPrinterPrint(t *testing.T) {
	type testStruct struct {
		Key        string         `yaml:"Key" json:"Key"`
		Map        map[string]int `yaml:"Map" json:"Map"`
		StringList []string       `yaml:"StringList" json:"StringList"`
		IntList    []int          `yaml:"IntList" json:"IntList"`
	}
	testData := testStruct{
		"testValue",
		map[string]int{"TestSubkey": 1},
		[]string{"a", "b", "c"},
		[]int{1, 2, 3},
	}
	printer := &YAMLPrinter{}
	buf := bytes.NewBuffer([]byte{})

	err := printer.Print([]byte("invalidJSON"), buf)
	if err == nil {
		t.Error("Error: didn't fail on invalid JSON data")
	}

	jTestData, err := json.Marshal(&testData)
	if err != nil {
		t.Fatal("Unexpected error: couldn't marshal test data")
	}
	err = printer.Print(jTestData, buf)
	if err != nil {
		t.Fatal(err)
	}
	var poutput testStruct
	err = yaml.Unmarshal(buf.Bytes(), &poutput)
	if err != nil {
		t.Fatal(err)
	}
	if !reflect.DeepEqual(testData, poutput) {
		t.Errorf("Test data and unmarshaled data are not equal: %#v vs %#v", poutput, testData)
	}

	obj := api.Pod{
		JSONBase: api.JSONBase{ID: "foo"},
	}
	buf.Reset()
	printer.PrintObj(obj, buf)
	var objOut api.Pod
	err = yaml.Unmarshal([]byte(buf.String()), &objOut)
	if err != nil {
		t.Errorf("Unexpeted error: %#v", err)
	}
	if !reflect.DeepEqual(obj, objOut) {
		t.Errorf("Unexpected inequality: %#v vs %#v", obj, objOut)
	}
}
Ejemplo n.º 10
0
// DecodeInto parses a YAML or JSON string and stores it in obj. Returns an error
// if data.Kind is set and doesn't match the type of obj. Obj should be a
// pointer to an api type.
// If obj's version doesn't match that in data, an attempt will be made to convert
// data into obj's version.
func (s *Scheme) DecodeInto(data []byte, obj interface{}) error {
	dataVersion, dataKind, err := s.DataVersionAndKind(data)
	if err != nil {
		return err
	}
	objVersion, objKind, err := s.ObjectVersionAndKind(obj)
	if err != nil {
		return err
	}
	if dataKind == "" {
		// Assume objects with unset Kind fields are being unmarshalled into the
		// correct type.
		dataKind = objKind
	}
	if dataKind != objKind {
		return fmt.Errorf("data of kind '%v', obj of type '%v'", dataKind, objKind)
	}
	if dataVersion == "" {
		// Assume objects with unset Version fields are being unmarshalled into the
		// correct type.
		dataVersion = objVersion
	}

	if objVersion == dataVersion {
		// Easy case!
		err = yaml.Unmarshal(data, obj)
		if err != nil {
			return err
		}
	} else {
		external, err := s.NewObject(dataVersion, dataKind)
		if err != nil {
			return fmt.Errorf("Unable to create new object of type ('%s', '%s')", dataVersion, dataKind)
		}
		// yaml is a superset of json, so we use it to decode here. That way,
		// we understand both.
		err = yaml.Unmarshal(data, external)
		if err != nil {
			return err
		}
		err = s.converter.Convert(external, obj, 0)
		if err != nil {
			return err
		}
	}

	// Version and Kind should be blank in memory.
	return s.SetVersionAndKind("", "", obj)
}
Ejemplo n.º 11
0
func TestIntOrStringMarshalJSONUnmarshalYAML(t *testing.T) {
	cases := []struct {
		input IntOrString
	}{
		{IntOrString{Kind: IntstrInt, IntVal: 123}},
		{IntOrString{Kind: IntstrString, StrVal: "123"}},
	}

	for _, c := range cases {
		input := IntOrStringHolder{c.input}
		jsonMarshalled, err := json.Marshal(&input)
		if err != nil {
			t.Errorf("1: Failed to marshal input: '%v': %v", input, err)
		}

		var result IntOrStringHolder
		err = yaml.Unmarshal(jsonMarshalled, &result)
		if err != nil {
			t.Errorf("2: Failed to unmarshal '%+v': %v", string(jsonMarshalled), err)
		}

		if !reflect.DeepEqual(input, result) {
			t.Errorf("3: Failed to marshal input '%+v': got %+v", input, result)
		}
	}
}
Ejemplo n.º 12
0
func (s *S) TestUnmarshalErrors(c *C) {
	for _, item := range unmarshalErrorTests {
		var value interface{}
		err := yaml.Unmarshal([]byte(item.data), &value)
		c.Assert(err, ErrorMatches, item.error, Commentf("Partial unmarshal: %#v", value))
	}
}
Ejemplo n.º 13
0
func (s *S) TestUnmarshal(c *C) {
	for i, item := range unmarshalTests {
		t := reflect.ValueOf(item.value).Type()
		var value interface{}
		switch t.Kind() {
		case reflect.Map:
			value = reflect.MakeMap(t).Interface()
		case reflect.String:
			t := reflect.ValueOf(item.value).Type()
			v := reflect.New(t)
			value = v.Interface()
		default:
			pt := reflect.ValueOf(item.value).Type()
			pv := reflect.New(pt.Elem())
			value = pv.Interface()
		}
		err := yaml.Unmarshal([]byte(item.data), value)
		c.Assert(err, IsNil, Commentf("Item #%d", i))
		if t.Kind() == reflect.String {
			c.Assert(*value.(*string), Equals, item.value, Commentf("Item #%d", i))
		} else {
			c.Assert(value, DeepEquals, item.value, Commentf("Item #%d", i))
		}
	}
}
Ejemplo n.º 14
0
// handleContainer handles container requests against the Kubelet.
func (s *Server) handleContainer(w http.ResponseWriter, req *http.Request) {
	defer req.Body.Close()
	data, err := ioutil.ReadAll(req.Body)
	if err != nil {
		s.error(w, err)
		return
	}
	// This is to provide backward compatibility. It only supports a single manifest
	var pod api.BoundPod
	var containerManifest api.ContainerManifest
	err = yaml.Unmarshal(data, &containerManifest)
	if err != nil {
		s.error(w, err)
		return
	}
	pod.Name = containerManifest.ID
	pod.UID = containerManifest.UUID
	pod.Spec.Containers = containerManifest.Containers
	pod.Spec.Volumes = containerManifest.Volumes
	pod.Spec.RestartPolicy = containerManifest.RestartPolicy
	//TODO: sha1 of manifest?
	if pod.Name == "" {
		pod.Name = "1"
	}
	if pod.UID == "" {
		pod.UID = "1"
	}
	s.updates <- PodUpdate{[]api.BoundPod{pod}, SET}

}
Ejemplo n.º 15
0
func TestTimeMarshalJSONUnmarshalYAML(t *testing.T) {
	cases := []struct {
		input Time
	}{
		{Time{}},
		{Date(1998, time.May, 5, 5, 5, 5, 50, time.UTC).Rfc3339Copy()},
		{Date(1998, time.May, 5, 5, 5, 5, 0, time.UTC).Rfc3339Copy()},
	}

	for _, c := range cases {
		input := TimeHolder{c.input}
		jsonMarshalled, err := json.Marshal(&input)
		if err != nil {
			t.Errorf("1: Failed to marshal input: '%v': %v", input, err)
		}

		var result TimeHolder
		err = yaml.Unmarshal(jsonMarshalled, &result)
		if err != nil {
			t.Errorf("2: Failed to unmarshal '%+v': %v", string(jsonMarshalled), err)
		}

		if !reflect.DeepEqual(input, result) {
			t.Errorf("3: Failed to marshal input '%+v': got %+v", input, result)
		}
	}
}
Ejemplo n.º 16
0
// DataToObjects converts the raw JSON data into API objects
func DataToObjects(m meta.RESTMapper, t runtime.ObjectTyper, data []byte) (result []runtime.Object, errors util.ErrorList) {
	configObj := []runtime.RawExtension{}

	if err := yaml.Unmarshal(data, &configObj); err != nil {
		errors = append(errors, fmt.Errorf("config unmarshal: %v", err))
		return result, errors
	}

	for i, in := range configObj {
		version, kind, err := t.DataVersionAndKind(in.RawJSON)
		if err != nil {
			errors = append(errors, fmt.Errorf("item[%d] kind: %v", i, err))
			continue
		}

		mapping, err := m.RESTMapping(version, kind)
		if err != nil {
			errors = append(errors, fmt.Errorf("item[%d] mapping: %v", i, err))
			continue
		}

		obj, err := mapping.Codec.Decode(in.RawJSON)
		if err != nil {
			errors = append(errors, fmt.Errorf("item[%d] decode: %v", i, err))
			continue
		}
		result = append(result, obj)
	}
	return
}
Ejemplo n.º 17
0
func readSimpleService(filename string) SimpleService {
	inData, err := ReadConfigData(filename)
	checkErr(err)

	simpleService := SimpleService{}
	err = yaml.Unmarshal(inData, &simpleService)
	checkErr(err)

	if simpleService.Name == "" {
		_, simpleService.Name = ParseDockerImage(simpleService.Image)
		// TODO: encode/scrub the name
	}
	simpleService.Name = strings.ToLower(simpleService.Name)

	// TODO: Validate the image name and extract exposed ports

	// TODO: Do more validation
	if !util.IsDNSLabel(simpleService.Name) {
		checkErr(fmt.Errorf("name (%s) is not a valid DNS label", simpleService.Name))
	}

	if simpleService.Replicas == 0 {
		simpleService.Replicas = 1
	}

	return simpleService
}
Ejemplo n.º 18
0
func extractFromFile(name string) (kubelet.Pod, error) {
	var pod kubelet.Pod

	file, err := os.Open(name)
	if err != nil {
		return pod, err
	}
	defer file.Close()

	data, err := ioutil.ReadAll(file)
	if err != nil {
		glog.Errorf("Couldn't read from file: %v", err)
		return pod, err
	}

	if err := yaml.Unmarshal(data, &pod.Manifest); err != nil {
		return pod, fmt.Errorf("could not unmarshal manifest: %v", err)
	}

	podName := pod.Manifest.ID
	if podName == "" {
		podName = simpleSubdomainSafeHash(name)
	}
	pod.Name = podName

	return pod, nil
}
Ejemplo n.º 19
0
func serviceUpdate(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	defer r.Body.Close()
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return err
	}
	var y serviceYaml
	err = yaml.Unmarshal(body, &y)
	if err != nil {
		return err
	}
	err = y.validate()
	if err != nil {
		return err
	}
	u, err := t.User()
	if err != nil {
		return err
	}
	rec.Log(u.Email, "update-service", y.Id, y.Endpoint)
	s, err := getServiceByOwner(y.Id, u)
	if err != nil {
		return err
	}
	s.Endpoint = y.Endpoint
	s.Password = y.Password
	if err = s.Update(); err != nil {
		return err
	}
	w.WriteHeader(http.StatusNoContent)
	return nil
}
Ejemplo n.º 20
0
// ReMarshal parses interface{} into concrete types
func ReMarshal(config interface{}, target interface{}) error {
	data, err := yaml.Marshal(config)
	if err != nil {
		return err
	}
	return yaml.Unmarshal(data, target)
}
Ejemplo n.º 21
0
// Decode converts a YAML or JSON string back into a pointer to an api object.
// Deduces the type based upon the APIVersion and Kind fields, which are set
// by Encode. Only versioned objects (APIVersion != "") are accepted. The object
// will be converted into the in-memory unversioned type before being returned.
func Decode(data []byte) (interface{}, error) {
	version, kind, err := VersionAndKind(data)
	if err != nil {
		return nil, err
	}
	if version == "" {
		return nil, fmt.Errorf("API Version not set in '%s'", string(data))
	}
	obj, err := New(version, kind)
	if err != nil {
		return nil, fmt.Errorf("Unable to create new object of type ('%s', '%s')", version, kind)
	}
	// yaml is a superset of json, so we use it to decode here. That way,
	// we understand both.
	err = yaml.Unmarshal(data, obj)
	if err != nil {
		return nil, err
	}
	obj, err = internalize(obj)
	if err != nil {
		return nil, err
	}
	jsonBase, err := FindJSONBase(obj)
	if err != nil {
		return nil, err
	}
	// Don't leave these set. Type and version info is deducible from go's type.
	jsonBase.SetKind("")
	jsonBase.SetAPIVersion("")
	return obj, nil
}
Ejemplo n.º 22
0
// LoadYaml loads byte data for a yaml file into a Config
// TODO (z): These should all be private, but right now tests depend on parsing bytes into yaml
func LoadYaml(data []byte) (Config, error) {
	config := Config{}
	if err := yaml.Unmarshal(data, &config); err != nil {
		log.Print("Failed to parse data in configuration. Aborting")
		return Config{}, err
	}
	return config, nil
}
Ejemplo n.º 23
0
func readServerConfig(filename string) (cfg ServerConfig, err error) {
	data, err := ioutil.ReadFile(filename)
	if err != nil {
		return
	}
	err = yaml.Unmarshal(data, &cfg)
	return
}
Ejemplo n.º 24
0
// Extract data from YAML file into a list of containers.
func (kl *Kubelet) ExtractYAMLData(buf []byte, output interface{}) error {
	err := yaml.Unmarshal(buf, output)
	if err != nil {
		glog.Errorf("Couldn't unmarshal configuration: %v", err)
		return err
	}
	return nil
}
Ejemplo n.º 25
0
func getHeaderMatcher(config []byte) (Matcher, error) {

	headerConfig := TestHeaderConfig{}
	yaml.Unmarshal(config, &headerConfig)

	factory := headerMatcherFactory{}
	return factory.Create(headerConfig.Headers)
}
Ejemplo n.º 26
0
// unmarshalYAML converts given YAML data
// into a config object.
func unmarshalYAML(data []byte) *Config {
	var config *Config
	err := yaml.Unmarshal(data, &config)
	if err != nil {
		err = displaySyntaxError(data, err)
		panic(StatusError{err, 65})
	}
	return config
}
Ejemplo n.º 27
0
func (s *S) TestUnmarshalWholeDocumentWithSetter(c *C) {
	obj := &typeWithSetter{}
	err := yaml.Unmarshal([]byte(setterTests[0].data), obj)
	c.Assert(err, IsNil)
	c.Assert(obj.tag, Equals, setterTests[0].tag)
	value, ok := obj.value.(map[interface{}]interface{})
	c.Assert(ok, Equals, true)
	c.Assert(value["_"], DeepEquals, setterTests[0].value)
}
Ejemplo n.º 28
0
func unmarshalYAML(data []byte) Containers {
	var containers Containers
	err := yaml.Unmarshal(data, &containers)
	if err != nil {
		err = displaySyntaxError(data, err)
		panic(err)
	}
	return containers
}
Ejemplo n.º 29
0
func categoriesForSite(site Site) ([]string, error) {
	resp := make([]string, 0)
	posts := make(listPostsResponse)

	// Get directory listing for Location /source/_posts/
	fis, err := ioutil.ReadDir(site.Location + "/source/_posts/")
	if err != nil {
		return resp, err
	}
	for iter := 0; iter < len(fis); iter++ {
		slug := strings.Replace(fis[iter].Name(), ".md", "", 1)
		item := listPostItem{
			Slug:     slug,
			Filename: fis[iter].Name(),
		}

		// Pull rest of information from yaml
		filedata, err := ioutil.ReadFile(site.Location + "/source/_posts/" + fis[iter].Name())
		if err != nil {
			continue
		}
		postconfig := postYaml{}
		err = yaml.Unmarshal([]byte(filedata), &postconfig)
		if err != nil {
			continue
		}

		item.Author = postconfig.Author
		item.Title = postconfig.Title
		item.Permalink = postconfig.Permalink
		item.Categories = postconfig.Categories

		posts[slug] = item
	}

	cMap := make(map[string]bool)

	// Get unique categories as map keys
	for k := range posts {
		for cIdx := range posts[k].Categories {
			if !cMap[posts[k].Categories[cIdx]] {
				cMap[posts[k].Categories[cIdx]] = true
			}
		}
	}

	// Pull out keys into an array to return
	for ck, _ := range cMap {
		resp = append(resp, ck)
	}

	// But make sure we do an alpha sort first
	sort.Strings(resp)

	return resp, nil
}
Ejemplo n.º 30
0
func setUpWithDrone(input string) (string, error) {
	var buildStruct PublishToDrone
	err := yaml.Unmarshal([]byte(input), &buildStruct)
	if err != nil {
		return "", err
	}
	bf := buildfile.New()
	buildStruct.Publish.Write(bf, &repo.Repo{Name: "name"})
	return bf.String(), err
}