<commit_before>package geom

import (
	"math"
)

// Extenter represents an interface that returns a boundbox.
type Extenter interface {
	Extent() (extent [4]float64)
}

// MinMaxer is a wrapper for an Extent that gets min/max of the extent
type MinMaxer interface {
	MinX() float64
	MinY() float64
	MaxX() float64
	MaxY() float64
}

// Extent represents the minx, miny, maxx and maxy
// A nil extent represents the whole universe.
type Extent [4]float64

/* ========================= ATTRIBUTES ========================= */

// Vertices return the verticies of the Bounding Box. The verticies are ordered in the following maner.
// (minx,miny), (maxx,miny), (maxx,maxy), (minx,maxy)
func (e *Extent) Vertices() [][2]float64 {
	return [][2]float64{
		{e.MinX(), e.MinY()},
		{e.MaxX(), e.MinY()},
		{e.MaxX(), e.MaxY()},
		{e.MinX(), e.MaxY()},
	}
}

// ClockwiseFunc returns weather the set of points should be considered clockwise or counterclockwise. The last point is not the same as the first point, and the function should connect these points as needed.
type ClockwiseFunc func(...[2]float64) bool

// Edges returns the clockwise order of the edges that make up the extent.
func (e *Extent) Edges(cwfn ClockwiseFunc) [][2][2]float64 {
	v := e.Vertices()
	if cwfn != nil && !cwfn(v...) {
		v[0], v[1], v[2], v[3] = v[3], v[2], v[1], v[0]
	}
	return [][2][2]float64{
		{v[0], v[1]},
		{v[1], v[2]},
		{v[2], v[3]},
		{v[3], v[0]},
	}
}

// MaxX is the larger of the x values.
func (e *Extent) MaxX() float64 {
	if e == nil {
		return math.MaxFloat64
	}
	return e[2]
}

// MinX  is the smaller of the x values.
func (e *Extent) MinX() float64 {
	if e == nil {
		return -math.MaxFloat64
	}
	return e[0]
}

// MaxY is the larger of the y values.
func (e *Extent) MaxY() float64 {
	if e == nil {
		return math.MaxFloat64
	}
	return e[3]
}

// MinY is the smaller of the y values.
func (e *Extent) MinY() float64 {
	if e == nil {
		return -math.MaxFloat64
	}
	return e[1]
}

// Min returns the (MinX, MinY) values
func (e *Extent) Min() [2]float64 {
	return [2]float64{e[0], e[1]}
}

// Max returns the (MaxX, MaxY) values
func (e *Extent) Max() [2]float64 {
	return [2]float64{e[2], e[3]}
}

// XSpan is the distance of the Extent in X or inf
// TODO (gdey): look at how to have this function take into account the dpi.
func (e *Extent) XSpan() float64 {
	if e == nil {
		return math.Inf(1)
	}
	return e[2] - e[0]
}

// YSpan is the distance of the Extent in Y or Inf
func (e *Extent) YSpan() float64 {
	if e == nil {
		return math.Inf(1)
	}
	return e[3] - e[1]
}

// Extent returns back the min and max of the Extent
func (e *Extent) Extent() [4]float64 {
	return [4]float64{e.MinX(), e.MinY(), e.MaxX(), e.MaxY()}
}

/* ========================= EXPANDING BOUNDING BOX ========================= */

// Add will expand the extent to contain the given extent.
func (e *Extent) Add(extent MinMaxer) {
	if e == nil {
		return
	}
	e[0] = math.Min(e[0], extent.MinX())
	e[2] = math.Max(e[2], extent.MaxX())
	e[1] = math.Min(e[1], extent.MinY())
	e[3] = math.Max(e[3], extent.MaxY())
}

// AddPoints will expand the extent to contain the given points.
func (e *Extent) AddPoints(points ...[2]float64) {
	// A nil extent is all encompassing.
	if e == nil {
		return
	}
	if len(points) == 0 {
		return
	}
	for _, pt := range points {
		e[0] = math.Min(pt[0], e[0])
		e[1] = math.Min(pt[1], e[1])
		e[2] = math.Max(pt[0], e[2])
		e[3] = math.Max(pt[1], e[3])
	}
}

// AddPointers will expand the Extent if a point is outside it
func (e *Extent) AddPointers(pts ...Pointer) {
	for i := range pts {
		e.AddPoints(pts[i].XY())
	}
}

// AddGeometry expands the specified envelop to contain g.
func (e *Extent) AddGeometry(g Geometry) error {
	return getExtent(g, e)
}

// AsPolygon will return the extent as a Polygon
func (e *Extent) AsPolygon() Polygon { return Polygon{e.Vertices()} }

// Area returns the area of the extent, if the extent is nil, it will return 0
func (e *Extent) Area() float64 {
	return math.Abs((e.MaxY() - e.MinY()) * (e.MaxX() - e.MinX()))
}

// NewExtent returns an Extent for the provided points; in following format [4]float64{ MinX, MinY, MaxX, MaxY }
func NewExtent(points ...[2]float64) *Extent {
	var xy [2]float64
	if len(points) == 0 {
		return nil
	}

	extent := Extent{points[0][0], points[0][1], points[0][0], points[0][1]}
	if len(points) == 1 {
		return &extent
	}
	for i := 1; i < len(points); i++ {
		xy = points[i]
		// Check the x coords
		switch {
		case xy[0] < extent[0]:
			extent[0] = xy[0]
		case xy[0] > extent[2]:
			extent[2] = xy[0]
		}
		// Check the y coords
		switch {
		case xy[1] < extent[1]:
			extent[1] = xy[1]
		case xy[1] > extent[3]:
			extent[3] = xy[1]
		}
	}
	return &extent
}

// NewExtentFromGeometry tries to create an extent based on the geometry
func NewExtentFromGeometry(g Geometry) (*Extent, error) {
	e := Extent{}
	err := getExtent(g, &e)
	if err != nil {
		return nil, err
	}
	return &e, nil
}

// Contains will return whether the given  extent is inside of the  extent.
func (e *Extent) Contains(ne MinMaxer) bool {
	// Nil extent contains the world.
	if e == nil {
		return true
	}
	if ne == nil {
		return false
	}
	return e.MinX() <= ne.MinX() &&
		e.MaxX() >= ne.MaxX() &&
		e.MinY() <= ne.MinY() &&
		e.MaxY() >= ne.MaxY()
}

// ContainsPoint will return whether the given point is inside of the extent.
func (e *Extent) ContainsPoint(pt [2]float64) bool {
	if e == nil {
		return true
	}
	return e.MinX() <= pt[0] && pt[0] <= e.MaxX() &&
		e.MinY() <= pt[1] && pt[1] <= e.MaxY()
}

// ContainsLine will return weather the given line completely inside of the extent.
func (e *Extent) ContainsLine(l [2][2]float64) bool {
	if e == nil {
		return true
	}
	return e.ContainsPoint(l[0]) && e.ContainsPoint(l[1])
}

// ContainsGeom will return weather the given geometry is completely inside of the extent.
func (e *Extent) ContainsGeom(g Geometry) (bool, error) {
	if e.IsUniverse() {
		return true, nil
	}
	// Check to see if it can be a MinMaxer, if so use that.
	if extenter, ok := g.(MinMaxer); ok {
		return e.Contains(extenter), nil
	}
	// we will use a exntent that contains the geometry, and check to see if this extent contains that extent.
	var ne = new(Extent)
	if err := ne.AddGeometry(g); err != nil {
		return false, err
	}
	return e.Contains(ne), nil
}

// ScaleBy will scale the points in the extent by the given scale factor.
func (e *Extent) ScaleBy(s float64) *Extent {
	if e == nil {
		return nil
	}
	return NewExtent(
		[2]float64{e[0] * s, e[1] * s},
		[2]float64{e[2] * s, e[3] * s},
	)
}

// ExpandBy will expand extent by the given factor.
func (e *Extent) ExpandBy(s float64) *Extent {
	if e == nil {
		return nil
	}
	return NewExtent(
		[2]float64{e[0] - s, e[1] - s},
		[2]float64{e[2] + s, e[3] + s},
	)
}

// Clone returns a new Extent with contents copied.
func (e *Extent) Clone() *Extent {
	if e == nil {
		return nil
	}
	return &Extent{e[0], e[1], e[2], e[3]}
}

// Intersect will return a new extent that is the intersect of the two extents.
//
// +-------------------------+
// |                         |
// |       A      +----------+------+
// |              |//////////|      |
// |              |/// C ////|      |
// |              |//////////|      |
// +--------------+----------+      |
//                |             B   |
//                +-----------------+
// For example the for the above Box A intersects Box B at the area surround by C.
//
// If the Boxes don't intersect does will be false, otherwise ibb will be the intersect.
func (e *Extent) Intersect(ne *Extent) (*Extent, bool) {
	// if e in nil, then the intersect is ne. As a nil extent is the whole universe.
	if e == nil {
		return ne.Clone(), true
	}
	// if ne is nil, then the intersect is e. As a nil extent is the whole universe.
	if ne == nil {
		return e.Clone(), true
	}

	minx := e.MinX()
	if minx < ne.MinX() {
		minx = ne.MinX()
	}
	maxx := e.MaxX()
	if maxx > ne.MaxX() {
		maxx = ne.MaxX()
	}
	// The boxes don't intersect.
	if minx >= maxx {
		return nil, false
	}
	miny := e.MinY()
	if miny < ne.MinY() {
		miny = ne.MinY()
	}
	maxy := e.MaxY()
	if maxy > ne.MaxY() {
		maxy = ne.MaxY()
	}

	// The boxes don't intersect.
	if miny >= maxy {
		return nil, false
	}
	return &Extent{minx, miny, maxx, maxy}, true
}

// IsUniverse returns weather the extent contains the universe. This is true if the clip box is nil or the x,y values are max values.
func (e *Extent) IsUniverse() bool {
	return e == nil || (e.MinX() == -math.MaxFloat64 && e.MaxX() == math.MaxFloat64 &&
		e.MinY() == -math.MaxFloat64 && e.MaxY() == math.MaxFloat64)
}
<commit_msg>[bug fix] NewExtentFromGeometry incorrect ext (#39)<commit_after>package geom

import (
	"math"
)

// Extenter represents an interface that returns a boundbox.
type Extenter interface {
	Extent() (extent [4]float64)
}

// MinMaxer is a wrapper for an Extent that gets min/max of the extent
type MinMaxer interface {
	MinX() float64
	MinY() float64
	MaxX() float64
	MaxY() float64
}

// Extent represents the minx, miny, maxx and maxy
// A nil extent represents the whole universe.
type Extent [4]float64

/* ========================= ATTRIBUTES ========================= */

// Vertices return the verticies of the Bounding Box. The verticies are ordered in the following maner.
// (minx,miny), (maxx,miny), (maxx,maxy), (minx,maxy)
func (e *Extent) Vertices() [][2]float64 {
	return [][2]float64{
		{e.MinX(), e.MinY()},
		{e.MaxX(), e.MinY()},
		{e.MaxX(), e.MaxY()},
		{e.MinX(), e.MaxY()},
	}
}

// ClockwiseFunc returns weather the set of points should be considered clockwise or counterclockwise. The last point is not the same as the first point, and the function should connect these points as needed.
type ClockwiseFunc func(...[2]float64) bool

// Edges returns the clockwise order of the edges that make up the extent.
func (e *Extent) Edges(cwfn ClockwiseFunc) [][2][2]float64 {
	v := e.Vertices()
	if cwfn != nil && !cwfn(v...) {
		v[0], v[1], v[2], v[3] = v[3], v[2], v[1], v[0]
	}
	return [][2][2]float64{
		{v[0], v[1]},
		{v[1], v[2]},
		{v[2], v[3]},
		{v[3], v[0]},
	}
}

// MaxX is the larger of the x values.
func (e *Extent) MaxX() float64 {
	if e == nil {
		return math.MaxFloat64
	}
	return e[2]
}

// MinX  is the smaller of the x values.
func (e *Extent) MinX() float64 {
	if e == nil {
		return -math.MaxFloat64
	}
	return e[0]
}

// MaxY is the larger of the y values.
func (e *Extent) MaxY() float64 {
	if e == nil {
		return math.MaxFloat64
	}
	return e[3]
}

// MinY is the smaller of the y values.
func (e *Extent) MinY() float64 {
	if e == nil {
		return -math.MaxFloat64
	}
	return e[1]
}

// Min returns the (MinX, MinY) values
func (e *Extent) Min() [2]float64 {
	return [2]float64{e[0], e[1]}
}

// Max returns the (MaxX, MaxY) values
func (e *Extent) Max() [2]float64 {
	return [2]float64{e[2], e[3]}
}

// XSpan is the distance of the Extent in X or inf
// TODO (gdey): look at how to have this function take into account the dpi.
func (e *Extent) XSpan() float64 {
	if e == nil {
		return math.Inf(1)
	}
	return e[2] - e[0]
}

// YSpan is the distance of the Extent in Y or Inf
func (e *Extent) YSpan() float64 {
	if e == nil {
		return math.Inf(1)
	}
	return e[3] - e[1]
}

// Extent returns back the min and max of the Extent
func (e *Extent) Extent() [4]float64 {
	return [4]float64{e.MinX(), e.MinY(), e.MaxX(), e.MaxY()}
}

/* ========================= EXPANDING BOUNDING BOX ========================= */

// Add will expand the extent to contain the given extent.
func (e *Extent) Add(extent MinMaxer) {
	if e == nil {
		return
	}
	e[0] = math.Min(e[0], extent.MinX())
	e[2] = math.Max(e[2], extent.MaxX())
	e[1] = math.Min(e[1], extent.MinY())
	e[3] = math.Max(e[3], extent.MaxY())
}

// AddPoints will expand the extent to contain the given points.
func (e *Extent) AddPoints(points ...[2]float64) {
	// A nil extent is all encompassing.
	if e == nil {
		return
	}
	if len(points) == 0 {
		return
	}
	for _, pt := range points {
		e[0] = math.Min(pt[0], e[0])
		e[1] = math.Min(pt[1], e[1])
		e[2] = math.Max(pt[0], e[2])
		e[3] = math.Max(pt[1], e[3])
	}
}

// AddPointers will expand the Extent if a point is outside it
func (e *Extent) AddPointers(pts ...Pointer) {
	for i := range pts {
		e.AddPoints(pts[i].XY())
	}
}

// AddGeometry expands the specified envelop to contain g.
func (e *Extent) AddGeometry(g Geometry) error {
	return getExtent(g, e)
}

// AsPolygon will return the extent as a Polygon
func (e *Extent) AsPolygon() Polygon { return Polygon{e.Vertices()} }

// Area returns the area of the extent, if the extent is nil, it will return 0
func (e *Extent) Area() float64 {
	return math.Abs((e.MaxY() - e.MinY()) * (e.MaxX() - e.MinX()))
}

// NewExtent returns an Extent for the provided points; in following format [4]float64{ MinX, MinY, MaxX, MaxY }
func NewExtent(points ...[2]float64) *Extent {
	var xy [2]float64
	if len(points) == 0 {
		return nil
	}

	extent := Extent{points[0][0], points[0][1], points[0][0], points[0][1]}
	if len(points) == 1 {
		return &extent
	}
	for i := 1; i < len(points); i++ {
		xy = points[i]
		// Check the x coords
		switch {
		case xy[0] < extent[0]:
			extent[0] = xy[0]
		case xy[0] > extent[2]:
			extent[2] = xy[0]
		}
		// Check the y coords
		switch {
		case xy[1] < extent[1]:
			extent[1] = xy[1]
		case xy[1] > extent[3]:
			extent[3] = xy[1]
		}
	}
	return &extent
}

// NewExtentFromGeometry tries to create an extent based on the geometry
func NewExtentFromGeometry(g Geometry) (*Extent, error) {
	var pts []Point
	if err := getCoordinates(g, &pts); err != nil {
		return nil, err
	}
	if len(pts) == 0 {
		return nil, nil
	}
	e := Extent{pts[0][0], pts[0][1], pts[0][0], pts[0][1]}
	for _, pt := range pts {
		e.AddPoints([2]float64(pt))
	}

	return &e, nil
}

// Contains will return whether the given  extent is inside of the  extent.
func (e *Extent) Contains(ne MinMaxer) bool {
	// Nil extent contains the world.
	if e == nil {
		return true
	}
	if ne == nil {
		return false
	}
	return e.MinX() <= ne.MinX() &&
		e.MaxX() >= ne.MaxX() &&
		e.MinY() <= ne.MinY() &&
		e.MaxY() >= ne.MaxY()
}

// ContainsPoint will return whether the given point is inside of the extent.
func (e *Extent) ContainsPoint(pt [2]float64) bool {
	if e == nil {
		return true
	}
	return e.MinX() <= pt[0] && pt[0] <= e.MaxX() &&
		e.MinY() <= pt[1] && pt[1] <= e.MaxY()
}

// ContainsLine will return weather the given line completely inside of the extent.
func (e *Extent) ContainsLine(l [2][2]float64) bool {
	if e == nil {
		return true
	}
	return e.ContainsPoint(l[0]) && e.ContainsPoint(l[1])
}

// ContainsGeom will return weather the given geometry is completely inside of the extent.
func (e *Extent) ContainsGeom(g Geometry) (bool, error) {
	if e.IsUniverse() {
		return true, nil
	}
	// Check to see if it can be a MinMaxer, if so use that.
	if extenter, ok := g.(MinMaxer); ok {
		return e.Contains(extenter), nil
	}
	// we will use a exntent that contains the geometry, and check to see if this extent contains that extent.
	var ne = new(Extent)
	if err := ne.AddGeometry(g); err != nil {
		return false, err
	}
	return e.Contains(ne), nil
}

// ScaleBy will scale the points in the extent by the given scale factor.
func (e *Extent) ScaleBy(s float64) *Extent {
	if e == nil {
		return nil
	}
	return NewExtent(
		[2]float64{e[0] * s, e[1] * s},
		[2]float64{e[2] * s, e[3] * s},
	)
}

// ExpandBy will expand extent by the given factor.
func (e *Extent) ExpandBy(s float64) *Extent {
	if e == nil {
		return nil
	}
	return NewExtent(
		[2]float64{e[0] - s, e[1] - s},
		[2]float64{e[2] + s, e[3] + s},
	)
}

// Clone returns a new Extent with contents copied.
func (e *Extent) Clone() *Extent {
	if e == nil {
		return nil
	}
	return &Extent{e[0], e[1], e[2], e[3]}
}

// Intersect will return a new extent that is the intersect of the two extents.
//
// +-------------------------+
// |                         |
// |       A      +----------+------+
// |              |//////////|      |
// |              |/// C ////|      |
// |              |//////////|      |
// +--------------+----------+      |
//                |             B   |
//                +-----------------+
// For example the for the above Box A intersects Box B at the area surround by C.
//
// If the Boxes don't intersect does will be false, otherwise ibb will be the intersect.
func (e *Extent) Intersect(ne *Extent) (*Extent, bool) {
	// if e in nil, then the intersect is ne. As a nil extent is the whole universe.
	if e == nil {
		return ne.Clone(), true
	}
	// if ne is nil, then the intersect is e. As a nil extent is the whole universe.
	if ne == nil {
		return e.Clone(), true
	}

	minx := e.MinX()
	if minx < ne.MinX() {
		minx = ne.MinX()
	}
	maxx := e.MaxX()
	if maxx > ne.MaxX() {
		maxx = ne.MaxX()
	}
	// The boxes don't intersect.
	if minx >= maxx {
		return nil, false
	}
	miny := e.MinY()
	if miny < ne.MinY() {
		miny = ne.MinY()
	}
	maxy := e.MaxY()
	if maxy > ne.MaxY() {
		maxy = ne.MaxY()
	}

	// The boxes don't intersect.
	if miny >= maxy {
		return nil, false
	}
	return &Extent{minx, miny, maxx, maxy}, true
}

// IsUniverse returns weather the extent contains the universe. This is true if the clip box is nil or the x,y values are max values.
func (e *Extent) IsUniverse() bool {
	return e == nil || (e.MinX() == -math.MaxFloat64 && e.MaxX() == math.MaxFloat64 &&
		e.MinY() == -math.MaxFloat64 && e.MaxY() == math.MaxFloat64)
}
<|endoftext|>


<commit_before>// Support for blogs in autosite
//
// Example usage:
//   myblog := NewBlog(
//     "Some title",       // for HTML <head>
//     "blog/*/*/*.tmpl",  // pattern for posts on disk
//     "blogdomain.com",   // live domain
//     []string{           // shared templates
//       "base.tmpl",
//       "blog.tmpl",
//     },
//     []string{           // templates for special handlers
//       "base.tmpl",
//       "listing.tmpl",
//     },
//   )
//   myblog.Register()
//
// This will host pages like blogdomain.com/2014/02/Foo and
// 2016/01/Bar if there's files blog/2014/02/Foo.tmpl and
// blog/2016/01/Bar.tmpl relative to the calling package, also
// using "base.tmpl" and "blog.tmpl" for all blog posts.
//
// Special handlers for listing posts will be registered, using
// "base.tmpl" and "listing.tmpl":
//   * the index (/) will show all posts from all years.
//   * for each year YYYY that has at least one blog post, /YYYY will
//     be registered, showing all posts from that year.
//   * for each year + month YYYY/MM that have at least one blog post,
//     /YYYY/MM will be registered, showing all posts from that month
//     in that year.
//
// Within the templates for these "blog listing" handlers, the following
// data is available in addition to the usual:
//   * {{.Data.Posts}}: a slice of the posts for that unit of time (all time, one
//     year, or one month, respectively)
//   * {{.Data.TimeUnit}}: the unit of time itself ("April, 2014",
//     "2014" or "all time", respectively)
package autosite

import (
	"fmt"
	"log"
	"sort"
	"strings"
)

// NewBlog creates a new blog.
//
// NewBlog panics on errors reading templates.
func NewBlog(title, glob, live string, articleTmpls []string, listingTmpls []string, logger LoggerFunc, isLive bool) Site {
	b := blog{internalNew(title, glob, live, articleTmpls, logger, isLive)}
	b.addHandlers(listingTmpls)
	return &b
}

// blog is a type of autosite.
type blog struct {
	s site // backing site.
}

// Register registers the HTTP handlers for the blog.
func (b *blog) Register() {
	b.s.Register()
}

// ChangeURI changes the URI a page will be served on.
func (b *blog) ChangeURI(uri, newURI string) {
	b.s.ChangeURI(uri, newURI)
}

// AddRedirect registers an URI that redirects.
func (b *blog) AddRedirect(uri, newURI string) {
	b.s.AddRedirect(uri, newURI)
}

// pageData is the data needed to serve a listing page.
type pageData struct {
	TimeUnit string
	Posts    posts
}
type posts []page

// listingData is the backing data used for a listing page.
type listingData struct {
	byDate map[date]posts
	byYear map[year]posts
}

// Len gives the length of the posts.
func (p posts) Len() int {
	return len(p)
}

// Less is true when post i was published before post j.
func (p posts) Less(i, j int) bool {
	return p[i].Date.before(p[j].Date)
}

// Swap changes places of element i and j.
func (p posts) Swap(i, j int) {
	p[i], p[j] = p[j], p[i]
}

// addListing adds a listing of blog posts.
func (b *blog) addListing(uri string, tfmt string, p posts, listingTmpls []string) {
	data := pageData{
		TimeUnit: tfmt,
		Posts:    p,
	}
	b.s.addPage(uri, date{}, data, listingTmpls)
}

// byMonth accumulates a map of year -> month -> posts.
func (b blog) listingData() listingData {
	byDate := make(map[date]posts)
	byYear := make(map[year]posts)
	for uri, p := range b.s.pages {
		parts := strings.Split(uri, "/")
		d, err := getDate(parts[1], parts[2])
		if err != nil {
			log.Fatalf("failed to parse date: %v\n", err)
		}
		byDate[d] = append(byDate[d], p)
		byYear[d.Year] = append(byYear[d.Year], p)
	}
	return listingData{byDate, byYear}
}

// addHandlers registers custom handlers for the blog.
func (b *blog) addHandlers(listingTmpls []string) {
	data := b.listingData()
	for d, inDate := range data.byDate {
		sort.Sort(sort.Reverse(inDate))
		b.addListing(fmt.Sprintf("/%d/%.2d/", d.Year, d.Month), fmt.Sprintf("%s, %d", d.Month, d.Year), inDate, listingTmpls)
	}
	for y, inYear := range data.byYear {
		sort.Sort(sort.Reverse(inYear))
		b.addListing(fmt.Sprintf("/%d/", y), fmt.Sprintf("%d", y), inYear, listingTmpls)
	}
	var allPosts posts
	for _, p := range data.byYear {
		allPosts = append(allPosts, p...)
	}
	sort.Sort(sort.Reverse(allPosts))
	b.addListing("/", "all years", allPosts, listingTmpls)
}
<commit_msg>blog.go now uses anonymous fields to get promoted methods from site<commit_after>// Support for blogs in autosite
//
// Example usage:
//   myblog := NewBlog(
//     "Some title",       // for HTML <head>
//     "blog/*/*/*.tmpl",  // pattern for posts on disk
//     "blogdomain.com",   // live domain
//     []string{           // shared templates
//       "base.tmpl",
//       "blog.tmpl",
//     },
//     []string{           // templates for special handlers
//       "base.tmpl",
//       "listing.tmpl",
//     },
//   )
//   myblog.Register()
//
// This will host pages like blogdomain.com/2014/02/Foo and
// 2016/01/Bar if there's files blog/2014/02/Foo.tmpl and
// blog/2016/01/Bar.tmpl relative to the calling package, also
// using "base.tmpl" and "blog.tmpl" for all blog posts.
//
// Special handlers for listing posts will be registered, using
// "base.tmpl" and "listing.tmpl":
//   * the index (/) will show all posts from all years.
//   * for each year YYYY that has at least one blog post, /YYYY will
//     be registered, showing all posts from that year.
//   * for each year + month YYYY/MM that have at least one blog post,
//     /YYYY/MM will be registered, showing all posts from that month
//     in that year.
//
// Within the templates for these "blog listing" handlers, the following
// data is available in addition to the usual:
//   * {{.Data.Posts}}: a slice of the posts for that unit of time (all time, one
//     year, or one month, respectively)
//   * {{.Data.TimeUnit}}: the unit of time itself ("April, 2014",
//     "2014" or "all time", respectively)
package autosite

import (
	"fmt"
	"log"
	"sort"
	"strings"
)

// NewBlog creates a new blog.
//
// NewBlog panics on errors reading templates.
func NewBlog(title, glob, live string, articleTmpls []string, listingTmpls []string, logger LoggerFunc, isLive bool) Site {
	b := blog{internalNew(title, glob, live, articleTmpls, logger, isLive)}
	b.addHandlers(listingTmpls)
	return &b
}

// blog is a type of autosite.
type blog struct {
	site // backing site
}

// pageData is the data needed to serve a listing page.
type pageData struct {
	TimeUnit string
	Posts    posts
}
type posts []page

// listingData is the backing data used for a listing page.
type listingData struct {
	byDate map[date]posts
	byYear map[year]posts
}

// Len gives the length of the posts.
func (p posts) Len() int {
	return len(p)
}

// Less is true when post i was published before post j.
func (p posts) Less(i, j int) bool {
	return p[i].Date.before(p[j].Date)
}

// Swap changes places of element i and j.
func (p posts) Swap(i, j int) {
	p[i], p[j] = p[j], p[i]
}

// addListing adds a listing of blog posts.
func (b *blog) addListing(uri string, tfmt string, p posts, listingTmpls []string) {
	data := pageData{
		TimeUnit: tfmt,
		Posts:    p,
	}
	b.site.addPage(uri, date{}, data, listingTmpls)
}

// byMonth accumulates a map of year -> month -> posts.
func (b blog) listingData() listingData {
	byDate := make(map[date]posts)
	byYear := make(map[year]posts)
	for uri, p := range b.site.pages {
		parts := strings.Split(uri, "/")
		d, err := getDate(parts[1], parts[2])
		if err != nil {
			log.Fatalf("failed to parse date: %v\n", err)
		}
		byDate[d] = append(byDate[d], p)
		byYear[d.Year] = append(byYear[d.Year], p)
	}
	return listingData{byDate, byYear}
}

// addHandlers registers custom handlers for the blog.
func (b *blog) addHandlers(listingTmpls []string) {
	data := b.listingData()
	for d, inDate := range data.byDate {
		sort.Sort(sort.Reverse(inDate))
		b.addListing(fmt.Sprintf("/%d/%.2d/", d.Year, d.Month), fmt.Sprintf("%s, %d", d.Month, d.Year), inDate, listingTmpls)
	}
	for y, inYear := range data.byYear {
		sort.Sort(sort.Reverse(inYear))
		b.addListing(fmt.Sprintf("/%d/", y), fmt.Sprintf("%d", y), inYear, listingTmpls)
	}
	var allPosts posts
	for _, p := range data.byYear {
		allPosts = append(allPosts, p...)
	}
	sort.Sort(sort.Reverse(allPosts))
	b.addListing("/", "all years", allPosts, listingTmpls)
}
<|endoftext|>


<commit_before>package nats_output

import (
	"context"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net"
	"strings"
	"sync"
	"time"

	"github.com/google/uuid"
	"github.com/karimra/gnmic/formatters"
	"github.com/karimra/gnmic/outputs"
	"github.com/nats-io/nats.go"
	"github.com/prometheus/client_golang/prometheus"
	"google.golang.org/protobuf/proto"
)

const (
	natsConnectWait         = 2 * time.Second
	natsReconnectBufferSize = 100 * 1024 * 1024
	defaultSubjectName      = "telemetry"
	defaultFormat           = "event"
	defaultNumWorkers       = 1
	defaultWriteTimeout     = 5 * time.Second
	defaultAddress          = "localhost:4222"
	loggingPrefix           = "[nats_output] "
)

func init() {
	outputs.Register("nats", func() outputs.Output {
		return &NatsOutput{
			Cfg:    &Config{},
			wg:     new(sync.WaitGroup),
			logger: log.New(ioutil.Discard, loggingPrefix, log.LstdFlags|log.Lmicroseconds),
		}
	})
}

type protoMsg struct {
	m    proto.Message
	meta outputs.Meta
}

// NatsOutput //
type NatsOutput struct {
	Cfg      *Config
	ctx      context.Context
	cancelFn context.CancelFunc
	msgChan  chan *protoMsg
	wg       *sync.WaitGroup
	logger   *log.Logger
	mo       *formatters.MarshalOptions
	evps     []formatters.EventProcessor
}

// Config //
type Config struct {
	Name               string        `mapstructure:"name,omitempty"`
	Address            string        `mapstructure:"address,omitempty"`
	SubjectPrefix      string        `mapstructure:"subject-prefix,omitempty"`
	Subject            string        `mapstructure:"subject,omitempty"`
	Username           string        `mapstructure:"username,omitempty"`
	Password           string        `mapstructure:"password,omitempty"`
	ConnectTimeWait    time.Duration `mapstructure:"connect-time-wait,omitempty"`
	Format             string        `mapstructure:"format,omitempty"`
	OverrideTimestamps bool          `mapstructure:"override-timestamps,omitempty"`
	NumWorkers         int           `mapstructure:"num-workers,omitempty"`
	WriteTimeout       time.Duration `mapstructure:"write-timeout,omitempty"`
	Debug              bool          `mapstructure:"debug,omitempty"`
	EnableMetrics      bool          `mapstructure:"enable-metrics,omitempty"`
	EventProcessors    []string      `mapstructure:"event-processors,omitempty"`
}

func (n *NatsOutput) String() string {
	b, err := json.Marshal(n)
	if err != nil {
		return ""
	}
	return string(b)
}

func (n *NatsOutput) SetLogger(logger *log.Logger) {
	if logger != nil && n.logger != nil {
		n.logger.SetOutput(logger.Writer())
		n.logger.SetFlags(logger.Flags())
	}
}

func (n *NatsOutput) SetEventProcessors(ps map[string]map[string]interface{}, logger *log.Logger, tcs map[string]interface{}) {
	for _, epName := range n.Cfg.EventProcessors {
		if epCfg, ok := ps[epName]; ok {
			epType := ""
			for k := range epCfg {
				epType = k
				break
			}
			if in, ok := formatters.EventProcessors[epType]; ok {
				ep := in()
				err := ep.Init(epCfg[epType], formatters.WithLogger(logger), formatters.WithTargets(tcs))
				if err != nil {
					n.logger.Printf("failed initializing event processor '%s' of type='%s': %v", epName, epType, err)
					continue
				}
				n.evps = append(n.evps, ep)
				n.logger.Printf("added event processor '%s' of type=%s to nats output", epName, epType)
				continue
			}
			n.logger.Printf("%q event processor has an unknown type=%q", epName, epType)
			continue
		}
		n.logger.Printf("%q event processor not found!", epName)
	}
}

// Init //
func (n *NatsOutput) Init(ctx context.Context, name string, cfg map[string]interface{}, opts ...outputs.Option) error {
	err := outputs.DecodeConfig(cfg, n.Cfg)
	if err != nil {
		return err
	}
	if n.Cfg.Name == "" {
		n.Cfg.Name = name
	}
	for _, opt := range opts {
		opt(n)
	}
	err = n.setDefaults()
	if err != nil {
		return err
	}
	n.msgChan = make(chan *protoMsg)
	initMetrics()
	n.mo = &formatters.MarshalOptions{
		Format:     n.Cfg.Format,
		OverrideTS: n.Cfg.OverrideTimestamps,
	}
	n.ctx, n.cancelFn = context.WithCancel(ctx)
	n.wg.Add(n.Cfg.NumWorkers)
	for i := 0; i < n.Cfg.NumWorkers; i++ {
		cfg := *n.Cfg
		cfg.Name = fmt.Sprintf("%s-%d", cfg.Name, i)
		go n.worker(ctx, i, &cfg)
	}

	go func() {
		<-ctx.Done()
		n.Close()
	}()
	return nil
}

func (n *NatsOutput) setDefaults() error {
	if n.Cfg.Format == "" {
		n.Cfg.Format = defaultFormat
	}
	if !(n.Cfg.Format == "event" || n.Cfg.Format == "protojson" || n.Cfg.Format == "proto" || n.Cfg.Format == "json") {
		return fmt.Errorf("unsupported output format '%s' for output type NATS", n.Cfg.Format)
	}
	if n.Cfg.Address == "" {
		n.Cfg.Address = defaultAddress
	}
	if n.Cfg.ConnectTimeWait <= 0 {
		n.Cfg.ConnectTimeWait = natsConnectWait
	}
	if n.Cfg.Subject == "" && n.Cfg.SubjectPrefix == "" {
		n.Cfg.Subject = defaultSubjectName
	}
	if n.Cfg.Name == "" {
		n.Cfg.Name = "gnmic-" + uuid.New().String()
	}
	if n.Cfg.NumWorkers <= 0 {
		n.Cfg.NumWorkers = defaultNumWorkers
	}
	if n.Cfg.WriteTimeout <= 0 {
		n.Cfg.WriteTimeout = defaultWriteTimeout
	}
	return nil
}

// Write //
func (n *NatsOutput) Write(ctx context.Context, rsp proto.Message, meta outputs.Meta) {
	if rsp == nil || n.mo == nil {
		return
	}

	wctx, cancel := context.WithTimeout(ctx, n.Cfg.WriteTimeout)
	defer cancel()

	select {
	case <-ctx.Done():
		return
	case n.msgChan <- &protoMsg{m: rsp, meta: meta}:
	case <-wctx.Done():
		if n.Cfg.Debug {
			n.logger.Printf("writing expired after %s, NATS output might not be initialized", n.Cfg.WriteTimeout)
		}
		if n.Cfg.EnableMetrics {
			NatsNumberOfFailSendMsgs.WithLabelValues(n.Cfg.Name, "timeout").Inc()
		}
		return
	}
}

func (n *NatsOutput) WriteEvent(ctx context.Context, ev *formatters.EventMsg) {}

// Close //
func (n *NatsOutput) Close() error {
	//	n.conn.Close()
	n.cancelFn()
	n.wg.Wait()
	return nil
}

// Metrics //
func (n *NatsOutput) RegisterMetrics(reg *prometheus.Registry) {
	if !n.Cfg.EnableMetrics {
		return
	}
	if err := registerMetrics(reg); err != nil {
		n.logger.Printf("failed to register metric: %+v", err)
	}
}

func (n *NatsOutput) createNATSConn(c *Config) (*nats.Conn, error) {
	opts := []nats.Option{
		nats.Name(c.Name),
		nats.SetCustomDialer(n),
		nats.ReconnectWait(n.Cfg.ConnectTimeWait),
		nats.ReconnectBufSize(natsReconnectBufferSize),
		nats.ErrorHandler(func(_ *nats.Conn, _ *nats.Subscription, err error) {
			n.logger.Printf("NATS error: %v", err)
		}),
		nats.DisconnectHandler(func(c *nats.Conn) {
			n.logger.Println("Disconnected from NATS")
		}),
		nats.ClosedHandler(func(c *nats.Conn) {
			n.logger.Println("NATS connection is closed")
		}),
	}
	if c.Username != "" && c.Password != "" {
		opts = append(opts, nats.UserInfo(c.Username, c.Password))
	}
	nc, err := nats.Connect(c.Address, opts...)
	if err != nil {
		return nil, err
	}
	return nc, nil
}

// Dial //
func (n *NatsOutput) Dial(network, address string) (net.Conn, error) {
	ctx, cancel := context.WithCancel(n.ctx)
	defer cancel()

	for {
		n.logger.Printf("attempting to connect to %s", address)
		if ctx.Err() != nil {
			return nil, ctx.Err()
		}

		select {
		case <-n.ctx.Done():
			return nil, n.ctx.Err()
		default:
			d := &net.Dialer{}
			if conn, err := d.DialContext(ctx, network, address); err == nil {
				n.logger.Printf("successfully connected to NATS server %s", address)
				return conn, nil
			}
			time.Sleep(n.Cfg.ConnectTimeWait)
		}
	}
}

func (n *NatsOutput) worker(ctx context.Context, i int, cfg *Config) {
	defer n.wg.Done()
	var natsConn *nats.Conn
	var err error
	workerLogPrefix := fmt.Sprintf("worker-%d", i)
	n.logger.Printf("%s starting", workerLogPrefix)
CRCONN:
	natsConn, err = n.createNATSConn(cfg)
	if err != nil {
		n.logger.Printf("%s failed to create connection: %v", workerLogPrefix, err)
		time.Sleep(n.Cfg.ConnectTimeWait)
		goto CRCONN
	}
	defer natsConn.Close()
	n.logger.Printf("%s initialized nats producer: %+v", workerLogPrefix, cfg)
	for {
		select {
		case <-ctx.Done():
			n.logger.Printf("%s flushing", workerLogPrefix)
			natsConn.FlushTimeout(time.Second)
			n.logger.Printf("%s shutting down", workerLogPrefix)
			return
		case m := <-n.msgChan:
			b, err := n.mo.Marshal(m.m, m.meta, n.evps...)
			if err != nil {
				if n.Cfg.Debug {
					n.logger.Printf("%s failed marshaling proto msg: %v", workerLogPrefix, err)
				}
				if n.Cfg.EnableMetrics {
					NatsNumberOfFailSendMsgs.WithLabelValues(cfg.Name, "marshal_error").Inc()
				}
				continue
			}
			subject := n.subjectName(cfg, m.meta)
			var start time.Time
			if n.Cfg.EnableMetrics {
				start = time.Now()
			}
			err = natsConn.Publish(subject, b)
			if err != nil {
				if n.Cfg.Debug {
					n.logger.Printf("%s failed to write to nats subject '%s': %v", workerLogPrefix, subject, err)
				}
				if n.Cfg.EnableMetrics {
					NatsNumberOfFailSendMsgs.WithLabelValues(cfg.Name, "publish_error").Inc()
				}
				natsConn.Close()
				time.Sleep(cfg.ConnectTimeWait)
				goto CRCONN
			}
			if n.Cfg.EnableMetrics {
				NatsSendDuration.WithLabelValues(cfg.Name).Set(float64(time.Since(start).Nanoseconds()))
				NatsNumberOfSentMsgs.WithLabelValues(cfg.Name, subject).Inc()
				NatsNumberOfSentBytes.WithLabelValues(cfg.Name, subject).Add(float64(len(b)))
			}
		}
	}
}

func (n *NatsOutput) subjectName(c *Config, meta outputs.Meta) string {
	if c.SubjectPrefix != "" {
		ssb := strings.Builder{}
		ssb.WriteString(n.Cfg.SubjectPrefix)
		if s, ok := meta["source"]; ok {
			source := strings.ReplaceAll(s, ".", "-")
			source = strings.ReplaceAll(source, " ", "_")
			ssb.WriteString(".")
			ssb.WriteString(source)
		}
		if subname, ok := meta["subscription-name"]; ok {
			ssb.WriteString(".")
			ssb.WriteString(subname)
		}
		return strings.ReplaceAll(ssb.String(), " ", "_")
	}
	return strings.ReplaceAll(n.Cfg.Subject, " ", "_")
}

func (n *NatsOutput) SetName(name string) {
	sb := strings.Builder{}
	if name != "" {
		sb.WriteString(name)
		sb.WriteString("-")
	}
	sb.WriteString(n.Cfg.Name)
	sb.WriteString("-nats-pub")
	n.Cfg.Name = sb.String()
}

func (n *NatsOutput) SetClusterName(name string) {}
<commit_msg>NATS output<commit_after>package nats_output

import (
	"context"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net"
	"strings"
	"sync"
	"text/template"
	"time"

	"github.com/google/uuid"
	"github.com/karimra/gnmic/formatters"
	"github.com/karimra/gnmic/outputs"
	"github.com/nats-io/nats.go"
	"github.com/prometheus/client_golang/prometheus"
	"google.golang.org/protobuf/proto"
)

const (
	natsConnectWait         = 2 * time.Second
	natsReconnectBufferSize = 100 * 1024 * 1024
	defaultSubjectName      = "telemetry"
	defaultFormat           = "event"
	defaultNumWorkers       = 1
	defaultWriteTimeout     = 5 * time.Second
	defaultAddress          = "localhost:4222"
	loggingPrefix           = "[nats_output] "
)

func init() {
	outputs.Register("nats", func() outputs.Output {
		return &NatsOutput{
			Cfg:    &Config{},
			wg:     new(sync.WaitGroup),
			logger: log.New(ioutil.Discard, loggingPrefix, log.LstdFlags|log.Lmicroseconds),
		}
	})
}

type protoMsg struct {
	m    proto.Message
	meta outputs.Meta
}

// NatsOutput //
type NatsOutput struct {
	Cfg      *Config
	ctx      context.Context
	cancelFn context.CancelFunc
	msgChan  chan *protoMsg
	wg       *sync.WaitGroup
	logger   *log.Logger
	mo       *formatters.MarshalOptions
	evps     []formatters.EventProcessor

	targetTpl *template.Template
}

// Config //
type Config struct {
	Name               string        `mapstructure:"name,omitempty"`
	Address            string        `mapstructure:"address,omitempty"`
	SubjectPrefix      string        `mapstructure:"subject-prefix,omitempty"`
	Subject            string        `mapstructure:"subject,omitempty"`
	Username           string        `mapstructure:"username,omitempty"`
	Password           string        `mapstructure:"password,omitempty"`
	ConnectTimeWait    time.Duration `mapstructure:"connect-time-wait,omitempty"`
	Format             string        `mapstructure:"format,omitempty"`
	AddTarget          string        `mapstructure:"add-target,omitempty"`
	TargetTemplate     string        `mapstructure:"target-template,omitempty"`
	OverrideTimestamps bool          `mapstructure:"override-timestamps,omitempty"`
	NumWorkers         int           `mapstructure:"num-workers,omitempty"`
	WriteTimeout       time.Duration `mapstructure:"write-timeout,omitempty"`
	Debug              bool          `mapstructure:"debug,omitempty"`
	EnableMetrics      bool          `mapstructure:"enable-metrics,omitempty"`
	EventProcessors    []string      `mapstructure:"event-processors,omitempty"`
}

func (n *NatsOutput) String() string {
	b, err := json.Marshal(n)
	if err != nil {
		return ""
	}
	return string(b)
}

func (n *NatsOutput) SetLogger(logger *log.Logger) {
	if logger != nil && n.logger != nil {
		n.logger.SetOutput(logger.Writer())
		n.logger.SetFlags(logger.Flags())
	}
}

func (n *NatsOutput) SetEventProcessors(ps map[string]map[string]interface{}, logger *log.Logger, tcs map[string]interface{}) {
	for _, epName := range n.Cfg.EventProcessors {
		if epCfg, ok := ps[epName]; ok {
			epType := ""
			for k := range epCfg {
				epType = k
				break
			}
			if in, ok := formatters.EventProcessors[epType]; ok {
				ep := in()
				err := ep.Init(epCfg[epType], formatters.WithLogger(logger), formatters.WithTargets(tcs))
				if err != nil {
					n.logger.Printf("failed initializing event processor '%s' of type='%s': %v", epName, epType, err)
					continue
				}
				n.evps = append(n.evps, ep)
				n.logger.Printf("added event processor '%s' of type=%s to nats output", epName, epType)
				continue
			}
			n.logger.Printf("%q event processor has an unknown type=%q", epName, epType)
			continue
		}
		n.logger.Printf("%q event processor not found!", epName)
	}
}

// Init //
func (n *NatsOutput) Init(ctx context.Context, name string, cfg map[string]interface{}, opts ...outputs.Option) error {
	err := outputs.DecodeConfig(cfg, n.Cfg)
	if err != nil {
		return err
	}
	if n.Cfg.Name == "" {
		n.Cfg.Name = name
	}
	for _, opt := range opts {
		opt(n)
	}
	err = n.setDefaults()
	if err != nil {
		return err
	}
	n.msgChan = make(chan *protoMsg)
	initMetrics()
	n.mo = &formatters.MarshalOptions{
		Format:     n.Cfg.Format,
		OverrideTS: n.Cfg.OverrideTimestamps,
	}
	if n.Cfg.TargetTemplate == "" {
		n.targetTpl = outputs.DefaultTargetTemplate
	} else if n.Cfg.AddTarget != "" {
		n.targetTpl, err = template.New("target-template").
			Funcs(outputs.TemplateFuncs).
			Parse(n.Cfg.TargetTemplate)
		if err != nil {
			return err
		}
	}
	n.ctx, n.cancelFn = context.WithCancel(ctx)
	n.wg.Add(n.Cfg.NumWorkers)
	for i := 0; i < n.Cfg.NumWorkers; i++ {
		cfg := *n.Cfg
		cfg.Name = fmt.Sprintf("%s-%d", cfg.Name, i)
		go n.worker(ctx, i, &cfg)
	}

	go func() {
		<-ctx.Done()
		n.Close()
	}()
	return nil
}

func (n *NatsOutput) setDefaults() error {
	if n.Cfg.Format == "" {
		n.Cfg.Format = defaultFormat
	}
	if !(n.Cfg.Format == "event" || n.Cfg.Format == "protojson" || n.Cfg.Format == "proto" || n.Cfg.Format == "json") {
		return fmt.Errorf("unsupported output format '%s' for output type NATS", n.Cfg.Format)
	}
	if n.Cfg.Address == "" {
		n.Cfg.Address = defaultAddress
	}
	if n.Cfg.ConnectTimeWait <= 0 {
		n.Cfg.ConnectTimeWait = natsConnectWait
	}
	if n.Cfg.Subject == "" && n.Cfg.SubjectPrefix == "" {
		n.Cfg.Subject = defaultSubjectName
	}
	if n.Cfg.Name == "" {
		n.Cfg.Name = "gnmic-" + uuid.New().String()
	}
	if n.Cfg.NumWorkers <= 0 {
		n.Cfg.NumWorkers = defaultNumWorkers
	}
	if n.Cfg.WriteTimeout <= 0 {
		n.Cfg.WriteTimeout = defaultWriteTimeout
	}
	return nil
}

// Write //
func (n *NatsOutput) Write(ctx context.Context, rsp proto.Message, meta outputs.Meta) {
	if rsp == nil || n.mo == nil {
		return
	}

	wctx, cancel := context.WithTimeout(ctx, n.Cfg.WriteTimeout)
	defer cancel()

	select {
	case <-ctx.Done():
		return
	case n.msgChan <- &protoMsg{m: rsp, meta: meta}:
	case <-wctx.Done():
		if n.Cfg.Debug {
			n.logger.Printf("writing expired after %s, NATS output might not be initialized", n.Cfg.WriteTimeout)
		}
		if n.Cfg.EnableMetrics {
			NatsNumberOfFailSendMsgs.WithLabelValues(n.Cfg.Name, "timeout").Inc()
		}
		return
	}
}

func (n *NatsOutput) WriteEvent(ctx context.Context, ev *formatters.EventMsg) {}

// Close //
func (n *NatsOutput) Close() error {
	//	n.conn.Close()
	n.cancelFn()
	n.wg.Wait()
	return nil
}

// Metrics //
func (n *NatsOutput) RegisterMetrics(reg *prometheus.Registry) {
	if !n.Cfg.EnableMetrics {
		return
	}
	if err := registerMetrics(reg); err != nil {
		n.logger.Printf("failed to register metric: %+v", err)
	}
}

func (n *NatsOutput) createNATSConn(c *Config) (*nats.Conn, error) {
	opts := []nats.Option{
		nats.Name(c.Name),
		nats.SetCustomDialer(n),
		nats.ReconnectWait(n.Cfg.ConnectTimeWait),
		nats.ReconnectBufSize(natsReconnectBufferSize),
		nats.ErrorHandler(func(_ *nats.Conn, _ *nats.Subscription, err error) {
			n.logger.Printf("NATS error: %v", err)
		}),
		nats.DisconnectHandler(func(c *nats.Conn) {
			n.logger.Println("Disconnected from NATS")
		}),
		nats.ClosedHandler(func(c *nats.Conn) {
			n.logger.Println("NATS connection is closed")
		}),
	}
	if c.Username != "" && c.Password != "" {
		opts = append(opts, nats.UserInfo(c.Username, c.Password))
	}
	nc, err := nats.Connect(c.Address, opts...)
	if err != nil {
		return nil, err
	}
	return nc, nil
}

// Dial //
func (n *NatsOutput) Dial(network, address string) (net.Conn, error) {
	ctx, cancel := context.WithCancel(n.ctx)
	defer cancel()

	for {
		n.logger.Printf("attempting to connect to %s", address)
		if ctx.Err() != nil {
			return nil, ctx.Err()
		}

		select {
		case <-n.ctx.Done():
			return nil, n.ctx.Err()
		default:
			d := &net.Dialer{}
			if conn, err := d.DialContext(ctx, network, address); err == nil {
				n.logger.Printf("successfully connected to NATS server %s", address)
				return conn, nil
			}
			time.Sleep(n.Cfg.ConnectTimeWait)
		}
	}
}

func (n *NatsOutput) worker(ctx context.Context, i int, cfg *Config) {
	defer n.wg.Done()
	var natsConn *nats.Conn
	var err error
	workerLogPrefix := fmt.Sprintf("worker-%d", i)
	n.logger.Printf("%s starting", workerLogPrefix)
CRCONN:
	natsConn, err = n.createNATSConn(cfg)
	if err != nil {
		n.logger.Printf("%s failed to create connection: %v", workerLogPrefix, err)
		time.Sleep(n.Cfg.ConnectTimeWait)
		goto CRCONN
	}
	defer natsConn.Close()
	n.logger.Printf("%s initialized nats producer: %+v", workerLogPrefix, cfg)
	for {
		select {
		case <-ctx.Done():
			n.logger.Printf("%s flushing", workerLogPrefix)
			natsConn.FlushTimeout(time.Second)
			n.logger.Printf("%s shutting down", workerLogPrefix)
			return
		case m := <-n.msgChan:
			err = outputs.AddSubscriptionTarget(m.m, m.meta, n.Cfg.AddTarget, n.targetTpl)
			if err != nil {
				n.logger.Printf("failed to add target to the response: %v", err)
			}
			b, err := n.mo.Marshal(m.m, m.meta, n.evps...)
			if err != nil {
				if n.Cfg.Debug {
					n.logger.Printf("%s failed marshaling proto msg: %v", workerLogPrefix, err)
				}
				if n.Cfg.EnableMetrics {
					NatsNumberOfFailSendMsgs.WithLabelValues(cfg.Name, "marshal_error").Inc()
				}
				continue
			}
			subject := n.subjectName(cfg, m.meta)
			var start time.Time
			if n.Cfg.EnableMetrics {
				start = time.Now()
			}
			err = natsConn.Publish(subject, b)
			if err != nil {
				if n.Cfg.Debug {
					n.logger.Printf("%s failed to write to nats subject '%s': %v", workerLogPrefix, subject, err)
				}
				if n.Cfg.EnableMetrics {
					NatsNumberOfFailSendMsgs.WithLabelValues(cfg.Name, "publish_error").Inc()
				}
				natsConn.Close()
				time.Sleep(cfg.ConnectTimeWait)
				goto CRCONN
			}
			if n.Cfg.EnableMetrics {
				NatsSendDuration.WithLabelValues(cfg.Name).Set(float64(time.Since(start).Nanoseconds()))
				NatsNumberOfSentMsgs.WithLabelValues(cfg.Name, subject).Inc()
				NatsNumberOfSentBytes.WithLabelValues(cfg.Name, subject).Add(float64(len(b)))
			}
		}
	}
}

func (n *NatsOutput) subjectName(c *Config, meta outputs.Meta) string {
	if c.SubjectPrefix != "" {
		ssb := strings.Builder{}
		ssb.WriteString(n.Cfg.SubjectPrefix)
		if s, ok := meta["source"]; ok {
			source := strings.ReplaceAll(s, ".", "-")
			source = strings.ReplaceAll(source, " ", "_")
			ssb.WriteString(".")
			ssb.WriteString(source)
		}
		if subname, ok := meta["subscription-name"]; ok {
			ssb.WriteString(".")
			ssb.WriteString(subname)
		}
		return strings.ReplaceAll(ssb.String(), " ", "_")
	}
	return strings.ReplaceAll(n.Cfg.Subject, " ", "_")
}

func (n *NatsOutput) SetName(name string) {
	sb := strings.Builder{}
	if name != "" {
		sb.WriteString(name)
		sb.WriteString("-")
	}
	sb.WriteString(n.Cfg.Name)
	sb.WriteString("-nats-pub")
	n.Cfg.Name = sb.String()
}

func (n *NatsOutput) SetClusterName(name string) {}
<|endoftext|>


<commit_before>package main

import irc "github.com/gentux/goirc/client"
import "math/rand"
import "regexp"

// Guilhem Bot core
func handleMessageGuilhem(conn *irc.Conn, line *irc.Line) {
	technicalAnswers := []string{
		"Mais ! C'est de la merde !",
		"On doit le réecrire en GO ça !",
		"Mais c'est pas du tout ça le problème, en fait […]",
		"C'est fait roulé sous les aisselles",
		"I have no *FRAKING* idea!",
		"NAN ! Je suis en copie, je suis perché !",
		"je veux pas devenir un SPOF",
		"'Tain IRC ! Protocole de merde ! Je vais mettre en place un XM Peuh Peuh",
		"Non mais ça c'est soooo 2011 !",
		"Les mecs de chez […] ont rien compris, j'te fouterai tout le monde à la porte moi.",
		"Désolé d'avoir raison",
	}

	personalAnswers := []string{
		"Mais j'suis pas super fort, j'suis mieux qu'ça même. J'suis surpuissant.",
		"Je parle fort… J'ai du charisme, c'est tout !",
		"Yves, Yves, YVES ! Laisse nous parler",
		"Yves, Yves, YVES ! Après le daily stp !",
		"J'ai un super nom pour le deuxième DC ! WATTOO WATTOO",
		"Attends, y'a mon nom sur un t-shirt s'il-te-plaît !",
		"J'me vois pas me priver de bonheur parce que les autres veulent une vie de merde",
		"«Vous ne pouvez *résoudre* un problème avec le *même* type de *pensée* qui a créé le problème» A.Einstein",
		"Il faut que je me sente… Important",
		"Oula oui, au moins ça !",
		"«rm -Rf de-toute-l-équipe», oui !",
		"Nan, mais j'illumine la tour eiffel depuis chez moi !",
	}

	openstackRegexp, _ := regexp.Compile("jarvis|openstack|keystone|deploy|datacenter|bug|meeting|horizon|glance|cinder|contrail|coreos|dc|nova|neutron")
	technicalPoint := openstackRegexp.FindString(line.Text())
	randomLine := rand.Intn(30)

	if technicalPoint != "" {
		conn.Privmsg(
			channelName,
			technicalAnswers[rand.Intn(len(technicalAnswers))],
		)
	} else if randomLine < 3 {
		conn.Privmsg(
			channelName,
			personalAnswers[rand.Intn(len(personalAnswers))],
		)
	}
}

// ygbo Bot core
func handleMessageYgbot(conn *irc.Conn, line *irc.Line) {
	technicalAnswers := []string{
		"Ça a été codé avec le cul!",
		"C'est fait par un mec qu'a le QI d'un annus artificiel!",
		"J'y crois pas! jamais vu une horreur pareille!",
	}

	personalAnswers := []string{
		"Hein?",
		"Hein?",
		"Hein?",
		"Tu disait?",
	}

	personalAnswersFix := []string{
		"",
		"",
		"",
		"s/disait/disias/",
	}

	personalAnswersFixFix := []string{
		"",
		"",
		"",
		"t'ain d'clavire!",
	}

	openstackRegexp, _ := regexp.Compile("javascript|JavaScript|bug|git")
	technicalPoint := openstackRegexp.FindString(line.Text())
	randomLine := rand.Intn(30)

	if technicalPoint != "" {
		randomAnswer := rand.Intn(len(technicalAnswers))
		conn.Privmsg(
			channelName,
			technicalAnswers[randomAnswer],
		)
	} else if randomLine < 3 {
		randomAnswer := rand.Intn(len(personalAnswers))
		conn.Privmsg(
			channelName,
			personalAnswers[randomAnswer],
		)
		if personalAnswersFix[randomAnswer] != "" {
			conn.Privmsg(
				channelName,
				personalAnswersFix[randomAnswer],
			)
			if personalAnswersFixFix[randomAnswer] != "" {
				conn.Privmsg(
					channelName,
					personalAnswersFixFix[randomAnswer],
				)
			}
		}
	}
}

// Carot Bot core
func handleMessageCarot(conn *irc.Conn, line *irc.Line) {
	answers := []string{
		"J'vais t'peter la gueule",
		"Tu veux t'battre ?",
		"Oh, il est beau ton bleu !",
		"Chocolat !?",
		"Est-ce que t'es bon au baby ?",
		"Demain, j'ramène des cookies",
		"On va boire une bière vite fait ce soir ?",
		"Lâche pas les manettes !!",
	}

	randomLine := rand.Intn(30)
	if randomLine < 3 {
		conn.Privmsg(channelName, answers[rand.Intn(len(answers))])
	}
}

// VBAbot Bot core
func handleMessageVBAbot(conn *irc.Conn, line *irc.Line) {
	technicalAnswers := []string{
		"Un truc codé avec emacs par un manchot !",
		"Evidemment c est encore la faute aux sysadmins :(",
		"Ils auraient du le faire en cobol !",
		"Cette bouse me reveille toutes les nuits !",
		"As tu teste en dev ? integration ? staging ?",
		"Dans le doute reboot ?",
		"Et si on le recodait en LISP ?",
	}

	personalAnswers := []string{
		"ASV ?",
		"L'informatique dans les nuages..",
		"Jamais le vendredi",
		"R IP",
		"Oui ce serait effectivement mieux avec jabber",
		"Ce sera en salle cumulonimbus",
	}

	techRegexp, _ := regexp.Compile("opencontrail|contrail|neutron|django|nova|nagios|kibana|bss|prod|staging|integration|irc|bot")
	technicalPoint := techRegexp.FindString(line.Text())
	randomLine := rand.Intn(30)

	if technicalPoint != "" {
		randomAnswer := rand.Intn(len(technicalAnswers))
		conn.Privmsg(
			channelName,
			technicalAnswers[randomAnswer],
		)
	} else if randomLine < 3 {
		randomAnswer := rand.Intn(len(personalAnswers))
		conn.Privmsg(
			channelName,
			personalAnswers[randomAnswer],
		)
	}
}
<commit_msg>More strings and fixes<commit_after>package main

import irc "github.com/gentux/goirc/client"
import "math/rand"
import "regexp"

// Guilhem Bot core
func handleMessageGuilhem(conn *irc.Conn, line *irc.Line) {
	technicalAnswers := []string{
		"Mais ! C'est de la merde !",
		"On doit le réecrire en GO ça !",
		"Mais c'est pas du tout ça le problème, en fait […]",
		"C'est fait roulé sous les aisselles",
		"I have no *FRAKING* idea!",
		"NAN ! Je suis en copie, je suis perché !",
		"je veux pas devenir un SPOF",
		"'Tain IRC ! Protocole de merde ! Je vais mettre en place un XM Peuh Peuh",
		"Non mais ça c'est soooo 2011 !",
		"Les mecs de chez […] ont rien compris, j'te fouterai tout le monde à la porte moi.",
		"Désolé d'avoir raison",
	}

	personalAnswers := []string{
		"Mais j'suis pas super fort, j'suis mieux qu'ça même. J'suis surpuissant.",
		"Je parle fort… J'ai du charisme, c'est tout !",
		"Yves, Yves, YVES ! Laisse nous parler",
		"Yves, Yves, YVES ! Après le daily stp !",
		"J'ai un super nom pour le deuxième DC ! WATTOO WATTOO",
		"Attends, y'a mon nom sur un t-shirt s'il-te-plaît !",
		"J'me vois pas me priver de bonheur parce que les autres veulent une vie de merde",
		"«Vous ne pouvez *résoudre* un problème avec le *même* type de *pensée* qui a créé le problème» A.Einstein",
		"Il faut que je me sente… Important",
		"Oula oui, au moins ça !",
		"«rm -Rf de-toute-l-équipe», oui !",
		"Nan, mais j'illumine la tour eiffel depuis chez moi !",
	}

	openstackRegexp, _ := regexp.Compile("jarvis|openstack|keystone|deploy|datacenter|bug|meeting|horizon|glance|cinder|contrail|coreos|dc|nova|neutron")
	technicalPoint := openstackRegexp.FindString(line.Text())
	randomLine := rand.Intn(30)

	if technicalPoint != "" {
		conn.Privmsg(
			channelName,
			technicalAnswers[rand.Intn(len(technicalAnswers))],
		)
	} else if randomLine < 3 {
		conn.Privmsg(
			channelName,
			personalAnswers[rand.Intn(len(personalAnswers))],
		)
	}
}

// ygbo Bot core
func handleMessageYgbot(conn *irc.Conn, line *irc.Line) {
	technicalAnswers := []string{
		"Ça a été codé avec le cul!",
		"C'est fait par un mec qu'a le QI d'un anus artificiel!",
		"J'y crois pas! jamais vu une horreur pareille!",
		"Une GUI pensée avec un QI minimum est sensé être plus simple que la CLI, si la CLI est plus simple, c'est que la target de la GUI est ratée!",
		"Les paquets Debian sont au développement ce que les cartons d'emballage sont aux meubles Ikéa.",
	}

	personalAnswers := []string{
		"Hein?",
		"Hein?",
		"Hein?",
		"Tu disait?",
	}

	personalAnswersFix := []string{
		"",
		"",
		"",
		"s/disait/disias/",
	}

	personalAnswersFixFix := []string{
		"",
		"",
		"",
		"t'ain d'clavire!",
	}

	openstackRegexp, _ := regexp.Compile("(?i)javascript|bug|git|kibana|jira|selenium|packaging|kde|unity")
	technicalPoint := openstackRegexp.FindString(line.Text())
	randomLine := rand.Intn(30)

	if technicalPoint != "" {
		randomAnswer := rand.Intn(len(technicalAnswers))
		conn.Privmsg(
			channelName,
			technicalAnswers[randomAnswer],
		)
	} else if randomLine < 3 {
		randomAnswer := rand.Intn(len(personalAnswers))
		conn.Privmsg(
			channelName,
			personalAnswers[randomAnswer],
		)
		if personalAnswersFix[randomAnswer] != "" {
			conn.Privmsg(
				channelName,
				personalAnswersFix[randomAnswer],
			)
			if personalAnswersFixFix[randomAnswer] != "" {
				conn.Privmsg(
					channelName,
					personalAnswersFixFix[randomAnswer],
				)
			}
		}
	}
}

// Carot Bot core
func handleMessageCarot(conn *irc.Conn, line *irc.Line) {
	answers := []string{
		"J'vais t'peter la gueule",
		"Tu veux t'battre ?",
		"Oh, il est beau ton bleu !",
		"Chocolat !?",
		"Est-ce que t'es bon au baby ?",
		"Demain, j'ramène des cookies",
		"On va boire une bière vite fait ce soir ?",
		"Lâche pas les manettes !!",
	}

	randomLine := rand.Intn(30)
	if randomLine < 3 {
		conn.Privmsg(channelName, answers[rand.Intn(len(answers))])
	}
}

// VBAbot Bot core
func handleMessageVBAbot(conn *irc.Conn, line *irc.Line) {
	technicalAnswers := []string{
		"Un truc codé avec emacs par un manchot !",
		"Evidemment c est encore la faute aux sysadmins :(",
		"Ils auraient du le faire en cobol !",
		"Cette bouse me reveille toutes les nuits !",
		"As tu teste en dev ? integration ? staging ?",
		"Dans le doute reboot ?",
		"Et si on le recodait en LISP ?",
	}

	personalAnswers := []string{
		"ASV ?",
		"L'informatique dans les nuages..",
		"Jamais le vendredi",
		"R IP",
		"Oui ce serait effectivement mieux avec jabber",
		"Ce sera en salle cumulonimbus",
	}

	techRegexp, _ := regexp.Compile("opencontrail|contrail|neutron|django|nova|nagios|kibana|bss|prod|staging|integration|irc|bot")
	technicalPoint := techRegexp.FindString(line.Text())
	randomLine := rand.Intn(30)

	if technicalPoint != "" {
		randomAnswer := rand.Intn(len(technicalAnswers))
		conn.Privmsg(
			channelName,
			technicalAnswers[randomAnswer],
		)
	} else if randomLine < 3 {
		randomAnswer := rand.Intn(len(personalAnswers))
		conn.Privmsg(
			channelName,
			personalAnswers[randomAnswer],
		)
	}
}
<|endoftext|>


<commit_before>package main

import (
	"bufio"
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"strings"

	"github.com/codegangsta/cli"
)

func sync(c *cli.Context) {
	repo := c.String("repo")
	gopath := c.String("gopath")

	absRepo, err := filepath.Abs(repo)
	if err != nil {
		println("could not resolve repo: " + err.Error())
		os.Exit(1)
	}

	absGopath, err := filepath.Abs(gopath)
	if err != nil {
		println("could not resolve gopath: " + err.Error())
		os.Exit(1)
	}

	pkgRoots := map[string]*Repo{}

	for _, dep := range c.Args() {
		root, repo, err := getDepRoot(absRepo, absGopath, dep)
		if err != nil {
			println("failed to get dependency repo: " + err.Error())
			os.Exit(1)
		}

		pkgRoots[root] = repo
	}

	existingSubmodules, err := detectExistingGoSubmodules(repo, gopath)
	if err != nil {
		println("failed to detect existing submodules: " + err.Error())
		os.Exit(1)
	}

	for _, submodule := range existingSubmodules {
		rm := exec.Command("git", "rm", "--cached", submodule)
		rm.Dir = repo
		rm.Stderr = os.Stderr

		err := rm.Run()
		if err != nil {
			println("error clearing submodule: " + err.Error())
			os.Exit(1)
		}
	}

	for pkgRoot, pkgRepo := range pkgRoots {
		relRoot, err := filepath.Rel(absRepo, pkgRoot)
		if err != nil {
			println("could not resolve submodule: " + err.Error())
			os.Exit(1)
		}

		fmt.Println(relRoot)

		add := exec.Command("git", "add", pkgRoot)
		add.Dir = repo
		add.Stderr = os.Stderr

		err = add.Run()
		if err != nil {
			println("error clearing submodule: " + err.Error())
			os.Exit(1)
		}

		if pkgRepo == nil {
			// non-git dependency; vendored
			continue
		}

		gitmodules := filepath.Join(repo, ".gitmodules")

		gitConfig := exec.Command("git", "config", "--file", gitmodules, "submodule."+relRoot+".path", relRoot)
		gitConfig.Stderr = os.Stderr

		err = gitConfig.Run()
		if err != nil {
			println("error configuring submodule: " + err.Error())
			os.Exit(1)
		}

		gitConfig = exec.Command("git", "config", "--file", gitmodules, "submodule."+relRoot+".url", pkgRepo.Origin)
		gitConfig.Stderr = os.Stderr

		err = gitConfig.Run()
		if err != nil {
			println("error configuring submodule: " + err.Error())
			os.Exit(1)
		}

		if pkgRepo.Branch != "" {
			gitConfig = exec.Command("git", "config", "--file", gitmodules, "submodule."+relRoot+".branch", pkgRepo.Branch)
			gitConfig.Stderr = os.Stderr

			err = gitConfig.Run()
			if err != nil {
				println("error configuring submodule: " + err.Error())
				os.Exit(1)
			}
		}
	}
}

func detectExistingGoSubmodules(repo string, gopath string) ([]string, error) {
	srcPath := filepath.Join(gopath, "src")

	submoduleStatus := exec.Command("git", "submodule", "status", srcPath)
	submoduleStatus.Dir = repo

	submoduleStatus.Stderr = os.Stderr

	statusOut, err := submoduleStatus.StdoutPipe()
	if err != nil {
		return nil, err
	}

	lineScanner := bufio.NewScanner(statusOut)

	err = submoduleStatus.Start()
	if err != nil {
		return nil, err
	}

	submodules := []string{}
	for lineScanner.Scan() {
		segments := strings.Split(lineScanner.Text(), " ")

		if len(segments) < 3 {
			return nil, fmt.Errorf("invalid git status output: %q", lineScanner.Text())
		}

		submodules = append(submodules, segments[2])
	}

	err = submoduleStatus.Wait()
	if err != nil {
		return nil, err
	}

	return submodules, nil
}
<commit_msg>remove submodule with -f<commit_after>package main

import (
	"bufio"
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"strings"

	"github.com/codegangsta/cli"
)

func sync(c *cli.Context) {
	repo := c.String("repo")
	gopath := c.String("gopath")

	absRepo, err := filepath.Abs(repo)
	if err != nil {
		println("could not resolve repo: " + err.Error())
		os.Exit(1)
	}

	absGopath, err := filepath.Abs(gopath)
	if err != nil {
		println("could not resolve gopath: " + err.Error())
		os.Exit(1)
	}

	pkgRoots := map[string]*Repo{}

	for _, dep := range c.Args() {
		root, repo, err := getDepRoot(absRepo, absGopath, dep)
		if err != nil {
			println("failed to get dependency repo: " + err.Error())
			os.Exit(1)
		}

		pkgRoots[root] = repo
	}

	existingSubmodules, err := detectExistingGoSubmodules(repo, gopath)
	if err != nil {
		println("failed to detect existing submodules: " + err.Error())
		os.Exit(1)
	}

	for _, submodule := range existingSubmodules {
		rm := exec.Command("git", "rm", "--cached", "-f", submodule)
		rm.Dir = repo
		rm.Stderr = os.Stderr

		err := rm.Run()
		if err != nil {
			println("error clearing submodule: " + err.Error())
			os.Exit(1)
		}
	}

	for pkgRoot, pkgRepo := range pkgRoots {
		relRoot, err := filepath.Rel(absRepo, pkgRoot)
		if err != nil {
			println("could not resolve submodule: " + err.Error())
			os.Exit(1)
		}

		fmt.Println(relRoot)

		add := exec.Command("git", "add", pkgRoot)
		add.Dir = repo
		add.Stderr = os.Stderr

		err = add.Run()
		if err != nil {
			println("error clearing submodule: " + err.Error())
			os.Exit(1)
		}

		if pkgRepo == nil {
			// non-git dependency; vendored
			continue
		}

		gitmodules := filepath.Join(repo, ".gitmodules")

		gitConfig := exec.Command("git", "config", "--file", gitmodules, "submodule."+relRoot+".path", relRoot)
		gitConfig.Stderr = os.Stderr

		err = gitConfig.Run()
		if err != nil {
			println("error configuring submodule: " + err.Error())
			os.Exit(1)
		}

		gitConfig = exec.Command("git", "config", "--file", gitmodules, "submodule."+relRoot+".url", pkgRepo.Origin)
		gitConfig.Stderr = os.Stderr

		err = gitConfig.Run()
		if err != nil {
			println("error configuring submodule: " + err.Error())
			os.Exit(1)
		}

		if pkgRepo.Branch != "" {
			gitConfig = exec.Command("git", "config", "--file", gitmodules, "submodule."+relRoot+".branch", pkgRepo.Branch)
			gitConfig.Stderr = os.Stderr

			err = gitConfig.Run()
			if err != nil {
				println("error configuring submodule: " + err.Error())
				os.Exit(1)
			}
		}
	}
}

func detectExistingGoSubmodules(repo string, gopath string) ([]string, error) {
	srcPath := filepath.Join(gopath, "src")

	submoduleStatus := exec.Command("git", "submodule", "status", srcPath)
	submoduleStatus.Dir = repo

	submoduleStatus.Stderr = os.Stderr

	statusOut, err := submoduleStatus.StdoutPipe()
	if err != nil {
		return nil, err
	}

	lineScanner := bufio.NewScanner(statusOut)

	err = submoduleStatus.Start()
	if err != nil {
		return nil, err
	}

	submodules := []string{}
	for lineScanner.Scan() {
		segments := strings.Split(lineScanner.Text(), " ")

		if len(segments) < 3 {
			return nil, fmt.Errorf("invalid git status output: %q", lineScanner.Text())
		}

		submodules = append(submodules, segments[2])
	}

	err = submoduleStatus.Wait()
	if err != nil {
		return nil, err
	}

	return submodules, nil
}
<|endoftext|>


<commit_before>/*
Copyright 2014 Rohith All rights reserved.

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 marathon

import (
	"fmt"
	"strings"
)

// Tasks is a collection of marathon tasks
type Tasks struct {
	Tasks []Task `json:"tasks"`
}

// Task is the definition for a marathon task
type Task struct {
	ID                 string               `json:"id"`
	AppID              string               `json:"appId"`
	Host               string               `json:"host"`
	HealthCheckResults []*HealthCheckResult `json:"healthCheckResults"`
	Ports              []int                `json:"ports"`
	ServicePorts       []int                `json:"servicePorts"`
	StagedAt           string               `json:"stagedAt"`
	StartedAt          string               `json:"startedAt"`
	Version            string               `json:"version"`
}

// AllTasksOpts contains a payload for AllTasks method
//		status:		Return only those tasks whose status matches this parameter.
//				If not specified, all tasks are returned. Possible values: running, staging. Default: none.
type AllTasksOpts struct {
	Status string `url:"status,omitempty"`
}

// KillApplicationTasksOpts contains a payload for KillApplicationTasks method
//		host:		kill only those tasks on a specific host (optional)
//		scale:		Scale the app down (i.e. decrement its instances setting by the number of tasks killed) after killing the specified tasks
type KillApplicationTasksOpts struct {
	Host  string `url:"host,omitempty"`
	Scale bool   `url:"scale,omitempty"`
}

// KillTaskOpts contains a payload for task killing methods
//		scale:		Scale the app down
type KillTaskOpts struct {
	Scale bool `url:"scale,omitempty"`
}

// HasHealthCheckResults checks if the task has any health checks
func (r *Task) HasHealthCheckResults() bool {
	return r.HealthCheckResults != nil && len(r.HealthCheckResults) > 0
}

// AllTasks lists tasks of all applications.
//		opts: 		AllTasksOpts request payload
func (r *marathonClient) AllTasks(opts *AllTasksOpts) (*Tasks, error) {
	u, err := addOptions(marathonAPITasks, opts)
	if err != nil {
		return nil, err
	}

	tasks := new(Tasks)
	if err := r.apiGet(u, nil, tasks); err != nil {
		return nil, err
	}

	return tasks, nil
}

// Tasks retrieves a list of tasks for an application
//		id:		the id of the application
func (r *marathonClient) Tasks(id string) (*Tasks, error) {
	tasks := new(Tasks)
	if err := r.apiGet(fmt.Sprintf("%s/%s/tasks", marathonAPIApps, trimRootPath(id)), nil, tasks); err != nil {
		return nil, err
	}

	return tasks, nil
}

// KillApplicationTasks kills all tasks relating to an application
//		id:		the id of the application
//		opts: 		KillApplicationTasksOpts request payload
func (r *marathonClient) KillApplicationTasks(id string, opts *KillApplicationTasksOpts) (*Tasks, error) {
	u := fmt.Sprintf("%s/%s/tasks", marathonAPIApps, trimRootPath(id))
	u, err := addOptions(u, opts)
	if err != nil {
		return nil, err
	}

	tasks := new(Tasks)
	if err := r.apiDelete(u, nil, tasks); err != nil {
		return nil, err
	}

	return tasks, nil
}

// KillTask kills the task associated with a given ID
// 	taskID:		the id for the task
//	opts:		KillTaskOpts request payload
func (r *marathonClient) KillTask(taskID string, opts *KillTaskOpts) (*Task, error) {
	appName := taskID[0:strings.LastIndex(taskID, ".")]
	u := fmt.Sprintf("%s/%s/tasks/%s", marathonAPIApps, appName, taskID)
	u, err := addOptions(u, opts)
	if err != nil {
		return nil, err
	}

	wrappedTask := new(struct {
		Task Task `json:"task"`
	})

	if err := r.apiDelete(u, nil, wrappedTask); err != nil {
		return nil, err
	}

	return &wrappedTask.Task, nil
}

// KillTasks kills tasks associated with given array of ids
//	tasks:		the array of task ids
//	opts:		KillTaskOpts request payload
func (r *marathonClient) KillTasks(tasks []string, opts *KillTaskOpts) error {
	u := fmt.Sprintf("%s/delete", marathonAPITasks)
	u, err := addOptions(u, opts)
	if err != nil {
		return nil
	}

	var post struct {
		IDs []string `json:"ids"`
	}
	post.IDs = tasks

	return r.apiPost(u, &post, nil)
}

// TaskEndpoints gets the endpoints i.e. HOST_IP:DYNAMIC_PORT for a specific application service
// I.e. a container running apache, might have ports 80/443 (translated to X dynamic ports), but i want
// port 80 only and i only want those whom have passed the health check
//
// Note: I've NO IDEA how to associate the health_check_result to the actual port, I don't think it's
// possible at the moment, however, given marathon will fail and restart an application even if one of x ports of a task is
// down, the per port check is redundant??? .. personally, I like it anyhow, but hey
//

//		name:		the identifier for the application
//		port:		the container port you are interested in
//		health: 	whether to check the health or not
func (r *marathonClient) TaskEndpoints(name string, port int, healthCheck bool) ([]string, error) {
	// step: get the application details
	application, err := r.Application(name)
	if err != nil {
		return nil, err
	}

	// step: we need to get the port index of the service we are interested in
	portIndex, err := application.Container.Docker.ServicePortIndex(port)
	if err != nil {
		return nil, err
	}

	// step: do we have any tasks?
	if application.Tasks == nil || len(application.Tasks) == 0 {
		return nil, nil
	}

	// step: if we are checking health the 'service' has a health check?
	healthCheck = healthCheck && application.HasHealthChecks()

	// step: iterate the tasks and extract the dynamic ports
	var list []string
	for _, task := range application.Tasks {
		if !healthCheck || task.allHealthChecksAlive() {
			endpoint := fmt.Sprintf("%s:%d", task.Host, task.Ports[portIndex])
			list = append(list, endpoint)
		}
	}

	return list, nil
}

func (r *Task) allHealthChecksAlive() bool {
	// check: does the task have a health check result, if NOT, it's because the
	// health of the task hasn't yet been performed, hence we assume it as DOWN
	if !r.HasHealthCheckResults() {
		return false
	}
	// step: check the health results then
	for _, check := range r.HealthCheckResults {
		if check.Alive == false {
			return false
		}
	}

	return true
}
<commit_msg>Add SlaveID field in Task struct<commit_after>/*
Copyright 2014 Rohith All rights reserved.

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 marathon

import (
	"fmt"
	"strings"
)

// Tasks is a collection of marathon tasks
type Tasks struct {
	Tasks []Task `json:"tasks"`
}

// Task is the definition for a marathon task
type Task struct {
	ID                 string               `json:"id"`
	AppID              string               `json:"appId"`
	Host               string               `json:"host"`
	HealthCheckResults []*HealthCheckResult `json:"healthCheckResults"`
	Ports              []int                `json:"ports"`
	ServicePorts       []int                `json:"servicePorts"`
	SlaveID            string               `json:"slaveId"`
	StagedAt           string               `json:"stagedAt"`
	StartedAt          string               `json:"startedAt"`
	Version            string               `json:"version"`
}

// AllTasksOpts contains a payload for AllTasks method
//		status:		Return only those tasks whose status matches this parameter.
//				If not specified, all tasks are returned. Possible values: running, staging. Default: none.
type AllTasksOpts struct {
	Status string `url:"status,omitempty"`
}

// KillApplicationTasksOpts contains a payload for KillApplicationTasks method
//		host:		kill only those tasks on a specific host (optional)
//		scale:		Scale the app down (i.e. decrement its instances setting by the number of tasks killed) after killing the specified tasks
type KillApplicationTasksOpts struct {
	Host  string `url:"host,omitempty"`
	Scale bool   `url:"scale,omitempty"`
}

// KillTaskOpts contains a payload for task killing methods
//		scale:		Scale the app down
type KillTaskOpts struct {
	Scale bool `url:"scale,omitempty"`
}

// HasHealthCheckResults checks if the task has any health checks
func (r *Task) HasHealthCheckResults() bool {
	return r.HealthCheckResults != nil && len(r.HealthCheckResults) > 0
}

// AllTasks lists tasks of all applications.
//		opts: 		AllTasksOpts request payload
func (r *marathonClient) AllTasks(opts *AllTasksOpts) (*Tasks, error) {
	u, err := addOptions(marathonAPITasks, opts)
	if err != nil {
		return nil, err
	}

	tasks := new(Tasks)
	if err := r.apiGet(u, nil, tasks); err != nil {
		return nil, err
	}

	return tasks, nil
}

// Tasks retrieves a list of tasks for an application
//		id:		the id of the application
func (r *marathonClient) Tasks(id string) (*Tasks, error) {
	tasks := new(Tasks)
	if err := r.apiGet(fmt.Sprintf("%s/%s/tasks", marathonAPIApps, trimRootPath(id)), nil, tasks); err != nil {
		return nil, err
	}

	return tasks, nil
}

// KillApplicationTasks kills all tasks relating to an application
//		id:		the id of the application
//		opts: 		KillApplicationTasksOpts request payload
func (r *marathonClient) KillApplicationTasks(id string, opts *KillApplicationTasksOpts) (*Tasks, error) {
	u := fmt.Sprintf("%s/%s/tasks", marathonAPIApps, trimRootPath(id))
	u, err := addOptions(u, opts)
	if err != nil {
		return nil, err
	}

	tasks := new(Tasks)
	if err := r.apiDelete(u, nil, tasks); err != nil {
		return nil, err
	}

	return tasks, nil
}

// KillTask kills the task associated with a given ID
// 	taskID:		the id for the task
//	opts:		KillTaskOpts request payload
func (r *marathonClient) KillTask(taskID string, opts *KillTaskOpts) (*Task, error) {
	appName := taskID[0:strings.LastIndex(taskID, ".")]
	u := fmt.Sprintf("%s/%s/tasks/%s", marathonAPIApps, appName, taskID)
	u, err := addOptions(u, opts)
	if err != nil {
		return nil, err
	}

	wrappedTask := new(struct {
		Task Task `json:"task"`
	})

	if err := r.apiDelete(u, nil, wrappedTask); err != nil {
		return nil, err
	}

	return &wrappedTask.Task, nil
}

// KillTasks kills tasks associated with given array of ids
//	tasks:		the array of task ids
//	opts:		KillTaskOpts request payload
func (r *marathonClient) KillTasks(tasks []string, opts *KillTaskOpts) error {
	u := fmt.Sprintf("%s/delete", marathonAPITasks)
	u, err := addOptions(u, opts)
	if err != nil {
		return nil
	}

	var post struct {
		IDs []string `json:"ids"`
	}
	post.IDs = tasks

	return r.apiPost(u, &post, nil)
}

// TaskEndpoints gets the endpoints i.e. HOST_IP:DYNAMIC_PORT for a specific application service
// I.e. a container running apache, might have ports 80/443 (translated to X dynamic ports), but i want
// port 80 only and i only want those whom have passed the health check
//
// Note: I've NO IDEA how to associate the health_check_result to the actual port, I don't think it's
// possible at the moment, however, given marathon will fail and restart an application even if one of x ports of a task is
// down, the per port check is redundant??? .. personally, I like it anyhow, but hey
//

//		name:		the identifier for the application
//		port:		the container port you are interested in
//		health: 	whether to check the health or not
func (r *marathonClient) TaskEndpoints(name string, port int, healthCheck bool) ([]string, error) {
	// step: get the application details
	application, err := r.Application(name)
	if err != nil {
		return nil, err
	}

	// step: we need to get the port index of the service we are interested in
	portIndex, err := application.Container.Docker.ServicePortIndex(port)
	if err != nil {
		return nil, err
	}

	// step: do we have any tasks?
	if application.Tasks == nil || len(application.Tasks) == 0 {
		return nil, nil
	}

	// step: if we are checking health the 'service' has a health check?
	healthCheck = healthCheck && application.HasHealthChecks()

	// step: iterate the tasks and extract the dynamic ports
	var list []string
	for _, task := range application.Tasks {
		if !healthCheck || task.allHealthChecksAlive() {
			endpoint := fmt.Sprintf("%s:%d", task.Host, task.Ports[portIndex])
			list = append(list, endpoint)
		}
	}

	return list, nil
}

func (r *Task) allHealthChecksAlive() bool {
	// check: does the task have a health check result, if NOT, it's because the
	// health of the task hasn't yet been performed, hence we assume it as DOWN
	if !r.HasHealthCheckResults() {
		return false
	}
	// step: check the health results then
	for _, check := range r.HealthCheckResults {
		if check.Alive == false {
			return false
		}
	}

	return true
}
<|endoftext|>


<commit_before>package task

import (
	"context"
	"fmt"
	"io"
	"os"
	"strings"
	"sync/atomic"

	"github.com/go-task/task/v2/internal/compiler"
	compilerv1 "github.com/go-task/task/v2/internal/compiler/v1"
	compilerv2 "github.com/go-task/task/v2/internal/compiler/v2"
	"github.com/go-task/task/v2/internal/execext"
	"github.com/go-task/task/v2/internal/logger"
	"github.com/go-task/task/v2/internal/output"
	"github.com/go-task/task/v2/internal/taskfile"
	"github.com/go-task/task/v2/internal/taskfile/read"
	"github.com/go-task/task/v2/internal/taskfile/version"

	"github.com/Masterminds/semver"
	"golang.org/x/sync/errgroup"
)

const (
	// MaximumTaskCall is the max number of times a task can be called.
	// This exists to prevent infinite loops on cyclic dependencies
	MaximumTaskCall = 100
)

// Executor executes a Taskfile
type Executor struct {
	Taskfile *taskfile.Taskfile
	Dir      string
	Force    bool
	Watch    bool
	Verbose  bool
	Silent   bool
	Dry      bool
	Details  bool

	Stdin  io.Reader
	Stdout io.Writer
	Stderr io.Writer

	Logger      *logger.Logger
	Compiler    compiler.Compiler
	Output      output.Output
	OutputStyle string

	taskvars taskfile.Vars

	taskCallCount map[string]*int32
}

// Run runs Task
func (e *Executor) Run(ctx context.Context, calls ...taskfile.Call) error {
	// check if given tasks exist
	for _, c := range calls {
		if _, ok := e.Taskfile.Tasks[c.Task]; !ok {
			// FIXME: move to the main package
			e.PrintTasksHelp()
			return &taskNotFoundError{taskName: c.Task}
		}
	}

	if e.Details {
		e.printTaskDetails(calls[0].Task)
		return nil
	}

	if e.Watch {
		return e.watchTasks(calls...)
	}

	for _, c := range calls {
		if err := e.RunTask(ctx, c); err != nil {
			return err
		}
	}
	return nil
}

func (e *Executor) printTaskDetails(task string) {
	t := e.Taskfile.Tasks[task]
	s := t.Details
	if s == "" {
		e.Logger.Errf("task: There is no detailed description for task: %s", task)
		return
	}

	e.Logger.Outf("task: " + task)
	e.Logger.Outf("")

	Logger := e.Logger
	displayTaskDetailedDescription(s, Logger)

	if len(t.Deps) > 0 {
		e.Logger.Outf("")
		e.Logger.Outf("dependencies:")

		for _, d := range t.Deps {
			e.Logger.Outf(" - %s", d.Task)
		}
	}

	e.Logger.Outf("")
	e.Logger.Outf("commands:")

	for _, c := range t.Cmds {
		e.Logger.Outf(" - %s", c.Cmd)
	}
}

func displayTaskDetailedDescription(description string, Logger *logger.Logger) {
	lines := strings.Split(description, "\n")
	for i, line := range lines {
		notLastLine := i+1 < len(lines)
		if notLastLine || line != "" {
			Logger.Outf(line)
		}
	}
}

// Setup setups Executor's internal state
func (e *Executor) Setup() error {
	var err error
	e.Taskfile, err = read.Taskfile(e.Dir)
	if err != nil {
		return err
	}
	e.taskvars, err = read.Taskvars(e.Dir)
	if err != nil {
		return err
	}

	v, err := semver.NewConstraint(e.Taskfile.Version)
	if err != nil {
		return fmt.Errorf(`task: could not parse taskfile version "%s": %v`, e.Taskfile.Version, err)
	}

	if e.Stdin == nil {
		e.Stdin = os.Stdin
	}
	if e.Stdout == nil {
		e.Stdout = os.Stdout
	}
	if e.Stderr == nil {
		e.Stderr = os.Stderr
	}
	e.Logger = &logger.Logger{
		Stdout:  e.Stdout,
		Stderr:  e.Stderr,
		Verbose: e.Verbose,
	}
	switch {
	case version.IsV1(v):
		e.Compiler = &compilerv1.CompilerV1{
			Dir:    e.Dir,
			Vars:   e.taskvars,
			Logger: e.Logger,
		}
	case version.IsV2(v), version.IsV21(v), version.IsV22(v):
		e.Compiler = &compilerv2.CompilerV2{
			Dir:          e.Dir,
			Taskvars:     e.taskvars,
			TaskfileVars: e.Taskfile.Vars,
			Expansions:   e.Taskfile.Expansions,
			Logger:       e.Logger,
		}
	case version.IsV23(v):
		return fmt.Errorf(`task: Taskfile versions greater than v2.3 not implemented in the version of Task`)
	}

	if !version.IsV21(v) && e.Taskfile.Output != "" {
		return fmt.Errorf(`task: Taskfile option "output" is only available starting on Taskfile version v2.1`)
	}
	if !version.IsV22(v) && len(e.Taskfile.Includes) > 0 {
		return fmt.Errorf(`task: Including Taskfiles is only available starting on Taskfile version v2.2`)
	}
	if e.OutputStyle != "" {
		e.Taskfile.Output = e.OutputStyle
	}
	switch e.Taskfile.Output {
	case "", "interleaved":
		e.Output = output.Interleaved{}
	case "group":
		e.Output = output.Group{}
	case "prefixed":
		e.Output = output.Prefixed{}
	default:
		return fmt.Errorf(`task: output option "%s" not recognized`, e.Taskfile.Output)
	}

	if !version.IsV21(v) {
		err := fmt.Errorf(`task: Taskfile option "ignore_error" is only available starting on Taskfile version v2.1`)

		for _, task := range e.Taskfile.Tasks {
			if task.IgnoreError {
				return err
			}
			for _, cmd := range task.Cmds {
				if cmd.IgnoreError {
					return err
				}
			}
		}
	}

	e.taskCallCount = make(map[string]*int32, len(e.Taskfile.Tasks))
	for k := range e.Taskfile.Tasks {
		e.taskCallCount[k] = new(int32)
	}
	return nil
}

// RunTask runs a task by its name
func (e *Executor) RunTask(ctx context.Context, call taskfile.Call) error {
	t, err := e.CompiledTask(call)
	if err != nil {
		return err
	}
	if !e.Watch && atomic.AddInt32(e.taskCallCount[call.Task], 1) >= MaximumTaskCall {
		return &MaximumTaskCallExceededError{task: call.Task}
	}

	if err := e.runDeps(ctx, t); err != nil {
		return err
	}

	if !e.Force {
		upToDate, err := e.isTaskUpToDate(ctx, t)
		if err != nil {
			return err
		}
		if upToDate {
			if !e.Silent {
				e.Logger.Errf(`task: Task "%s" is up to date`, t.Task)
			}
			return nil
		}
	}

	for i := range t.Cmds {
		if err := e.runCommand(ctx, t, call, i); err != nil {
			if err2 := e.statusOnError(t); err2 != nil {
				e.Logger.VerboseErrf("task: error cleaning status on error: %v", err2)
			}

			if execext.IsExitError(err) && t.IgnoreError {
				e.Logger.VerboseErrf("task: task error ignored: %v", err)
				continue
			}

			return &taskRunError{t.Task, err}
		}
	}
	return nil
}

func (e *Executor) runDeps(ctx context.Context, t *taskfile.Task) error {
	g, ctx := errgroup.WithContext(ctx)

	for _, d := range t.Deps {
		d := d

		g.Go(func() error {
			return e.RunTask(ctx, taskfile.Call{Task: d.Task, Vars: d.Vars})
		})
	}

	return g.Wait()
}

func (e *Executor) runCommand(ctx context.Context, t *taskfile.Task, call taskfile.Call, i int) error {
	cmd := t.Cmds[i]

	switch {
	case cmd.Task != "":
		return e.RunTask(ctx, taskfile.Call{Task: cmd.Task, Vars: cmd.Vars})
	case cmd.Cmd != "":
		if e.Verbose || (!cmd.Silent && !t.Silent && !e.Silent) {
			e.Logger.Errf(cmd.Cmd)
		}

		if e.Dry {
			return nil
		}

		stdOut := e.Output.WrapWriter(e.Stdout, t.Prefix)
		stdErr := e.Output.WrapWriter(e.Stderr, t.Prefix)
		defer stdOut.Close()
		defer stdErr.Close()

		err := execext.RunCommand(ctx, &execext.RunCommandOptions{
			Command: cmd.Cmd,
			Dir:     t.Dir,
			Env:     getEnviron(t),
			Stdin:   e.Stdin,
			Stdout:  stdOut,
			Stderr:  stdErr,
		})
		if execext.IsExitError(err) && cmd.IgnoreError {
			e.Logger.VerboseErrf("task: command error ignored: %v", err)
			return nil
		}
		return err
	default:
		return nil
	}
}

func getEnviron(t *taskfile.Task) []string {
	if t.Env == nil {
		return nil
	}

	environ := os.Environ()
	for k, v := range t.Env.ToStringMap() {
		environ = append(environ, fmt.Sprintf("%s=%s", k, v))
	}
	return environ
}
<commit_msg>refactoring<commit_after>package task

import (
	"context"
	"fmt"
	"io"
	"os"
	"strings"
	"sync/atomic"

	"github.com/go-task/task/v2/internal/compiler"
	compilerv1 "github.com/go-task/task/v2/internal/compiler/v1"
	compilerv2 "github.com/go-task/task/v2/internal/compiler/v2"
	"github.com/go-task/task/v2/internal/execext"
	"github.com/go-task/task/v2/internal/logger"
	"github.com/go-task/task/v2/internal/output"
	"github.com/go-task/task/v2/internal/taskfile"
	"github.com/go-task/task/v2/internal/taskfile/read"
	"github.com/go-task/task/v2/internal/taskfile/version"

	"github.com/Masterminds/semver"
	"golang.org/x/sync/errgroup"
)

const (
	// MaximumTaskCall is the max number of times a task can be called.
	// This exists to prevent infinite loops on cyclic dependencies
	MaximumTaskCall = 100
)

// Executor executes a Taskfile
type Executor struct {
	Taskfile *taskfile.Taskfile
	Dir      string
	Force    bool
	Watch    bool
	Verbose  bool
	Silent   bool
	Dry      bool
	Details  bool

	Stdin  io.Reader
	Stdout io.Writer
	Stderr io.Writer

	Logger      *logger.Logger
	Compiler    compiler.Compiler
	Output      output.Output
	OutputStyle string

	taskvars taskfile.Vars

	taskCallCount map[string]*int32
}

// Run runs Task
func (e *Executor) Run(ctx context.Context, calls ...taskfile.Call) error {
	// check if given tasks exist
	for _, c := range calls {
		if _, ok := e.Taskfile.Tasks[c.Task]; !ok {
			// FIXME: move to the main package
			e.PrintTasksHelp()
			return &taskNotFoundError{taskName: c.Task}
		}
	}

	if e.Details {
		e.printTaskDetails(calls[0].Task)
		return nil
	}

	if e.Watch {
		return e.watchTasks(calls...)
	}

	for _, c := range calls {
		if err := e.RunTask(ctx, c); err != nil {
			return err
		}
	}
	return nil
}

func (e *Executor) printTaskDetails(task string) {
	t := e.Taskfile.Tasks[task]
	s := t.Details
	if s == "" {
		e.Logger.Errf("task: There is no detailed description for task: %s", task)
		return
	}

	e.Logger.Outf("task: " + task)
	e.Logger.Outf("")

	printTaskDetailedDescription(s, e.Logger)
	printDependencies(t.Deps, e.Logger)

	e.Logger.Outf("")
	e.Logger.Outf("commands:")

	for _, c := range t.Cmds {
		e.Logger.Outf(" - %s", c.Cmd)
	}
}

func printDependencies(deps []*taskfile.Dep, logger *logger.Logger) {
	hasDependencies := len(deps) > 0
	if hasDependencies {
		logger.Outf("")
		logger.Outf("dependencies:")

		for _, d := range deps {
			logger.Outf(" - %s", d.Task)
		}
	}
}

func printTaskDetailedDescription(description string, Logger *logger.Logger) {
	lines := strings.Split(description, "\n")
	for i, line := range lines {
		notLastLine := i+1 < len(lines)
		if notLastLine || line != "" {
			Logger.Outf(line)
		}
	}

}

// Setup setups Executor's internal state
func (e *Executor) Setup() error {
	var err error
	e.Taskfile, err = read.Taskfile(e.Dir)
	if err != nil {
		return err
	}
	e.taskvars, err = read.Taskvars(e.Dir)
	if err != nil {
		return err
	}

	v, err := semver.NewConstraint(e.Taskfile.Version)
	if err != nil {
		return fmt.Errorf(`task: could not parse taskfile version "%s": %v`, e.Taskfile.Version, err)
	}

	if e.Stdin == nil {
		e.Stdin = os.Stdin
	}
	if e.Stdout == nil {
		e.Stdout = os.Stdout
	}
	if e.Stderr == nil {
		e.Stderr = os.Stderr
	}
	e.Logger = &logger.Logger{
		Stdout:  e.Stdout,
		Stderr:  e.Stderr,
		Verbose: e.Verbose,
	}
	switch {
	case version.IsV1(v):
		e.Compiler = &compilerv1.CompilerV1{
			Dir:    e.Dir,
			Vars:   e.taskvars,
			Logger: e.Logger,
		}
	case version.IsV2(v), version.IsV21(v), version.IsV22(v):
		e.Compiler = &compilerv2.CompilerV2{
			Dir:          e.Dir,
			Taskvars:     e.taskvars,
			TaskfileVars: e.Taskfile.Vars,
			Expansions:   e.Taskfile.Expansions,
			Logger:       e.Logger,
		}
	case version.IsV23(v):
		return fmt.Errorf(`task: Taskfile versions greater than v2.3 not implemented in the version of Task`)
	}

	if !version.IsV21(v) && e.Taskfile.Output != "" {
		return fmt.Errorf(`task: Taskfile option "output" is only available starting on Taskfile version v2.1`)
	}
	if !version.IsV22(v) && len(e.Taskfile.Includes) > 0 {
		return fmt.Errorf(`task: Including Taskfiles is only available starting on Taskfile version v2.2`)
	}
	if e.OutputStyle != "" {
		e.Taskfile.Output = e.OutputStyle
	}
	switch e.Taskfile.Output {
	case "", "interleaved":
		e.Output = output.Interleaved{}
	case "group":
		e.Output = output.Group{}
	case "prefixed":
		e.Output = output.Prefixed{}
	default:
		return fmt.Errorf(`task: output option "%s" not recognized`, e.Taskfile.Output)
	}

	if !version.IsV21(v) {
		err := fmt.Errorf(`task: Taskfile option "ignore_error" is only available starting on Taskfile version v2.1`)

		for _, task := range e.Taskfile.Tasks {
			if task.IgnoreError {
				return err
			}
			for _, cmd := range task.Cmds {
				if cmd.IgnoreError {
					return err
				}
			}
		}
	}

	e.taskCallCount = make(map[string]*int32, len(e.Taskfile.Tasks))
	for k := range e.Taskfile.Tasks {
		e.taskCallCount[k] = new(int32)
	}
	return nil
}

// RunTask runs a task by its name
func (e *Executor) RunTask(ctx context.Context, call taskfile.Call) error {
	t, err := e.CompiledTask(call)
	if err != nil {
		return err
	}
	if !e.Watch && atomic.AddInt32(e.taskCallCount[call.Task], 1) >= MaximumTaskCall {
		return &MaximumTaskCallExceededError{task: call.Task}
	}

	if err := e.runDeps(ctx, t); err != nil {
		return err
	}

	if !e.Force {
		upToDate, err := e.isTaskUpToDate(ctx, t)
		if err != nil {
			return err
		}
		if upToDate {
			if !e.Silent {
				e.Logger.Errf(`task: Task "%s" is up to date`, t.Task)
			}
			return nil
		}
	}

	for i := range t.Cmds {
		if err := e.runCommand(ctx, t, call, i); err != nil {
			if err2 := e.statusOnError(t); err2 != nil {
				e.Logger.VerboseErrf("task: error cleaning status on error: %v", err2)
			}

			if execext.IsExitError(err) && t.IgnoreError {
				e.Logger.VerboseErrf("task: task error ignored: %v", err)
				continue
			}

			return &taskRunError{t.Task, err}
		}
	}
	return nil
}

func (e *Executor) runDeps(ctx context.Context, t *taskfile.Task) error {
	g, ctx := errgroup.WithContext(ctx)

	for _, d := range t.Deps {
		d := d

		g.Go(func() error {
			return e.RunTask(ctx, taskfile.Call{Task: d.Task, Vars: d.Vars})
		})
	}

	return g.Wait()
}

func (e *Executor) runCommand(ctx context.Context, t *taskfile.Task, call taskfile.Call, i int) error {
	cmd := t.Cmds[i]

	switch {
	case cmd.Task != "":
		return e.RunTask(ctx, taskfile.Call{Task: cmd.Task, Vars: cmd.Vars})
	case cmd.Cmd != "":
		if e.Verbose || (!cmd.Silent && !t.Silent && !e.Silent) {
			e.Logger.Errf(cmd.Cmd)
		}

		if e.Dry {
			return nil
		}

		stdOut := e.Output.WrapWriter(e.Stdout, t.Prefix)
		stdErr := e.Output.WrapWriter(e.Stderr, t.Prefix)
		defer stdOut.Close()
		defer stdErr.Close()

		err := execext.RunCommand(ctx, &execext.RunCommandOptions{
			Command: cmd.Cmd,
			Dir:     t.Dir,
			Env:     getEnviron(t),
			Stdin:   e.Stdin,
			Stdout:  stdOut,
			Stderr:  stdErr,
		})
		if execext.IsExitError(err) && cmd.IgnoreError {
			e.Logger.VerboseErrf("task: command error ignored: %v", err)
			return nil
		}
		return err
	default:
		return nil
	}
}

func getEnviron(t *taskfile.Task) []string {
	if t.Env == nil {
		return nil
	}

	environ := os.Environ()
	for k, v := range t.Env.ToStringMap() {
		environ = append(environ, fmt.Sprintf("%s=%s", k, v))
	}
	return environ
}
<|endoftext|>