userdata

package module
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 30, 2022 License: MIT Imports: 10 Imported by: 0

README

Go User Data

The library for cloud-init User Data

Getting Started

Use go get to install the library

go get github.com/Aton-Kish/gouserdata

Import in your application

import (
	userdata "github.com/Aton-Kish/gouserdata"
)

Usage

This example shows how to make mime multipart user data.

package main

import (
	"bytes"
	"fmt"
	"log"
	"os"

	userdata "github.com/Aton-Kish/gouserdata"
)

func main() {
	d := userdata.NewMultipart()

	cfg, err := os.ReadFile("cloud-config.yaml")
	if err != nil {
			log.Fatal(err)
	}
	d.AddPart(userdata.MediaTypeCloudConfig, cfg)

	j2, err := os.ReadFile("script.j2")
	if err != nil {
		log.Fatal(err)
	}
	d.AddPart(userdata.MediaTypeJinja2, j2)

	hook, err := os.ReadFile("boothook.sh")
	if err != nil {
		log.Fatal(err)
	}
	d.AddPart(userdata.MediaTypeCloudBoothook, hook)

	buf := new(bytes.Buffer)
	d.Render(buf)

	fmt.Println(buf.String())
}

License

This library is licensed under the MIT License, see LICENSE.

Documentation

Overview

The library for cloud-init User Data. This is licensed under the MIT License.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Header struct {
	textproto.MIMEHeader
}

func NewHeader

func NewHeader() *Header

func (*Header) Render

func (h *Header) Render(w io.Writer) error

type MediaType

type MediaType string
const (
	MediaTypeCloudBoothook           MediaType = "text/cloud-boothook"
	MediaTypeCloudConfig             MediaType = "text/cloud-config"
	MediaTypeCloudConfigArchive      MediaType = "text/cloud-config-archive"
	MediaTypeCloudConfigJsonp        MediaType = "text/cloud-config-jsonp"
	MediaTypeJinja2                  MediaType = "text/jinja2"
	MediaTypePartHandler             MediaType = "text/part-handler"
	MediaTypeXIncludeOnceUrl         MediaType = "text/x-include-once-url"
	MediaTypeXIncludeUrl             MediaType = "text/x-include-url"
	MediaTypeXShellscript            MediaType = "text/x-shellscript"
	MediaTypeXShellscriptPerBoot     MediaType = "text/x-shellscript-per-boot"
	MediaTypeXShellscriptPerInstance MediaType = "text/x-shellscript-per-instance"
	MediaTypeXShellscriptPerOnce     MediaType = "text/x-shellscript-per-once"
)

type Multipart

type Multipart struct {
	Header Header
	Parts  []Part
	// contains filtered or unexported fields
}

func NewMultipart

func NewMultipart() *Multipart

func (*Multipart) AddPart

func (m *Multipart) AddPart(mediaType MediaType, body []byte)

func (*Multipart) Boundary added in v0.2.0

func (m *Multipart) Boundary() string

func (*Multipart) Render

func (m *Multipart) Render(w io.Writer) error
Example
package main

import (
	"bytes"
	"fmt"
	"strings"

	userdata "github.com/Aton-Kish/gouserdata"
)

func main() {
	d := userdata.NewMultipart()

	cfg := []byte(`#cloud-config
timezone: Europe/London`)
	d.AddPart(userdata.MediaTypeCloudConfig, cfg)

	scr := []byte(`#!/bin/bash
echo 'Hello World'`)
	d.AddPart(userdata.MediaTypeXShellscript, scr)

	buf := new(bytes.Buffer)
	d.Render(buf)

	output := buf.String()
	output = strings.ReplaceAll(output, "\r\n", "\n") // for testing
	fmt.Println(output)
}
Output:

Content-Type: multipart/mixed; boundary="+Go+User+Data+Boundary=="
Mime-Version: 1.0

--+Go+User+Data+Boundary==
Content-Transfer-Encoding: 7bit
Content-Type: text/cloud-config; charset=us-ascii

#cloud-config
timezone: Europe/London

--+Go+User+Data+Boundary==
Content-Transfer-Encoding: 7bit
Content-Type: text/x-shellscript; charset=us-ascii

#!/bin/bash
echo 'Hello World'

--+Go+User+Data+Boundary==--
Example (IncludesUtf8)
package main

import (
	"bytes"
	"fmt"
	"strings"

	userdata "github.com/Aton-Kish/gouserdata"
)

func main() {
	d := userdata.NewMultipart()

	cfg := []byte(`#cloud-config
timezone: Asia/Tokyo`)
	d.AddPart(userdata.MediaTypeCloudConfig, cfg)

	scr := []byte(`#!/bin/bash
echo 'こんにちは世界'`)
	d.AddPart(userdata.MediaTypeXShellscript, scr)

	buf := new(bytes.Buffer)
	d.Render(buf)

	output := buf.String()
	output = strings.ReplaceAll(output, "\r\n", "\n") // for testing
	fmt.Println(output)
}
Output:

Content-Type: multipart/mixed; boundary="+Go+User+Data+Boundary=="
Mime-Version: 1.0

--+Go+User+Data+Boundary==
Content-Transfer-Encoding: 7bit
Content-Type: text/cloud-config; charset=us-ascii

#cloud-config
timezone: Asia/Tokyo

--+Go+User+Data+Boundary==
Content-Transfer-Encoding: base64
Content-Type: text/x-shellscript; charset=utf-8

IyEvYmluL2Jhc2gKZWNobyAn44GT44KT44Gr44Gh44Gv5LiW55WMJw==

--+Go+User+Data+Boundary==--

func (*Multipart) SetBoundary

func (m *Multipart) SetBoundary(boundary string) error
Example
package main

import (
	"bytes"
	"fmt"
	"log"
	"strings"

	userdata "github.com/Aton-Kish/gouserdata"
)

func main() {
	d := userdata.NewMultipart()

	if err := d.SetBoundary("+Custom+User+Data+Boundary+"); err != nil {
		log.Fatal(err)
	}

	scr := []byte(`#!/bin/bash
echo 'Hello World'`)
	d.AddPart(userdata.MediaTypeXShellscript, scr)

	buf := new(bytes.Buffer)
	d.Render(buf)

	output := buf.String()
	output = strings.ReplaceAll(output, "\r\n", "\n") // for testing
	fmt.Println(output)
}
Output:

Content-Type: multipart/mixed; boundary=+Custom+User+Data+Boundary+
Mime-Version: 1.0

--+Custom+User+Data+Boundary+
Content-Transfer-Encoding: 7bit
Content-Type: text/x-shellscript; charset=us-ascii

#!/bin/bash
echo 'Hello World'

--+Custom+User+Data+Boundary+--

type Part

type Part struct {
	Header Header
	Body   []byte
	// contains filtered or unexported fields
}

func NewPart

func NewPart() *Part

func (*Part) MediaType added in v0.2.0

func (p *Part) MediaType() MediaType

func (*Part) Render

func (p *Part) Render(w io.Writer) error

func (*Part) SetBody added in v0.2.0

func (p *Part) SetBody(mediaType MediaType, body []byte)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL