Skip to content

Commit 09799aa

Browse files
authored
add policy build --optimize --entrypoint support (#205)
1 parent 1328a3b commit 09799aa

File tree

13 files changed

+103
-78
lines changed

13 files changed

+103
-78
lines changed

.goreleaser-pre.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ before:
1515

1616
builds:
1717
# https://goreleaser.com/customization/build/
18-
- id: build
18+
- id: policy
1919
main: ./cmd/policy
2020
binary: policy
2121
goos:
@@ -43,7 +43,7 @@ archives:
4343
- formats:
4444
- zip
4545
ids:
46-
- build
46+
- policy
4747
files:
4848
- LICENSE
4949
- README.md

.goreleaser.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ before:
1616

1717
builds:
1818
# https://goreleaser.com/customization/build/
19-
- id: build
19+
- id: policy
2020
main: ./cmd/policy
2121
binary: policy
2222
goos:
@@ -44,7 +44,7 @@ archives:
4444
- formats:
4545
- zip
4646
ids:
47-
- build
47+
- policy
4848
files:
4949
- LICENSE
5050
- README.md

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ LABEL org.opencontainers.image.url="https://openpolicyregistry.io"
2626

2727
RUN apk add --no-cache bash
2828
WORKDIR /app
29-
COPY --from=build /src/dist/build_linux_amd64_v1/policy /app/
29+
COPY --from=build /src/dist/policy_linux_amd64_v1/policy /app/
3030

3131
COPY --from=build /src/scripts /app/
3232
RUN chmod +x /app/*.sh

cmd/policy/main.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ import (
1616
)
1717

1818
const (
19-
appName string = "policy"
19+
appName string = "policy"
20+
verbosityError int = 0
21+
verbosityInfo int = 1
22+
verbosityDebug int = 2
2023
)
2124

2225
var tmpConfig *config.Config
@@ -69,7 +72,10 @@ func resolveTmpConfig(context *kong.Context) {
6972
return
7073
}
7174

72-
configPath, _ := context.FlagValue(configFlag).(string)
75+
configPath, ok := context.FlagValue(configFlag).(string)
76+
if !ok {
77+
panic("config path cast failed")
78+
}
7379

7480
cfgLogger, err := config.NewLoggerConfig(config.Path(configPath), nil)
7581
if err != nil {
@@ -121,12 +127,6 @@ var PolicyCLI struct {
121127
Version VersionCmd `cmd:"" help:"Prints version information."`
122128
}
123129

124-
const (
125-
logLevelError int = 0
126-
logLevelInfo int = 1
127-
logLevelDebug int = 2
128-
)
129-
130130
func (g *Globals) setup() func() {
131131
configFile := g.Config
132132

@@ -136,11 +136,11 @@ func (g *Globals) setup() func() {
136136
config.Path(configFile),
137137
func(c *config.Config) {
138138
switch g.Verbosity {
139-
case logLevelError:
139+
case verbosityError:
140140
c.Logging.LogLevel = "error"
141-
case logLevelInfo:
141+
case verbosityInfo:
142142
c.Logging.LogLevel = "info"
143-
case logLevelDebug:
143+
case verbosityDebug:
144144
c.Logging.LogLevel = "debug"
145145
default:
146146
c.Logging.LogLevel = "trace"

pkg/app/build.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import (
1212
"github.com/aserto-dev/runtime"
1313
"github.com/containerd/errdefs"
1414
"github.com/distribution/reference"
15-
"github.com/rs/zerolog"
16-
1715
oras "github.com/opcr-io/oras-go/v2"
1816
"github.com/opcr-io/oras-go/v2/content"
1917
orasoci "github.com/opcr-io/oras-go/v2/content/oci"
@@ -22,6 +20,7 @@ import (
2220
"github.com/opencontainers/go-digest"
2321
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
2422
"github.com/pkg/errors"
23+
"github.com/rs/zerolog"
2524
)
2625

2726
const (
@@ -30,7 +29,10 @@ const (
3029
)
3130

3231
//nolint:funlen
33-
func (c *PolicyApp) Build(ref string, path []string, annotations map[string]string,
32+
func (c *PolicyApp) Build(
33+
ref string,
34+
path []string,
35+
annotations map[string]string,
3436
runConfigFile string,
3537
target string,
3638
optimizationLevel int,

pkg/app/init.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/aserto-dev/scc-lib/generators"
1010
"github.com/magefile/mage/sh"
11+
"github.com/opcr-io/policy/pkg/x"
1112
"github.com/opcr-io/policy/templates"
1213

1314
"github.com/pkg/errors"
@@ -96,11 +97,9 @@ func (c *PolicyApp) generateContent(generatorConf *generators.Config, outPath, s
9697
return nil
9798
}
9899

99-
const ownerReadWriteExecute os.FileMode = 0o700
100-
101100
func (c *PolicyApp) validatePath(path string) error {
102101
if exist, _ := generators.DirExist(path); !exist {
103-
if err := os.MkdirAll(path, ownerReadWriteExecute); err != nil {
102+
if err := os.MkdirAll(path, x.OwnerReadWriteExecute); err != nil {
104103
return errors.Errorf("root path not a directory '%s'", path)
105104
}
106105
}

pkg/app/rm.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@ func (c *PolicyApp) Rm(existingRef string, force bool) error {
4949
if !ok {
5050
return errors.ErrNotFound.WithMessage("policy [%s] not in the local store", existingRef)
5151
}
52+
5253
// attach ref name annotation for comparison.
5354
if len(ref.Annotations) == 0 || ref.Annotations[ocispec.AnnotationRefName] == "" {
5455
oldAnnotations := ref.Annotations
55-
5656
ref.Annotations = make(map[string]string)
57+
5758
if oldAnnotations != nil {
5859
ref.Annotations = oldAnnotations
5960
}
@@ -84,7 +85,7 @@ func (c *PolicyApp) Rm(existingRef string, force bool) error {
8485
}
8586

8687
func (c *PolicyApp) removeBasedOnManifest(ociClient *oci.Oci, ref *ocispec.Descriptor, refString string) error {
87-
anotherImagewithSameDigest, err := c.buildFromSameImage(ref)
88+
anotherImageWithSameDigest, err := c.buildFromSameImage(ref)
8889
if err != nil {
8990
return err
9091
}
@@ -94,7 +95,7 @@ func (c *PolicyApp) removeBasedOnManifest(ociClient *oci.Oci, ref *ocispec.Descr
9495
return err
9596
}
9697

97-
if anotherImagewithSameDigest {
98+
if anotherImageWithSameDigest {
9899
return nil
99100
}
100101

pkg/app/tag.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ func (c *PolicyApp) Tag(existingRef, newRef string) error {
1717
}
1818

1919
existingRefParsed := existingRef
20+
2021
if strings.Contains(existingRef, ":") || strings.Contains(existingRef, "/") {
2122
existingRefParsed, err = parser.CalculatePolicyRef(existingRef, c.Configuration.DefaultDomain)
2223
if err != nil {

pkg/app/transport.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ func (c *PolicyApp) TransportWithTrustedCAs() *http.Transport {
1818
rootCAs *x509.CertPool
1919
err error
2020
)
21+
2122
if runtime.GOOS != `windows` {
2223
rootCAs, err = x509.SystemCertPool()
2324
if err != nil {
2425
c.UI.Problem().WithErr(err).WithEnd(1).Msg("Failed to load system cert pool.")
2526
}
2627
} else {
27-
// remove runtime check when updating to go1.18 https://github.com/deviceinsight/kafkactl/issues/108.
28+
// CLEANUP: Remove runtime check when updating to go1.18 https://github.com/deviceinsight/kafkactl/issues/108
2829
if len(c.Configuration.CA) > 0 {
2930
c.UI.Exclamation().Msg("Cannot use custom CAs on Windows. Please configure your system store to trust your CAs.")
3031
}

pkg/cc/config/config.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,12 @@ import (
1010
"github.com/docker/cli/cli/config"
1111
"github.com/docker/cli/cli/config/credentials"
1212
"github.com/go-viper/mapstructure/v2"
13+
"github.com/opcr-io/policy/pkg/x"
1314
"github.com/pkg/errors"
1415
"github.com/rs/zerolog"
1516
"github.com/spf13/viper"
1617
)
1718

18-
const (
19-
OwnerReadWrite os.FileMode = 0o600
20-
)
21-
2219
// Overrider is a func that mutates configuration.
2320
type Overrider func(*Config)
2421

@@ -150,12 +147,12 @@ func (c *Config) ReplHistoryFile() string {
150147

151148
func (c *Config) SaveDefaultDomain() error {
152149
if _, err := os.Stat(c.FileStoreRoot); err != nil {
153-
if err := os.Mkdir(c.FileStoreRoot, OwnerReadWrite); err != nil {
150+
if err := os.Mkdir(c.FileStoreRoot, x.OwnerReadWrite); err != nil {
154151
return err
155152
}
156153
}
157154

158-
return os.WriteFile(filepath.Join(c.FileStoreRoot, defaultDomain), []byte(c.DefaultDomain), OwnerReadWrite)
155+
return os.WriteFile(filepath.Join(c.FileStoreRoot, defaultDomain), []byte(c.DefaultDomain), x.OwnerReadWrite)
159156
}
160157

161158
func (c *Config) LoadDefaultDomain() error {

0 commit comments

Comments
 (0)