Skip to content

Commit

Permalink
cli: move subcommands into cmd package
Browse files Browse the repository at this point in the history
We would like to test the commands programmatically from Go, so we need
to make them importable.
  • Loading branch information
burgerdev committed Feb 29, 2024
1 parent 5176ade commit f0591ab
Show file tree
Hide file tree
Showing 15 changed files with 35 additions and 34 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,11 @@ jobs:
run: |
yq < workspace/coordinator.yaml \
'select(.kind == "Deployment") | .spec.template.metadata.annotations["io.katacontainers.config.agent.policy"]' |
base64 -d | sha256sum | cut -d " " -f1 > cli/assets/coordinator-policy-hash
base64 -d | sha256sum | cut -d " " -f1 > cli/cmd/assets/coordinator-policy-hash
git config --global user.name "edgelessci"
git config --global user.email "[email protected]"
git add cli/assets/coordinator-policy-hash
git add cli/cmd/assets/coordinator-policy-hash
git diff --staged --quiet || git commit -m "release: update coordinator policy hash"
- name: Build CLI
run: |
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 7 additions & 1 deletion cli/constants.go → cli/cmd/common.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

import (
_ "embed"
Expand Down Expand Up @@ -37,3 +37,9 @@ func cachedir(subdir string) (string, error) {
}
return filepath.Join(dir, subdir), nil
}

func must(err error) {
if err != nil {
panic(err)
}
}
4 changes: 2 additions & 2 deletions cli/generate.go → cli/cmd/generate.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

import (
"bytes"
Expand Down Expand Up @@ -30,7 +30,7 @@ const (
nunkiRoleAnnotationKey = "nunki.edgeless.systems/pod-role"
)

func newGenerateCmd() *cobra.Command {
func NewGenerateCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "generate [flags] paths...",
Short: "generate policies and inject into Kubernetes resources",
Expand Down
2 changes: 1 addition & 1 deletion cli/log.go → cli/cmd/log.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion cli/policies.go → cli/cmd/policies.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion cli/runtime.go → cli/cmd/runtime.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

// DefaultCoordinatorPolicyHash is derived from the coordinator release candidate and injected at release build time.
//
Expand Down
4 changes: 2 additions & 2 deletions cli/set.go → cli/cmd/set.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

import (
"context"
Expand Down Expand Up @@ -28,7 +28,7 @@ import (
"google.golang.org/grpc/status"
)

func newSetCmd() *cobra.Command {
func NewSetCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "set [flags] paths...",
Short: "Set the given manifest at the coordinator",
Expand Down
4 changes: 2 additions & 2 deletions cli/verify.go → cli/cmd/verify.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

import (
"crypto/sha256"
Expand All @@ -20,7 +20,7 @@ import (
"github.com/spf13/cobra"
)

func newVerifyCmd() *cobra.Command {
func NewVerifyCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "verify",
Short: "Verify a nunki deployment",
Expand Down
25 changes: 10 additions & 15 deletions cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"os/signal"

"github.com/edgelesssys/nunki/cli/cmd"
"github.com/spf13/cobra"
)

Expand All @@ -25,23 +26,23 @@ func execute() error {
var version = "0.0.0-dev"

func newRootCmd() *cobra.Command {
cmd := &cobra.Command{
root := &cobra.Command{
Short: "nunki",
PersistentPreRun: preRunRoot,
Version: version,
}
cmd.SetOut(os.Stdout)
root.SetOut(os.Stdout)

cmd.PersistentFlags().String("log-level", "warn", "set logging level (debug, info, warn, error, or a number)")
root.PersistentFlags().String("log-level", "warn", "set logging level (debug, info, warn, error, or a number)")

cmd.InitDefaultVersionFlag()
cmd.AddCommand(
newGenerateCmd(),
newSetCmd(),
newVerifyCmd(),
root.InitDefaultVersionFlag()
root.AddCommand(
cmd.NewGenerateCmd(),
cmd.NewSetCmd(),
cmd.NewVerifyCmd(),
)

return cmd
return root
}

// signalContext returns a context that is canceled on the handed signal.
Expand Down Expand Up @@ -74,9 +75,3 @@ func signalContext(ctx context.Context, sig os.Signal) (context.Context, context
func preRunRoot(cmd *cobra.Command, _ []string) {
cmd.SilenceUsage = true
}

func must(err error) {
if err != nil {
panic(err)
}
}
8 changes: 4 additions & 4 deletions packages/by-name/cli-release/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

(nunki.overrideAttrs (_finalAttrs: previousAttrs: {
prePatch = ''
install -D ${lib.getExe genpolicy} cli/assets/genpolicy
install -D ${genpolicy.settings}/genpolicy-settings.json cli/assets/genpolicy-settings.json
install -D ${genpolicy.rules}/genpolicy-rules.rego cli/assets/genpolicy-rules.rego
install -D ${lib.getExe genpolicy} cli/cmd/assets/genpolicy
install -D ${genpolicy.settings}/genpolicy-settings.json cli/cmd/assets/genpolicy-settings.json
install -D ${genpolicy.rules}/genpolicy-rules.rego cli/cmd/assets/genpolicy-rules.rego
'';

ldflags = previousAttrs.ldflags ++ [
"-X main.DefaultCoordinatorPolicyHash=${builtins.readFile ../../../cli/assets/coordinator-policy-hash}"
"-X main.DefaultCoordinatorPolicyHash=${builtins.readFile ../../../cli/cmd/assets/coordinator-policy-hash}"
];
})).cli
6 changes: 3 additions & 3 deletions packages/by-name/nunki/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ buildGoModule rec {
subPackages = [ "coordinator" "initializer" "cli" ];

prePatch = ''
install -D ${lib.getExe genpolicy} cli/assets/genpolicy
install -D ${genpolicy.settings-dev}/genpolicy-settings.json cli/assets/genpolicy-settings.json
install -D ${genpolicy.rules}/genpolicy-rules.rego cli/assets/genpolicy-rules.rego
install -D ${lib.getExe genpolicy} cli/cmd/assets/genpolicy
install -D ${genpolicy.settings-dev}/genpolicy-settings.json cli/cmd/assets/genpolicy-settings.json
install -D ${genpolicy.rules}/genpolicy-rules.rego cli/cmd/assets/genpolicy-rules.rego
'';

CGO_ENABLED = 0;
Expand Down

0 comments on commit f0591ab

Please sign in to comment.