government / cmd / government / inspect.go

@ 141e6346099989c841d4af3293458485a50c86e7 | history


package main

import (
	"fmt"

	"github.com/spf13/cobra"

	"government/gov2lisp"
	"government/policy"
	"government/ratify"
)

// tree stops after policy processing, without decoding, so it can still
// show a graph whose shape the decoder would reject. That makes it the
// tool for looking at a broken program.
func newTreeCmd() *cobra.Command {
	var lf loadFlags
	cmd := &cobra.Command{
		Use:   "tree prog.gov",
		Short: "Print the policy tree",
		Args:  oneFile,
		RunE: func(cmd *cobra.Command, args []string) error {
			path := args[0]
			data, err := readFile(path)
			if err != nil {
				return err
			}
			opts := lf.programOptions()
			certs, err := ratify.Ratify(data, ratify.Options{At: opts.At, Pin: opts.Pin, CRLs: opts.CRLs})
			if err != nil {
				return refused("%s: %v", path, err)
			}
			g, err := policy.Program(certs, policy.Options{MaxNodes: opts.MaxNodes})
			if err != nil {
				return refused("%s: %v", path, err)
			}
			fmt.Fprint(cmd.OutOrStdout(), g.Draw())
			return nil
		},
	}
	lf = addLoadFlags(cmd.Flags())
	return cmd
}

func newDecompileCmd() *cobra.Command {
	var lf loadFlags
	cmd := &cobra.Command{
		Use:   "decompile prog.gov",
		Short: "Print equivalent GovLisp source",
		Args:  oneFile,
		RunE: func(cmd *cobra.Command, args []string) error {
			p, err := loadProgram(args[0], lf)
			if err != nil {
				return err
			}
			fmt.Fprint(cmd.OutOrStdout(), gov2lisp.DecompileTree(p.Tree))
			return nil
		},
	}
	lf = addLoadFlags(cmd.Flags())
	return cmd
}