Skip to content

Commit

Permalink
show: Commands to output app manifest and compose
Browse files Browse the repository at this point in the history
Add a new command `show` with two subcommands to print App manifest and
compose file/project.

Signed-off-by: Mike Sul <[email protected]>
  • Loading branch information
mike-sul committed Nov 25, 2024
1 parent 8885713 commit b73aa0f
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 0 deletions.
61 changes: 61 additions & 0 deletions cmd/composectl/cmd/compose.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package composectl

import (
"fmt"
"github.com/containerd/containerd/platforms"
"github.com/foundriesio/composeapp/pkg/compose"
v1 "github.com/foundriesio/composeapp/pkg/compose/v1"
"github.com/spf13/cobra"
"path"
)

var (
composeCmd = &cobra.Command{
Use: "compose",
Short: "compose <ref>",
Long: ``,
Args: cobra.ExactArgs(1),
}
)

type (
composeOptions struct {
SrcStorePath *string
Locally *bool
}
)

func init() {
opts := composeOptions{}

opts.SrcStorePath = composeCmd.Flags().StringP("source-store-path", "l", "",
"A path to the source store root directory")
opts.Locally = composeCmd.Flags().BoolP("local", "", false,
"Print compose config/file of app stored locally")
composeCmd.Run = func(cmd *cobra.Command, args []string) {
doOutputComposeFile(cmd, args, &opts)
}

showCmd.AddCommand(composeCmd)
}

func doOutputComposeFile(cmd *cobra.Command, args []string, opts *composeOptions) {
if *opts.Locally && len(*opts.SrcStorePath) == 0 {
opts.SrcStorePath = &config.StoreRoot
}
var blobProvider compose.BlobProvider
if len(*opts.SrcStorePath) > 0 {
blobProvider = compose.NewStoreBlobProvider(path.Join(*opts.SrcStorePath, "blobs", "sha256"))
} else {
authorizer := compose.NewRegistryAuthorizer(config.DockerCfg)
resolver := compose.NewResolver(authorizer, config.ConnectTime)
blobProvider = compose.NewRemoteBlobProvider(resolver)
}
app, _, err := v1.NewAppLoader().LoadAppTree(cmd.Context(), blobProvider, platforms.OnlyStrict(config.Platform), args[0])
DieNotNil(err)
composeProject, err := app.GetCompose(cmd.Context(), blobProvider)
DieNotNil(err)
b, err := composeProject.MarshalYAML()
DieNotNil(err)
fmt.Printf("%s", string(b))
}
56 changes: 56 additions & 0 deletions cmd/composectl/cmd/manifest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package composectl

import (
"fmt"
"github.com/foundriesio/composeapp/pkg/compose"
"github.com/foundriesio/composeapp/pkg/compose/v1"
"github.com/spf13/cobra"
"path"
)

var (
manifestCmd = &cobra.Command{
Use: "manifest",
Short: "manifest <ref>",
Long: ``,
Args: cobra.ExactArgs(1),
}
)

type (
manifestOptions struct {
SrcStorePath *string
Locally *bool
}
)

func init() {
opts := manifestOptions{}

opts.SrcStorePath = manifestCmd.Flags().StringP("source-store-path", "l", "",
"A path to the source store root directory")
opts.Locally = manifestCmd.Flags().BoolP("local", "", false,
"Print manifest of app stored locally")
manifestCmd.Run = func(cmd *cobra.Command, args []string) {
doOutputManifest(cmd, args, &opts)
}

showCmd.AddCommand(manifestCmd)
}

func doOutputManifest(cmd *cobra.Command, args []string, opts *manifestOptions) {
if *opts.Locally && len(*opts.SrcStorePath) == 0 {
opts.SrcStorePath = &config.StoreRoot
}
var blobProvider compose.BlobProvider
if len(*opts.SrcStorePath) > 0 {
blobProvider = compose.NewStoreBlobProvider(path.Join(*opts.SrcStorePath, "blobs", "sha256"))
} else {
authorizer := compose.NewRegistryAuthorizer(config.DockerCfg)
resolver := compose.NewResolver(authorizer, config.ConnectTime)
blobProvider = compose.NewRemoteBlobProvider(resolver)
}
b, err := compose.ReadBlobWithReadLimit(cmd.Context(), blobProvider, args[0], v1.AppManifestMaxSize)
DieNotNil(err)
fmt.Printf("%s\n", string(b))
}
14 changes: 14 additions & 0 deletions cmd/composectl/cmd/show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package composectl

import (
"github.com/spf13/cobra"
)

var showCmd = &cobra.Command{
Use: "show",
Short: "output app manifest or compose file",
}

func init() {
rootCmd.AddCommand(showCmd)
}
2 changes: 2 additions & 0 deletions pkg/compose/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package compose
import (
"context"
"fmt"
composetypes "github.com/compose-spec/compose-go/types"
"github.com/containerd/containerd/platforms"
"github.com/containerd/containerd/reference"
"github.com/opencontainers/go-digest"
Expand All @@ -26,6 +27,7 @@ type (
HasLayersMeta(arch string) bool
GetBlobRuntimeSize(desc *ocispec.Descriptor, arch string, blockSize int64) int64
GetComposeRoot() *TreeNode
GetCompose(ctx context.Context, provider BlobProvider) (*composetypes.Project, error)
}
AppLoader interface {
LoadAppTree(context.Context, BlobProvider, platforms.MatchComparer, string) (App, *AppTree, error)
Expand Down
5 changes: 5 additions & 0 deletions pkg/compose/v1/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ func (a *appCtx) GetComposeRoot() *compose.TreeNode {
return nil
}

func (a *appCtx) GetCompose(ctx context.Context, provider compose.BlobProvider) (project *composetypes.Project, err error) {
project, _, err = readAndLoadComposeProject(ctx, provider, a)
return
}

func ReadAppManifest(ctx context.Context, provider compose.BlobProvider, ref string) (*appCtx, *ocispec.Descriptor, error) {
appRef, err := parseAndCheckAppRef(ref)
if err != nil {
Expand Down

0 comments on commit b73aa0f

Please sign in to comment.