github.com/SupersunnySea/draft@v0.16.0/pkg/draft/manifest/manifest.go (about)

     1  package manifest
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  
     7  	"github.com/technosophos/moniker"
     8  )
     9  
    10  const (
    11  	// DefaultEnvironmentName is the name invoked from draft.toml on `draft up` when
    12  	// --environment is not supplied.
    13  	DefaultEnvironmentName = "development"
    14  	// DefaultNamespace specifies the namespace apps should be deployed to by default.
    15  	DefaultNamespace = "default"
    16  	// DefaultWatchDelaySeconds is the time delay between files being changed and when a
    17  	// new draft up` invocation is called when --watch is supplied.
    18  	DefaultWatchDelaySeconds = 2
    19  	// DefaultDockerfile is the Dockerfile being used by default
    20  	DefaultDockerfile = "Dockerfile"
    21  )
    22  
    23  // Manifest represents a draft.toml
    24  type Manifest struct {
    25  	Environments map[string]*Environment `toml:"environments"`
    26  }
    27  
    28  // Environment represents the environment for a given app at build time
    29  type Environment struct {
    30  	Name              string            `toml:"name,omitempty"`
    31  	ContainerBuilder  string            `toml:"container-builder,omitempty"`
    32  	Registry          string            `toml:"registry,omitempty"`
    33  	ResourceGroupName string            `toml:"resource-group-name,omitempty"`
    34  	BuildTarPath      string            `toml:"build-tar,omitempty"`
    35  	ChartTarPath      string            `toml:"chart-tar,omitempty"`
    36  	Namespace         string            `toml:"namespace,omitempty"`
    37  	Values            []string          `toml:"set,omitempty"`
    38  	Wait              bool              `toml:"wait"`
    39  	Watch             bool              `toml:"watch"`
    40  	WatchDelay        int               `toml:"watch-delay,omitempty"`
    41  	OverridePorts     []string          `toml:"override-ports,omitempty"`
    42  	AutoConnect       bool              `toml:"auto-connect"`
    43  	CustomTags        []string          `toml:"custom-tags,omitempty"`
    44  	Dockerfile        string            `toml:"dockerfile"`
    45  	Chart             string            `toml:"chart"`
    46  	ImageBuildArgs    map[string]string `toml:"image-build-args,omitempty"`
    47  }
    48  
    49  // New creates a new manifest with the Environments intialized.
    50  func New() *Manifest {
    51  	m := Manifest{
    52  		Environments: make(map[string]*Environment),
    53  	}
    54  	m.Environments[DefaultEnvironmentName] = &Environment{
    55  		Name:        generateName(),
    56  		Namespace:   DefaultNamespace,
    57  		Wait:        true,
    58  		Watch:       false,
    59  		WatchDelay:  DefaultWatchDelaySeconds,
    60  		AutoConnect: false,
    61  		Dockerfile:  DefaultDockerfile,
    62  	}
    63  	return &m
    64  }
    65  
    66  // generateName generates a name based on the current working directory or a random name.
    67  func generateName() string {
    68  	var name string
    69  	cwd, err := os.Getwd()
    70  	if err == nil {
    71  		name = filepath.Base(cwd)
    72  	} else {
    73  		namer := moniker.New()
    74  		name = namer.NameSep("-")
    75  	}
    76  	return name
    77  }