Skip to content

Commit

Permalink
cli: create default policy setting file and manifest on generate
Browse files Browse the repository at this point in the history
If files do not exist yet, generate will write default files.
  • Loading branch information
malt3 committed Jan 11, 2024
1 parent ae694db commit d893349
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
tools/genpolicy.cache/*
tools/genpolicy-msft.json
data/*
!tools/genpolicy.cache/.gitkeep
*.bin
edgcoco
Expand Down
32 changes: 31 additions & 1 deletion cli/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ func runGenerate(cmd *cobra.Command, args []string) error {
return err
}

if err := createDefaultFileIfNotExist(filepath.Join(flags.policyPath, flags.settingsPath), manifest.DefaultGenpolicyMSFT); err != nil {
return fmt.Errorf("failed to create default policy file: %w", err)
}
if err := generatePolicies(cmd.Context(), flags.policyPath, flags.settingsPath, paths, logger); err != nil {
return fmt.Errorf("failed to generate policies: %w", err)
}
Expand All @@ -65,7 +68,7 @@ func runGenerate(cmd *cobra.Command, args []string) error {
return fmt.Errorf("failed to create policy map: %w", err)
}

manifestData, err := os.ReadFile(flags.manifestPath)
manifestData, err := readOrCreateDefault(flags.manifestPath, manifest.DefaultManifestJSON)
if err != nil {
return fmt.Errorf("failed to read manifest file: %w", err)
}
Expand Down Expand Up @@ -202,3 +205,30 @@ func parseGenerateFlags(cmd *cobra.Command) (*generateFlags, error) {
manifestPath: manifestPath,
}, nil
}

// readOrCreateDefault reads the file at path,
// or creates it with the default value if it doesn't exist.
func readOrCreateDefault(path string, deflt []byte) ([]byte, error) {
data, err := os.ReadFile(path)
if err == nil {
return data, nil
}
if !os.IsNotExist(err) {
return nil, err
}
if err := os.WriteFile(path, deflt, 0o644); err != nil {
return nil, err
}
return deflt, nil
}

func createDefaultFileIfNotExist(path string, deflt []byte) error {
_, err := os.Stat(path)
if err == nil {
return nil
}
if !os.IsNotExist(err) {
return err
}
return os.WriteFile(path, deflt, 0o644)
}
Empty file added data/.gitkeep
Empty file.
11 changes: 11 additions & 0 deletions internal/manifest/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package manifest

import (
_ "embed"
)

//go:embed genpolicy-msft.json
var DefaultGenpolicyMSFT []byte

//go:embed manifest.json
var DefaultManifestJSON []byte
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ generate target=default_deploy_target:
mkdir -p ./{{worspace_dir}}
rm -rf ./{{worspace_dir}}/deployment
cp -R ./deployments/{{target}} ./{{worspace_dir}}/deployment
cp ./data/manifest.json ./{{worspace_dir}}/manifest.json
nix run .#yq-go -- -i ". \
| with(select(.spec.template.spec.containers[].image | contains(\"nunki/coordinator\")); \
.spec.template.spec.containers[0].image = \"${container_registry}/nunki/coordinator:latest\")" \
Expand Down
1 change: 1 addition & 0 deletions packages/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ let
fileset = lib.fileset.unions [
../go.mod
../go.sum
(lib.fileset.fileFilter (file: lib.hasSuffix ".json" file.name) ../internal/manifest) # go embed
(lib.fileset.fileFilter (file: lib.hasSuffix ".go" file.name) ../.)
];
};
Expand Down

0 comments on commit d893349

Please sign in to comment.