core/config/config.go
core/config/config.goBrowse 1970 files
984 tokens
4,187 bytes
Token encoding: o200k_base
Snapshot 21a254f
← Back to SKILL.md
1package config2 3import (4 "encoding/json"5 6 "github.com/invopop/jsonschema"7 "github.com/railwayapp/railpack/core/plan"8 "github.com/railwayapp/railpack/internal/utils"9)10 11const (12 SchemaUrl = "https://schema.railpack.com"13)14 15type DeployConfig struct {16 AptPackages []string `json:"aptPackages,omitempty" jsonschema:"description=List of apt packages to include at runtime"`17 Base *plan.Layer `json:"base,omitempty" jsonschema:"description=The base image to use for the deploy step"`18 Inputs []plan.Layer `json:"inputs,omitempty" jsonschema:"description=The inputs for the deploy step"`19 StartCmd string `json:"startCommand,omitempty" jsonschema:"description=The command to run in the container"`20 Variables map[string]string `json:"variables,omitempty" jsonschema:"description=The variables available to this step. The key is the name of the variable that is referenced in a variable command"`21 Paths []string `json:"paths,omitempty" jsonschema:"description=The paths to prepend to the $PATH environment variable"`22}23 24type StepConfig struct {25 plan.Step26 DeployOutputs []plan.Filter `json:"deployOutputs,omitempty" jsonschema:"description=Parts of this step that should be included in the final image. If empty, the /app directory will be used."`27}28 29type Config struct {30 Provider *string `json:"provider,omitempty" jsonschema:"enum=php,enum=golang,enum=java,enum=rust,enum=ruby,enum=elixir,enum=python,enum=deno,enum=dotnet,enum=node,enum=gleam,enum=cpp,enum=staticfile,enum=shell,description=The provider to use"`31 BuildAptPackages []string `json:"buildAptPackages,omitempty" jsonschema:"description=List of apt packages to install during the build step"`32 Steps map[string]*StepConfig `json:"steps,omitempty" jsonschema:"description=Map of step names to step definitions"`33 Deploy *DeployConfig `json:"deploy,omitempty" jsonschema:"description=Deploy configuration"`34 Packages map[string]string `json:"packages,omitempty" jsonschema:"description=Map of package name to package version"`35 Caches map[string]*plan.Cache `json:"caches,omitempty" jsonschema:"description=Map of cache name to cache definitions. The cache key can be referenced in an exec command"`36 Secrets []string `json:"secrets,omitempty" jsonschema:"description=Secrets that should be made available to commands that have useSecrets set to true"`37 Exclude []string `json:"exclude,omitempty" jsonschema:"description=File patterns to exclude from the build context (alternative to .dockerignore). Supports negation patterns starting with ! to include files that would otherwise be excluded"`38}39 40func EmptyConfig() *Config {41 return &Config{42 Steps: make(map[string]*StepConfig),43 Packages: make(map[string]string),44 Caches: make(map[string]*plan.Cache),45 Deploy: &DeployConfig{},46 }47}48 49func (c *Config) GetOrCreateStep(name string) *StepConfig {50 if existingStep, exists := c.Steps[name]; exists {51 return existingStep52 }53 54 step := &StepConfig{55 Step: *plan.NewStep(name),56 }57 c.Steps[name] = step58 59 return step60}61 62// Merge combines multiple configs by merging their values with later configs taking precedence63func Merge(configs ...*Config) *Config {64 if len(configs) == 0 {65 return EmptyConfig()66 }67 68 result := EmptyConfig()69 for _, config := range configs {70 if config == nil {71 continue72 }73 74 utils.MergeStructs(result, config)75 }76 77 return result78}79 80func (s *StepConfig) UnmarshalJSON(data []byte) error {81 var temp struct {82 DeployOutputs []plan.Filter `json:"deployOutputs,omitempty"`83 }84 if err := json.Unmarshal(data, &temp); err != nil {85 return err86 }87 s.DeployOutputs = temp.DeployOutputs88 89 return s.Step.UnmarshalJSON(data)90}91 92func (Config) JSONSchemaExtend(schema *jsonschema.Schema) {93 schema.Properties.Set("$schema", &jsonschema.Schema{94 Type: "string",95 Description: "The schema for this config",96 })97}98 99func GetJsonSchema() *jsonschema.Schema {100 r := jsonschema.Reflector{101 DoNotReference: true,102 Anonymous: true,103 }104 105 schema := r.Reflect(&Config{})106 return schema107}108