-
Notifications
You must be signed in to change notification settings - Fork 37
feat(policy): add policy develop init #2229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
ee93be0
0823855
3b55df9
7af13be
b1548fc
eb48119
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// | ||
// Copyright 2025 The Chainloop Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newPolicyCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "policy", | ||
Short: "Craft chainloop policies", | ||
} | ||
|
||
cmd.AddCommand(newPolicyDevelopCmd()) | ||
return cmd | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// | ||
// Copyright 2025 The Chainloop Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newPolicyDevelopCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "develop", | ||
Aliases: []string{"dev"}, | ||
Short: "Tools for policy development", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick, it might make sense to add this link https://docs.chainloop.dev/guides/custom-policies |
||
} | ||
|
||
cmd.AddCommand(newPolicyDevelopInitCmd()) | ||
return cmd | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// | ||
// Copyright 2025 The Chainloop Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/chainloop-dev/chainloop/app/cli/internal/action" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newPolicyDevelopInitCmd() *cobra.Command { | ||
var ( | ||
force bool | ||
embedded bool | ||
name string | ||
description string | ||
) | ||
|
||
cmd := &cobra.Command{ | ||
Use: "init [directory]", | ||
Short: "Initialize a new policy", | ||
Long: `Initialize a new policy by creating template policy files in the specified directory. | ||
By default, it creates chainloop-policy.yaml and chainloop-policy.rego files.`, | ||
Example: ` | ||
# Initialize in current directory with separate files | ||
chainloop policy develop init | ||
|
||
# Initialize in specific directory with embedded format | ||
chainloop policy develop init ./policies --embedded`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. one thing that we do not do in any command in chainloop is to use arguments, we always use flags. The reason being that they are more flexible and easier to change in the future. |
||
RunE: func(cmd *cobra.Command, args []string) error { | ||
// Default to current directory if not specified | ||
dir := "." | ||
if len(args) > 0 { | ||
dir = args[0] | ||
} | ||
|
||
opts := &action.PolicyInitOpts{ | ||
Force: force, | ||
Embedded: embedded, | ||
Name: name, | ||
Description: description, | ||
} | ||
|
||
policyInit, err := action.NewPolicyInit(opts, actionOpts) | ||
if err != nil { | ||
return fmt.Errorf("failed to initialize policy: %w", err) | ||
} | ||
|
||
if err := policyInit.Run(cmd.Context(), dir); err != nil { | ||
return newGracefulError(err) | ||
} | ||
|
||
logger.Info().Msg("Initialized policy files") | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
cmd.Flags().BoolVarP(&force, "force", "f", false, "overwrite existing files") | ||
cmd.Flags().BoolVar(&embedded, "embedded", false, "initialize an embedded policy (single YAML file)") | ||
cmd.Flags().StringVar(&name, "name", "", "name of the policy") | ||
cmd.Flags().StringVar(&description, "description", "", "description of the policy") | ||
|
||
return cmd | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// | ||
// Copyright 2025 The Chainloop Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package action | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
policy "github.com/chainloop-dev/chainloop/app/cli/internal/policy/init" | ||
) | ||
|
||
type PolicyInitOpts struct { | ||
Force bool | ||
Embedded bool | ||
Name string | ||
Description string | ||
} | ||
|
||
type PolicyInit struct { | ||
*ActionsOpts | ||
opts *PolicyInitOpts | ||
} | ||
|
||
func NewPolicyInit(opts *PolicyInitOpts, actionOpts *ActionsOpts) (*PolicyInit, error) { | ||
return &PolicyInit{ | ||
ActionsOpts: actionOpts, | ||
opts: opts, | ||
}, nil | ||
} | ||
|
||
func (action *PolicyInit) Run(_ context.Context, dir string) error { | ||
initOpts := &policy.InitOptions{ | ||
Dir: dir, | ||
Embedded: action.opts.Embedded, | ||
Force: action.opts.Force, | ||
Name: action.opts.Name, | ||
Description: action.opts.Description, | ||
} | ||
|
||
if err := policy.Initialize(initOpts); err != nil { | ||
return fmt.Errorf("initializing policy: %w", err) | ||
} | ||
|
||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this sounds a little bit better to me since it might represent a verb