commands: add stubs for policy commands

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
Tonis Tiigi
2026-01-14 09:09:10 -08:00
parent 61843e0031
commit d232d4c392
5 changed files with 144 additions and 0 deletions
+76
View File
@@ -0,0 +1,76 @@
package policy
import (
"os"
"strings"
"github.com/moby/buildkit/frontend/dockerui"
"github.com/moby/buildkit/solver/pb"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
func evalCmd() *cobra.Command {
var filename string
var printOutput bool
cmd := &cobra.Command{
Use: "eval [source]",
Short: "Evaluate policy for a source",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return runEval(args, filename, printOutput)
},
}
cmd.Flags().StringVar(&filename, "filename", "", "Policy filename to evaluate")
cmd.Flags().BoolVar(&printOutput, "print", false, "Print policy output")
return cmd
}
func runEval(args []string, filename string, printOutput bool) error {
if len(args) > 0 {
if _, err := parseSource(args[0]); err != nil {
return err
}
}
_ = filename
_ = printOutput
return errors.New("not implemented")
}
func parseSource(input string) (*pb.SourceOp, error) {
if strings.HasPrefix(input, "docker-image://") {
return &pb.SourceOp{Identifier: input}, nil
}
if strings.HasPrefix(input, "git://") {
_, ok, err := dockerui.DetectGitContext(input, nil)
if !ok {
return nil, errors.Errorf("invalid git context %s", input)
}
if err != nil {
return nil, err
}
return &pb.SourceOp{Identifier: input}, nil
}
if strings.HasPrefix(input, "http://") || strings.HasPrefix(input, "https://") {
_, ok, err := dockerui.DetectGitContext(input, nil)
if ok {
return &pb.SourceOp{
Identifier: "git://" + input,
Attrs: map[string]string{
"git.fullurl": input,
},
}, nil
}
if err != nil {
return nil, err
}
return &pb.SourceOp{Identifier: input}, nil
}
// everything else is treated as a local path
if _, err := os.Stat(input); err != nil {
return nil, errors.Wrapf(err, "invalid local path %s", input)
}
return &pb.SourceOp{Identifier: "local://context"}, nil
}
+22
View File
@@ -0,0 +1,22 @@
package policy
import (
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
func jsonSchemaCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "json-schema",
Short: "Print policy JSON schema",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return runJSONSchema()
},
}
return cmd
}
func runJSONSchema() error {
return errors.New("not implemented")
}
+21
View File
@@ -0,0 +1,21 @@
package policy
import (
"github.com/spf13/cobra"
)
// RootCmd creates the policy command tree.
func RootCmd(rootcmd *cobra.Command) *cobra.Command {
cmd := &cobra.Command{
Use: "policy",
Short: "Commands for working with build policies",
}
cmd.AddCommand(
jsonSchemaCmd(),
evalCmd(),
testCmd(),
)
return cmd
}
+23
View File
@@ -0,0 +1,23 @@
package policy
import (
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
func testCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "test <path>",
Short: "Run policy tests",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return runTest(args[0])
},
}
return cmd
}
func runTest(path string) error {
_ = path
return errors.New("not implemented")
}