Skip to content

Commit

Permalink
start
Browse files Browse the repository at this point in the history
  • Loading branch information
flotter committed Oct 5, 2024
1 parent 999e48b commit 2af242a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
7 changes: 7 additions & 0 deletions client/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ type AddLayerOptions struct {
// has the given label. False (the default) means append a new layer.
Combine bool

// Inner true means a new layer append may go into an existing
// subdirectory, even through it may not result in appending it
// to the end of the layers slice (it becomes an insert).
Inner bool

// Label is the label for the new layer if appending, and the label of the
// layer to combine with if Combine is true.
Label string
Expand All @@ -38,12 +43,14 @@ func (client *Client) AddLayer(opts *AddLayerOptions) error {
var payload = struct {
Action string `json:"action"`
Combine bool `json:"combine"`
Inner bool `json:"inner"`
Label string `json:"label"`
Format string `json:"format"`
Layer string `json:"layer"`
}{
Action: "add",
Combine: opts.Combine,
Inner: opts.Inner,
Label: opts.Label,
Format: "yaml",
Layer: string(opts.LayerData),
Expand Down
3 changes: 3 additions & 0 deletions internals/cli/cmd_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type cmdAdd struct {
client *client.Client

Combine bool `long:"combine"`
Inner bool `long:"inner"`
Positional struct {
Label string `positional-arg-name:"<label>" required:"1"`
LayerPath string `positional-arg-name:"<layer-path>" required:"1"`
Expand All @@ -48,6 +49,7 @@ func init() {
Description: cmdAddDescription,
ArgsHelp: map[string]string{
"--combine": "Combine the new layer with an existing layer that has the given label (default is to append)",
"--inner": "Allow appending a new layer inside an existing subdirectory",
},
New: func(opts *CmdOptions) flags.Commander {
return &cmdAdd{client: opts.Client}
Expand All @@ -65,6 +67,7 @@ func (cmd *cmdAdd) Execute(args []string) error {
}
opts := client.AddLayerOptions{
Combine: cmd.Combine,
Inner: cmd.Inner,
Label: cmd.Positional.Label,
LayerData: data,
}
Expand Down
1 change: 1 addition & 0 deletions internals/daemon/api_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func v1PostLayers(c *Command, r *http.Request, _ *UserState) Response {
var payload struct {
Action string `json:"action"`
Combine bool `json:"combine"`
Inner bool `json:"inner"`
Label string `json:"label"`
Format string `json:"format"`
Layer string `json:"layer"`
Expand Down
28 changes: 25 additions & 3 deletions internals/plan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,14 @@ func (p *Plan) checkCycles() error {
return err
}

// labelExp represents a match of a valid layer label, which may include a
// directory prefix (which excludes the '.d' ending).
//
// Examples:
// foo
// bar/baz
var labelExp = regexp.MustCompile("^(([a-z](?:-?[a-z0-9]){2,})/)?([a-z](?:-?[a-z0-9]){2,})$")

func ParseLayer(order int, label string, data []byte) (*Layer, error) {
layer := &Layer{
Services: make(map[string]*Service),
Expand Down Expand Up @@ -1349,7 +1357,14 @@ func validServiceAction(action ServiceAction, additionalValid ...ServiceAction)
}
}

var fnameExp = regexp.MustCompile("^([0-9]{3})-([a-z](?:-?[a-z0-9]){2,}).yaml$")
// fnameExp represents a match of a valid layer path, which typically
// consists of only the file name. Optionally, it include a single level
// subdirectory prefix which ends with '.d'.
//
// Examples:
// 001-abc.yaml => ["001-abc.yaml", "", "", "", "001", "abc"]
// 002-foo.d/001-bar.yaml => ["002-foo.d/001-bar.yaml", "002-foo.d/", "002", "foo", "001", "bar"]
var fnameExp = regexp.MustCompile("^(([0-9]{3})-([a-z](?:-?[a-z0-9]){2,})\.d/)?([0-9]{3})-([a-z](?:-?[a-z0-9]){2,}).yaml$")

Check failure on line 1367 in internals/plan/plan.go

View workflow job for this annotation

GitHub Actions / lint

unknown escape sequence (typecheck)

Check failure on line 1367 in internals/plan/plan.go

View workflow job for this annotation

GitHub Actions / lint

unknown escape sequence (typecheck)

func ReadLayersDir(dirname string) ([]*Layer, error) {
finfos, err := os.ReadDir(dirname)
Expand Down Expand Up @@ -1381,8 +1396,15 @@ func ReadLayersDir(dirname string) ([]*Layer, error) {
// Errors from package os generally include the path.
return nil, fmt.Errorf("cannot read layer file: %v", err)
}
label := match[2]
order, err := strconv.Atoi(match[1])

label = match[5]
orderStr = match[4]
if match[1] != "" {
// We have a subdirectory
label = match[3] + "/" + label
orderStr = match[2] + orderStr
}
order, err := strconv.Atoi(orderStr)
if err != nil {
panic(fmt.Sprintf("internal error: filename regexp is wrong: %v", err))
}
Expand Down

0 comments on commit 2af242a

Please sign in to comment.